Skip to content

Commit d66aefa

Browse files
committed
Improve CI scripts
1. Create shared '.ci/common.sh': - Extracted common functions: cleanup, ASSERT, print_success/error - Centralized platform detection and timeout configuration - Added bash strict mode (set -euo pipefail) - Registered trap for guaranteed cleanup (EXIT INT TERM) 2. Simplify format checker (check-format.sh): - Use clang-format -n --Werror for direct validation - Eliminate temporary file generation (expected-format) 3. Create composite action ('.github/actions/setup-semu'): - Centralize dependency installation logic - Support both Linux (apt) and macOS (brew) - Apply optimizations: DEBIAN_FRONTEND=noninteractive, HOMEBREW_NO_AUTO_UPDATE
1 parent 6d0bb9f commit d66aefa

File tree

6 files changed

+146
-75
lines changed

6 files changed

+146
-75
lines changed

.ci/autorun.sh

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,8 @@
11
#!/usr/bin/env bash
22

3-
function cleanup {
4-
sleep 1
5-
pkill -9 semu
6-
}
7-
8-
function ASSERT {
9-
$*
10-
local RES=$?
11-
if [ $RES -ne 0 ]; then
12-
echo 'Assert failed: "' $* '"'
13-
exit $RES
14-
fi
15-
}
16-
17-
cleanup
18-
19-
# macOS needs more time to boot compared to Linux, so the timeout is set to
20-
# 600 seconds for macOS to handle the longer startup. For Linux, 90 seconds
21-
# is sufficient due to its faster boot process.
22-
UNAME_S=$(uname -s)
23-
if [[ ${UNAME_S} == "Darwin" ]]; then
24-
TIMEOUT=600
25-
else # Linux
26-
TIMEOUT=90
27-
fi
3+
# Source common functions and settings
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
source "${SCRIPT_DIR}/common.sh"
286

297
ASSERT expect <<DONE
308
set timeout ${TIMEOUT}
@@ -35,16 +13,17 @@ expect "riscv32 GNU/Linux" { send "\x01"; send "x" } timeout { exit 3 }
3513
DONE
3614

3715
ret=$?
38-
cleanup
3916

4017
MESSAGES=("OK!" \
4118
"Fail to boot" \
4219
"Fail to login" \
4320
"Fail to run commands" \
4421
)
4522

46-
COLOR_G='\e[32;01m' # Green
47-
COLOR_N='\e[0m'
48-
printf "\n[ ${COLOR_G}${MESSAGES[$ret]}${COLOR_N} ]\n"
23+
if [ $ret -eq 0 ]; then
24+
print_success "${MESSAGES[$ret]}"
25+
else
26+
print_error "${MESSAGES[$ret]}"
27+
fi
4928

5029
exit ${ret}

.ci/check-format.sh

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

3-
SOURCES=$(git ls-files '*.c' '*.cxx' '*.cpp' '*.h' '*.hpp')
3+
set -euo pipefail
44

5-
set -x
5+
SOURCES=$(git ls-files '*.c' '*.cxx' '*.cpp' '*.h' '*.hpp')
66

7-
for file in ${SOURCES};
8-
do
9-
clang-format-18 ${file} > expected-format
10-
diff -u -p --label="${file}" --label="expected coding style" ${file} expected-format
11-
done
12-
exit $(clang-format-18 --output-replacements-xml ${SOURCES} | egrep -c "</replacement>")
7+
# Use clang-format dry-run mode with --Werror to fail on format violations
8+
# This eliminates the need for temporary files and manual diff comparisons
9+
clang-format-18 -n --Werror ${SOURCES}

.ci/common.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env bash
2+
# Common functions and variables for semu CI scripts
3+
4+
set -euo pipefail
5+
6+
# Detect platform
7+
MACHINE_TYPE=$(uname -m)
8+
OS_TYPE=$(uname -s)
9+
10+
# Cleanup function - kills all semu processes
11+
cleanup() {
12+
sleep 1
13+
pkill -9 semu 2>/dev/null || true
14+
}
15+
16+
# Register cleanup on exit
17+
trap cleanup EXIT INT TERM
18+
19+
# ASSERT function - executes command and exits on failure
20+
ASSERT() {
21+
local cmd_output
22+
local exit_code
23+
24+
set +e
25+
cmd_output=$("$@" 2>&1)
26+
exit_code=$?
27+
set -e
28+
29+
if [ $exit_code -ne 0 ]; then
30+
echo "Assert failed: $*" >&2
31+
echo "Output: $cmd_output" >&2
32+
exit $exit_code
33+
fi
34+
}
35+
36+
# Determine timeout based on platform
37+
# macOS needs more time to boot compared to Linux, so the timeout is set to
38+
# 600 seconds for macOS to handle the longer startup. For Linux, 90 seconds
39+
# is sufficient due to its faster boot process.
40+
get_timeout() {
41+
local default_timeout="${SEMU_TEST_TIMEOUT:-}"
42+
43+
if [ -n "$default_timeout" ]; then
44+
echo "$default_timeout"
45+
return
46+
fi
47+
48+
case "${OS_TYPE}" in
49+
Darwin)
50+
echo "600"
51+
;;
52+
Linux)
53+
echo "90"
54+
;;
55+
*)
56+
echo "90"
57+
;;
58+
esac
59+
}
60+
61+
# Export TIMEOUT for use in scripts
62+
TIMEOUT=$(get_timeout)
63+
export TIMEOUT
64+
65+
# Color codes for output
66+
COLOR_GREEN='\e[32;01m'
67+
COLOR_RED='\e[31;01m'
68+
COLOR_RESET='\e[0m'
69+
70+
# Print success message
71+
print_success() {
72+
printf "\n[ ${COLOR_GREEN}$1${COLOR_RESET} ]\n"
73+
}
74+
75+
# Print error message
76+
print_error() {
77+
printf "\n[ ${COLOR_RED}$1${COLOR_RESET} ]\n" >&2
78+
}

.ci/test-netdev.sh

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,33 @@
11
#!/usr/bin/env bash
22

3-
function cleanup {
4-
sleep 1
5-
pkill -9 semu
6-
}
7-
8-
function ASSERT {
9-
$*
10-
local RES=$?
11-
if [ $RES -ne 0 ]; then
12-
echo 'Assert failed: "' $* '"'
13-
exit $RES
14-
fi
15-
}
3+
# Source common functions and settings
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
source "${SCRIPT_DIR}/common.sh"
166

17-
cleanup
7+
# Override timeout for netdev tests
8+
# Network tests need different timeout: 30s for Linux, 600s for macOS
9+
case "${OS_TYPE}" in
10+
Darwin)
11+
TIMEOUT=600
12+
;;
13+
Linux)
14+
TIMEOUT=30
15+
;;
16+
*)
17+
TIMEOUT=30
18+
;;
19+
esac
1820

19-
# macOS needs more time to boot compared to Linux, so the timeout is set to
20-
# 600 seconds for macOS to handle the longer startup. For Linux, 90 seconds
21-
# is sufficient due to its faster boot process.
22-
UNAME_S=$(uname -s)
23-
if [[ ${UNAME_S} == "Darwin" ]]; then
24-
TIMEOUT=600
25-
else # Linux
26-
TIMEOUT=30
27-
fi
28-
29-
function TEST_NETDEV {
21+
# Test network device functionality
22+
TEST_NETDEV() {
3023
local NETDEV=$1
3124
local CMD_PREFIX=""
3225

33-
if [ $NETDEV == tap ]; then
26+
if [ "$NETDEV" == "tap" ]; then
3427
CMD_PREFIX="sudo "
3528
fi
3629

37-
ASSERT expect <<DONE
30+
ASSERT expect <<DONE
3831
set timeout ${TIMEOUT}
3932
spawn ${CMD_PREFIX}make check NETDEV=${NETDEV}
4033
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
@@ -56,17 +49,16 @@ ASSERT expect <<DONE
5649
DONE
5750
}
5851

59-
# Network devices
52+
# Network devices to test
6053
NETWORK_DEVICES=(tap user)
6154

6255
for NETDEV in "${NETWORK_DEVICES[@]}"; do
6356
cleanup
6457
echo "Test network device: $NETDEV"
65-
TEST_NETDEV $NETDEV
58+
TEST_NETDEV "$NETDEV"
6659
done
6760

6861
ret=$?
69-
cleanup
7062

7163
MESSAGES=("OK!" \
7264
"Fail to boot" \
@@ -75,8 +67,10 @@ MESSAGES=("OK!" \
7567
"Fail to transfer packet" \
7668
)
7769

78-
COLOR_G='\e[32;01m' # Green
79-
COLOR_N='\e[0m'
80-
printf "\n[ ${COLOR_G}${MESSAGES[$ret]}${COLOR_N} ]\n"
70+
if [ $ret -eq 0 ]; then
71+
print_success "${MESSAGES[$ret]}"
72+
else
73+
print_error "${MESSAGES[$ret]}"
74+
fi
8175

8276
exit ${ret}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: 'Setup semu dependencies'
2+
description: 'Install build dependencies for semu based on the runner OS'
3+
4+
runs:
5+
using: 'composite'
6+
steps:
7+
- name: Install dependencies (Linux)
8+
if: runner.os == 'Linux'
9+
shell: bash
10+
run: |
11+
sudo apt-get update
12+
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \
13+
build-essential \
14+
device-tree-compiler \
15+
expect \
16+
libasound2-dev \
17+
libudev-dev
18+
19+
- name: Install dependencies (macOS)
20+
if: runner.os == 'macOS'
21+
shell: bash
22+
env:
23+
HOMEBREW_NO_AUTO_UPDATE: 1
24+
HOMEBREW_NO_ANALYTICS: 1
25+
run: |
26+
brew install make dtc expect e2fsprogs

.github/workflows/main.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ jobs:
3838
restore-keys: |
3939
${{ runner.os }}-submodules-
4040
- name: install-dependencies
41-
run: |
42-
sudo apt-get update
43-
sudo apt-get install -y build-essential device-tree-compiler expect libasound2-dev libudev-dev
41+
uses: ./.github/actions/setup-semu
4442
- name: install sound multiplexer ${{ matrix.dependency }}
4543
if: matrix.dependency != 'none'
4644
run: |
@@ -81,8 +79,7 @@ jobs:
8179
restore-keys: |
8280
${{ runner.os }}-submodules-
8381
- name: install-dependencies
84-
run: |
85-
brew install make dtc expect e2fsprogs
82+
uses: ./.github/actions/setup-semu
8683
- name: default build
8784
run: make
8885
shell: bash

0 commit comments

Comments
 (0)