Skip to content

Commit 59d8b0d

Browse files
committed
format: finish lint and formatting scripts
1 parent 37db2c0 commit 59d8b0d

File tree

6 files changed

+280
-41
lines changed

6 files changed

+280
-41
lines changed

.github/workflows/license.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

scripts/format.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

scripts/license_cfg.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2026 ETH Zurich and University of Bologna.
2+
# Solderpad Hardware License, Version 0.51, see LICENSE for details.
3+
# SPDX-License-Identifier: SHL-0.51
4+
5+
# Authors:
6+
# - Thomas Benz <tbenz@iis.ee.ethz.ch>
7+
8+
header-regex: >
9+
(?:#\!/.*\n)*[/|#]+ Copyright (\(c\))* *([0-9]+) ETH Zurich and University of Bologna\.\n[/|#]+ (Solderpad Hardware License, Version 0\.51|Licensed under the Apache License, Version 2.0), see LICENSE for details\.\n[/|#]+ SPDX-License-Identifier: (SHL-0\.51|Apache-2.0)
10+
11+
excludes:
12+
- CHANGELOG.md
13+
- LICENSE.md
14+
- README.md
15+
- vsim/transcript
16+
17+
exclude-ext:
18+
- aig
19+
- fst
20+
- html
21+
- ini
22+
- jpg
23+
- json
24+
- local
25+
- lock
26+
- log
27+
- patched
28+
- svg
29+
- vcd
30+
- wlf
31+
32+
exclude-paths:
33+
- artistic/artistic
34+
- artistic/meerkat_work
35+
- artistic/renderics
36+
- ihp13/bondpad
37+
- ihp13/patches
38+
- ihp13/pdk
39+
- klayout/out
40+
- openroad/out
41+
- openroad/reports
42+
- openroad/save
43+
- sw/bin
44+
- verilator/obj_dir
45+
- vsim/reports
46+
- vsim/work
47+
- xilinx/build
48+
- xilinx/out
49+
- yosys/out
50+
- yosys/reports
51+
- yosys/tmp
52+
53+
54+
allowed-years:
55+
- 2020
56+
- 2021
57+
- 2022
58+
- 2023
59+
- 2024
60+
- 2025
61+
- 2026

scripts/lint_license.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2026 ETH Zurich and University of Bologna.
2+
# Solderpad Hardware License, Version 0.51, see LICENSE for details.
3+
# SPDX-License-Identifier: SHL-0.51
4+
5+
# Authors:
6+
# - Thomas Benz <tbenz@iis.ee.ethz.ch>
7+
8+
"""Lints the header ensuring consistent file headers"""
9+
import glob
10+
import os
11+
import sys
12+
import re
13+
import yaml
14+
15+
16+
def lint_license(file: str, config: dict) -> int:
17+
"""Lint headers"""
18+
# filter paths
19+
if not os.path.isfile(file):
20+
# print(f'Path ignore: {file}')
21+
return 0
22+
23+
# filter binary files
24+
try:
25+
with open(file, "r", encoding="utf-8") as fh:
26+
content = fh.read()
27+
except UnicodeDecodeError:
28+
return 0
29+
30+
# filter certain dirs
31+
for path in config["exclude-paths"]:
32+
if file.startswith(path):
33+
return 0
34+
35+
# filter extension
36+
ext = file.split(".")[-1]
37+
if ext in config["exclude-ext"]:
38+
return 0
39+
40+
# filter file
41+
if file in config["excludes"]:
42+
return 0
43+
44+
# parse header
45+
header_info = re.findall(config["header-regex"], content)
46+
47+
# check if there is no match at all -> error
48+
if len(header_info) == 0:
49+
print(f"[WARNING][LICENSE-LINT] {file} Wrong or missing license header ")
50+
return 1
51+
52+
# check year
53+
if not int(header_info[0][1]) in config["allowed-years"]:
54+
print(f"[WARNING][LICENSE-LINT] {file} Copyright outdated")
55+
return 1
56+
57+
# all checks pass
58+
print(f"[INFO][LICENSE-LINT] {file}")
59+
return 0
60+
61+
62+
def main():
63+
errors = 0
64+
error_files = []
65+
66+
# get arguments
67+
_, cfg_file = sys.argv
68+
69+
# read cfg file
70+
with open(cfg_file, "r") as cfh:
71+
config = yaml.safe_load(cfh)
72+
73+
# fetch file list
74+
all_files = sorted(glob.glob("**/*", recursive=True))
75+
76+
# check all files
77+
for file in all_files:
78+
if lint_license(file, config):
79+
error_files.append(file)
80+
errors += 1
81+
82+
# return the accumulated errors
83+
return errors
84+
85+
86+
if __name__ == "__main__":
87+
sys.exit(main())

scripts/run_checks.sh

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/bin/bash
2+
# Copyright (c) 2026 ETH Zurich and University of Bologna.
3+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Authors:
7+
# - Thomas Benz <tbenz@iis.ee.ethz.ch>
8+
9+
set -e # Exit on error
10+
set -u # Error on undefined vars
11+
12+
################
13+
# Helpers
14+
################
15+
16+
show_help() {
17+
cat << EOF
18+
Checks Coordinator
19+
20+
Usage:
21+
scripts/run_checks.sh [OPTIONS]
22+
23+
Options:
24+
--help, -h Show this help message
25+
--dry-run, -n Only print commands instead of executing
26+
--verbose, -v Print commands while executing
27+
--sv Lint SystemVerilog files
28+
--license Lint license headers
29+
--python Lint and format python files
30+
--cxx Lint and format C code
31+
32+
Example:
33+
scripts/run_checks.sh --sv --license --python --cxx
34+
35+
EOF
36+
exit 0
37+
}
38+
39+
40+
run_cmd() {
41+
if [ "$DRYRUN" = 1 ]; then
42+
echo $1
43+
else
44+
eval $1
45+
fi
46+
}
47+
48+
49+
lint_sv() {
50+
run_cmd "echo [INFO][Verible] Check SystemVerilog sources"
51+
run_cmd "verible-verilog-lint $(find . -name '*.sv' -exec realpath {} \;) --lint_fatal --waiver_files scripts/verible.waver"
52+
}
53+
54+
55+
lint_license() {
56+
run_cmd "echo [INFO][Verible] Check license headers"
57+
run_cmd "python3 scripts/lint_license.py scripts/license_cfg.yml"
58+
}
59+
60+
61+
lint_and_format_python() {
62+
run_cmd "echo -e [INFO][Black] Formatting Python Code..."
63+
run_cmd "black */*.py"
64+
}
65+
66+
67+
lint_and_format_c() {
68+
run_cmd "echo -e [INFO][ClangFormat] Formatting C Code..."
69+
run_cmd "python scripts/run_clang_format.py -ir sw/ --clang-format-executable=clang-format"
70+
}
71+
72+
73+
####################
74+
# Parse Arguments
75+
####################
76+
77+
DRYRUN=0
78+
79+
# default action if no argument is given
80+
if [ $# -eq 0 ]; then
81+
show_help
82+
return 0
83+
fi
84+
85+
# check for global arguments
86+
for arg in "$@"; do
87+
[[ "$arg" == -v || "$arg" == --verbose ]] && set -x
88+
[[ "$arg" == -n || "$arg" == --dry-run ]] && DRYRUN=1
89+
done
90+
91+
# parse arguments
92+
while [[ $# -gt 0 ]]; do
93+
case "$1" in
94+
--help|-h)
95+
show_help
96+
;;
97+
--verbose|-v)
98+
shift
99+
;;
100+
--dry-run|-n)
101+
shift
102+
;;
103+
# script-specific commands
104+
--sv)
105+
lint_sv
106+
shift
107+
;;
108+
--license)
109+
lint_license
110+
shift
111+
;;
112+
--python)
113+
lint_and_format_python
114+
shift
115+
;;
116+
--cxx)
117+
lint_and_format_c
118+
shift
119+
;;
120+
# Error handling
121+
*)
122+
echo "[ERROR] Unknown option: $1 (use --help for usage)" >&2
123+
exit 1
124+
;;
125+
esac
126+
done

scripts/verible.waiver

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright 2026 ETH Zurich and University of Bologna.
2+
# Solderpad Hardware License, Version 0.51, see LICENSE for details.
3+
# SPDX-License-Identifier: SHL-0.51
4+
5+
# Authors:
6+
# - Thomas Benz <tbenz@iis.ee.ethz.ch>

0 commit comments

Comments
 (0)