Debian Codegraph @

util/build-run-test-coverage-linux.sh

raw
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 setfacl
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 # Exit the script if an unexpected error arise
28 set -e
29 # Treat unset variables as errors
30 set -u
31 # Ensure pipeline failures are caught (not just the last command's exit code)
32 set -o pipefail
33 # Print expanded commands to stdout before running them
34 set -x
35
36 ME="${0}"
37 ME_dir="$(dirname -- "$(readlink -fm -- "${ME}")")"
38 REPO_main_dir="$(dirname -- "${ME_dir}")"
39
40 # Features to enable for the `coreutils` package
41 FEATURES_OPTION=${FEATURES_OPTION:-"--features=feat_os_unix"}
42 COVERAGE_DIR=${COVERAGE_DIR:-"${REPO_main_dir}/coverage"}
43
44 # Find llvm-profdata (which is used for coverage builds)
45 LLVM_PROFDATA="$(find "$(rustc --print sysroot)" -name llvm-profdata)"
46 if [ -z "${LLVM_PROFDATA}" ]; then
47 echo "Error: llvm-profdata not found. Install it with: rustup component add llvm-tools"
48 exit 1
49 fi
50
51 PROFRAW_DIR="${COVERAGE_DIR}/traces"
52 PROFDATA_DIR="${COVERAGE_DIR}/data"
53 REPORT_DIR="${COVERAGE_DIR}/report"
54 REPORT_PATH="${REPORT_DIR}/total.lcov.info"
55
56 # Some tests set umask(0777) via uutests pre_exec. LLVM profile files created
57 # under that umask can be mode 000; later writes fail with EACCES and the error
58 # lands on utility stderr (breaks exact asserts, e.g. mkfifo).
59 # Default ACL keeps new .profraw files owner-writable. Keep LLVM's bounded
60 # online-merge pool (%4m) instead of one file per process (%p-%m).
61 prepare_profraw_dir() {
62 rm -rf "${PROFRAW_DIR}"
63 mkdir -p "${PROFRAW_DIR}"
64 chmod 700 "${PROFRAW_DIR}"
65 if ! command -v setfacl >/dev/null 2>&1; then
66 if [ -n "${CI:-}" ]; then
67 echo "error: setfacl required for coverage profraw dir (install acl)" >&2
68 exit 1
69 fi
70 return 0
71 fi
72 if ! setfacl -d -m u::rw,g::---,o::---,m::rw "${PROFRAW_DIR}"; then
73 if [ -n "${CI:-}" ]; then
74 echo "error: setfacl default ACL failed on ${PROFRAW_DIR}" >&2
75 exit 1
76 fi
77 fi
78 if ! setfacl -m u::rwx,g::---,o::---,m::rwx "${PROFRAW_DIR}"; then
79 if [ -n "${CI:-}" ]; then
80 echo "error: setfacl access ACL failed on ${PROFRAW_DIR}" >&2
81 exit 1
82 fi
83 fi
84 }
85
86 prepare_profraw_dir
87 rm -rf "${PROFDATA_DIR}" && mkdir -p "${PROFDATA_DIR}"
88 rm -rf "${REPORT_DIR}" && mkdir -p "${REPORT_DIR}"
89
90 #shellcheck disable=SC2086
91 UTIL_LIST=$("${ME_dir}"/show-utils.sh ${FEATURES_OPTION})
92
93 export RUSTC_BOOTSTRAP=1
94 export CARGO_INCREMENTAL=0
95 export RUSTFLAGS="-Cinstrument-coverage -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort"
96 export RUSTDOCFLAGS="-Cpanic=abort"
97 export LLVM_PROFILE_FILE="${PROFRAW_DIR}/coverage-%4m.profraw"
98
99 # Disable expanded command printing for the rest of the program
100 set +x
101
102 run_test_and_aggregate() {
103 echo "# Running coverage tests for ${1}"
104
105 # Build and run tests for the UTIL
106 cargo nextest run \
107 --profile coverage \
108 --no-fail-fast \
109 --color=always \
110 2>&1 \
111 ${2} \
112 | grep -v 'SKIP'
113 # Note: Do not print the skipped tests on the output as there will be many.
114
115 echo "## Tests for (${1}) generated $(du -h -d1 ${PROFRAW_DIR} | cut -f 1) of profraw files"
116
117 # Aggregate all the trace files into a profdata file
118 PROFDATA_FILE="${PROFDATA_DIR}/${1}.profdata"
119 echo "## Aggregating coverage files under ${PROFDATA_FILE}"
120 "${LLVM_PROFDATA}" merge \
121 -sparse \
122 -o ${PROFDATA_FILE} \
123 ${PROFRAW_DIR}/*.profraw \
124 || true
125 # We don't want an error in `llvm-profdata` to abort the whole program
126 }
127
128 for UTIL in ${UTIL_LIST}; do
129
130 if [ "${UTIL}" = "stty" ]; then
131 run_test_and_aggregate \
132 "${UTIL}" \
133 "-p coreutils -p uu_${UTIL} -E test(/^test_${UTIL}::/) ${FEATURES_OPTION}"
134 else
135 run_test_and_aggregate \
136 "${UTIL}" \
137 "-p coreutils -E test(/^test_${UTIL}::/) ${FEATURES_OPTION}"
138 fi
139
140 echo "## Clear the trace directory to free up space"
141 prepare_profraw_dir
142 done;
143
144 echo "Running coverage tests over uucore"
145 run_test_and_aggregate "uucore" "-p uucore --all-features"
146
147 echo "# Aggregating all the profraw files under ${REPORT_PATH}"
148 grcov \
149 "${PROFDATA_DIR}" \
150 --binary-path "${REPO_main_dir}/target/debug/" \
151 --output-types lcov \
152 --output-path ${REPORT_PATH} \
153 --llvm \
154 --excl-start "^mod test.*\{" \
155 --excl-stop "^\}" \
156 --keep-only "${REPO_main_dir}"'/src/*'
157
158
159 # Notify the report file to github
160 echo "report=${REPORT_PATH}" >> $GITHUB_OUTPUT