Skip to content

Commit ea801a2

Browse files
authored
Merge pull request #101 from sysprog21/improve-cicd
Improve CI/CD
2 parents 00cc4ed + 188b9de commit ea801a2

File tree

6 files changed

+274
-79
lines changed

6 files changed

+274
-79
lines changed

.ci/autorun.sh

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
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

7+
# Clean up any existing semu processes before starting tests
178
cleanup
189

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
28-
2910
ASSERT expect <<DONE
3011
set timeout ${TIMEOUT}
3112
spawn make check
@@ -34,17 +15,18 @@ expect "# " { send "uname -a\n" } timeout { exit 2 }
3415
expect "riscv32 GNU/Linux" { send "\x01"; send "x" } timeout { exit 3 }
3516
DONE
3617

37-
ret=$?
38-
cleanup
18+
ret="$?"
3919

4020
MESSAGES=("OK!" \
4121
"Fail to boot" \
4222
"Fail to login" \
4323
"Fail to run commands" \
4424
)
4525

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

50-
exit ${ret}
32+
exit "$ret"

.ci/check-format.sh

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

3-
SOURCES=$(find $(git rev-parse --show-toplevel) | egrep "\.(c|cxx|cpp|h|hpp)\$")
3+
set -euo pipefail
44

5-
set -x
5+
# Get list of source files into array (POSIX-compatible for macOS bash 3.2)
6+
SOURCES=()
7+
while IFS= read -r file; do
8+
SOURCES+=("$file")
9+
done < <(git ls-files '*.c' '*.cxx' '*.cpp' '*.h' '*.hpp')
610

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>")
11+
# Use clang-format dry-run mode with --Werror to fail on format violations
12+
# This eliminates the need for temporary files and manual diff comparisons
13+
clang-format-18 -n --Werror "${SOURCES[@]}"

.ci/common.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 exit_code
22+
23+
set +e
24+
"$@"
25+
exit_code=$?
26+
set -e
27+
28+
if [ "$exit_code" -ne 0 ]; then
29+
echo "Assert failed: $*" >&2
30+
exit "$exit_code"
31+
fi
32+
}
33+
34+
# Determine timeout based on platform
35+
# macOS needs more time to boot compared to Linux, so the timeout is set to
36+
# 600 seconds for macOS to handle the longer startup. For Linux, 90 seconds
37+
# is sufficient due to its faster boot process.
38+
get_timeout() {
39+
local default_timeout="${SEMU_TEST_TIMEOUT:-}"
40+
41+
if [ -n "$default_timeout" ]; then
42+
echo "$default_timeout"
43+
return
44+
fi
45+
46+
case "${OS_TYPE}" in
47+
Darwin)
48+
echo "600"
49+
;;
50+
Linux)
51+
echo "90"
52+
;;
53+
*)
54+
echo "90"
55+
;;
56+
esac
57+
}
58+
59+
# Export TIMEOUT for use in scripts
60+
TIMEOUT="$(get_timeout)"
61+
export TIMEOUT
62+
63+
# Color codes for output
64+
COLOR_GREEN='\e[32;01m'
65+
COLOR_RED='\e[31;01m'
66+
COLOR_RESET='\e[0m'
67+
68+
# Print success message
69+
print_success() {
70+
printf "\n[ ${COLOR_GREEN}%s${COLOR_RESET} ]\n" "$1"
71+
}
72+
73+
# Print error message
74+
print_error() {
75+
printf "\n[ ${COLOR_RED}%s${COLOR_RESET} ]\n" "$1" >&2
76+
}

.ci/test-netdev.sh

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

3-
function cleanup {
4-
sleep 1
5-
pkill -9 semu
6-
}
3+
# Source common functions and settings
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
source "${SCRIPT_DIR}/common.sh"
76

8-
function ASSERT {
9-
$*
10-
local RES=$?
11-
if [ $RES -ne 0 ]; then
12-
echo 'Assert failed: "' $* '"'
13-
exit $RES
14-
fi
15-
}
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
1620

21+
# Clean up any existing semu processes before starting tests
1722
cleanup
1823

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 {
30-
local NETDEV=$1
24+
# Test network device functionality
25+
TEST_NETDEV() {
26+
local NETDEV="$1"
3127
local CMD_PREFIX=""
3228

33-
if [ $NETDEV == tap ]; then
29+
if [ "$NETDEV" = "tap" ]; then
3430
CMD_PREFIX="sudo "
3531
fi
3632

37-
ASSERT expect <<DONE
33+
ASSERT expect <<DONE
3834
set timeout ${TIMEOUT}
3935
spawn ${CMD_PREFIX}make check NETDEV=${NETDEV}
4036
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
@@ -56,17 +52,16 @@ ASSERT expect <<DONE
5652
DONE
5753
}
5854

59-
# Network devices
55+
# Network devices to test
6056
NETWORK_DEVICES=(tap user)
6157

6258
for NETDEV in "${NETWORK_DEVICES[@]}"; do
6359
cleanup
6460
echo "Test network device: $NETDEV"
65-
TEST_NETDEV $NETDEV
61+
TEST_NETDEV "$NETDEV"
6662
done
6763

68-
ret=$?
69-
cleanup
64+
ret="$?"
7065

7166
MESSAGES=("OK!" \
7267
"Fail to boot" \
@@ -75,8 +70,10 @@ MESSAGES=("OK!" \
7570
"Fail to transfer packet" \
7671
)
7772

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

82-
exit ${ret}
79+
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

0 commit comments

Comments
 (0)