Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49e52f9

Browse files
committed
Issue #18481: Add C coverage reporting with gcov and lcov. A new make target
"coverage-report" creates an instrumented Python build, runs unit tests and creates a HTML. The report can be updated with "make coverage-lcov".
1 parent 3b998d1 commit 49e52f9

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

.hgignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Modules/Setup.local
3636
Modules/config.c
3737
Modules/ld_so_aix$
3838
Parser/pgen$
39+
^lcov-report/
3940
^core
4041
^python-gdb.py
4142
^python.exe-gdb.py
@@ -91,3 +92,7 @@ Modules/_testembed
9192
.coverage
9293
coverage/
9394
htmlcov/
95+
*.gcda
96+
*.gcno
97+
*.gcov
98+
coverage.info

Makefile.pre.in

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ HOST_GNU_TYPE= @host@
211211
PROFILE_TASK= $(srcdir)/Tools/pybench/pybench.py -n 2 --with-gc --with-syscheck
212212
#PROFILE_TASK= $(srcdir)/Lib/test/regrtest.py
213213

214+
# report files for gcov / lcov coverage report
215+
COVERAGE_INFO= $(abs_builddir)/coverage.info
216+
COVERAGE_REPORT=$(abs_builddir)/lcov-report
217+
COVERAGE_REPORT_OPTIONS=--no-branch-coverage --title "CPython lcov report"
218+
219+
214220
# === Definitions added by makesetup ===
215221

216222

@@ -463,11 +469,48 @@ run_profile_task:
463469
build_all_use_profile:
464470
$(MAKE) all CFLAGS="$(CFLAGS) -fprofile-use -fprofile-correction"
465471

472+
# Compile and run with gcov
473+
.PHONY=coverage coverage-lcov coverage-report
466474
coverage:
467475
@echo "Building with support for coverage checking:"
468-
$(MAKE) clean
476+
$(MAKE) clean profile-removal
469477
$(MAKE) all CFLAGS="$(CFLAGS) -O0 -pg -fprofile-arcs -ftest-coverage" LIBS="$(LIBS) -lgcov"
470478

479+
coverage-lcov:
480+
@echo "Creating Coverage HTML report with LCOV:"
481+
@rm -f $(COVERAGE_INFO)
482+
@rm -rf $(COVERAGE_REPORT)
483+
@lcov --capture --directory $(abs_builddir) \
484+
--base-directory $(realpath $(abs_builddir)) \
485+
--path $(realpath $(abs_srcdir)) \
486+
--output-file $(COVERAGE_INFO)
487+
: # remove 3rd party modules and system headers
488+
@lcov --remove $(COVERAGE_INFO) \
489+
'*/Modules/_ctypes/libffi*/*' \
490+
'*/Modules/_sha3/keccak/*' \
491+
'*/Modules/_decimal/libmpdec/*' \
492+
'*/Modules/expat/*' \
493+
'*/Modules/zlib/*' \
494+
'*/Include/*' \
495+
'/usr/include/*' \
496+
'/usr/local/include/*' \
497+
--output-file $(COVERAGE_INFO)
498+
@genhtml $(COVERAGE_INFO) --output-directory $(COVERAGE_REPORT) \
499+
$(COVERAGE_REPORT_OPTIONS)
500+
@echo
501+
@echo "lcov report at $(COVERAGE_REPORT)/index.html"
502+
@echo
503+
504+
coverage-report:
505+
: # force rebuilding of parser and importlib
506+
@touch $(GRAMMAR_INPUT)
507+
@touch $(srcdir)/Lib/importlib/_bootstrap.py
508+
: # build with coverage info
509+
$(MAKE) coverage
510+
: # run tests, ignore failures
511+
$(TESTRUNNER) $(TESTOPTS) || true
512+
: # build lcov report
513+
$(MAKE) coverage-lcov
471514

472515
# Build the interpreter
473516
$(BUILDPYTHON): Modules/python.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY)
@@ -1396,6 +1439,8 @@ clean: pycremoval
13961439

13971440
profile-removal:
13981441
find . -name '*.gc??' -exec rm -f {} ';'
1442+
rm -f $(COVERAGE_INFO)
1443+
rm -rf $(COVERAGE_REPORT)
13991444

14001445
clobber: clean profile-removal
14011446
-rm -f $(BUILDPYTHON) $(PGEN) $(LIBRARY) $(LDLIBRARY) $(DLLLIBRARY) \

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,10 @@ IDLE
725725
Build
726726
-----
727727

728+
- Issue #18481: Add C coverage reporting with gcov and lcov. A new make target
729+
"coverage-report" creates an instrumented Python build, runs unit tests
730+
and creates a HTML. The report can be updated with "make coverage-lcov".
731+
728732
- Issue #17845: Clarified the message printed when some module are not built.
729733

730734
- Issue #18256: Compilation fix for recent AIX releases. Patch by

0 commit comments

Comments
 (0)