Skip to content

Commit 3d05d90

Browse files
committed
Refactor this as a Makefile target
1 parent 8805021 commit 3d05d90

File tree

6 files changed

+156
-14
lines changed

6 files changed

+156
-14
lines changed

.github/workflows/c-coverage.yml

+5-14
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,17 @@ jobs:
4040
uses: hendrikmuhs/ccache-action@v1
4141
- name: Configure clang (with coverage)
4242
run: |
43-
echo "CC=/usr/lib/ccache/clang-12 -fprofile-instr-generate -fcoverage-mapping" >> $GITHUB_ENV
44-
echo "CXX=/usr/lib/ccache/clang++-12 -fprofile-instr-generate -fcoverage-mapping" >> $GITHUB_ENV
43+
echo "CC=clang" >> $GITHUB_ENV
44+
echo "CXX=clang++" >> $GITHUB_ENV
4545
- name: Configure CPython
4646
run: ./configure --with-pydebug --with-openssl=$OPENSSL_DIR
47-
- name: Build CPython
48-
run: make -j4
49-
- name: Collect coverage data
50-
# Specify the LLVM_PROFILE_FILE using %m so multiple shared objects can write
51-
# in parallel. Set the full path to the directory so results aren't written
52-
# into temporary directories created by tests.
53-
# Using "-j 1" is important, or the Github Action runs out of memory
54-
run: LLVM_PROFILE_FILE=${PWD}/python%m.profraw xvfb-run ./python -m test -j 1
5547
- name: Generate coverage report
56-
run: |
57-
llvm-profdata-12 merge -sparse python*.profraw -o python.profdata
58-
llvm-cov-12 show -format=html -output-dir=cpython-coverage -instr-profile=python.profdata -show-branches=count -show-regions python .
48+
# Using "-j1" is important, or the Github Action runs out of memory
49+
run: EXTRATESTOPTS=-j1 xvfb-run make coverage-report-llvm
5950
- name: Publish coverage-report
6051
uses: JamesIves/github-pages-deploy-action@v4
6152
with:
62-
folder: cpython-coverage
53+
folder: llvm-cov-report
6354
repository-name: '' # TODO Destination
6455
token: ${{ secrets.COVERAGE_DEPLOY_TOKEN }} # TODO: Use an organization-level token
6556
single-commit: true

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*.gc??
2222
*.profclang?
2323
*.profraw
24+
*.profdata
2425
*.dyn
2526
.gdb_history
2627
.purify

Makefile.pre.in

+44
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ PGO_PROF_USE_FLAG=@PGO_PROF_USE_FLAG@
5151
LLVM_PROF_MERGER=@LLVM_PROF_MERGER@
5252
LLVM_PROF_FILE=@LLVM_PROF_FILE@
5353
LLVM_PROF_ERR=@LLVM_PROF_ERR@
54+
LLVM_PROFDATA=@LLVM_PROFDATA@
55+
LLVM_COV=@LLVM_COV@
5456
DTRACE= @DTRACE@
5557
DFLAGS= @DFLAGS@
5658
DTRACE_HEADERS= @DTRACE_HEADERS@
@@ -321,6 +323,10 @@ COVERAGE_REPORT=$(abs_builddir)/lcov-report
321323
COVERAGE_LCOV_OPTIONS=--rc lcov_branch_coverage=1
322324
COVERAGE_REPORT_OPTIONS=--rc lcov_branch_coverage=1 --branch-coverage --title "CPython $(VERSION) LCOV report [commit $(shell $(GITVERSION))]"
323325

326+
# report files for llvm-cov coverage report
327+
COVERAGE_INFO_LLVM= $(abs_builddir)/coverage.profdata
328+
COVERAGE_REPORT_LLVM=$(abs_builddir)/llvm-cov-report
329+
COVERAGE_REPORT_OPTIONS_LLVM=-show-branches=count -show-regions
324330

325331
# === Definitions added by makesetup ===
326332

@@ -693,6 +699,44 @@ coverage-report: regen-token regen-frozen
693699
@ # build lcov report
694700
$(MAKE) coverage-lcov
695701

702+
# Compile and calculate coverage with llvm-cov
703+
.PHONY=check-clang coverage-llvm coverage-profdata coverage-report-llvm
704+
705+
# Check whether the compiler is clang, and if not, error out.
706+
check-clang:
707+
($(CC) --version | grep clang) || \
708+
(echo "LLVM coverage only works with clang. Set CC=clang and CXX=clang++ and re-run ./configure"; exit 1)
709+
710+
coverage-llvm: check-clang
711+
@echo "Building with support for coverage checking:"
712+
$(MAKE) clean
713+
@ # Override CC rather than CFLAGS since these flags must come first
714+
$(MAKE) @DEF_MAKE_RULE@ CC="$(CC) -fprofile-instr-generate -fcoverage-mapping"
715+
716+
coverage-profdata:
717+
@echo "Creating Coverage HTML report with llvm-profdata/llvm-cov:"
718+
@rm -f $(COVERAGE_INFO_LLVM)
719+
@rm -rf $(COVERAGE_REPORT_LLVM)
720+
@ # Merge coverage results
721+
$(LLVM_PROFDATA) merge -sparse python*.profraw -o $(COVERAGE_INFO_LLVM)
722+
@ # Generate HTML
723+
$(LLVM_COV) show -format=html -output-dir=$(COVERAGE_REPORT_LLVM) -instr-profile=$(COVERAGE_INFO_LLVM) $(COVERAGE_REPORT_OPTIONS_LLVM) python .
724+
@echo
725+
@echo "llvm-cov report at $(COVERAGE_REPORT_LLVM)/index.html"
726+
@echo
727+
728+
# Force regeneration of parser and importlib
729+
# Specify the LLVM_PROFILE_FILE using %m so multiple shared objects can write
730+
# in parallel. Set the full path to the directory so results aren't written
731+
# into temporary directories created by tests.
732+
coverage-report-llvm: regen-token regen-importlib
733+
@ # build with coverage info
734+
$(MAKE) coverage-llvm
735+
@ # run tests, ignore failures
736+
LLVM_PROFILE_FILE=${PWD}/python%m.profraw $(TESTRUNNER) $(TESTOPTS) || true
737+
@ # build llvm-cov report
738+
$(MAKE) coverage-profdata
739+
696740
# Run "Argument Clinic" over all source files
697741
.PHONY=clinic
698742
clinic: check-clean-src $(srcdir)/Modules/_blake2/blake2s_impl.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A new Makefile target ``coverage-report-llvm`` will use ``clang`` and
2+
``llvm-cov`` to generate a coverage report. This provides more details about
3+
branch coverage and subexpressions than the existing ``gcc`` and ``lcov``
4+
based ``coverage-report``.

configure

+100
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+2
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,8 @@ case $CC in
19571957
LLVM_PROF_FILE=""
19581958
;;
19591959
esac
1960+
AC_SUBST(LLVM_COV)
1961+
AC_PATH_TOOL(LLVM_COV, llvm-cov, '', ${llvm_path})
19601962

19611963
# XXX Shouldn't the code above that fiddles with BASECFLAGS and OPT be
19621964
# merged with this chunk of code?

0 commit comments

Comments
 (0)