Files
servo/etc/coverage_workspace_wrapper.py
Jonathan Schwender 38d977343c Add code-coverage option to mach run, test-wpt and test-devtools (#39916)
Add a `--coverage` flag to `./mach build` and a `./mach coverage-report`
command to use `cargo llvm-cov` to generate a coverage report from the
raw profiles.

The workflow is: 
```
./mach build --coverage [--profile <cargo_profile>]
# Or test-wpt or test-devtools
./mach run --coverage [--profile <cargo_profile>]
# Note, that coverage-report needs to know the cargo build profile.
./mach coverage-report [--profile <cargo_profile>] [optional parameters for cargo-llvm-cov]
```
According to the LLVM documentation on source based coverage, the
optimization profile should not influence the accuracy of the coverage
profile, so we can gather coverage data from optimized builds.

Note that `./mach test-devtools --coverage` will not produce any
coverage profiles yet, since the test runner kills the servo binary,
which prevents writing the profile data at shutdown.
The same problem also affects `test-wpt` with `servodriver`, which will
be fixed by https://github.com/servo/servo/pull/40455.

Testing: Manually tested. A CI workflow to test wpt coverage will be
added in a follow-up PR.

---------

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
2025-11-07 14:38:24 +00:00

34 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright 2025 The Servo Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
"""
This is a simple script intended to be used as a `RUSTC_WORKSPACE_WRAPPER`, adding the
required flags for code coverage instrumentation, only to the local libraries in our
workspace. This reduces the runtime overhead and the profile size significantly.
We are not interested in the code coverage metrics of outside dependencies anyway.
"""
import os
import sys
def main():
# The first argument is the path to rustc, followed by its arguments
args = sys.argv[1:]
args += ["-Cinstrument-coverage=true", "--cfg=coverage"]
# Execute rustc with the modified arguments
os.execvp(args[0], args)
if __name__ == "__main__":
main()