Skip to content

Commit ec53029

Browse files
committed
Add coverage support
1 parent f336ff5 commit ec53029

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

Makefile.am

+77
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,81 @@ if LUA_SUB
88
LUA_SUBDIR = lua
99
endif
1010

11+
COVERAGE_INFO_FILE = $(top_builddir)/coverage.info
12+
COVERAGE_REPORT_DIR = $(top_builddir)/coverage
13+
14+
.PHONY = coverage-requirement-check clean-coverage-report
15+
16+
coverage-requirement-check:
17+
@if test ! -e $(GCOV); then \
18+
echo "Cannot find $(GCOV). Please install gcov."; \
19+
exit 1; \
20+
fi
21+
@if test ! -e $(LCOV); then \
22+
echo "Cannot find $(LCOV). Please install lcov."; \
23+
exit 1; \
24+
fi
25+
@if test ! -e $(GENHTML); then \
26+
echo "Cannot find $(GENHTML). Please install lcov."; \
27+
exit 1; \
28+
fi
29+
30+
coverage: coverage-requirement-check clean-coverage coverage-build coverage-check coverage-report
31+
@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."
32+
33+
coverage-build: coverage-requirement-check
34+
@if test `find $(top_builddir) -name "*.gcno" | wc -l` -eq 0; then \
35+
echo "Start to remove old non-instrumented object files..."; \
36+
$(MAKE) $(AM_MAKEFLAGS) clean; \
37+
echo "Successfully removed old non-instrumented object files."; \
38+
fi
39+
@echo "Start to build libraries with coverage options..."
40+
$(MAKE) $(AM_MAKEFLAGS) \
41+
CFLAGS="$(CFLAGS) $(COVERAGE_CFLAGS) $(COVERAGE_OPTFLAGS)" \
42+
CXXFLAGS="$(CXXFLAGS) $(COVERAGE_CXXFLAGS) $(COVERAGE_OPTFLAGS)" \
43+
LDFLAGS="$(LDFLAGS) $(COVERAGE_LDFLAGS)" \
44+
LIBS="$(LIBS) $(COVERAGE_LIBS)"
45+
@echo "Successfully built libraries with coverage options."
46+
47+
coverage-check: coverage-requirement-check
48+
@echo "Start to run tests with instrumented libraries..."
49+
$(MAKE) $(AM_MAKEFLAGS) check \
50+
CFLAGS="$(CFLAGS) $(COVERAGE_CFLAGS) $(COVERAGE_OPTFLAGS)" \
51+
CXXFLAGS="$(CXXFLAGS) $(COVERAGE_CXXFLAGS) $(COVERAGE_OPTFLAGS)" \
52+
LDFLAGS="$(LDFLAGS) $(COVERAGE_LDFLAGS)" \
53+
LIBS="$(LIBS) $(COVERAGE_LIBS)"
54+
@echo "Successfully run tests with instrumented libraries."
55+
56+
# TODO(Eiichiro Iwata): Remove GNU make extension(abspath function)
57+
# Automake treat GNU make extension as warning
58+
# when using -Wall or -Wportability in AM_INIT_AUTOMAKE.
59+
coverage-report: coverage-requirement-check
60+
@echo "Start to create coverage reports..."
61+
$(LCOV) --capture \
62+
--directory "$(top_builddir)/" \
63+
--output-file $(COVERAGE_INFO_FILE) \
64+
--gcov-tool $(GCOV) \
65+
--compat-libtool --checksum
66+
$(GENHTML) --prefix "$(top_srcdir)" \
67+
--output-directory $(COVERAGE_REPORT_DIR) \
68+
--title $(PACKAGE_NAME) \
69+
--legend --show-details \
70+
$(GENHTML_OPTIONS) \
71+
$(COVERAGE_INFO_FILE)
72+
@echo "Successfully created coverage reports into $(COVERAGE_REPORT_DIR) directory."
73+
74+
clean-coverage-report:
75+
-rm -rf $(COVERAGE_INFO_FILE)
76+
-rm -rf $(COVERAGE_REPORT_DIR)
77+
78+
clean-coverage: clean-coverage-report
79+
-$(LCOV) --gcov-tool $(GCOV) --zerocounters --directory $(top_builddir)
80+
@if xargs --version 2>/dev/null; then \
81+
find $(top_builddir) -name "*.gcno" | xargs --no-run-if-empty rm; \
82+
else \
83+
find $(top_builddir) -name "*.gcno" | xargs rm; \
84+
fi
85+
86+
clean-local: clean-coverage
87+
1188
SUBDIRS = src tests utils doc $(LUA_SUBDIR)

configure.ac

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ AC_LINK_IFELSE([
173173
AC_MSG_WARN([Libucl references could be thread-unsafe because atomic builtins are missing])
174174
])
175175

176+
AX_CODE_COVERAGE
177+
176178
AC_CONFIG_FILES(Makefile \
177179
src/Makefile \
178180
lua/Makefile

m4/gcov.m4

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# SYNOPSIS
2+
#
3+
# Add code coverage support with gcov/lcov.
4+
#
5+
# AX_CODE_COVERAGE()
6+
#
7+
# DESCRIPTION
8+
#
9+
# Provides a --enable-coverage option which checks for available
10+
# gcov/lcov binaries and provides ENABLE_CODE_COVERAGE conditional.
11+
#
12+
# LAST MODIFICATION
13+
#
14+
# $Id: coverage.m4 40881 2013-08-20 17:54:39Z damon $
15+
#
16+
# COPYLEFT
17+
#
18+
# Copyright (c) 2012 Roy H. Stogner <[email protected]>
19+
# Copyright (c) 2010 Karl W. Schulz <[email protected]>
20+
#
21+
# Copying and distribution of this file, with or without modification, are
22+
# permitted in any medium without royalty provided the copyright notice
23+
# and this notice are preserved.
24+
25+
AC_DEFUN([AX_CODE_COVERAGE],
26+
[
27+
28+
AC_ARG_ENABLE(coverage, AC_HELP_STRING([--enable-coverage],[configure code coverage analysis tools]))
29+
30+
HAVE_GCOV_TOOLS=0
31+
32+
GCOV_FLAGS=""
33+
34+
if test "x$enable_coverage" = "xyes"; then
35+
36+
# ----------------------------
37+
# Check for gcov/lcov binaries
38+
# ----------------------------
39+
40+
AC_ARG_VAR([GCOV], [Coverage testing command])
41+
if test "x$GCOV" = "x"; then
42+
AC_PATH_PROG(GCOV, gcov, no)
43+
else
44+
AC_PATH_PROG(GCOV, $GCOV, no)
45+
fi
46+
47+
AC_PATH_PROG(LCOV, lcov, no)
48+
AC_PATH_PROG(GENHTML, genhtml)
49+
50+
# ----------------------------------
51+
# include coverage compiler options
52+
# ----------------------------------
53+
AC_MSG_CHECKING([for clang])
54+
55+
AC_COMPILE_IFELSE(
56+
[AC_LANG_PROGRAM([], [[
57+
#ifndef __clang__
58+
not clang
59+
#endif
60+
]])],
61+
[CLANG=yes], [CLANG=no])
62+
63+
AC_MSG_RESULT([$CLANG])
64+
HAVE_GCOV_TOOLS=1
65+
COVERAGE_CFLAGS="-fprofile-arcs -ftest-coverage"
66+
COVERAGE_LDFLAGS="--coverage -fprofile-arcs -ftest-coverage"
67+
COVERAGE_OPTFLAGS="-O0"
68+
69+
# Test for C...
70+
CFLAGS="${GCOV_FLAGS} ${CFLAGS}"
71+
CXXFLAGS="${GCOV_FLAGS} ${CXXFLAGS}"
72+
if test "x$GCC" = "xyes" -a "x$CLANG" = "xno"; then
73+
COVERAGE_LIBS="-lgcov"
74+
else
75+
COVERAGE_LIBS=""
76+
fi
77+
fi
78+
79+
AC_SUBST([GCOV])
80+
AC_SUBST([LCOV])
81+
AC_SUBST([GENHTML])
82+
AC_SUBST([GENHTML_OPTIONS])
83+
AC_SUBST([COVERAGE_CFLAGS])
84+
AC_SUBST([COVERAGE_OPTFLAGS])
85+
AC_SUBST([COVERAGE_LDFLAGS])
86+
AC_SUBST([COVERAGE_LIBS])
87+
AM_CONDITIONAL(CODE_COVERAGE_ENABLED,test x$HAVE_GCOV_TOOLS = x1)
88+
89+
])

0 commit comments

Comments
 (0)