1#!/bin/sh
2
3export LC_ALL=C
4
5o="$(mktemp)"
6gb="$(mktemp)"
7input="$(mktemp)"
8output="$(mktemp)"
9errput="$(mktemp)"
10rc=0
11
12trap "rm -f '$o' '$gb' '$input' '$output' '$errput'" EXIT
13
14bold="$(tput bold)"
15resbold="$(tput sgr0)"
16red="$(tput setaf 1)"
17green="$(tput setaf 2)"
18orange="$(tput setaf 3)"
19rescolors="$(tput op)"
20
21RGBASM=../../rgbasm
22RGBLINK=../../rgblink
23
24tryDiff () {
25	if ! diff -u --strip-trailing-cr "$1" "$2"; then
26		echo "${bold}${red}${i%.asm}${variant}.$3 mismatch!${rescolors}${resbold}"
27		false
28	fi
29}
30
31tryCmp () {
32	if ! cmp "$1" "$2"; then
33		../../contrib/gbdiff.bash "$1" "$2"
34		echo "${bold}${red}${i%.asm}${variant}.out.bin mismatch!${rescolors}${resbold}"
35		false
36	fi
37}
38
39# Add the version constants test, outputting the closest tag to the HEAD
40if git describe --tags --abbrev=0 > version.out; then
41	$RGBASM --version >> version.out
42	cat > version.asm <<EOF
43IF !DEF(__RGBDS_RC__)
44	PRINTLN "v{d:__RGBDS_MAJOR__}.{d:__RGBDS_MINOR__}.{d:__RGBDS_PATCH__}"
45ELSE
46	PRINTLN "v{d:__RGBDS_MAJOR__}.{d:__RGBDS_MINOR__}.{d:__RGBDS_PATCH__}-rc{d:__RGBDS_RC__}"
47ENDC
48	PRINTLN "rgbasm {__RGBDS_VERSION__}"
49EOF
50else
51	echo "${bold}${orange}Warning: cannot run version test!${rescolors}${resbold}"
52	rm -f version.asm
53fi
54
55# Add the quote test, except on Windows
56if uname | grep -viq mingw; then
57	cat > quote\"file.asm <<EOF
58WARN __FILE__
59EOF
60	cat > quote\"file.out <<EOF
61EOF
62	cat > quote\"file.err <<EOF
63warning: quote"file.asm(1): [-Wuser]
64    quote"file.asm
65while expanding symbol "__FILE__"
66EOF
67fi
68
69# Check whether to use '.simple.err' files if they exist
70# (rgbasm with pre-3.0 Bison just reports "syntax error")
71$RGBASM -Weverything -o $o syntax-error.asm > $output 2> $errput
72simple_error=0
73if ! diff --strip-trailing-cr syntax-error.err $errput; then
74	echo "${bold}${orange}Warning: using .simple.err files when available.${rescolors}${resbold}"
75	simple_error=1
76fi
77
78for i in *.asm; do
79	for variant in '' '.pipe'; do
80		echo "${bold}${green}${i%.asm}${variant}...${rescolors}${resbold}"
81		desired_errname=${i%.asm}.err
82		if [ "$simple_error" -eq 1 ] && [ -e ${i%.asm}.simple.err ]; then
83			desired_errname=${i%.asm}.simple.err
84		fi
85		if [ -z "$variant" ]; then
86			$RGBASM -Weverything -o $o $i > $output 2> $errput
87			desired_output=${i%.asm}.out
88			desired_errput=$desired_errname
89		else
90			# `include-recursion.asm` refers to its own name inside the test code.
91			# Skip testing with stdin input for that file.
92			if [ "$i" = "include-recursion.asm" ]; then
93				continue
94			fi
95
96			# Stop! This is not a Useless Use Of Cat. Using cat instead of
97			# stdin redirection makes the input an unseekable pipe - a scenario
98			# that's harder to deal with and was broken when the feature was
99			# first implemented.
100			cat $i | $RGBASM -Weverything -o $o - > $output 2> $errput
101
102			# Use two otherwise unused files for temp storage
103			desired_output=$input
104			desired_errput=$gb
105			# Escape regex metacharacters
106			subst="$(printf '%s\n' "$i" | sed 's:[][\/.^$*]:\\&:g')"
107			# Replace the file name with a dash to match changed output
108			sed "s/$subst/<stdin>/g" ${i%.asm}.out > $desired_output
109			sed "s/$subst/<stdin>/g" $desired_errname > $desired_errput
110		fi
111
112		tryDiff $desired_output $output out
113		our_rc=$?
114		tryDiff $desired_errput $errput err
115		our_rc=$(($? || $our_rc))
116
117		bin=${i%.asm}.out.bin
118		if [ -f $bin ]; then
119			$RGBLINK -o $gb $o
120			dd if=$gb count=1 bs=$(printf %s $(wc -c < $bin)) > $output 2>/dev/null
121			tryCmp $bin $output
122			our_rc=$(($? || $our_rc))
123		fi
124
125		rc=$(($rc || $our_rc))
126		if [ $our_rc -ne 0 ]; then break; fi
127	done
128done
129
130exit $rc
131