1 dnl GNOME_CODE_COVERAGE
3 dnl Defines CODE_COVERAGE_CFLAGS and CODE_COVERAGE_LDFLAGS which should be
4 dnl included in the CFLAGS and LIBS/LDFLAGS variables of every build target
5 dnl (program or library) which should be built with code coverage support.
6 dnl Also defines GNOME_CODE_COVERAGE_RULES which should be substituted in your
7 dnl Makefile; and $enable_code_coverage which can be used in subsequent
10 dnl Note that all optimisation flags in CFLAGS must be disabled when code
11 dnl coverage is enabled.
13 dnl Derived from Makefile.decl in GLib, originally licenced under LGPLv2.1+.
14 dnl This file is licenced under LGPLv2.1+.
18 dnl GNOME_CODE_COVERAGE
21 dnl @GNOME_CODE_COVERAGE_RULES@
22 dnl my_program_LIBS = … $(CODE_COVERAGE_LDFLAGS) …
23 dnl my_program_CFLAGS = … $(CODE_COVERAGE_CFLAGS) …
25 dnl This results in a “check-code-coverage” rule being added to any Makefile.am
26 dnl which includes “@GNOME_CODE_COVERAGE_RULES@” (assuming the module has been
27 dnl configured with --enable-code-coverage). Running `make check-code-coverage`
28 dnl in that directory will run the module’s test suite (`make check`) and build
29 dnl a code coverage report detailing the code which was touched, then print the
30 dnl URI for the report.
32 AC_DEFUN([GNOME_CODE_COVERAGE],[
33 dnl Check for --enable-code-coverage
34 AC_MSG_CHECKING([whether to build with code coverage support])
35 AC_ARG_ENABLE([code-coverage], AS_HELP_STRING([--enable-code-coverage], [Whether to enable code coverage support]),, enable_code_coverage=no)
36 AM_CONDITIONAL([CODE_COVERAGE_ENABLED], [test x$enable_code_coverage = xyes])
37 AC_SUBST([CODE_COVERAGE_ENABLED], [$enable_code_coverage])
38 AC_MSG_RESULT($enable_code_coverage)
40 AS_IF([ test "$enable_code_coverage" = "yes" ], [
41 dnl Check if gcc is being used
42 AS_IF([ test "$GCC" = "no" ], [
43 AC_MSG_ERROR([not compiling with gcc, which is required for gcov code coverage])
46 # List of supported lcov versions.
47 lcov_version_list="1.6 1.7 1.8 1.9 1.10"
49 AC_CHECK_PROG([LCOV], [lcov], [lcov])
50 AC_CHECK_PROG([GENHTML], [genhtml], [genhtml])
52 AS_IF([ test "$LCOV" ], [
53 AC_CACHE_CHECK([for lcov version], gnome_cv_lcov_version, [
54 gnome_cv_lcov_version=invalid
55 lcov_version=`$LCOV -v 2>/dev/null | $SED -e 's/^.* //'`
56 for lcov_check_version in $lcov_version_list; do
57 if test "$lcov_version" = "$lcov_check_version"; then
58 gnome_cv_lcov_version="$lcov_check_version (ok)"
63 lcov_msg="To enable code coverage reporting you must have one of the following lcov versions installed: $lcov_version_list"
64 AC_MSG_ERROR([$lcov_msg])
67 case $gnome_cv_lcov_version in
69 lcov_msg="You must have one of the following versions of lcov: $lcov_version_list (found: $lcov_version)."
70 AC_MSG_ERROR([$lcov_msg])
75 AS_IF([ test -z "$GENHTML" ], [
76 AC_MSG_ERROR([Could not find genhtml from the lcov package])
79 dnl Build the code coverage flags
80 CODE_COVERAGE_CFLAGS="-O0 -g -fprofile-arcs -ftest-coverage"
81 CODE_COVERAGE_LDFLAGS="-lgcov"
83 AC_SUBST([CODE_COVERAGE_CFLAGS])
84 AC_SUBST([CODE_COVERAGE_LDFLAGS])
87 GNOME_CODE_COVERAGE_RULES='
91 # - CODE_COVERAGE_DIRECTORY: Top-level directory for code coverage reporting.
92 # (Default: $(top_builddir))
93 # - CODE_COVERAGE_OUTPUT_FILE: Filename and path for the .info file generated
94 # by lcov for code coverage. (Default:
95 # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info)
96 # - CODE_COVERAGE_OUTPUT_DIRECTORY: Directory for generated code coverage
97 # reports to be created. (Default:
98 # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage)
99 # - CODE_COVERAGE_LCOV_OPTIONS: Extra options to pass to the lcov instance.
101 # - CODE_COVERAGE_GENHTML_OPTIONS: Extra options to pass to the genhtml
102 # instance. (Default: empty)
103 # - CODE_COVERAGE_IGNORE_PATTERN: Extra glob pattern of files to ignore
105 # The generated report will be titled using the $(PACKAGE_NAME) and
106 # $(PACKAGE_VERSION). In order to add the current git hash to the title,
107 # use the git-version-gen script, available online.
110 CODE_COVERAGE_DIRECTORY ?= $(top_builddir)
111 CODE_COVERAGE_OUTPUT_FILE ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info
112 CODE_COVERAGE_OUTPUT_DIRECTORY ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage
113 CODE_COVERAGE_LCOV_OPTIONS ?=
114 CODE_COVERAGE_GENHTML_OPTIONS ?=
115 CODE_COVERAGE_IGNORE_PATTERN ?=
117 code_coverage_quiet = $(code_coverage_quiet_$(V))
118 code_coverage_quiet_ = $(code_coverage_quiet_$(AM_DEFAULT_VERBOSITY))
119 code_coverage_quiet_0 = --quiet
121 # Use recursive makes in order to ignore errors during check
123 ifeq ($(CODE_COVERAGE_ENABLED),yes)
124 -$(MAKE) $(AM_MAKEFLAGS) -k check
125 $(MAKE) $(AM_MAKEFLAGS) code-coverage-capture
127 @echo "Need to reconfigure with --enable-code-coverage"
130 # Capture code coverage data
131 code-coverage-capture: code-coverage-capture-hook
132 ifeq ($(CODE_COVERAGE_ENABLED),yes)
133 $(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)
134 $(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)"
135 -@rm -f $(CODE_COVERAGE_OUTPUT_FILE).tmp
136 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)
137 @echo "file://$(abs_builddir)/$(CODE_COVERAGE_OUTPUT_DIRECTORY)/index.html"
139 @echo "Need to reconfigure with --enable-code-coverage"
142 # Hook rule executed before code-coverage-capture, overridable by the user
143 code-coverage-capture-hook:
145 ifeq ($(CODE_COVERAGE_ENABLED),yes)
146 clean: code-coverage-clean
148 -$(LCOV) --directory $(top_builddir) -z
149 -rm -rf $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_FILE).tmp $(CODE_COVERAGE_OUTPUT_DIRECTORY)
150 -find . -name "*.gcda" -o -name "*.gcov" -delete
154 GITIGNOREFILES += $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_DIRECTORY)
156 DISTCHECK_CONFIGURE_FLAGS ?=
157 DISTCHECK_CONFIGURE_FLAGS += --disable-code-coverage
159 .PHONY: check-code-coverage code-coverage-capture code-coverage-capture-hook code-coverage-clean
162 AC_SUBST([GNOME_CODE_COVERAGE_RULES])
163 m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([GNOME_CODE_COVERAGE_RULES])])