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