[PATCH v2] Add coverage target

Dmitry V. Levin ldv@altlinux.org
Tue Jan 12 04:29:29 GMT 2021


Implement a target for capturing code coverage using lcov.
It is available when elfutils is configured using --enable-gcov.

Tested with
autoreconf -if && ./configure --enable-maintainer-mode --enable-gcov &&
make && make check && make coverage

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
---
 .gitignore   |  2 ++
 ChangeLog    |  6 ++++++
 Makefile.am  | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 configure.ac |  3 +++
 4 files changed, 60 insertions(+)

diff --git a/.gitignore b/.gitignore
index 9bf350c1..8bcd88d7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,6 +16,7 @@
 =*
 Makefile
 Makefile.in
+/*.lcov
 /ABOUT-NLS
 /INSTALL
 /aclocal.m4
@@ -26,6 +27,7 @@ Makefile.in
 /config.log
 /config.status
 /configure
+/coverage
 /elfutils.spec
 /stamp-h1
 /version.h
diff --git a/ChangeLog b/ChangeLog
index 03c90b6b..09c1d968 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2021-01-12  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* configure.ac [--enable-gcov]: Check for gcov, lcov, and genhtml.
+	* Makefile.am [GCOV] (coverage): New target.
+	* .gitignore: Update.
+
 2020-12-20  Dmitry V. Levin  <ldv@altlinux.org>
 
 	* .gitignore: Move subdirectory patterns to separate .gitignore files.
diff --git a/Makefile.am b/Makefile.am
index 818e3599..30f763a9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -47,6 +47,55 @@ distcheck-hook:
 rpm: dist
 	rpmbuild -ts --sign elfutils-@PACKAGE_VERSION@.tar.bz2
 
+if GCOV
+
+COVERAGE_OUTPUT_FILE = $(PACKAGE_NAME).lcov
+COVERAGE_OUTPUT_DIRECTORY = coverage
+COVERAGE_OUTPUT_INDEX_HTML = $(COVERAGE_OUTPUT_DIRECTORY)/index.html
+COVERAGE_TITLE = $(PACKAGE_NAME)-$(PACKAGE_VERSION)
+
+COVERAGE_DIRS = $(filter-out tests,$(SUBDIRS))
+src_COVERAGE_DIRS = $(patsubst %,$(srcdir)/%,$(COVERAGE_DIRS))
+build_COVERAGE_DIRS = $(patsubst %,$(builddir)/%,$(COVERAGE_DIRS))
+all_COVERAGE_DIRS = $(sort $(src_COVERAGE_DIRS) $(build_COVERAGE_DIRS))
+LCOV_DIRS_ARGS = $(patsubst %,--directory=%,$(all_COVERAGE_DIRS))
+
+CLEANFILES = $(COVERAGE_OUTPUT_FILE)
+
+.PHONY: coverage coverage-clean
+
+clean-local: coverage-clean
+distclean-local: coverage-clean
+
+coverage-clean:
+	-rm -rf $(COVERAGE_OUTPUT_DIRECTORY)
+
+coverage: $(COVERAGE_OUTPUT_INDEX_HTML)
+	@echo 'file://$(abs_builddir)/$(COVERAGE_OUTPUT_INDEX_HTML)'
+
+$(COVERAGE_OUTPUT_INDEX_HTML): $(COVERAGE_OUTPUT_FILE)
+	LC_ALL=C $(GENHTML) \
+		--legend \
+		--show-details \
+		--rc=genhtml_branch_coverage=1 \
+		--title='$(COVERAGE_TITLE)' \
+		--prefix='$(abspath $(abs_srcdir))' \
+		--prefix='$(abspath $(abs_builddir)/..)' \
+		--output-directory='$(COVERAGE_OUTPUT_DIRECTORY)' \
+		$<
+
+$(COVERAGE_OUTPUT_FILE):
+	$(LCOV) \
+		--capture \
+		--no-external \
+		--no-checksum \
+		--rc=lcov_branch_coverage=1 \
+		--gcov-tool='$(GCOV)' \
+		--output-file='$@' \
+		$(LCOV_DIRS_ARGS)
+
+endif
+
 # Tell version 3.79 and up of GNU make to not build goals in this
 # directory in parallel.
 .NOTPARALLEL:
diff --git a/configure.ac b/configure.ac
index 60747bc8..346ab800 100644
--- a/configure.ac
+++ b/configure.ac
@@ -311,6 +311,9 @@ if test "$use_gcov" = yes; then
   CFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage"
   CXXFLAGS="$CXXFLAGS -fprofile-arcs -ftest-coverage"
   LDFLAGS="$LDFLAGS -fprofile-arcs"
+  AC_CHECK_PROG([GCOV], [gcov], [gcov])
+  AC_CHECK_PROG([LCOV], [lcov], [lcov])
+  AC_CHECK_PROG([GENHTML], [genhtml], [genhtml])
 fi
 AM_CONDITIONAL(GCOV, test "$use_gcov" = yes)
 
-- 
ldv


More information about the Elfutils-devel mailing list