From c5f46706b0a023968f88378b5a3009155aca3958 Mon Sep 17 00:00:00 2001
Message-Id: <c5f46706b0a023968f88378b5a3009155aca3958.1335897988.git.blauwirbel@gmail.com>
From: Blue Swirl <blauwirbel@gmail.com>
Date: Tue, 1 May 2012 18:45:39 +0000
Subject: [PATCH] tests: add gcov target

Add support for compiling for GCOV test coverage, enabled
with '--enable-gcov' during configure.

After tests, test coverage can be reported with 'make gcov'.

Add LDFLAGS (which may include GCOV flags) to libcacard
Makefile to avoid a build breakage.

Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
---
 configure          |   18 ++++++++++++++++++
 libcacard/Makefile |    2 +-
 tests/Makefile     |   33 +++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 1 deletions(-)

diff --git a/configure b/configure
index 3c72fa0..dfea46a 100755
--- a/configure
+++ b/configure
@@ -152,6 +152,8 @@ strip_opt="yes"
 tcg_interpreter="no"
 bigendian="no"
 mingw32="no"
+gcov="no"
+gcov_tool="gcov"
 EXESUF=""
 prefix="/usr/local"
 mandir="\${prefix}/share/man"
@@ -556,6 +558,8 @@ for opt do
   ;;
   --python=*) python="$optarg"
   ;;
+  --gcov=*) gcov_tool="$optarg"
+  ;;
   --smbd=*) smbd="$optarg"
   ;;
   --extra-cflags=*)
@@ -576,6 +580,8 @@ for opt do
   ;;
   --enable-gprof) gprof="yes"
   ;;
+  --enable-gcov) gcov="yes"
+  ;;
   --static)
     static="yes"
     LDFLAGS="-static $LDFLAGS"
@@ -1110,6 +1116,8 @@ echo "  --disable-guest-agent    disable building of the QEMU Guest Agent"
 echo "  --enable-guest-agent     enable building of the QEMU Guest Agent"
 echo "  --with-coroutine=BACKEND coroutine backend. Supported options:"
 echo "                           gthread, ucontext, sigaltstack, windows"
+echo "  --enable-gcov            enable test coverage analysis with gcov"
+echo "  --gcov=GCOV              use specified gcov [$gcov_tool]"
 echo ""
 echo "NOTE: The object files are built at the place where configure is launched"
 exit 1
@@ -2883,6 +2891,11 @@ if test "$mingw32" = "yes" ; then
     done
 fi
 
+if test "$gcov" = "yes" ; then
+  QEMU_CFLAGS="-fprofile-arcs -ftest-coverage $QEMU_CFLAGS"
+  LDFLAGS="-fprofile-arcs -ftest-coverage $LDFLAGS"
+fi
+
 qemu_confdir=$sysconfdir$confsuffix
 qemu_datadir=$datadir$confsuffix
 
@@ -3008,6 +3021,8 @@ echo "OpenGL support    $opengl"
 echo "libiscsi support  $libiscsi"
 echo "build guest agent $guest_agent"
 echo "coroutine backend $coroutine_backend"
+echo "gcov              $gcov_tool"
+echo "gcov enabled      $gcov"
 
 if test "$sdl_too_old" = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -3412,6 +3427,9 @@ echo "LIBS_TOOLS+=$libs_tools" >> $config_host_mak
 echo "EXESUF=$EXESUF" >> $config_host_mak
 echo "LIBS_QGA+=$libs_qga" >> $config_host_mak
 echo "POD2MAN=$POD2MAN" >> $config_host_mak
+if test "$gcov" = "yes" ; then
+  echo "GCOV=$gcov_tool" >> $config_host_mak
+fi
 
 # generate list of library paths for linker script
 
diff --git a/libcacard/Makefile b/libcacard/Makefile
index c6a896a..47a5eb1 100644
--- a/libcacard/Makefile
+++ b/libcacard/Makefile
@@ -18,7 +18,7 @@ QEMU_CFLAGS+=-I../
 libcacard.lib-y=$(addsuffix .lo,$(basename $(libcacard-y)))
 
 vscclient: $(libcacard-y) $(QEMU_OBJS) vscclient.o
-	$(call quiet-command,$(CC) -o $@ $^ $(libcacard_libs) $(LIBS),"  LINK  $@")
+	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $^ $(libcacard_libs) $(LIBS),"  LINK  $@")
 
 clean:
 	rm -f *.o */*.o *.d */*.d *.a */*.a *~ */*~ vscclient *.lo .libs/* *.la *.pc
diff --git a/tests/Makefile b/tests/Makefile
index 9988681..a316180 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,18 +1,42 @@
 export SRC_PATH
 
 check-unit-y = tests/check-qdict$(EXESUF)
+GCOV_FILES = qdict.c
 check-unit-y += tests/check-qfloat$(EXESUF)
+GCOV_FILES += qfloat.c
 check-unit-y += tests/check-qint$(EXESUF)
+GCOV_FILES += qint.c
 check-unit-y += tests/check-qstring$(EXESUF)
+GCOV_FILES += qstring.c
 check-unit-y += tests/check-qlist$(EXESUF)
+GCOV_FILES += qlist.c
 check-unit-y += tests/check-qjson$(EXESUF)
+GCOV_FILES += qjson.c
 check-unit-y += tests/test-qmp-output-visitor$(EXESUF)
+GCOV_QAPI_FILES += qapi/qmp-output-visitor.c
 check-unit-y += tests/test-qmp-input-visitor$(EXESUF)
+GCOV_QAPI_FILES += qapi/qmp-input-visitor.c
 check-unit-y += tests/test-qmp-input-strict$(EXESUF)
 check-unit-y += tests/test-qmp-commands$(EXESUF)
+GCOV_QAPI_FILES += qapi/qmp-dispatch.c
 check-unit-y += tests/test-string-input-visitor$(EXESUF)
+GCOV_QAPI_FILES += qapi/string-input-visitor.c
 check-unit-y += tests/test-string-output-visitor$(EXESUF)
+GCOV_QAPI_FILES += qapi/string-output-visitor.c
 check-unit-y += tests/test-coroutine$(EXESUF)
+ifeq ($(CONFIG_WIN32),y)
+GCOV_FILES += coroutine-win32.c
+else
+ifeq ($(CONFIG_UCONTEXT_COROUTINE),y)
+GCOV_FILES += coroutine-ucontext.c
+else
+ifeq ($(CONFIG_SIGALTSTACK_COROUTINE),y)
+GCOV_FILES += coroutine-sigaltstack.c
+else
+GCOV_FILES += coroutine-gthread.c
+endif
+endif
+endif
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -20,8 +44,10 @@ check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 # really in libqtest, not in the testcases themselves.
 check-qtest-i386-y = tests/rtc-test
 check-qtest-x86_64-y = $(check-qtest-i386-y)
+GCOV_I386_SOFTMMU_FILES += hw/mc146818rtc.c
 check-qtest-sparc-y = tests/m48t59-test$(EXESUF)
 check-qtest-sparc64-y = tests/m48t59-test$(EXESUF)
+GCOV_LIBHW64_FILES += hw/m48t59.c
 
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h
 
@@ -87,6 +113,7 @@ check-help:
 	@echo " make check-unit           Run qobject tests"
 	@echo " make check-block          Run block tests"
 	@echo " make check-report.html    Generates an HTML test report"
+	@echo " make gcov                 Report test coverage"
 	@echo
 	@echo "Please note that HTML reports do not regenerate if the unit tests"
 	@echo "has not changed."
@@ -126,6 +153,12 @@ check-report.xml: $(patsubst %,check-report-qtest-%.xml, $(QTEST_TARGETS)) check
 check-report.html: check-report.xml
 	$(call quiet-command,gtester-report $< > $@, "  GEN    $@")
 
+# Gcov test coverage
+gcov:
+	$(GCOV) -n $(GCOV_FILES)
+	(cd i386-softmmu; $(GCOV) -n $(GCOV_I386_SOFTMMU_FILES))
+	(cd libhw64; $(GCOV) -n $(GCOV_LIBHW64_FILES))
+	(cd qapi; $(GCOV) -n $(GCOV_QAPI_FILES))
 
 # Other tests
 
-- 
1.7.2.5

