File: test.sh

package info (click to toggle)
qbe 1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,004 kB
  • sloc: ansic: 13,368; yacc: 843; ml: 466; sh: 379; python: 232; makefile: 100
file content (248 lines) | stat: -rwxr-xr-x 3,514 bytes parent folder | download
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
#!/bin/sh

dir=`dirname "$0"`
if [ -z "${bin:-}" ]; then
	bin=$dir/../qbe
fi
if [ -z "${binref:-}" ]; then
	binref=${bin}.ref
fi

tmp=/tmp/qbe.zzzz

drv=$tmp.c
asm=$tmp.s
asmref=$tmp.ref.s
exe=$tmp.exe
out=$tmp.out

qemu_not_needed() {
	"$@"
}

cc=
find_cc_and_qemu() {
	if [ -n "$cc" ]; then
		return
	fi
	target="$1"
	candidate_cc="$2"
	if $candidate_cc -v >/dev/null 2>&1; then
		cc=$candidate_cc
		echo "cc: $cc"

		if [ "$target" = "$(uname -m)" ]; then
			qemu=qemu_not_needed
			echo "qemu: not needed, testing native architecture"
		else
			qemu="$3"
			if $qemu -version >/dev/null 2>&1; then
				sysroot=$($candidate_cc -print-sysroot)
				if [ -n "$sysroot" ]; then
					qemu="$qemu -L $sysroot"
				fi
				echo "qemu: $qemu"
			else
				qemu=
				echo "qemu: not found"
			fi
		fi
		echo

	fi
}

init() {
	case "$TARGET" in
	arm64)
		for p in aarch64-linux-musl aarch64-linux-gnu
		do
			find_cc_and_qemu aarch64 "$p-gcc -no-pie -static" "qemu-aarch64"
		done
		if test -z "$cc" -o -z "$qemu"
		then
			echo "Cannot find arm64 compiler or qemu."
			exit 77
		fi
		bin="$bin -t arm64"
		;;
	rv64)
		for p in riscv64-linux-musl riscv64-linux-gnu
		do
			find_cc_and_qemu riscv64 "$p-gcc -no-pie -static" "qemu-riscv64"
		done
		if test -z "$cc" -o -z "$qemu"
		then
			echo "Cannot find riscv64 compiler or qemu."
			exit 77
		fi
		bin="$bin -t rv64"
		;;
	x86_64|amd64)
		for p in x86_64-linux-musl x86_64-linux-gnu
		do
			find_cc_and_qemu x86_64 "$p-gcc -no-pie -static" "qemu-x86_64"
		done
		if test -z "$cc" -o -z "$qemu"
		then
			echo "Cannot find x86_64 compiler or qemu."
			exit 77
		fi
		bin="$bin -t amd64_sysv"
		;;
	"")
		case `uname` in
		*Darwin*)
			cc="cc"
			;;
		*OpenBSD*)
			cc="cc -nopie -lpthread"
			;;
		*FreeBSD*)
			cc="cc -lpthread"
			;;
		*)
			cc="${CC:-cc}"
			ccpost="-lpthread"
			;;
		esac
		TARGET=`$bin -t?`
		;;
	*)
		echo "Unknown target '$TARGET'."
		exit 77
		;;
	esac
}

cleanup() {
	rm -f $drv $asm $exe $out
}

extract() {
	WHAT="$1"
	FILE="$2"

	awk "
		/^# >>> $WHAT/ {
			p = 1
			next
		}
		/^# <<</ {
			p = 0
		}
		p
	" $FILE \
	| sed -e 's/# //' \
	| sed -e 's/#$//'
}

once() {
	t="$1"

	if ! test -f $t
	then
		echo "invalid test file $t" >&2
		exit 1
	fi

	if
		sed -e 1q $t |
		grep "skip.* $TARGET\( .*\)\?$" \
		>/dev/null
	then
		return 0
	fi

	printf "%-45s" "$(basename $t)..."

	if ! $bin -o $asm $t
	then
		echo "[qbe fail]"
		return 1
	fi

	if test -x $binref
	then
		$binref -o $asmref $t 2>/dev/null
	fi

	extract driver $t > $drv
	extract output $t > $out

	if test -s $drv
	then
		src="$drv $asm"
	else
		src="$asm"
	fi

	if ! $cc -g -o $exe $src $ccpost
	then
		echo "[cc fail]"
		return 1
	fi

	if test -s $out
	then
		$qemu $exe a b c | diff -u - $out
		ret=$?
		reason="output"
	else
		$qemu $exe a b c
		ret=$?
		reason="returned $ret"
	fi

	if test $ret -ne 0
	then
		echo "[$reason fail]"
		return 1
	fi

	echo "[ok]"

	if test -f $asmref && ! cmp -s $asm $asmref
	then
		loc0=`wc -l $asm    | cut -d' ' -f1`
		loc1=`wc -l $asmref | cut -d' ' -f1`
		printf "    asm diff: %+d\n" $(($loc0 - $loc1))
		return 0
	fi
}

#trap cleanup TERM QUIT

init

if test -z "$1"
then
	echo "usage: tools/test.sh {all, SSAFILE}" 2>&1
	exit 1
fi

case "$1" in
"all")
	fail=0
	count=0
	for t in $dir/../test/[!_]*.ssa
	do
		once $t
		fail=`expr $fail + $?`
		count=`expr $count + 1`
	done
	if test $fail -ge 1
	then
		echo
		echo "$fail of $count tests failed!"
	else
		echo
		echo "All is fine!"
	fi
	exit $fail
	;;
*)
	once $1
	exit $?
	;;
esac