Skip to content
Permalink
Browse files

Add coverage support

  • Loading branch information...
vstakhov committed Feb 8, 2016
1 parent f336ff5 commit ec5302974257501896efb9a0462d84d56e9501ff
Showing with 168 additions and 0 deletions.
  1. +77 −0 Makefile.am
  2. +2 −0 configure.ac
  3. +89 −0 m4/gcov.m4
@@ -8,4 +8,81 @@ if LUA_SUB
LUA_SUBDIR = lua
endif

COVERAGE_INFO_FILE = $(top_builddir)/coverage.info
COVERAGE_REPORT_DIR = $(top_builddir)/coverage

.PHONY = coverage-requirement-check clean-coverage-report

coverage-requirement-check:
@if test ! -e $(GCOV); then \
echo "Cannot find $(GCOV). Please install gcov."; \
exit 1; \
fi
@if test ! -e $(LCOV); then \
echo "Cannot find $(LCOV). Please install lcov."; \
exit 1; \
fi
@if test ! -e $(GENHTML); then \
echo "Cannot find $(GENHTML). Please install lcov."; \
exit 1; \
fi

coverage: coverage-requirement-check clean-coverage coverage-build coverage-check coverage-report
@echo "Please execute 'make clean' before 'make' or 'make check' to remove instrumented object files(compiled with -O0 etc.). Note that 'make clean' also remove coverage data."

coverage-build: coverage-requirement-check
@if test `find $(top_builddir) -name "*.gcno" | wc -l` -eq 0; then \
echo "Start to remove old non-instrumented object files..."; \
$(MAKE) $(AM_MAKEFLAGS) clean; \
echo "Successfully removed old non-instrumented object files."; \
fi
@echo "Start to build libraries with coverage options..."
$(MAKE) $(AM_MAKEFLAGS) \
CFLAGS="$(CFLAGS) $(COVERAGE_CFLAGS) $(COVERAGE_OPTFLAGS)" \
CXXFLAGS="$(CXXFLAGS) $(COVERAGE_CXXFLAGS) $(COVERAGE_OPTFLAGS)" \
LDFLAGS="$(LDFLAGS) $(COVERAGE_LDFLAGS)" \
LIBS="$(LIBS) $(COVERAGE_LIBS)"
@echo "Successfully built libraries with coverage options."

coverage-check: coverage-requirement-check
@echo "Start to run tests with instrumented libraries..."
$(MAKE) $(AM_MAKEFLAGS) check \
CFLAGS="$(CFLAGS) $(COVERAGE_CFLAGS) $(COVERAGE_OPTFLAGS)" \
CXXFLAGS="$(CXXFLAGS) $(COVERAGE_CXXFLAGS) $(COVERAGE_OPTFLAGS)" \
LDFLAGS="$(LDFLAGS) $(COVERAGE_LDFLAGS)" \
LIBS="$(LIBS) $(COVERAGE_LIBS)"
@echo "Successfully run tests with instrumented libraries."

# TODO(Eiichiro Iwata): Remove GNU make extension(abspath function)
# Automake treat GNU make extension as warning
# when using -Wall or -Wportability in AM_INIT_AUTOMAKE.
coverage-report: coverage-requirement-check
@echo "Start to create coverage reports..."
$(LCOV) --capture \
--directory "$(top_builddir)/" \
--output-file $(COVERAGE_INFO_FILE) \
--gcov-tool $(GCOV) \
--compat-libtool --checksum
$(GENHTML) --prefix "$(top_srcdir)" \
--output-directory $(COVERAGE_REPORT_DIR) \
--title $(PACKAGE_NAME) \
--legend --show-details \
$(GENHTML_OPTIONS) \
$(COVERAGE_INFO_FILE)
@echo "Successfully created coverage reports into $(COVERAGE_REPORT_DIR) directory."

clean-coverage-report:
-rm -rf $(COVERAGE_INFO_FILE)
-rm -rf $(COVERAGE_REPORT_DIR)

clean-coverage: clean-coverage-report
-$(LCOV) --gcov-tool $(GCOV) --zerocounters --directory $(top_builddir)
@if xargs --version 2>/dev/null; then \
find $(top_builddir) -name "*.gcno" | xargs --no-run-if-empty rm; \
else \
find $(top_builddir) -name "*.gcno" | xargs rm; \
fi

clean-local: clean-coverage

SUBDIRS = src tests utils doc $(LUA_SUBDIR)
@@ -173,6 +173,8 @@ AC_LINK_IFELSE([
AC_MSG_WARN([Libucl references could be thread-unsafe because atomic builtins are missing])
])

AX_CODE_COVERAGE

AC_CONFIG_FILES(Makefile \
src/Makefile \
lua/Makefile
@@ -0,0 +1,89 @@
# SYNOPSIS
#
# Add code coverage support with gcov/lcov.
#
# AX_CODE_COVERAGE()
#
# DESCRIPTION
#
# Provides a --enable-coverage option which checks for available
# gcov/lcov binaries and provides ENABLE_CODE_COVERAGE conditional.
#
# LAST MODIFICATION
#
# $Id: coverage.m4 40881 2013-08-20 17:54:39Z damon $
#
# COPYLEFT
#
# Copyright (c) 2012 Roy H. Stogner <roystgnr@ices.utexas.edu>
# Copyright (c) 2010 Karl W. Schulz <karl@ices.utexas.edu>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved.

AC_DEFUN([AX_CODE_COVERAGE],
[

AC_ARG_ENABLE(coverage, AC_HELP_STRING([--enable-coverage],[configure code coverage analysis tools]))

HAVE_GCOV_TOOLS=0

GCOV_FLAGS=""

if test "x$enable_coverage" = "xyes"; then

# ----------------------------
# Check for gcov/lcov binaries
# ----------------------------

AC_ARG_VAR([GCOV], [Coverage testing command])
if test "x$GCOV" = "x"; then
AC_PATH_PROG(GCOV, gcov, no)
else
AC_PATH_PROG(GCOV, $GCOV, no)
fi

AC_PATH_PROG(LCOV, lcov, no)
AC_PATH_PROG(GENHTML, genhtml)

# ----------------------------------
# include coverage compiler options
# ----------------------------------
AC_MSG_CHECKING([for clang])

AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM([], [[
#ifndef __clang__
not clang
#endif
]])],
[CLANG=yes], [CLANG=no])

AC_MSG_RESULT([$CLANG])
HAVE_GCOV_TOOLS=1
COVERAGE_CFLAGS="-fprofile-arcs -ftest-coverage"
COVERAGE_LDFLAGS="--coverage -fprofile-arcs -ftest-coverage"
COVERAGE_OPTFLAGS="-O0"

# Test for C...
CFLAGS="${GCOV_FLAGS} ${CFLAGS}"
CXXFLAGS="${GCOV_FLAGS} ${CXXFLAGS}"
if test "x$GCC" = "xyes" -a "x$CLANG" = "xno"; then
COVERAGE_LIBS="-lgcov"
else
COVERAGE_LIBS=""
fi
fi

AC_SUBST([GCOV])
AC_SUBST([LCOV])
AC_SUBST([GENHTML])
AC_SUBST([GENHTML_OPTIONS])
AC_SUBST([COVERAGE_CFLAGS])
AC_SUBST([COVERAGE_OPTFLAGS])
AC_SUBST([COVERAGE_LDFLAGS])
AC_SUBST([COVERAGE_LIBS])
AM_CONDITIONAL(CODE_COVERAGE_ENABLED,test x$HAVE_GCOV_TOOLS = x1)

])

0 comments on commit ec53029

Please sign in to comment.
You can’t perform that action at this time.