-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
252 lines (220 loc) · 8.19 KB
/
build.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/bash
VERSION="1.7"
help() {
echo "Version v"$VERSION
echo "Usage:"
echo "build.sh [-e project_name] [-b] [-t top_file] [-s] [-a parameters_list] [-f] [-l] [-g] [-w] [-c] [-d] [-m]"
echo "Description:"
echo "-e: Specify a example project. For example: -e counter. If not specified, the default directory \"cpu\" will be used."
echo "-b: Build project using verilator and make tools automatically. It will generate the \"build\"(difftest) or \"build_test\" subfolder under the project directory."
echo "-t: Specify a file as verilog top file. If not specified, the default filename \"top.v\" will be used. This option is invalid when connected difftest."
echo "-s: Run simulation program. Use the \"build_test\" or \"build\"(difftest) folder as work path."
echo "-a: Parameters passed to the simulation program. For example: -a \"1 2 3 ......\". Multiple parameters require double quotes."
echo "-f: C++ compiler arguments for makefile. For example: -f \"-DGLOBAL_DEFINE=1 -ggdb3\". Multiple parameters require double quotes. This option is invalid when connected difftest."
echo "-l: C++ linker arguments for makefile. For example: -l \"-ldl -lm\". Multiple parameters require double quotes. This option is invalid when connected difftest."
echo "-g: Debug the simulation program with GDB."
echo "-w: Open the latest waveform file(.vcd) using gtkwave under work path. Use the \"build_test\" or \"build\"(difftest) folder as work path."
echo "-c: Delete \"build\" and \"build_test\" folders under the project directory."
echo "-d: Connect to XiangShan difftest framework."
echo "-m: Parameters passed to the difftest makefile. For example: -m \"EMU_TRACE=1 EMU_THREADS=4\". Multiple parameters require double quotes."
echo "-r: Run all test cases in the $$BIN_SOURCE_PATH directory. This option requires the project to be able to connect to difftest."
exit 0
}
create_soft_link() {
mkdir ${1} 1>/dev/null 2>&1
find -L ${1} -type l -delete
FILES=`eval "find ${2} -name ${3}"`
for FILE in ${FILES[@]}
do
eval "ln -s \"`realpath --relative-to="${1}" "$FILE"`\" \"${1}/${FILE##*/}\" 1>/dev/null 2>&1"
done
}
build_diff_proj() {
# Refresh the modification time of the top file, otherwise some changes to the RTL source code will not take effect in next compilation.
touch -m `find $BUILD_PATH -name $DIFFTEST_TOP_FILE` 1>/dev/null 2>&1
# create soft link ($BUILD_PATH/*.v -> $PROJECT_PATH/$VSRC_FOLDER/*.v)
create_soft_link $BUILD_PATH $PROJECT_PATH/$VSRC_FOLDER \"*.v\"
# create soft link ($PROJECT_PATH/difftest -> $OSCPU_PATH/difftest)
eval "ln -s \"`realpath --relative-to="$OSCPU_PATH/$DIFFTEST_FOLDER" "$PROJECT_PATH"`/$DIFFTEST_FOLDER\" \"$PROJECT_PATH/$DIFFTEST_FOLDER\" 1>/dev/null 2>&1"
cd $OSCPU_PATH/$DIFFTEST_FOLDER
# compile
# make DESIGN_DIR=$PROJECT_PATH $DIFFTEST_PARAM
make DESIGN_DIR=$OSCPU_PATH $DIFFTEST_PARAM
if [ $? -ne 0 ]; then
echo "Failed to run verilator!!!"
exit 1
fi
cd $OSCPU_PATH
}
build_proj() {
cd $PROJECT_PATH
# get all .cpp files
CSRC_LIST=`find $PROJECT_PATH/$CSRC_FOLDER -name "*.cpp"`
for CSRC_FILE in ${CSRC_LIST[@]}
do
CSRC_FILES="$CSRC_FILES $CSRC_FILE"
done
# get all vsrc subfolders
VSRC_SUB_FOLDER=`find $VSRC_FOLDER -type d`
for SUBFOLDER in ${VSRC_SUB_FOLDER[@]}
do
INCLUDE_VSRC_FOLDERS="$INCLUDE_VSRC_FOLDERS -I$SUBFOLDER"
done
# get all csrc subfolders
CSRC_SUB_FOLDER=`find $PROJECT_PATH/$CSRC_FOLDER -type d`
for SUBFOLDER in ${CSRC_SUB_FOLDER[@]}
do
INCLUDE_CSRC_FOLDERS="$INCLUDE_CSRC_FOLDERS -I$SUBFOLDER"
done
# compile
mkdir $BUILD_FOLDER 1>/dev/null 2>&1
eval "verilator --cc --exe --trace --assert -O3 -CFLAGS \"-std=c++11 -Wall $INCLUDE_CSRC_FOLDERS $CFLAGS\" $LDFLAGS -o $PROJECT_PATH/$BUILD_FOLDER/$EMU_FILE \
-Mdir $PROJECT_PATH/$BUILD_FOLDER/emu-compile $INCLUDE_VSRC_FOLDERS --build $V_TOP_FILE $CSRC_FILES"
if [ $? -ne 0 ]; then
echo "Failed to run verilator!!!"
exit 1
fi
cd $OSCPU_PATH
}
# Initialize variables
OSCPU_PATH=$(dirname $(readlink -f "$0"))
MYINFO_FILE=$OSCPU_PATH"/myinfo.txt"
EMU_FILE="emu"
PROJECT_FOLDER="cpu"
BUILD_FOLDER="build_test"
BUILD_PATH=$OSCPU_PATH"/build"
DIFF_BUILD_FOLDER="build"
VSRC_FOLDER="vsrc"
CSRC_FOLDER="csrc"
BIN_SOURCE_PATH="/home/huxuan/repo/am-kernels/tests/cpu-tests/build"
BIN_SOURCE_PATH_MICROBENCH="/home/huxuan/repo/am-kernels/benchmarks/microbench/build"
BIN_SOURCE_PATH_RISCVTESTS="ThirdParty/riscv-tests/build"
#BIN_FOLDER="ThirdParty/bin"
BUILD="false"
V_TOP_FILE="top.v"
SIMULATE="false"
CHECK_WAVE="false"
CLEAN="false"
PARAMETERS=
CFLAGS=
LDFLAGS=
GDB="false"
DIFFTEST="false"
DIFFTEST_FOLDER="ThirdParty/difftest"
DIFFTEST_TOP_FILE="SimTop.v"
NEMU_FOLDER="ThirdParty/NEMU"
DIFFTEST_HELPER_PATH="src/test/vsrc/common"
DIFFTEST_PARAM=
RUNALL="false"
# Check parameters
while getopts 'he:bt:sa:f:l:gwcdm:r' OPT; do
case $OPT in
h) help;;
e) PROJECT_FOLDER="$OPTARG";;
b) BUILD="true";;
t) V_TOP_FILE="$OPTARG";;
s) SIMULATE="true";;
a) PARAMETERS="$OPTARG";;
f) CFLAGS="$OPTARG";;
l) LDFLAGS="$OPTARG";;
g) GDB="true";;
w) CHECK_WAVE="true";;
c) CLEAN="true";;
d) DIFFTEST="true";;
m) DIFFTEST_PARAM="$OPTARG";;
r) RUNALL="true";;
?) help;;
esac
done
if [[ $RUNALL == "true" ]]; then
DIFFTEST="true"
fi
if [[ $LDFLAGS ]]; then
LDFLAGS="-LDFLAGS "\"$LDFLAGS\"
fi
PROJECT_PATH=$OSCPU_PATH/projects/$PROJECT_FOLDER
if [[ "$DIFFTEST" == "true" ]]; then
V_TOP_FILE=$DIFFTEST_TOP_FILE
export NEMU_HOME=$OSCPU_PATH/$NEMU_FOLDER
export NOOP_HOME=$OSCPU_PATH
fi
# Get id and name
ID=`sed '/^ID=/!d;s/.*=//' $MYINFO_FILE`
NAME=`sed '/^Name=/!d;s/.*=//' $MYINFO_FILE`
if [[ ${#ID} -le 7 ]] || [[ ${#NAME} -le 1 ]]; then
echo "Please fill your information in myinfo.txt!!!"
exit 1
fi
ID="${ID##*\r}"
NAME="${NAME##*\r}"
# Clean
if [[ "$CLEAN" == "true" ]]; then
rm -rf $BUILD_PATH
if [[ "$DIFFTEST" == "true" ]]; then
unlink $PROJECT_PATH/$DIFFTEST_FOLDER 1>/dev/null 2>&1
fi
exit 0
fi
# Build project
if [[ "$BUILD" == "true" ]]; then
[[ "$DIFFTEST" == "true" ]] && build_diff_proj || build_proj
#git commit
git add . -A --ignore-errors
# (echo $NAME && echo $ID && hostnamectl && uptime) | git commit -F - -q --author='tracer-oscpu2021 <tracer@oscpu.org>' --no-verify --allow-empty 1>/dev/null 2>&1
# sync
fi
# Simulate
if [[ "$SIMULATE" == "true" ]]; then
echo $BUILD_PATH
cd $BUILD_PATH
# create soft link ($BUILD_PATH/*.bin -> $OSCPU_PATH/$BIN_FOLDER/*.bin). Why? Because of laziness!
create_soft_link $OSCPU_PATH/$DIFF_BUILD_FOLDER $BIN_SOURCE_PATH \"*.bin\"
# run simulation program
echo "Simulating..."
if [[ "$GDB" == "true" ]]; then
gdb -s $EMU_FILE --args ./$EMU_FILE $PARAMETERS
else
./$EMU_FILE $PARAMETERS
fi
if [ $? -ne 0 ]; then
echo "Failed to simulate!!!"
FAILED="true"
fi
cd $OSCPU_PATH
fi
# Check waveform
if [[ "$CHECK_WAVE" == "true" ]]; then
cd $BUILD_PATH
gtkwave `ls -t | grep .vcd | head -n 1`
if [ $? -ne 0 ]; then
echo "Failed to run gtkwave!!!"
exit 1
fi
cd $OSCPU_PATH
fi
if [[ "$FAILED" == "true" ]]; then
exit 1
fi
# Run all
if [[ $RUNALL == "true" ]]; then
cd $BUILD_PATH
rm -f *.bin
create_soft_link $OSCPU_PATH/$DIFF_BUILD_FOLDER $BIN_SOURCE_PATH \"*.bin\"
create_soft_link $OSCPU_PATH/$DIFF_BUILD_FOLDER $BIN_SOURCE_PATH_MICROBENCH \"*.bin\"
create_soft_link $OSCPU_PATH/$DIFF_BUILD_FOLDER $OSCPU_PATH/$BIN_SOURCE_PATH_RISCVTESTS \"*.bin\"
mkdir log 1>/dev/null 2>&1
BIN_FILES=`ls *.bin`
for BIN_FILE in $BIN_FILES; do
FILE_NAME=${BIN_FILE%.*}
printf "%30s " $FILE_NAME
LOG_FILE=log/$FILE_NAME-log.txt
./$EMU_FILE -i $BIN_FILE &> $LOG_FILE
if (grep 'HIT GOOD TRAP' $LOG_FILE > /dev/null) then
echo -e "\033[1;32mPASS!\033[0m"
rm $LOG_FILE
else
echo -e "\033[1;31mFAIL!\033[0m see $BUILD_PATH/$LOG_FILE for more information"
fi
done
cd $OSCPU_PATH
fi