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