scripts/check-fmt 2.1 KiB raw
1
#!/bin/sh
2
# Checks that Radiance sources are in the canonical style, without changing
3
# them. Run from the repository top level.
4
#
5
# Usage: check-fmt [--staged | FILE...]
6
#
7
#   FILE...   check the given files in the working tree
8
#   --staged  check the staged versions of staged sources in the formatted scope
9
#   (none)    check every tracked source in the formatted scope
10
#
11
# The compiler is rebuilt first when it is stale. Every unformatted file is
12
# reported; the exit status is 1 if there is any.
13
14
# The scope globs below are git pathspecs, not shell patterns.
15
set -f
16
17
# Sources that are kept in the canonical style.
18
scope="lib/*.rad compiler/*.rad kernel/*.rad test/integration/*.rad
19
    test/host/*.rad"
20
21
# Memory and stack limits of hosted build tools.
22
limits=$(cat seed/emulator-args) || exit 2
23
24
rad=bin/radiance.rv64.dev
25
emulator=${RAD_EMULATOR:-emulator}
26
27
if ! make -s "$rad" >/dev/null; then
28
    echo "check-fmt: cannot build $rad" >&2
29
    exit 2
30
fi
31
32
tmp=
33
trap '[ -z "$tmp" ] || rm -rf "$tmp"' EXIT
34
prefix=
35
36
case ${1-} in
37
    --staged)
38
        [ $# -eq 1 ] || { echo "usage: check-fmt [--staged | FILE...]" >&2; exit 2; }
39
        # shellcheck disable=SC2086
40
        files=$(git diff --cached --name-only --diff-filter=ACMR -- $scope)
41
        [ -n "$files" ] || exit 0
42
        tmp=$(mktemp -d) || exit 2
43
        prefix=$tmp/
44
        # shellcheck disable=SC2086
45
        git checkout-index --prefix="$prefix" -- $files || exit 2
46
        ;;
47
    '')
48
        # shellcheck disable=SC2086
49
        files=$(git ls-files -- $scope)
50
        ;;
51
    *)
52
        files=$(printf '%s\n' "$@")
53
        ;;
54
esac
55
56
# Check one file per process, in parallel. xargs fails if any check does.
57
jobs=$(nproc 2>/dev/null || echo 4)
58
status=0
59
# shellcheck disable=SC2086
60
out=$(printf '%s\n' $files | sed "s|^|$prefix|" |
61
    xargs -P "$jobs" -n 1 "$emulator" $limits -run "$rad" -fmt.check 2>&1) ||
62
    status=1
63
if [ -n "$out" ]; then
64
    if [ -n "$prefix" ]; then
65
        out=$(printf '%s\n' "$out" | sed "s|$prefix||g")
66
    fi
67
    printf '%s\n' "$out" >&2
68
fi
69
70
if [ "$status" -ne 0 ]; then
71
    echo "check-fmt: run 'make fmt' or 'radiance -fmt' on the files above" >&2
72
fi
73
exit $status