Skip to content

Commit 4affb6a

Browse files
committed
Refactor this as a Makefile target
1 parent cf216b4 commit 4affb6a

File tree

6 files changed

+156
-14
lines changed

6 files changed

+156
-14
lines changed

.github/workflows/c-coverage.yml

Lines changed: 5 additions & 14 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ PGO_PROF_USE_FLAG=@PGO_PROF_USE_FLAG@
5050
LLVM_PROF_MERGER=@LLVM_PROF_MERGER@
5151
LLVM_PROF_FILE=@LLVM_PROF_FILE@
5252
LLVM_PROF_ERR=@LLVM_PROF_ERR@
53+
LLVM_PROFDATA=@LLVM_PROFDATA@
54+
LLVM_COV=@LLVM_COV@
5355
DTRACE= @DTRACE@
5456
DFLAGS= @DFLAGS@
5557
DTRACE_HEADERS= @DTRACE_HEADERS@
@@ -314,6 +316,10 @@ COVERAGE_REPORT=$(abs_builddir)/lcov-report
314316
COVERAGE_LCOV_OPTIONS=--rc lcov_branch_coverage=1
315317
COVERAGE_REPORT_OPTIONS=--rc lcov_branch_coverage=1 --branch-coverage --title "CPython $(VERSION) LCOV report [commit $(shell $(GITVERSION))]"
316318

319+
# report files for llvm-cov coverage report
320+
COVERAGE_INFO_LLVM= $(abs_builddir)/coverage.profdata
321+
COVERAGE_REPORT_LLVM=$(abs_builddir)/llvm-cov-report
322+
COVERAGE_REPORT_OPTIONS_LLVM=-show-branches=count -show-regions
317323

318324
# === Definitions added by makesetup ===
319325

@@ -696,6 +702,44 @@ coverage-report: regen-token regen-frozen
696702
@ # build lcov report
697703
$(MAKE) coverage-lcov
698704

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

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,8 @@ case $CC in
20452045
LLVM_PROF_FILE=""
20462046
;;
20472047
esac
2048+
AC_SUBST(LLVM_COV)
2049+
AC_PATH_TOOL(LLVM_COV, llvm-cov, '', ${llvm_path})
20482050

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

0 commit comments

Comments
 (0)