From 1f985dec38e387805f300e17500daee523538c03 Mon Sep 17 00:00:00 2001 From: Christian Prochaska Date: Tue, 6 Nov 2018 19:46:50 +0100 Subject: [PATCH] mk: add 'COVERAGE' option 'COVERAGE=yes' in 'target.mk' adds gcov-specific compiler flags and links the program with libgcov. Issue #3048 --- repos/base/mk/dep_prg.mk | 7 +++++++ repos/base/mk/generic.mk | 4 ++++ repos/base/mk/global.mk | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/repos/base/mk/dep_prg.mk b/repos/base/mk/dep_prg.mk index da81f5109..0bc776a9f 100644 --- a/repos/base/mk/dep_prg.mk +++ b/repos/base/mk/dep_prg.mk @@ -16,6 +16,13 @@ include $(BASE_DIR)/mk/util.inc PRG_DIR := $(dir $(TARGET_MK)) include $(TARGET_MK) +# +# Add libgcov if coverage is requested +# +ifeq ($(COVERAGE),yes) +LIBS += libgcov +endif + # # Include lib-import description files # diff --git a/repos/base/mk/generic.mk b/repos/base/mk/generic.mk index e76d713b7..3f4ef8f42 100644 --- a/repos/base/mk/generic.mk +++ b/repos/base/mk/generic.mk @@ -55,6 +55,10 @@ endif %.o: %.cc $(MSG_COMP)$@ $(VERBOSE)$(CXX) $(CXX_DEF) $(CC_CXX_OPT) $(INCLUDES) -c $< -o $@ +ifeq ($(COVERAGE),yes) + $(VERBOSE)mkdir -p $(INSTALL_DIR)/gcov_data/$(TARGET)/$(dir $@) + $(VERBOSE)ln -sf $(CURDIR)/$(@:.o=.gcno) $(INSTALL_DIR)/gcov_data/$(TARGET)/$(@:.o=.gcno) +endif %.o: %.cpp $(MSG_COMP)$@ diff --git a/repos/base/mk/global.mk b/repos/base/mk/global.mk index 84dba1c6c..33aedae19 100644 --- a/repos/base/mk/global.mk +++ b/repos/base/mk/global.mk @@ -103,6 +103,32 @@ ifneq ($(STDLIB),yes) LD_OPT_NOSTDLIB := -nostdlib -Wl,-nostdlib endif +# +# Add coverage options +# +# The directory for the coverage data (generated at runtime) is derived from +# the current build directory and is going to look like +# +# '/genodelabs/bin/test-log_gcov/2018-11-13/gcov_data/test-log_gcov' +# +# to match the path where the gcov note files (generated at build time) are +# stored in the depot package of the test program. +# +# If depot packages are not used, the path is going to look like +# +# '/gcov_data/test-log_gcov' +# +ifeq ($(COVERAGE),yes) +ifneq ($(findstring /depot/,$(CURDIR)),) +PROFILE_DIR = $(shell echo $(CURDIR) | \ + sed -e 's/^.*\/depot\//\//' \ + -e 's/\.build\/.*//')/gcov_data/$(TARGET) +else +PROFILE_DIR = /gcov_data/$(TARGET) +endif +CC_OPT += -fprofile-arcs -ftest-coverage -fprofile-dir=$(PROFILE_DIR) +endif + # # Default optimization and warning levels #