The meson build system has built-in code coverage support, making it easy to identify lines of code that are not exercised by tests. Meson's code coverage support works with the gcov-based tools gcovr and lcov. This post shows how to use kcov with meson instead so that code coverage can be reported when gcov is unavailable.
How do code coverage tools work?
The gcov-based tools rely on compiler instrumentation, which both gcc and llvm support. Special compiler options instruct the compiler to emit instrumentation in every compiled function in order to record which lines of code are reached.
The kcov tool takes a different approach that does not require compiler support. It uses run-time instrumentation (like breakpoints) instead of compile-time instrumentation. This makes it possible to use kcov on existing binaries without recompilation, as long as debug information is available. The tool maps program instructions to lines of source code using the debug information.
There are pros and cons regarding exact features, performance, limitations, etc. For the most part the gcov approach works well when recompilation is possible and the compiler supports gcov. In other cases kcov is needed.
How to run meson tests under kcov
Meson's built-in code coverage support is designed for gcov and therefore works as a post-processing step after meson test was run. The workflow is different with kcov since the test itself must be run under kcov so it can instrument the process.
Run meson test as follows to get per-test coverage results:
$ meson test --wrapper='kcov kcov-output'
The $BUILD_DIR/kcov-output/ directory will contain the coverage results, one set for each test that was run.
Merging coverage results
If your goal is a single coverage percentage for the entire test suite, then the per-test results need to be merged. The follow wrapper script can be used:
$ cat kcov-wrapper.sh #!/bin/sh test_name=$(basename $1) exec kcov kcov-runs/$test_name "$@"
And it is invoked like this:
$ rm -rf $BUILD_DIR/kcov-runs $ mkdir $BUILD_DIR/kcov-runs $ meson test --wrapper "$SOURCE_DIR/kcov-wrapper.sh" $ rm -rf $BUILD_DIR/kcov-output $ kcov --merge $BUILD_DIR/kcov-output $BUILD_DIR/kcov-runs/*
The merged results are located in the $BUILD_DIR/kcov-output/ directory.
Conclusion
Meson already has built-in support for gcov-based code coverage. If you cannot use gcov, then kcov is an alternative that is fairly easy to integrate into a meson project.