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_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 72 AC_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 ]) 146 147 CODE_COVERAGE_RULES=' 148 # Code coverage 149 # 150 # Optional: 151 # - CODE_COVERAGE_DIRECTORY: Top-level directory for code coverage reporting. 152 # (Default: $(top_builddir)) 153 # - CODE_COVERAGE_OUTPUT_FILE: Filename and path for the .info file generated 154 # by lcov for code coverage. (Default: 155 # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info) 156 # - CODE_COVERAGE_OUTPUT_DIRECTORY: Directory for generated code coverage 157 # reports to be created. (Default: 158 # $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage) 159 # - CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH: --gcov-tool pathtogcov 160 # - CODE_COVERAGE_LCOV_OPTIONS_DEFAULT: Extra options to pass to the lcov instance. 161 # (Default: $CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH) 162 # - CODE_COVERAGE_LCOV_OPTIONS: Extra options to pass to the lcov instance. 163 # (Default: $CODE_COVERAGE_LCOV_OPTIONS_DEFAULT) 164 # - CODE_COVERAGE_GENHTML_OPTIONS: Extra options to pass to the genhtml 165 # instance. (Default: empty) 166 # - CODE_COVERAGE_IGNORE_PATTERN: Extra glob pattern of files to ignore 167 # 168 # The generated report will be titled using the $(PACKAGE_NAME) and 169 # $(PACKAGE_VERSION). In order to add the current git hash to the title, 170 # use the git-version-gen script, available online. 171 172 # Optional variables 173 CODE_COVERAGE_DIRECTORY ?= $(top_builddir) 174 CODE_COVERAGE_OUTPUT_FILE ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage.info 175 CODE_COVERAGE_OUTPUT_DIRECTORY ?= $(PACKAGE_NAME)-$(PACKAGE_VERSION)-coverage 176 CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH ?= --gcov-tool "$(GCOV)" 177 CODE_COVERAGE_LCOV_OPTIONS_DEFAULT ?= $(CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH) 178 CODE_COVERAGE_LCOV_OPTIONS ?= $(CODE_COVERAGE_LCOV_OPTIONS_DEFAULT) 179 CODE_COVERAGE_GENHTML_OPTIONS ?= 180 CODE_COVERAGE_IGNORE_PATTERN ?= 181 182 code_coverage_quiet = $(code_coverage_quiet_$(V)) 183 code_coverage_quiet_ = $(code_coverage_quiet_$(AM_DEFAULT_VERBOSITY)) 184 code_coverage_quiet_0 = --quiet 185 186 # Use recursive makes in order to ignore errors during check 187 check-code-coverage: 188 ifeq ($(CODE_COVERAGE_ENABLED),yes) 189 -$(MAKE) $(AM_MAKEFLAGS) -k check 190 $(MAKE) $(AM_MAKEFLAGS) code-coverage-capture 191 else 192 @echo "Need to reconfigure with --enable-code-coverage" 193 endif 194 195 # Capture code coverage data 196 code-coverage-capture: code-coverage-capture-hook 197 ifeq ($(CODE_COVERAGE_ENABLED),yes) 198 $(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) 199 $(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)" 200 -@rm -f $(CODE_COVERAGE_OUTPUT_FILE).tmp 201 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) 202 @echo "file://$(abs_builddir)/$(CODE_COVERAGE_OUTPUT_DIRECTORY)/index.html" 203 else 204 @echo "Need to reconfigure with --enable-code-coverage" 205 endif 206 207 # Hook rule executed before code-coverage-capture, overridable by the user 208 code-coverage-capture-hook: 209 210 ifeq ($(CODE_COVERAGE_ENABLED),yes) 211 clean: code-coverage-clean 212 code-coverage-clean: 213 -$(LCOV) --directory $(top_builddir) -z 214 -rm -rf $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_FILE).tmp $(CODE_COVERAGE_OUTPUT_DIRECTORY) 215 -find . -name "*.gcda" -o -name "*.gcov" -delete 216 endif 217 218 GITIGNOREFILES ?= 219 GITIGNOREFILES += $(CODE_COVERAGE_OUTPUT_FILE) $(CODE_COVERAGE_OUTPUT_DIRECTORY) 220 221 DISTCHECK_CONFIGURE_FLAGS ?= 222 DISTCHECK_CONFIGURE_FLAGS += --disable-code-coverage 223 224 .PHONY: check-code-coverage code-coverage-capture code-coverage-capture-hook code-coverage-clean 225 ' 226 227 AC_SUBST([CODE_COVERAGE_RULES]) 228 m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([CODE_COVERAGE_RULES])]) 229 ]) 230