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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
#!/usr/bin/env bash
# build-aux/lint-src - Lint checks for source files
#
# Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
RED=$(tput setaf 1)
RESET=$(tput sgr0)
err() {
printf "${RED}%s${RESET}: %s\n" "$1" "$2" >&2
r=1
}
# `get-dscname FILENAME` reads FILENAME and prints the name that the
# comment at the top of the file self-identifies the file as.
get-dscname() {
if [[ $1 == */Documentation/* && "$(sed 1q -- "$1")" == 'NAME' ]]; then
sed -n '
2{
s,/,_,g;
s,^\s*_,Documentation/,;
s,$,.txt,;
p;
q;
}
' -- "$1"
else
sed -n '
1,3{
/^\#!/d;
/^<!--$/d;
/-\*- .* -\*-/d;
s,[/*\# ]*,,;
s/ - .*//;
p;
q;
}
' -- "$1"
fi
}
{
filetype=$1
filenames=("${@:2}")
r=0
for filename in "${filenames[@]}"; do
# File header ##########################################################
shebang="$(sed -n '1{/^#!/p;}' "$filename")"
if [[ -x $filename && (-z $shebang || $shebang == '#!/hint/'*) ]]; then
err "$filename" 'is executable but does not have a shebang'
elif [[ (-n $shebang && $shebang != '#!/hint/'*) && ! -x $filename ]]; then
err "$filename" 'has a shebang but is not executable'
fi
case "$shebang" in
'') : ;;
'#!/bin/sh') : ;;
'#!/usr/bin/env bash') : ;;
'#!/usr/bin/env python3') : ;;
*) err "$filename" 'has an unrecognized shebang' ;;
esac
if [[ -n $shebang && $shebang != */"$filetype" && $shebang != *' '"$filetype" ]]; then
err "$filename" "wrong shebang for $filetype"
fi
if [[ $filetype != kicad-* ]]; then
if ! grep -E -q 'Copyright \(C\) 202[4-9]((-|, )202[5-9])* Luke T. Shumaker' "$filename"; then
err "$filename" 'is missing a copyright statement'
fi
# if test -e .git && ! git diff --quiet milestone/2025-01-01 HEAD -- "$filename"; then
# if ! grep -E -q 'Copyright \(C\) .*2025 Luke T. Shumaker' "$filename"; then
# err "$filename" 'has an outdated copyright statement'
# fi
# fi
if ! grep -q '\sSPDX-License-Identifier[:] ' "$filename"; then
err "$filename" 'is missing an SPDX-License-Identifier'
fi
dscname_act=$(get-dscname "$filename")
dscname_exp=$(echo "$filename" | sed \
-e 's,.*include/,,' \
-e 's,.*static/,,' \
-e 's/\.wip$//')
if [[ $dscname_act != "$dscname_exp" ]]; then
err "$filename" "self-identifies as $dscname_act (expected $dscname_exp)"
fi
fi
# File body ############################################################
if grep -n --color=auto $'\\S.*\t' "$filename"; then
err "$filename" 'uses tabs for alignment'
fi
done
case "$filetype" in
unknown)
for filename in "${filenames[@]}"; do
err "$filename" 'cannot lint unknown file type'
done
;;
c)
for filename in "${filenames[@]}"; do
if [[ $filename == *.h ]]; then
dscname=$(get-dscname "$filename")
guard=$dscname
guard=${guard#*/config/}
if [[ $guard == */config.h ]]; then
guard=config.h
fi
guard=${guard//'/'/'_'}
guard=${guard//'.'/'_'}
guard="_${guard^^}_"
if ! { grep -Fxq "#ifndef ${guard}" "$filename" &&
grep -Fxq "#define ${guard}" "$filename" &&
grep -Fxq "#endif /* ${guard} */" "$filename"; }; then
err "$filename" "does not have ${guard} guard"
fi
if [[ $filename != libmisc/include/libmisc/obj.h ]] &&
grep -Fn --color=auto -e LO_IMPLEMENTATION_C -e LO_IMPLEMENTATION_STATIC "$filename"; then
err "$filename" "contains LO_IMPLEMENTATION_C and/or LO_IMPLEMENTATION_STATIC"
fi
fi
if [[ $filename == *.c ]]; then
if [[ $filename != libmisc/tests/test_obj.c ]] &&
grep -Fn --color=auto L_IMPLEMENTATION_H "$filename"; then
err "$filename" "contains LO_IMPLEMENTATION_H"
fi
fi
done
;;
sh | bash)
shellcheck "${filenames[@]}" || exit $?
shfmt --diff --case-indent --simplify "${filenames[@]}" || exit $?
;;
python3)
testfiles=()
for filename in "${filenames[@]}"; do
if [[ ${filename##*/} == test_*.py ]]; then
testfiles+=("$filename")
fi
done
set -x
mypy --strict --scripts-are-modules "${filenames[@]}" || exit $?
black --check "${filenames[@]}" || exit $?
isort --check "${filenames[@]}" || exit $?
pylint "${filenames[@]}" || exit $?
pytest "${testfiles[@]}" || exit $?
;;
python3-type-stubs)
set -x
mypy --strict --scripts-are-modules "${filenames[@]}" || exit $?
black --check "${filenames[@]}" || exit $?
isort --check "${filenames[@]}" || exit $?
;;
kicad-symbol-lib)
for filename in "${filenames[@]}"; do
rm -f -- "${filename}.tmp"
kicad-cli sym upgrade --force --output="${filename}.tmp" "$filename" || {
r=1
continue
}
diff -u -- "$filename" "${filename}.tmp" || r=1
rm -f -- "${filename}.tmp"
flags=()
excludes=()
case "$filename" in
*.orig.*)
continue
;;
*RP2040*)
# False-positive about being a power-converter chip.
excludes+=('S4.2')
;;
*W5500*)
# I removed NC pins, in violation of the KLC.
excludes+=('S4.5')
;;
*Micro_SD*)
# Inherit non-centering from kicad-symbols.
excludes+=('S3.1')
# The pads are drawn with stroke.width=0 and
# fill.type=outline, which is OK.
excludes+=('S3.3')
# Grouping pins physically instead of by function
# just seems more "right" for SD.
excludes+=('S4.2')
case "$filename" in
*_Card*)
flags+=(--footprints=.)
# False postive about pin types.
excludes+=('S4.4')
;;
*_Socket*)
# False positive about having a
# default footprint.
excludes+=('S5.1')
;;
esac
;;
*RJ45*)
# Grouping pins physically instead of by function
# just seems more "right" here.
excludes+=('S4.2')
# Being off-center is worth reducing the diff from
# the standard-lib version.
excludes+=('S3.1')
;;
*HDMI_A*)
# False positive about power pin grouping.
excludes+=('S4.2')
# False positive about having a default footprint.
excludes+=('S5.1')
;;
*Relay_SolidState*)
# False positive about "1-Form-A" being the filler-word "a".
excludes+=('S6.2')
;;
esac
if grep -q 'footprints:' -- "$filename"; then
flags+=(--footprints=.)
else
flags+=(--footprints=/usr/share/kicad/footprints)
fi
IFS=,
flags+=("--exclude=${excludes[*]}")
IFS=' '
# We could batch the check_symbol calls, but it's not really a
# speedup, and the flags thing would make the file-order weird.
./3rd-party/kicad-library-utils/klc-check/check_symbol.py \
--verbose --verbose \
--silent \
"${flags[@]}" \
-- "$filename" || r=$?
done
;;
kicad-footprint)
while read -d '' -r dirname; do
if [[ $dirname == *.orig.pretty ]]; then
continue
fi
rm -rf -- "${dirname}.tmp"
kicad-cli fp upgrade --force --output="${dirname}.tmp" "$dirname" || {
r=1
continue
}
diff -ruN --exclude='COPYING.*' -- "$dirname" "${dirname}.tmp" || r=1
rm -rf -- "${dirname}.tmp"
done < <(dirname --zero -- "${filenames[@]}" | sort --zero-terminated --unique)
for filename in "${filenames[@]}"; do
if [[ $filename == *.orig.pretty/* ]]; then
continue
fi
flags=()
excludes=()
case "$filename" in
*Micro_SD_Card*)
# no courtyard
excludes+=('F5.3')
# no datasheet
excludes+=('F9.1')
;;
*TS-1088-AR02016*)
# TODO
excludes+=('F5.2' 'F9.1')
;;
esac
# None of my footprints have 3D models.
excludes+=('F9.3')
IFS=,
flags+=("--exclude=${excludes[*]}")
IFS=' '
# We could batch the check_footprint calls, but it's not really a
# speedup, and the flags thing would make the file-order weird.
./3rd-party/kicad-library-utils/klc-check/check_footprint.py \
--verbose \
--verbose \
--silent \
--errors \
"${flags[@]}" \
-- "$filename" || r=$?
done
;;
kicad-schematic)
for filename in "${filenames[@]}"; do
kicad-cli sch erc --exit-code-violations --output=/dev/stderr \
--severity-warning \
--severity-error \
"$filename" || r=1
./build-aux/kicad-rp2040-check "$filename" || r=1
done
;;
kicad-pcb)
# TODO
# for filename in "${filenames[@]}"; do
# kicad-cli pcb drc --exit-code-violations --output=/dev/stderr \
# --severity-all \
# --schematic-parity \
# "$filename" || r=1
# done
;;
kicad-symbol-lib-table)
readarray -d '' -t sym_libs < <(git ls-files -z ':*.kicad_sym' ':!*.orig.kicad_sym')
for filename in "${filenames[@]}"; do
./3rd-party/kicad-library-utils/klc-check/check_lib_table.py --table="$filename" -- "${sym_libs[@]}" || r=1
done
;;
kicad-footprint-lib-table)
readarray -d '' -t fp_libs < <(git ls-files -z ':*.kicad_mod' ':!*.orig.pretty/*' | xargs -r0 dirname --zero --)
for filename in "${filenames[@]}"; do
./3rd-party/kicad-library-utils/klc-check/check_lib_table.py --table="$filename" -- "${fp_libs[@]}" || r=1
done
;;
kicad-project)
# TODO: Write/adopt linters for these file types
:
;;
make | cmake | gitignore | ini | 9p-idl | 9p-log | markdown | pip | man-cat)
# TODO: Write/adopt linters for these file types
:
;;
*)
err "$0" "unknown filetype: ${filetype}"
;;
esac
exit $r
}
|