It is possible to generate the coverage data without waiting for #38608 or building a custom rustc, by linking to a pre-built profiler library. It even works with 1.17.0 stable. I've checked on both macOS and Debian.
The problem of skipping the proper fix #38608, AFAIK, are:
- There might be incompatibility problem due to compiler_rt version difference
- The coverage metadata is not stored into the debug info.
Instructions
-
Install the necessary library
- macOS: install Xcode command line tools.
- Debian/Ubuntu:
apt-get install libclang-common-3.8-dev
(orapt-get install clang
to pull it in via dependency) (higher LLVM version should also work)
-
Clean up.
cargo clean && rm -f *.gcda *.gcno
-
Build the test. Only the last 2 lines (
-L… -l…
) are added to OP's instruction.# macOS: cargo rustc -- --test \ -Ccodegen-units=1 \ -Clink-dead-code \ -Cpasses=insert-gcov-profiling \ -Zno-landing-pads \ -L/Library/Developer/CommandLineTools/usr/lib/clang/8.1.0/lib/darwin/ \ -lclang_rt.profile_osx # Debian 9.0 cargo rustc -- --test \ -Ccodegen-units=1 \ -Clink-dead-code \ -Cpasses=insert-gcov-profiling \ -Zno-landing-pads \ -L/usr/lib/llvm-3.8/lib/clang/3.8.1/lib/linux/ \ -lclang_rt.profile-x86_64
-
Verify a
.gcno
file is generated.ls *.gcno
-
Run the test
./target/debug/deps/«name-of-the-executable»
-
Verify a
.gcda
file is generatedls *.gcda
-
Continue from OP's "Creating a coverage report" step.