Debian Codegraph @

devtools/gen-fortran-coverage.sh

raw
1 #!/usr/bin/env bash
2 # Generate .gcov coverage reports for the libMBD Fortran sources.
3 #
4 # Codecov's built-in gcov pass (and gcovr) mis-parse the cpp-preprocessed
5 # *.F90 sources: they report only a fraction of the executable lines and zero
6 # out fully exercised files (e.g. mbd_coulomb, mbd_c_api). Invoking `gcov`
7 # directly maps the preprocessed line table back onto the original source
8 # correctly, so we produce the reports here and hand them to Codecov with its
9 # own gcov plugin disabled (plugins: noop).
10 #
11 # Usage: gen-fortran-coverage.sh [OUTDIR]
12 # BLDDIR build directory containing the .gcda data (default: build)
13 # GCOV gcov executable to use (default: derived from $FC / gfortran)
14 set -euo pipefail
15
16 BLDDIR=${BLDDIR:-build}
17 outdir=${1:-coverage-fortran}
18
19 # A gcov that does not match the compiler silently emits empty data
20 # ("version 'B13*', prefer 'A13*'"), so derive it from the Fortran compiler:
21 # gfortran -> gcov, gfortran-13 -> gcov-13, <triple>-gfortran -> <triple>-gcov.
22 if [[ -z "${GCOV:-}" ]]; then
23 fc=$(command -v "${FC:-gfortran}" 2>/dev/null || true)
24 if [[ -n "$fc" ]]; then
25 GCOV=$(printf '%s' "$fc" | sed -E 's#gfortran(-[0-9]+)?$#gcov\1#')
26 else
27 GCOV=gcov
28 fi
29 fi
30 command -v "$GCOV" >/dev/null 2>&1 || { echo "gcov executable '$GCOV' not found" >&2; exit 1; }
31
32 # Only the library objects matter; the test-executable .gcda cover the test
33 # sources, which are ignored in codecov.yml.
34 absbld=$(cd "$BLDDIR" && pwd)
35 # Collect into an array without `mapfile`, which the bash 3.2 shipped on macOS
36 # does not provide.
37 gcda=()
38 while IFS= read -r f; do
39 gcda+=("$f")
40 done < <(find "$absbld" -path '*mbd.dir*' -name '*.gcda')
41 if [[ ${#gcda[@]} -eq 0 ]]; then
42 echo "no .gcda coverage data found under $BLDDIR" >&2
43 exit 1
44 fi
45
46 # gcov writes .gcov files into the current directory; -p keeps the full path in
47 # the name so the differently-instantiated *.F90 don't collide.
48 mkdir -p "$outdir"
49 ( cd "$outdir" && "$GCOV" -p "${gcda[@]}" ) >/dev/null
50
51 # nullglob so the loop/array is empty (not the literal pattern) when gcov
52 # produced nothing, e.g. a gcov/compiler version mismatch.
53 shopt -s nullglob
54 gcovs=("$outdir"/*.gcov)
55 shopt -u nullglob
56 if [[ ${#gcovs[@]} -eq 0 ]]; then
57 echo "$GCOV produced no .gcov files (gcov/compiler version mismatch?)" >&2
58 exit 1
59 fi
60
61 # gfortran flags lines whose sub-blocks were not all taken with a trailing '*'
62 # on the execution count (e.g. "1404*:"). gcov still counts these as executed,
63 # but Codecov's .gcov parser treats '*' lines as not-hit, which roughly halves
64 # the reported line coverage. Strip the marker so executed lines are plain hits
65 # and Codecov matches gcov's own line-coverage numbers. (sed without -i for
66 # BSD/GNU portability.)
67 for g in "${gcovs[@]}"; do
68 sed -E 's/^( *[0-9]+)\*:/\1:/' "$g" > "$g.tmp" && mv "$g.tmp" "$g"
69 done
70
71 echo "generated ${#gcovs[@]} .gcov file(s) in $outdir using $GCOV"