Skip to content

Commit 69626af

Browse files
committed
build: Add code coverage targets to Makefile
The Makefile gains several new targets that enable gathering code coverage when running the test suite. Using `make coverage` will build a stg executable with coverage instrumentation, run the test suite, merge the thousands of per-invocation coverage profiles into one master profile, generate an html coverage report (in htmlcov/), and print a coverage table to the terminal. Other targets may be used to do subsets of that work. The llvm-cov, llvm-profdata, and rustfilt tools must be installed/available. More information is available about how to install these tools and about code coverage for Rust, in general at: https://doc.rust-lang.org/rustc/instrument-coverage.html
1 parent 3c5bee1 commit 69626af

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/completion/stg.fish
22
/completion/stgit.bash
3+
/htmlcov
34
/target

Makefile

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ STG_PROFILE ?= release
66
CARGO ?= cargo --locked
77
CARGO_OFFLINE = $(CARGO) --offline
88
CARGO_RUN = $(CARGO_OFFLINE) --quiet run --profile=$(STG_PROFILE)
9+
RM ?= rm -f
10+
11+
ifeq ($(STG_PROFILE),dev)
12+
TARGET_DIR = target/debug
13+
else
14+
TARGET_DIR = target/$(STG_PROFILE)
15+
endif
916

1017
export DESTDIR CARGO STG_PROFILE
1118

@@ -70,6 +77,46 @@ unit-test:
7077
.PHONY: lint lint-format lint-clippy lint-api-doc lint-t unit-test
7178

7279

80+
coverage:
81+
$(MAKE) coverage-test
82+
$(MAKE) coverage-html
83+
$(MAKE) coverage-report
84+
85+
coverage-bin:
86+
RUSTFLAGS="-C instrument-coverage" $(CARGO) build --profile=$(STG_PROFILE)
87+
88+
coverage-html: $(TARGET_DIR)/stg.profdata
89+
llvm-cov show \
90+
--instr-profile=$< \
91+
--object=$(TARGET_DIR)/stg \
92+
-ignore-filename-regex='/.cargo/registry' \
93+
-ignore-filename-regex='rustc/' \
94+
-Xdemangler=rustfilt \
95+
-format=html \
96+
-output-dir=htmlcov
97+
98+
coverage-report: $(TARGET_DIR)/stg.profdata
99+
llvm-cov report \
100+
--instr-profile=$< \
101+
--object=$(TARGET_DIR)/stg \
102+
-ignore-filename-regex='/.cargo/registry' \
103+
-ignore-filename-regex='rustc/'
104+
105+
coverage-test: coverage-bin
106+
$(RM) $(TARGET_DIR)/stg.profdata
107+
$(MAKE) $(TARGET_DIR)/stg.profdata
108+
109+
$(TARGET_DIR)/stg.profdata:
110+
-$(RM) -r $(TARGET_DIR)/.profraw
111+
-mkdir $(TARGET_DIR)/.profraw
112+
LLVM_PROFILE_FILE=$(CURDIR)/$(TARGET_DIR)/.profraw/stg-%m-%p.profraw \
113+
$(MAKE) -C t all
114+
llvm-profdata merge -sparse -o $@ $(TARGET_DIR)/.profraw/*.profraw
115+
$(RM) -r $(TARGET_DIR)/.profraw
116+
117+
.PHONY: coverage coverage-bin coverage-html coverage-report coverage-test
118+
119+
73120
format:
74121
$(CARGO_OFFLINE) fmt
75122

@@ -86,6 +133,7 @@ clean:
86133
$(MAKE) -C t clean
87134
$(MAKE) -C completion clean
88135
$(MAKE) -C contrib clean
89-
rm -r target
136+
$(RM) -r target
137+
$(RM) -r htmlcov
90138

91139
.PHONY: format test test-patches clean

0 commit comments

Comments
 (0)