forked from decaf-lang/minidecaf-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.sh
executable file
·199 lines (172 loc) · 5.23 KB
/
check.sh
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
#!/bin/bash
export CC="riscv64-unknown-elf-gcc -std=c17 -march=rv32im -mabi=ilp32"
export QEMU=qemu-riscv32
export SPIKE="spike --isa=RV32G /usr/local/bin/pk"
if [ $(uname) = "Linux" ]; then
export EMU=$QEMU
else
export EMU=$SPIKE
fi
: ${USE_PARALLEL:=true}
: ${PROJ_PATH:=..}
export PROJ_PATH
if [[ $CI_COMMIT_REF_NAME == "stage-1" ]]; then
: ${STEP_FROM:=1}
: ${STEP_UNTIL:=4}
elif [[ $CI_COMMIT_REF_NAME == "stage-2" ]]; then
: ${STEP_FROM:=5}
: ${STEP_UNTIL:=6}
elif [[ $CI_COMMIT_REF_NAME == "stage-3" ]]; then
: ${STEP_FROM:=7}
: ${STEP_UNTIL:=8}
elif [[ $CI_COMMIT_REF_NAME == "stage-4" ]]; then
: ${STEP_FROM:=9}
: ${STEP_UNTIL:=10}
elif [[ $CI_COMMIT_REF_NAME == "stage-5" ]]; then
: ${STEP_FROM:=11}
: ${STEP_UNTIL:=11}
elif [ -v $CI_COMMIT_REF_NAME ]; then
echo "The test is not in CI."
echo "All testcases are taken into account, unless you manually set STEP_FROM and STEP_UNTIL."
: ${STEP_FROM:=1}
: ${STEP_UNTIL:=11}
else
echo "Warning: unknown branch"
echo "All testcases are taken into account, unless you manually set STEP_FROM and STEP_UNTIL."
: ${STEP_FROM:=1}
: ${STEP_UNTIL:=11}
fi
if [[ $STEP_UNTIL -lt $STEP_FROM ]]; then
echo "STEP_UNTIL < STEP_FROM: no tests run"
exit 0
else
JOBS=()
for step in `seq ${STEP_FROM} ${STEP_UNTIL}`; do
if [ -d "testcases/step${step}" ]; then
JOBS+=($(eval echo testcases/step${step}/*.c))
fi
done
FAILJOBS=()
for step in `seq ${STEP_FROM} ${STEP_UNTIL}`; do
if [ -d "failcases/step${step}" ]; then
FAILJOBS+=($(eval echo failcases/step${step}/*.c))
fi
done
fi
gen_asm() {
cfile=$(realpath "$1")
asmfile=$(realpath "$2")
# 根据特征文件判断所使用的语言
if [[ -f $PROJ_PATH/requirements.txt ]]; then # Python: minidecaf/requirements.txt
python3.9 $PROJ_PATH/main.py --input $cfile --riscv >$asmfile
elif [[ -f $PROJ_PATH/src/mind ]]; then # C++: use the executable
$PROJ_PATH/src/mind -l 5 -m riscv $cfile >$asmfile
else
touch _unrecog_impl
fi
}
export -f gen_asm
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
run_job() {
infile=$1
outbase=${infile%.c}
rm $outbase.{gcc,expected,err,my,actual,s} 1>/dev/null 2>&1
$CC $infile -o $outbase.gcc
$EMU $outbase.gcc >/dev/null
echo $? > $outbase.expected
if ! (
gen_asm $infile $outbase.s &&
$CC $outbase.s -o $outbase.my ) >$outbase.err 2>&1
then
echo -e "\n${YELLOW}ERR${NC} ${infile}"
echo "==== Error information ======================================================="
cat $outbase.err
echo -e "==============================================================================\n"
return 2
fi
$EMU $outbase.my >/dev/null
echo $? > $outbase.actual
if ! diff -q $outbase.expected $outbase.actual >/dev/null ; then
echo -e "\n${RED}FAIL${NC} ${infile}"
echo "==== Fail information (above: expected, below: actual) ======================="
diff $outbase.expected $outbase.actual
echo -e "==============================================================================\n"
return 1
else
echo -e "${GREEN}OK${NC} ${infile}"
return 0
fi
}
export -f run_job
run_failjob() {
infile=$1
outbase=${infile%.c}
rm $outbase.{my,s} 1>/dev/null 2>&1
if (gen_asm $infile $outbase.s &&
$CC $outbase.s -o $outbase.my) >/dev/null 2>&1
then
echo -e "\n${RED}FAIL${NC} ${infile}"
echo "==== Fail information (failed to detect input error) ========================="
cat $outbase.s
echo -e "==============================================================================\n"
return 1
else
echo -e "${GREEN}OK${NC} ${infile}"
return 0
fi
}
export -f run_failjob
check_env_and_parallel() {
if ! $CC --version >/dev/null 2>&1; then
echo "gcc not found"
exit 1
fi
echo "gcc found"
if ! $EMU -h >/dev/null 2>&1; then
echo "${EMU%% *} not found"
exit 1
fi
echo "${EMU%% *} found"
if $USE_PARALLEL && parallel --version >/dev/null 2>&1; then
echo "running tests in parallel"
return 0
else
echo "running tests serially"
return 1
fi
}
main() {
JOB_CNT=$((${#JOBS[@]} + ${#FAILJOBS[@]}))
echo "$JOB_CNT cases in total"
if check_env_and_parallel; then
parallel run_job ::: ${JOBS[@]}
parallel run_failjob ::: ${FAILJOBS[@]}
else
error_count=0
for job in ${JOBS[@]}; do
run_job $job
error_count=$(($error_count + $?))
done
for job in ${FAILJOBS[@]}; do
run_failjob $job
error_count=$(($error_count + $?))
done
return $error_count
fi
}
cd "$(dirname "$0")"
if ! [[ -d $PROJ_PATH ]]; then
echo "Put your code in $PROJ_PATH"
exit 1
fi
if ! (main); then
if [[ -f _unrecog_impl ]]; then
echo "Unrecognized implementation. Are you using one of the supported language & frameworks? Or did you put check.sh in the wrong place?"
rm _unrecog_impl;
fi
echo FAILED
exit 1;
fi