link pthread in tests
[qbe.git] / tools / test.sh
blob4412f6197977dae2f38b1e600ffe7c984961a161
1 #!/bin/sh
3 dir=`cd $(dirname "$0"); pwd`
4 bin=$dir/../qbe
5 binref=$dir/../qbe.ref
7 tmp=/tmp/qbe.zzzz
9 drv=$tmp.c
10 asm=$tmp.s
11 asmref=$tmp.ref.s
12 exe=$tmp.exe
13 out=$tmp.out
15 testcc() {
16 echo "int main() { }" | $1 -x c -o /dev/null - >/dev/null 2>&1
17 return $?
20 init() {
21 case "$TARGET" in
22 arm64)
23 for p in aarch64-linux-musl aarch64-linux-gnu
25 cc="$p-gcc -no-pie -static"
26 qemu="qemu-aarch64"
28 $cc -v >/dev/null 2>&1 &&
29 $qemu -version >/dev/null 2>&1
30 then
31 if sysroot=$($cc -print-sysroot) && test -n "$sysroot"
32 then
33 qemu="$qemu -L $sysroot"
35 break
37 cc=
38 done
39 if test -z "$cc"
40 then
41 echo "Cannot find arm64 compiler or qemu."
42 exit 1
44 bin="$bin -t arm64"
46 rv64)
47 for p in riscv64-linux-musl riscv64-linux-gnu
49 cc="$p-gcc -no-pie -static"
50 qemu="qemu-riscv64"
52 $cc -v >/dev/null 2>&1 &&
53 $qemu -version >/dev/null 2>&1
54 then
55 if sysroot=$($cc -print-sysroot) && test -n "$sysroot"
56 then
57 qemu="$qemu -L $sysroot"
59 break
61 cc=
62 done
63 if test -z "$cc"
64 then
65 echo "Cannot find riscv64 compiler or qemu."
66 exit 1
68 bin="$bin -t rv64"
70 "")
71 case `uname` in
72 *Darwin*)
73 cc="cc"
75 *OpenBSD*)
76 cc="cc -nopie -lpthread"
78 *FreeBSD*)
79 cc="cc -lpthread"
82 cc="${CC:-cc} -lpthread"
83 testcc "$cc" || cc="${CC:-cc}"
85 esac
86 TARGET=`$bin -t?`
89 echo "Unknown target '$TARGET'."
90 exit 1
92 esac
95 cleanup() {
96 rm -f $drv $asm $exe $out
99 extract() {
100 WHAT="$1"
101 FILE="$2"
103 awk "
104 /^# >>> $WHAT/ {
105 p = 1
106 next
108 /^# <<</ {
109 p = 0
112 " $FILE \
113 | sed -e 's/# //' \
114 | sed -e 's/#$//'
117 once() {
118 t="$1"
120 if ! test -f $t
121 then
122 echo "invalid test file $t" >&2
123 exit 1
127 sed -e 1q $t |
128 grep "skip.* $TARGET\( .*\)\?$" \
129 >/dev/null
130 then
131 return 0
134 printf "%-45s" "$(basename $t)..."
136 if ! $bin -o $asm $t
137 then
138 echo "[qbe fail]"
139 return 1
142 if test -x $binref
143 then
144 $binref -o $asmref $t 2>/dev/null
147 extract driver $t > $drv
148 extract output $t > $out
150 if test -s $drv
151 then
152 src="$drv $asm"
153 else
154 src="$asm"
157 if ! $cc -g -o $exe $src
158 then
159 echo "[cc fail]"
160 return 1
163 if test -s $out
164 then
165 $qemu $exe a b c | diff -u - $out
166 ret=$?
167 reason="output"
168 else
169 $qemu $exe a b c
170 ret=$?
171 reason="returned $ret"
174 if test $ret -ne 0
175 then
176 echo "[$reason fail]"
177 return 1
180 echo "[ok]"
182 if test -f $asmref && ! cmp -s $asm $asmref
183 then
184 loc0=`wc -l $asm | cut -d' ' -f1`
185 loc1=`wc -l $asmref | cut -d' ' -f1`
186 printf " asm diff: %+d\n" $(($loc0 - $loc1))
187 return 0
191 #trap cleanup TERM QUIT
193 init
195 if test -z "$1"
196 then
197 echo "usage: tools/test.sh {all, SSAFILE}" 2>&1
198 exit 1
201 case "$1" in
202 "all")
203 fail=0
204 for t in $dir/../test/[!_]*.ssa
206 once $t
207 fail=`expr $fail + $?`
208 done
209 if test $fail -ge 1
210 then
211 echo
212 echo "$fail test(s) failed!"
213 else
214 echo
215 echo "All is fine!"
217 exit $fail
220 once $1
221 exit $?
223 esac