1#!/bin/bash
2# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
6# Calculates coverage for the specified crate only
7# Usage:
8# $ ./bin/crate_coverage arch [additional arguments for cargo test]
9# Requirements:
10# $ rustup toolchain install nightly
11# $ cargo install grcov rust-covfix
12set -ex
13cd "${0%/*}/../"
14
15target_dir=$(
16    cargo metadata --no-deps --format-version 1 |
17        jq -r ".target_directory"
18)
19
20# Delete old coverage profiles
21find "$target_dir/debug" -name "*.gcda" -delete
22
23# Run test with coverage profiling
24(cd $1 && CARGO_INCREMENTAL=0 \
25    RUSTFLAGS="-Zprofile -Ccodegen-units=1 -Copt-level=0 -Coverflow-checks=off \
26-Zpanic_abort_tests" \
27    cargo +nightly test "${@:2}")
28
29# Calculate code coverage
30grcov "$target_dir/debug" -s . \
31    --ignore "/*" --ignore-not-existing \
32    -t lcov --llvm --branch \
33    -o /tmp/lcov.info
34
35# Apply code coverage fixes
36rust-covfix /tmp/lcov.info >lcov.info
37