|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# spell-checker:ignore (env/flags) Ccodegen Cinstrument Coverflow Cpanic Zpanic |
| 4 | +# spell-checker:ignore PROFDATA PROFRAW coreutil librairies nextest profdata profraw rustlib |
| 5 | + |
| 6 | +# This script will build, run and generate coverage reports for the whole |
| 7 | +# testsuite. |
| 8 | +# The biggest challenge of this process is managing the overwhelming generation |
| 9 | +# of trace files that are generated after EACH SINGLE invocation of a coreutil |
| 10 | +# in the testsuite. Moreover, because we run the testsuite against the multicall |
| 11 | +# binary, each trace file contains coverage information about the WHOLE |
| 12 | +# multicall binary, dependencies included, which results in a 5-6 MB file. |
| 13 | +# Running the testsuite easily creates +80 GB of trace files, which is |
| 14 | +# unmanageable in a CI environment. |
| 15 | +# |
| 16 | +# A workaround is to run the testsuite util per util, generate a report per |
| 17 | +# util, and remove the trace files. Therefore, we end up with several reports |
| 18 | +# that will get uploaded to codecov afterwards. The issue with this |
| 19 | +# approach is that the `grcov` call, which is responsible for transforming |
| 20 | +# `.profraw` trace files into a `lcov` file, takes a lot of time (~20s), mainly |
| 21 | +# because it has to browse all the sources. So calling it for each of the 100 |
| 22 | +# utils (with --all-features) results in an absurdly long execution time |
| 23 | +# (almost an hour). |
| 24 | + |
| 25 | +# TODO: Do not instrument 3rd party librairies to save space and performance |
| 26 | + |
| 27 | +set -ux |
| 28 | + |
| 29 | +FEATURES_OPTION="--features feat_os_unix" |
| 30 | + |
| 31 | +ME="${0}" |
| 32 | +ME_dir="$(dirname -- "$(readlink -fm -- "${ME}")")" |
| 33 | +REPO_main_dir="$(dirname -- "${ME_dir}")" |
| 34 | + |
| 35 | +LLVM_BIN_PATH="$(rustc --print sysroot)/lib/rustlib/x86_64-unknown-linux-gnu/bin" |
| 36 | +LLVM_PROFDATA="${LLVM_BIN_PATH}/llvm-profdata" |
| 37 | + |
| 38 | +PROFRAW_DIR="${REPO_main_dir}/coverage/traces" |
| 39 | +PROFDATA_DIR="${REPO_main_dir}/coverage/data" |
| 40 | +REPORT_DIR="${REPO_main_dir}/coverage/report" |
| 41 | + |
| 42 | +rm -rf "${PROFRAW_DIR}" && mkdir -p "${PROFRAW_DIR}" |
| 43 | +rm -rf "${PROFDATA_DIR}" && mkdir -p "${PROFDATA_DIR}" |
| 44 | +rm -rf "${REPORT_DIR}" && mkdir -p "${REPORT_DIR}" |
| 45 | + |
| 46 | +#shellcheck disable=SC2086 |
| 47 | +UTIL_LIST=$("${ME_dir}"/show-utils.sh ${FEATURES_OPTION}) |
| 48 | + |
| 49 | + |
| 50 | +export CARGO_INCREMENTAL=0 |
| 51 | +export RUSTFLAGS="-Cinstrument-coverage -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort" |
| 52 | +export RUSTDOCFLAGS="-Cpanic=abort" |
| 53 | +export RUSTUP_TOOLCHAIN="nightly-gnu" |
| 54 | +export LLVM_PROFILE_FILE="${PROFRAW_DIR}/coverage-%m-%p.profraw" |
| 55 | + |
| 56 | +for UTIL in ${UTIL_LIST}; do |
| 57 | + # Build and run tests for the UTIL |
| 58 | + cargo nextest run \ |
| 59 | + --profile coverage \ |
| 60 | + --no-fail-fast \ |
| 61 | + ${FEATURES_OPTION} \ |
| 62 | + -p coreutils \ |
| 63 | + -E "test(test_${UTIL})" # Filter to only run tests against the selected util. |
| 64 | + |
| 65 | + echo -e "${UTIL} trace generation: $(du -h -d1 ${PROFRAW_DIR})" >> "${REPORT_DIR}/usage" |
| 66 | + |
| 67 | + # Aggregate all the profraw files into a profdata file |
| 68 | + "${LLVM_PROFDATA}" merge \ |
| 69 | + -sparse \ |
| 70 | + -o "${PROFDATA_DIR}/${UTIL}.profdata" \ |
| 71 | + ${PROFRAW_DIR}/*.profraw |
| 72 | + |
| 73 | + # Clear the trace directory to free up space |
| 74 | + rm -rf "${PROFRAW_DIR}" && mkdir -p "${PROFRAW_DIR}" |
| 75 | +done; |
| 76 | + |
| 77 | +# Aggregate all the profraw files into a lcov file |
| 78 | +grcov \ |
| 79 | + "${PROFDATA_DIR}" \ |
| 80 | + --binary-path "${REPO_main_dir}/target/debug/coreutils" \ |
| 81 | + --output-types lcov \ |
| 82 | + --output-path "${REPORT_DIR}/${UTIL}.lcov.info" \ |
| 83 | + --llvm \ |
| 84 | + --keep-only "${REPO_main_dir}"'/src/*' |
0 commit comments