aboutsummaryrefslogtreecommitdiff
path: root/tests/check_code_quality.sh
blob: 3c81d92917c70cc559e1cbbb11c25e607e7e93c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env bash

set -euo pipefail

ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P)"
cd "$ROOT"

# Lint and analyze every C source under libffwd/, tests/, and bench/. Each
# platform keeps its own GPU backend and skips the other's, which it does not
# build (macOS builds MLX, Linux builds CUDA).
SYS="$(uname -s)"
SRC_SOURCES=()
for f in libffwd/*.c; do
  [[ "$SYS" == Darwin && "$f" == libffwd/backend_cuda.c ]] && continue
  [[ "$SYS" != Darwin && ( "$f" == libffwd/mlx.c || "$f" == libffwd/backend_mlx.c ) ]] && continue
  SRC_SOURCES+=("$f")
done

TEST_C_SOURCES=(tests/*.c)
BENCH_C_SOURCES=(bench/*.c)

# CUDA backend sources are C++17 and only built on Linux. Parse them with the
# clang CUDA front end in host-only mode so no GPU or device toolchain is
# required; macOS has no CUDA toolkit, so skip them there.
CU_SOURCES=()
if [[ "$SYS" != Darwin ]]; then
  for f in libffwd/*.cu; do
    [[ -e "$f" ]] && CU_SOURCES+=("$f")
  done
fi

ANALYZE_SOURCES=("${SRC_SOURCES[@]}")
TIDY_SOURCES=("${SRC_SOURCES[@]}" "${TEST_C_SOURCES[@]}" "${BENCH_C_SOURCES[@]}")

ANALYZE_COMMON=(
  clang --analyze -Xanalyzer -analyzer-output=text
  -Wall -Wextra
  -I. -Ilibffwd -Ideps/ae
)

PLATFORM_CFLAGS=()
if [[ "$(uname -s)" == "Darwin" ]]; then
  PLATFORM_CFLAGS=(
    -DUSE_BLAS -DACCELERATE_NEW_LAPACK -DUSE_MLX
    -I/opt/homebrew/opt/mlx-c/include
    -I/opt/homebrew/include
    -I/usr/local/include
  )
else
  PLATFORM_CFLAGS=(
    -DUSE_BLAS -DUSE_OPENBLAS
    -I/usr/include/openblas
  )
fi

# Flags for the CUDA backend front end. CUDA_HOME matches the Makefile default.
# --cuda-host-only parses host code only so device codegen and libdevice are not
# needed; the macros mirror the nvcc command in the Makefile.
CUDA_HOME="${CUDA_HOME:-/usr/local/cuda}"
CUDA_CFLAGS=(
  -x cuda --cuda-host-only "--cuda-path=$CUDA_HOME"
  -std=c++17
  -DUSE_BLAS -DUSE_OPENBLAS -DUSE_GPU
  -I"$CUDA_HOME/include"
  -I/usr/include/openblas
)

print_cmd() {
  printf '+'
  printf ' %q' "$@"
  printf '\n'
}

run() {
  print_cmd "$@"
  "$@"
}

run_analyze() {
  local output
  local status

  print_cmd "$@"

  set +e
  output="$("$@" 2>&1)"
  status=$?
  set -e

  if [[ -n "$output" ]]; then
    printf '%s\n' "$output"
  fi

  if (( status != 0 )); then
    return "$status"
  fi

  if grep -q 'warning:' <<<"$output"; then
    echo "clang analyzer warnings found" >&2
    return 1
  fi
}

have() {
  command -v "$1" >/dev/null 2>&1
}

find_clang_tidy() {
  local brew_path="/opt/homebrew/opt/llvm@21/bin/clang-tidy"

  if command -v clang-tidy >/dev/null 2>&1; then
    command -v clang-tidy
    return 0
  fi

  if [[ -x "$brew_path" ]]; then
    printf '%s\n' "$brew_path"
    return 0
  fi

  return 1
}

main() {
  local clang_tidy

  if ! have clang; then
    echo "clang not found" >&2
    return 1
  fi

  run git --no-pager diff --check
  run make cpu CC=clang

  run_analyze "${ANALYZE_COMMON[@]}" "${PLATFORM_CFLAGS[@]}" \
    "${ANALYZE_SOURCES[@]}"
  run_analyze "${ANALYZE_COMMON[@]}" "${PLATFORM_CFLAGS[@]}" \
    -I. -Ilibffwd -Itests "${TEST_C_SOURCES[@]}"
  run_analyze "${ANALYZE_COMMON[@]}" "${PLATFORM_CFLAGS[@]}" \
    -I. -Ilibffwd -Ibench -Itools/server "${BENCH_C_SOURCES[@]}"

  if (( ${#CU_SOURCES[@]} > 0 )); then
    run_analyze "${ANALYZE_COMMON[@]}" "${CUDA_CFLAGS[@]}" \
      "${CU_SOURCES[@]}"
  fi

  if clang_tidy="$(find_clang_tidy)"; then
    run \
      "$clang_tidy" \
      --config-file=.clang-tidy \
      "${TIDY_SOURCES[@]}" \
      -- \
      -std=c17 \
      "${PLATFORM_CFLAGS[@]}" \
      -I. \
      -Ilibffwd \
      -Itests \
      -Ibench \
      -Itools/server \
      -Ideps/ae

    if (( ${#CU_SOURCES[@]} > 0 )); then
      run \
        "$clang_tidy" \
        --config-file=.clang-tidy \
        "${CU_SOURCES[@]}" \
        -- \
        "${CUDA_CFLAGS[@]}" \
        -I. \
        -Ilibffwd \
        -Ideps/ae
    fi
  else
    echo "clang-tidy not found; skipping"
  fi

  if [[ "$(uname -s)" == "Darwin" ]]; then
    run make gpu CC=clang
  fi
}

main "$@"