Skip to content

Commit 88ddb73

Browse files
authored
Merge pull request #598 from ChinYikMing/shfmt
Add shell script formatter
2 parents e32a84a + 6d54805 commit 88ddb73

File tree

10 files changed

+113
-65
lines changed

10 files changed

+113
-65
lines changed

.ci/boot-linux-prepare.sh

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
check_platform
66

77
VBLK_IMG=build/disk.img
8-
which dd >/dev/null 2>&1 || { echo "Error: dd not found"; exit 1; }
9-
which mkfs.ext4 >/dev/null 2>&1 || which $(brew --prefix e2fsprogs)/sbin/mkfs.ext4 >/dev/null 2>&1 || \
10-
{ echo "Error: mkfs.ext4 not found"; exit 1; }
11-
which 7z >/dev/null 2>&1 || { echo "Error: 7z not found"; exit 1; }
8+
which dd > /dev/null 2>&1 || {
9+
echo "Error: dd not found"
10+
exit 1
11+
}
12+
which mkfs.ext4 > /dev/null 2>&1 || which $(brew --prefix e2fsprogs)/sbin/mkfs.ext4 > /dev/null 2>&1 \
13+
|| {
14+
echo "Error: mkfs.ext4 not found"
15+
exit 1
16+
}
17+
which 7z > /dev/null 2>&1 || {
18+
echo "Error: 7z not found"
19+
exit 1
20+
}
1221

1322
ACTION=$1
1423

@@ -23,11 +32,11 @@ case "$ACTION" in
2332
mkfs.ext4 ${VBLK_IMG}
2433
BLK_DEV=$(losetup -f)
2534
losetup ${BLK_DEV} ${VBLK_IMG}
26-
;;
35+
;;
2736
Darwin)
2837
$(brew --prefix e2fsprogs)/sbin/mkfs.ext4 ${VBLK_IMG}
2938
BLK_DEV=$(hdiutil attach -nomount ${VBLK_IMG})
30-
;;
39+
;;
3140
esac
3241

3342
# On Linux, ${VBLK_IMG} will be created by root and owned by root:root.
@@ -48,10 +57,10 @@ case "$ACTION" in
4857
case "${OS_TYPE}" in
4958
Linux)
5059
losetup -d ${BLK_DEV}
51-
;;
60+
;;
5261
Darwin)
5362
hdiutil detach ${BLK_DEV}
54-
;;
63+
;;
5564
esac
5665

5766
# delete disk image

.ci/boot-linux.sh

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
check_platform
66

7-
function cleanup {
7+
function cleanup
8+
{
89
sleep 1
910
pkill -9 rv32emu
1011
}
1112

12-
function ASSERT {
13+
function ASSERT
14+
{
1315
$*
1416
local RES=$?
1517
if [ ${RES} -ne 0 ]; then
@@ -30,45 +32,45 @@ OPTS_BASE+=" -i build/linux-image/rootfs.cpio"
3032

3133
TEST_OPTIONS=("base (${OPTS_BASE})")
3234
EXPECT_CMDS=('
33-
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
34-
expect "# " { send "uname -a\n" } timeout { exit 2 }
35-
expect "riscv32 GNU/Linux" { send "\x01"; send "x" } timeout { exit 3 }
35+
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
36+
expect "# " { send "uname -a\n" } timeout { exit 2 }
37+
expect "riscv32 GNU/Linux" { send "\x01"; send "x" } timeout { exit 3 }
3638
')
3739

3840
COLOR_G='\e[32;01m' # Green
3941
COLOR_R='\e[31;01m' # Red
4042
COLOR_Y='\e[33;01m' # Yellow
41-
COLOR_N='\e[0m' # No color
43+
COLOR_N='\e[0m' # No color
4244

43-
MESSAGES=("${COLOR_G}OK!" \
44-
"${COLOR_R}Fail to boot" \
45-
"${COLOR_R}Fail to login" \
46-
"${COLOR_R}Fail to run commands" \
47-
"${COLOR_R}Fail to find emu.txt in ${VBLK_IMG}"\
45+
MESSAGES=("${COLOR_G}OK!"
46+
"${COLOR_R}Fail to boot"
47+
"${COLOR_R}Fail to login"
48+
"${COLOR_R}Fail to run commands"
49+
"${COLOR_R}Fail to find emu.txt in ${VBLK_IMG}"
4850
)
4951

5052
if [ "${ENABLE_VBLK}" -eq "1" ]; then
5153
# Read-only
5254
TEST_OPTIONS+=("${OPTS_BASE} -x vblk:${VBLK_IMG},readonly")
5355
EXPECT_CMDS+=('
54-
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
55-
expect "# " { send "uname -a\n" } timeout { exit 2 }
56-
expect "riscv32 GNU/Linux" { send "mkdir mnt && mount /dev/vda mnt\n" } timeout { exit 3 }
57-
expect "# " { send "echo rv32emu > mnt/emu.txt\n" } timeout { exit 3 }
58-
expect -ex "-sh: can'\''t create mnt/emu.txt: Read-only file system" {} timeout { exit 3 }
59-
expect "# " { send "\x01"; send "x" } timeout { exit 3 }
56+
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
57+
expect "# " { send "uname -a\n" } timeout { exit 2 }
58+
expect "riscv32 GNU/Linux" { send "mkdir mnt && mount /dev/vda mnt\n" } timeout { exit 3 }
59+
expect "# " { send "echo rv32emu > mnt/emu.txt\n" } timeout { exit 3 }
60+
expect -ex "-sh: can'\''t create mnt/emu.txt: Read-only file system" {} timeout { exit 3 }
61+
expect "# " { send "\x01"; send "x" } timeout { exit 3 }
6062
')
6163

6264
# Read-write using disk image
6365
TEST_OPTIONS+=("${OPTS_BASE} -x vblk:${VBLK_IMG}")
6466
VBLK_EXPECT_CMDS='
65-
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
66-
expect "# " { send "uname -a\n" } timeout { exit 2 }
67-
expect "riscv32 GNU/Linux" { send "mkdir mnt && mount /dev/vda mnt\n" } timeout { exit 3 }
68-
expect "# " { send "echo rv32emu > mnt/emu.txt\n" } timeout { exit 3 }
69-
expect "# " { send "sync\n" } timeout { exit 3 }
70-
expect "# " { send "umount mnt\n" } timeout { exit 3 }
71-
expect "# " { send "\x01"; send "x" } timeout { exit 3 }
67+
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
68+
expect "# " { send "uname -a\n" } timeout { exit 2 }
69+
expect "riscv32 GNU/Linux" { send "mkdir mnt && mount /dev/vda mnt\n" } timeout { exit 3 }
70+
expect "# " { send "echo rv32emu > mnt/emu.txt\n" } timeout { exit 3 }
71+
expect "# " { send "sync\n" } timeout { exit 3 }
72+
expect "# " { send "umount mnt\n" } timeout { exit 3 }
73+
expect "# " { send "\x01"; send "x" } timeout { exit 3 }
7274
'
7375
EXPECT_CMDS+=("${VBLK_EXPECT_CMDS}")
7476

@@ -87,7 +89,7 @@ for i in "${!TEST_OPTIONS[@]}"; do
8789
fi
8890
RUN_LINUX="build/rv32emu ${OPTS}"
8991

90-
ASSERT expect <<-DONE
92+
ASSERT expect <<- DONE
9193
set timeout ${TIMEOUT}
9294
spawn ${RUN_LINUX}
9395
${EXPECT_CMDS[$i]}
@@ -100,7 +102,7 @@ for i in "${!TEST_OPTIONS[@]}"; do
100102
if [[ "${TEST_OPTIONS[$i]}" =~ vblk ]]; then
101103
# read-only test first, so the emu.txt definitely does not exist, skipping the check
102104
if [[ ! "${TEST_OPTIONS[$i]}" =~ readonly ]]; then
103-
7z l ${VBLK_IMG} | grep emu.txt >/dev/null 2>&1 || ret=4
105+
7z l ${VBLK_IMG} | grep emu.txt > /dev/null 2>&1 || ret=4
104106
fi
105107
printf "Virtio-blk Test: [ ${MESSAGES[$ret]}${COLOR_N} ]\n"
106108
fi

.ci/check-format.sh

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
#!/usr/bin/env bash
22

3-
set -e -u -o pipefail
3+
# The -e is not set because we want to get all the mismatch format at once
44

5-
SOURCES=$(find $(git rev-parse --show-toplevel) | egrep "\.(c|cxx|cpp|h|hpp)\$")
5+
set -u -o pipefail
66

77
set -x
88

9-
for file in ${SOURCES};
10-
do
9+
REPO_ROOT="$(git rev-parse --show-toplevel)"
10+
11+
C_SOURCES=$(find "${REPO_ROOT}" | egrep "\.(c|cxx|cpp|h|hpp)$")
12+
for file in ${C_SOURCES}; do
1113
clang-format-18 ${file} > expected-format
1214
diff -u -p --label="${file}" --label="expected coding style" ${file} expected-format
1315
done
14-
exit $(clang-format-18 --output-replacements-xml ${SOURCES} | egrep -c "</replacement>")
16+
C_MISMATCH_LINE_CNT=$(clang-format-18 --output-replacements-xml ${C_SOURCES} | egrep -c "</replacement>")
17+
18+
SH_SOURCES=$(find "${REPO_ROOT}" | egrep "\.sh$")
19+
for file in ${SH_SOURCES}; do
20+
shfmt -d "${file}"
21+
done
22+
SH_MISMATCH_FILE_CNT=$(shfmt -l ${SH_SOURCES})
23+
24+
exit $((C_MISMATCH_LINE_CNT + SH_MISMATCH_FILE_CNT))

.ci/common.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ OS_TYPE=$(uname -s)
66
check_platform()
77
{
88
case "${MACHINE_TYPE}/${OS_TYPE}" in
9-
x86_64/Linux | aarch64/Linux | arm64/Darwin)
10-
;;
9+
x86_64/Linux | aarch64/Linux | arm64/Darwin) ;;
10+
1111
*)
1212
echo "Unsupported platform: ${MACHINE_TYPE}/${OS_TYPE}"
1313
exit 1
@@ -16,7 +16,7 @@ check_platform()
1616
}
1717

1818
if [[ "${OS_TYPE}" == "Linux" ]]; then
19-
PARALLEL=-j$(nproc)
19+
PARALLEL=-j$(nproc)
2020
else
21-
PARALLEL=-j$(sysctl -n hw.logicalcpu)
21+
PARALLEL=-j$(sysctl -n hw.logicalcpu)
2222
fi

.ci/riscv-tests.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ make distclean
1818
# for this purpose, although the emulator can run all selected benchmarks with
1919
# much smaller memory mapping regions.
2020
make ENABLE_ARCH_TEST=1 ENABLE_EXT_M=1 ENABLE_EXT_A=1 ENABLE_EXT_F=1 ENABLE_EXT_C=1 \
21-
ENABLE_Zicsr=1 ENABLE_Zifencei=1 ENABLE_FULL4G=1 $PARALLEL
21+
ENABLE_Zicsr=1 ENABLE_Zifencei=1 ENABLE_FULL4G=1 $PARALLEL
2222
make arch-test RISCV_DEVICE=IMAFCZicsrZifencei hw_data_misaligned_support=1 $PARALLEL || exit 1
2323
make arch-test RISCV_DEVICE=FCZicsr hw_data_misaligned_support=1 $PARALLEL || exit 1
2424
make arch-test RISCV_DEVICE=IMZbaZbbZbcZbs hw_data_misaligned_support=1 $PARALLEL || exit 1
@@ -33,6 +33,6 @@ make arch-test RISCV_DEVICE=E $PARALLEL || exit 1
3333
# the hardware misalignment (hw_data_misaligned_support) option.
3434
make distclean
3535
make ENABLE_ARCH_TEST=1 ENABLE_JIT=1 ENABLE_T2C=0 \
36-
ENABLE_EXT_M=1 ENABLE_EXT_A=1 ENABLE_EXT_F=1 ENABLE_EXT_C=1 \
37-
ENABLE_Zicsr=1 ENABLE_Zifencei=1 ENABLE_FULL4G=1 $PARALLEL
36+
ENABLE_EXT_M=1 ENABLE_EXT_A=1 ENABLE_EXT_F=1 ENABLE_EXT_C=1 \
37+
ENABLE_Zicsr=1 ENABLE_Zifencei=1 ENABLE_FULL4G=1 $PARALLEL
3838
make arch-test RISCV_DEVICE=IMC hw_data_misaligned_support=0 $PARALLEL || exit 1

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Top-level EditorConfig file
2+
root = true
3+
4+
# Shell script-specific settings
5+
[*.sh]
6+
indent_style = space
7+
indent_size = 4
8+
end_of_line = lf
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
function_next_line = true
12+
switch_case_indent = true
13+
space_redirects = true
14+
binary_next_line = true

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ jobs:
505505
- uses: actions/checkout@v4
506506
- name: coding convention
507507
run: |
508-
sudo apt-get install -q=2 clang-format-18
508+
sudo apt-get install -q=2 clang-format-18 shfmt
509509
.ci/check-newline.sh
510510
.ci/check-format.sh
511511
shell: bash

CONTRIBUTING.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,32 @@ However, participation requires adherence to fundamental ground rules:
3838
* Code must strictly adhere to the established C coding style (refer to the guidelines below).
3939
While there is some flexibility in basic style, it is crucial to stick to the current coding standards.
4040
Complex algorithmic constructs without proper comments will not be accepted.
41+
* Shell scripts must be formatted before submission. Use consistent flags across the project to ensure uniform formatting.
4142
* External pull requests should include thorough documentation in the pull request comments for consideration.
4243
* When composing documentation, code comments, and other materials in English,
4344
please adhere to the American English (`en_US`) dialect.
4445
This variant should be considered the standard for all documentation efforts.
4546
For instance, opt for "initialize" over "initialise" and "color" rather than "colour".
4647

47-
Software requirement: [clang-format](https://clang.llvm.org/docs/ClangFormat.html) version 18 or later.
48+
Software requirement:
49+
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) version 18 or later.
50+
* [shfmt](https://github.com/mvdan/sh).
4851

49-
This repository consistently contains an up-to-date `.clang-format` file with rules that match the explained ones.
50-
For maintaining a uniform coding style, execute the command `clang-format -i *.{c,h}`.
52+
This repository consistently contains an up-to-date `.clang-format` file with rules that match the explained ones and uses shell script formatting supported by `shfmt`.
53+
For maintaining a uniform coding style, execute the command `clang-format -i *.{c,h}` and `shfmt -w $(find . -type f -name "*.sh")`.
54+
55+
## Coding Style for Shell Script
56+
57+
Shell scripts must be clean, consistent, and portable. The following `shfmt` rules(check `.editorconfig` file) are enforced project-wide:
58+
* Use spaces for indentation.
59+
* Indent with 4 spaces.
60+
* Use Unix-style line endings (LF).
61+
* Remove trailing whitespace at the end of lines.
62+
* Ensure the file ends with a newline.
63+
* Place the opening brace of a function on the next line.
64+
* Indent `case` statements within `switch` blocks.
65+
* Add spaces around redirection operators (e.g., `>`, `>>`).
66+
* Place binary operators (e.g., `&&`, `|`) on the next line when breaking lines.
5167

5268
## Coding Style for Modern C
5369

@@ -877,22 +893,22 @@ Author: Jim Huang <[email protected]>
877893
Date: Mon Feb 24 13:08:32 2025 +0800
878894
879895
Introduce CPU architecture filtering in scheduler
880-
896+
881897
In environments with mixed CPU architectures, it is crucial to ensure
882898
that an instance runs only on a host with a compatible CPU
883899
type—preventing, for example, a RISC-V instance from being scheduled on
884900
an Arm host.
885-
901+
886902
This new scheduler filter enforces that requirement by comparing an
887903
instance's architecture against the host's allowed architectures. For
888904
the libvirt driver, the host's guest capabilities are queried, and the
889905
permitted architectures are recorded in the permitted_instances_types
890906
list within the host's cpu_info dictionary.
891-
907+
892908
The filter systematically excludes hosts that do not support the
893909
instance's CPU architecture. Additionally, RISC-V has been added to the
894910
set of acceptable architectures for scheduling.
895-
911+
896912
Note that the CPU architecture filter is disabled by default.
897913
```
898914

tests/common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function fail()
1+
function fail()
22
{
33
echo "*** Fail"
44
exit 1

tests/dhrystone.sh

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ function run_dhrystone()
2525
sanity_check
2626
# Run Dhrystone benchmark and collect DMIPS values
2727
dmips_values=()
28-
for ((i=1; i<=$N_RUNS; i++))
29-
do
28+
for ((i = 1; i <= $N_RUNS; i++)); do
3029
echo "Running Dhrystone benchmark - Run #$i"
3130
dmips=$(run_dhrystone)
3231
exit_code=$?
@@ -48,7 +47,7 @@ fi
4847

4948
deviation=0
5049
for dmips in "${sorted_dmips[@]}"; do
51-
if (( $(echo "$dmips > $median" | bc -l) )); then
50+
if (($(echo "$dmips > $median" | bc -l))); then
5251
diff=$(echo "$dmips - $median" | bc -l)
5352
else
5453
diff=$(echo "$median - $dmips" | bc -l)
@@ -60,15 +59,14 @@ mad=$(echo "scale=2; $deviation / $num_dmips" | bc -l)
6059

6160
# Filter outliers based on MAD
6261
filtered_dmips=()
63-
for dmips in "${sorted_dmips[@]}"
64-
do
65-
if (( $(echo "$dmips > 0" | bc -l) )); then
66-
if (( $(echo "$dmips > $median" | bc -l) )); then
62+
for dmips in "${sorted_dmips[@]}"; do
63+
if (($(echo "$dmips > 0" | bc -l))); then
64+
if (($(echo "$dmips > $median" | bc -l))); then
6765
diff=$(echo "$dmips - $median" | bc -l)
6866
else
6967
diff=$(echo "$median - $dmips" | bc -l)
7068
fi
71-
if (( $(echo "$diff <= $mad * 2" | bc -l) )); then
69+
if (($(echo "$diff <= $mad * 2" | bc -l))); then
7270
filtered_dmips+=("$dmips")
7371
fi
7472
fi
@@ -83,8 +81,7 @@ echo -n "" > $benchmark_output
8381
num_filtered=${#filtered_dmips[@]}
8482
if ((num_filtered > 0)); then
8583
total_dmips=0
86-
for dmips in "${filtered_dmips[@]}"
87-
do
84+
for dmips in "${filtered_dmips[@]}"; do
8885
total_dmips=$(echo "scale=2; $total_dmips + $dmips" | bc -l)
8986
done
9087

0 commit comments

Comments
 (0)