Browse Source

build: Add optional code coverage support

Enable code coverage support with 'configure --enable-coverage'. Use
gcov and lcov for C code coverage, and Devel::Cover and cover for Perl
code coverage.
Guillem Jover 13 years ago
parent
commit
0581dda824
10 changed files with 127 additions and 2 deletions
  1. 4 0
      .gitignore
  2. 2 0
      Makecheck.am
  3. 43 1
      Makefile.am
  4. 1 0
      configure.ac
  5. 1 0
      doc/.gitignore
  6. 8 0
      doc/lcov-epilog
  7. 8 0
      doc/lcov-prolog
  8. 51 0
      m4/dpkg-coverage.m4
  9. 1 0
      scripts/.gitignore
  10. 8 1
      scripts/Makefile.am

+ 4 - 0
.gitignore

@@ -1,6 +1,10 @@
 # Inherited ignores
 *.a
 *.o
+*.gcno
+*.gcda
+*.gcov
+*.lcov
 .*.swp
 .deps/
 Makefile

+ 2 - 0
Makecheck.am

@@ -4,6 +4,7 @@
 #
 #  TEST_VERBOSE - set to 0 or 1 to control test suite verbosity
 #  TEST_ENV_VARS - environment variables to be set for the test suite
+#  TEST_COVERAGE - set to the perl module in charge of getting test coverage
 #  test_tmpdir - test suite temporary directory
 #  test_cases - list of test case files
 #  test_data - list of test data files
@@ -16,6 +17,7 @@ check-local: $(test_data) $(test_cases)
 	PATH="$(top_builddir)/src:$(top_builddir)/scripts:$(top_builddir)/utils:$(PATH)" \
 	  srcdir=$(srcdir) builddir=$(builddir) \
 	  PERL5LIB=$(top_srcdir)/scripts PERL_DL_NONLAZY=1 \
+	  PERL5OPT=$(TEST_COVERAGE) \
 	  $(PERL) -I$(top_srcdir)/scripts \
 	    -MExtUtils::Command::MM -e "test_harness($(TEST_VERBOSE), '.')" \
 	    $(addprefix $(srcdir)/,$(test_cases))

+ 43 - 1
Makefile.am

@@ -72,9 +72,50 @@ EXTRA_DIST = \
 doc: doc/Doxyfile
 	$(DOXYGEN) doc/Doxyfile
 
-clean-local:
+doc-clean:
 	rm -rf doc/html/
 
+# Code coverage support
+
+.PHONY: coverage coverage-clean
+
+if COVERAGE_ENABLED
+LCOV_OPTS = -q --checksum
+LCOV_CAPTURE_OPTS = $(LCOV_OPTS) --no-recursion \
+	-d $(top_builddir)/lib/dpkg \
+	-d $(top_builddir)/src \
+	-d $(top_builddir)/utils
+
+coverage: all
+	$(RM) -f *.lcov
+	find -name '*.gcda' -o -name '*.gcov' | xargs $(RM) -f
+	
+	$(LCOV) $(LCOV_CAPTURE_OPTS) -c -o dpkg_base.lcov -i
+	$(MAKE) -C lib/dpkg check
+	$(MAKE) -C src check
+	$(MAKE) -C utils check
+	$(LCOV) $(LCOV_CAPTURE_OPTS) -c -o dpkg_test.lcov
+	$(LCOV) $(LCOV_OPTS) -a dpkg_base.lcov -a dpkg_test.lcov \
+	  -o dpkg_merge.lcov
+	$(LCOV) $(LCOV_OPTS) -r dpkg_merge.lcov '/usr/include/*' -o dpkg.lcov
+	$(LCOV_GENHTML) -q --legend --title "dpkg C code coverage" \
+	  --html-prolog $(top_srcdir)/doc/lcov-prolog \
+	  --html-epilog $(top_srcdir)/doc/lcov-epilog \
+	  -o doc/coverage dpkg.lcov
+	
+	$(MAKE) -C scripts $@
+
+coverage-clean:
+	rm -rf doc/coverage/
+	find -name '*.gcno' -o -name '*.gcda' -o \
+	     -name '*.gcov' -o -name '*.lcov' | xargs rm -f
+else
+coverage:
+	@echo "Need to reconfigure with --enable-coverage"
+
+coverage-clean:
+endif
+
 .PHONY: update-po
 
 update-po:
@@ -102,3 +143,4 @@ dist-hook:
 		done ; \
 	fi
 
+clean-local: doc-clean coverage-clean

+ 1 - 0
configure.ac

@@ -91,6 +91,7 @@ AC_CHECK_PROG([HAVE_DOT], [dot], [YES], [NO])
 DPKG_PROG_PO4A
 DPKG_PROG_PERL
 DPKG_PROG_POD2MAN
+DPKG_CODE_COVERAGE
 
 # Checks for operating system services and capabilities.
 AC_SYS_LARGEFILE

+ 1 - 0
doc/.gitignore

@@ -1,2 +1,3 @@
 Doxyfile
 html
+coverage

+ 8 - 0
doc/lcov-epilog

@@ -0,0 +1,8 @@
+  <table width="100%" border=0 cellspacing=0 cellpadding=0>
+    <tr>
+      <td class="coverFile"><a href="scripts/coverage.html">scripts</a></td>
+    </tr>
+  </table>
+  <br />
+</body>
+</html>

+ 8 - 0
doc/lcov-prolog

@@ -0,0 +1,8 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html lang="en">
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+  <title>@pagetitle@</title>
+  <link rel="stylesheet" type="text/css" href="@basedir@gcov.css">
+</head>
+<body>

+ 51 - 0
m4/dpkg-coverage.m4

@@ -0,0 +1,51 @@
+# Copyright © 2010 Guillem Jover <guillem@debian.org>
+
+# DPKG_CODE_COVERAGE
+# ------------------
+# Add configuration option to enable code coverage support.
+AC_DEFUN([DPKG_CODE_COVERAGE],
+[
+AC_ARG_ENABLE(coverage,
+	AS_HELP_STRING([--enable-coverage],
+	               [whether to enable code coverage]),
+	[],
+	[enable_coverage=no])
+AM_CONDITIONAL(COVERAGE_ENABLED, test x$enable_coverage = xyes)
+
+if test "x$enable_coverage" = "xyes"; then
+   if test "x$GCC" = "xno"; then
+     AC_MSG_ERROR([not compiling with gcc, which is required for C coverage support])
+   fi
+
+   AC_CHECK_PROGS([GCOV], [gcov])
+   if test -z "$GCOV"; then
+     AC_MSG_ERROR([missing gcov, which is required for C coverage support])
+   fi
+
+   AC_CHECK_PROGS([LCOV], [lcov])
+   if test -z "$LCOV"; then
+      AC_MSG_ERROR([missing lcov, which is required for C coverage support])
+   fi
+
+   AC_CHECK_PROGS([LCOV_GENHTML], [genhtml])
+   if test -z "$LCOV_GENHTML"; then
+      AC_MSG_ERROR([missing genhtml, which is required for C coverage support])
+   fi
+
+   CFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage"
+   LDFLAGS="$LDFLAGS -fprofile-arcs -ftest-coverage"
+
+   AC_MSG_CHECKING([for Devel::Cover perl module])
+   if $($PERL -e "require Devel::Cover;" 2>/dev/null); then
+      PERL_COVERAGE="-MDevel::Cover"
+      AC_SUBST(PERL_COVERAGE)
+      AC_MSG_RESULT([ok])
+   else
+      AC_MSG_ERROR([Devel::Cover perl module is required for coverage support])
+   fi
+   AC_CHECK_PROGS([PERL_COVER], [cover])
+   if test -z "$PERL_COVER"; then
+      AC_MSG_ERROR([missing cover, which is required for perl coverage support])
+   fi
+fi
+])

+ 1 - 0
scripts/.gitignore

@@ -17,3 +17,4 @@ dpkg-source
 dpkg-statoverride
 dpkg-vendor
 t.tmp
+cover_db

+ 8 - 1
scripts/Makefile.am

@@ -159,10 +159,17 @@ if BUILD_POD_DOC
 	done
 endif
 
+coverage: check
+	$(PERL_COVER) -silent -outputdir $(top_builddir)/doc/coverage/scripts
+
+coverage-clean:
+	rm -rf cover_db
+
 TEST_VERBOSE= 0
 TEST_ENV_VARS = \
 	DPKG_DATADIR=$(srcdir)/.. \
 	DPKG_ORIGINS_DIR=$(srcdir)/t/origins
+TEST_COVERAGE = $(PERL_COVERAGE)
 
 test_tmpdir = t.tmp
 
@@ -247,5 +254,5 @@ $(test_tmpdir)/200_Dpkg_Shlibs/objdump.patterns: $(srcdir)/t/200_Dpkg_Shlibs/pat
 
 include $(top_srcdir)/Makecheck.am
 
-clean-local: check-clean
+clean-local: check-clean coverage-clean
 	rm -fr man