Skip to content

Commit f3412ea

Browse files
committed
Add runtime code coverage support
This feature can be enabled via `--enable-coverage` with `configure`. Related to: freebsd/atf#178 Signed-off-by: Enji Cooper <ngie@FreeBSD.org>
1 parent 066011f commit f3412ea

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎Makefile.am‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2727
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

29+
CODE_COVERAGE_DIRS =
30+
2931
#
3032
# Supporting logic to run our custom testsuite.
3133
#
@@ -198,6 +200,23 @@ installcheck-kyua:
198200
cd $(testsdir) && $(TESTS_ENVIRONMENT) \
199201
$(KYUA) --config='$(KYUA_TEST_CONFIG_FILE)' report --verbose; \
200202
exit $$ret
203+
204+
if CODE_COVERAGE
205+
CODE_COVERAGE_EXCLUDES = '/usr/include/*' '/usr/local/include/*'
206+
CODE_COVERAGE_OUTPUT_DIR ?= code_coverage
207+
EXTRA_GENHTML_ARGS ?=
208+
INSTALLCHECK_TARGETS+= installcheck-kyua-lcov
209+
210+
installcheck-kyua-lcov: installcheck-kyua
211+
mkdir -p $(CODE_COVERAGE_OUTPUT_DIR)/html
212+
$(LCOV) --capture --directory $(srcdir) \
213+
--output-file $(CODE_COVERAGE_OUTPUT_DIR)/lcov.info
214+
$(LCOV) --remove $(CODE_COVERAGE_OUTPUT_DIR)/lcov.info \
215+
$(CODE_COVERAGE_EXCLUDES) \
216+
--output-file $(CODE_COVERAGE_OUTPUT_DIR)/lcov-filtered.info
217+
$(GENHTML) -o $(CODE_COVERAGE_OUTPUT_DIR)/html $(EXTRA_GENHTML_ARGS) \
218+
$(CODE_COVERAGE_OUTPUT_DIR)/lcov-filtered.info
219+
endif
201220
endif
202221
else
203222
DIST_HOOKS += dist-no-atf
@@ -207,6 +226,16 @@ dist-no-atf:
207226
@false
208227
endif
209228

229+
if CODE_COVERAGE
230+
PHONY_TARGETS += mostlyclean-code-coverage
231+
mostlyclean-local: mostlyclean-code-coverage
232+
mostlyclean-code-coverage:
233+
-for dir in $(CODE_COVERAGE_DIRS); do \
234+
find $$dir \( -name \*.gcda -or -name \*.gcno \) -delete -print; \
235+
done
236+
endif
237+
238+
installcheck-local: $(INSTALLCHECK_TARGETS)
210239
if WITH_DOXYGEN
211240
# This should probably be html-local, but it seems better to generate the
212241
# documentation in all cases to get warnings about missing documentation every

‎configure.ac‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ if test ${enable_asan} = yes; then
6464
fi
6565
AM_CONDITIONAL([HAVE_ASAN], [test "${enable_asan}" = yes])
6666

67+
AC_ARG_ENABLE([code-coverage],
68+
AS_HELP_STRING([--enable-code-coverage], [Turn on runtime code coverage]),,
69+
[enable_code_coverage=no])
70+
71+
if test ${enable_code_coverage} = yes; then
72+
AC_MSG_NOTICE([building with code coverage])
73+
enable_developer=yes
74+
CXXFLAGS="${CXXFLAGS} --coverage"
75+
pc_cxxflags="${pc_cxxflags} --coverage"
76+
LDFLAGS="${LDFLAGS} --coverage"
77+
pc_ldflags="${pc_ldflags} --coverage"
78+
fi
79+
AM_CONDITIONAL([CODE_COVERAGE], [test "${enable_code_coverage}" = yes])
80+
6781
AC_ARG_ENABLE([lsan],
6882
AS_HELP_STRING([--enable-lsan], [Turn on LeakSanitizer]),,
6983
[enable_lsan=no])
@@ -136,6 +150,14 @@ KYUA_LUA
136150
AC_PATH_PROG([KYUA], [kyua])
137151
AM_CONDITIONAL([HAVE_KYUA], [test -n "${KYUA}"])
138152
AC_PATH_PROG([GIT], [git])
153+
if test x"${enable_code_coverage}" = xyes; then
154+
AC_MSG_NOTICE([Checking for lcov package])
155+
AC_PATH_PROG([GENHTML], [genhtml])
156+
AC_PATH_PROG([LCOV], [lcov])
157+
if test -z "${GENHTML}" -a -z "${LCOV}"; then
158+
AC_MSG_ERROR([Please install lcov to obtain code coverage])
159+
fi
160+
fi
139161

140162
# Define pkg-config and tests directory
141163
AC_SUBST(pkgconfigdir, \${libdir}/pkgconfig)

0 commit comments

Comments
 (0)