if GCOV_ENABLED

gcov: all
	@echo ""; \
	 project=`basename $(abs_srcdir)`; \
	 total_covered=0; \
	 total_actual=0; \
	 stats_length=`expr length "   9999 / 9999   100% "`; \
	 filename_width=`expr 80 - 2 - $$stats_length`; \
	 echo -e "Test coverage for $$project:\n"; \
	 for file in $(filter %.c,$(gcov_sources)); do \
		 gcov -o .libs/$${file/.c/.gcda} $$file > /dev/null; \
		 if test -f $$file.gcov; then \
		   actual=`grep -v ' -:' $$file.gcov | wc -l`; \
		   uncovered=`grep '#####:' $$file.gcov | wc -l`; \
		   covered=$$((actual - uncovered)); \
		   percent=$$((($$covered * 100) / $$actual)); \
		   total_covered=$$((total_covered + covered)); \
		   total_actual=$$((total_actual + actual)); \
		   filename=`echo $$file: | sed "s/^\.\///g"`; \
	     printf "  %-*s   %4i / %4i   %3i%%\n" $$filename_width $$filename \
	                                           $$covered $$actual $$percent; \
		 fi \
	 done; \
	 printf "\nSource statements:  %4i" $$total_actual; \
	 printf "\nCovered statements: %4i" $$total_covered; \
	 printf "\nTotal coverage:     %3i%%\n" \
	        $$((($$total_covered * 100) / $$total_actual))

else

gcov:
	@echo "You need to reconfigure with --enable-gcov"

endif	# GCOV_ENABLED

.PHONY: gcov
