test.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/bin/sh
  2. dir=`dirname "$0"`
  3. if [ -z "${bin:-}" ]; then
  4. bin=$dir/../qbe
  5. fi
  6. if [ -z "${binref:-}" ]; then
  7. binref=${bin}.ref
  8. fi
  9. tmp=/tmp/qbe.zzzz
  10. drv=$tmp.c
  11. asm=$tmp.s
  12. asmref=$tmp.ref.s
  13. exe=$tmp.exe
  14. out=$tmp.out
  15. qemu_not_needed() {
  16. "$@"
  17. }
  18. cc=
  19. find_cc_and_qemu() {
  20. if [ -n "$cc" ]; then
  21. return
  22. fi
  23. target="$1"
  24. candidate_cc="$2"
  25. if $candidate_cc -v >/dev/null 2>&1; then
  26. cc=$candidate_cc
  27. echo "cc: $cc"
  28. if [ "$target" = "$(uname -m)" ]
  29. then
  30. qemu=qemu_not_needed
  31. echo "qemu: not needed, testing native architecture"
  32. else
  33. qemu="$3"
  34. if $qemu -version >/dev/null 2>&1
  35. then
  36. sysroot=$($candidate_cc -print-sysroot)
  37. if [ -n "$sysroot" ]; then
  38. qemu="$qemu -L $sysroot"
  39. fi
  40. echo "qemu: $qemu"
  41. elif $qemu --version >/dev/null 2>&1
  42. then
  43. # wine
  44. :
  45. else
  46. qemu=
  47. echo "qemu: not found"
  48. fi
  49. fi
  50. echo
  51. fi
  52. }
  53. init() {
  54. case "$TARGET" in
  55. arm64)
  56. for p in aarch64-linux-musl aarch64-linux-gnu
  57. do
  58. find_cc_and_qemu aarch64 "$p-gcc -no-pie -static" "qemu-aarch64"
  59. done
  60. if test -z "$cc" -o -z "$qemu"
  61. then
  62. echo "Cannot find arm64 compiler or qemu."
  63. exit 77
  64. fi
  65. bin="$bin -t arm64"
  66. ;;
  67. rv64)
  68. for p in riscv64-linux-musl riscv64-linux-gnu
  69. do
  70. find_cc_and_qemu riscv64 "$p-gcc -no-pie -static" "qemu-riscv64"
  71. done
  72. if test -z "$cc" -o -z "$qemu"
  73. then
  74. echo "Cannot find riscv64 compiler or qemu."
  75. exit 77
  76. fi
  77. bin="$bin -t rv64"
  78. ;;
  79. x86_64)
  80. for p in x86_64-linux-musl x86_64-linux-gnu
  81. do
  82. find_cc_and_qemu x86_64 "$p-gcc -no-pie -static" "qemu-x86_64"
  83. done
  84. if test -z "$cc" -o -z "$qemu"
  85. then
  86. echo "Cannot find x86_64 compiler or qemu."
  87. exit 77
  88. fi
  89. bin="$bin -t amd64_sysv"
  90. ;;
  91. amd64_win)
  92. for p in x86_64-w64-mingw32
  93. do
  94. find_cc_and_qemu x86_64-w64 "$p-gcc -static" "wine"
  95. done
  96. if test -z "$cc"
  97. then
  98. echo "Cannot find windows compiler or wine."
  99. exit 1
  100. fi
  101. export WINEDEBUG=-all
  102. bin="$bin -t amd64_win"
  103. ;;
  104. "")
  105. case `uname` in
  106. *Darwin*)
  107. cc="cc"
  108. ;;
  109. *OpenBSD*)
  110. cc="cc -nopie -lpthread"
  111. ;;
  112. *FreeBSD*)
  113. cc="cc -lpthread"
  114. ;;
  115. *)
  116. cc="${CC:-cc}"
  117. ccpost="-lpthread"
  118. ;;
  119. esac
  120. TARGET=`$bin -t?`
  121. ;;
  122. *)
  123. echo "Unknown target '$TARGET'."
  124. exit 77
  125. ;;
  126. esac
  127. }
  128. cleanup() {
  129. rm -f $drv $asm $exe $out
  130. }
  131. extract() {
  132. WHAT="$1"
  133. FILE="$2"
  134. awk "
  135. /^# >>> $WHAT/ {
  136. p = 1
  137. next
  138. }
  139. /^# <<</ {
  140. p = 0
  141. }
  142. p
  143. " $FILE \
  144. | sed -e 's/# //' \
  145. | sed -e 's/#$//'
  146. }
  147. once() {
  148. t="$1"
  149. if ! test -f $t
  150. then
  151. echo "invalid test file $t" >&2
  152. exit 1
  153. fi
  154. if
  155. sed -e 1q $t |
  156. grep "skip.* $TARGET\( .*\)\?$" \
  157. >/dev/null
  158. then
  159. return 0
  160. fi
  161. printf "%-45s" "$(basename $t)..."
  162. if ! $bin -o $asm $t
  163. then
  164. echo "[qbe fail]"
  165. return 1
  166. fi
  167. if test -x $binref
  168. then
  169. $binref -o $asmref $t 2>/dev/null
  170. fi
  171. extract driver $t > $drv
  172. extract output $t > $out
  173. if test -s $drv
  174. then
  175. src="$drv $asm"
  176. else
  177. src="$asm"
  178. fi
  179. if ! $cc -g -o $exe $src $ccpost
  180. then
  181. echo "[cc fail]"
  182. return 1
  183. fi
  184. if test -s $out
  185. then
  186. $qemu $exe a b c | tr -d '\r' | diff -u - $out
  187. ret=$?
  188. reason="output"
  189. else
  190. $qemu $exe a b c
  191. ret=$?
  192. reason="returned $ret"
  193. fi
  194. if test $ret -ne 0
  195. then
  196. echo "[$reason fail]"
  197. return 1
  198. fi
  199. echo "[ok]"
  200. if test -f $asmref && ! cmp -s $asm $asmref
  201. then
  202. loc0=`wc -l $asm | cut -d' ' -f1`
  203. loc1=`wc -l $asmref | cut -d' ' -f1`
  204. printf " asm diff: %+d\n" $(($loc0 - $loc1))
  205. return 0
  206. fi
  207. }
  208. #trap cleanup TERM QUIT
  209. init
  210. if test -z "$1"
  211. then
  212. echo "usage: tools/test.sh {all, SSAFILE}" 2>&1
  213. exit 1
  214. fi
  215. case "$1" in
  216. "all")
  217. fail=0
  218. count=0
  219. for t in $dir/../test/[!_]*.ssa
  220. do
  221. once $t
  222. fail=`expr $fail + $?`
  223. count=`expr $count + 1`
  224. done
  225. if test $fail -ge 1
  226. then
  227. echo
  228. echo "$fail of $count tests failed!"
  229. else
  230. echo
  231. echo "All is fine!"
  232. fi
  233. exit $fail
  234. ;;
  235. *)
  236. once $1
  237. exit $?
  238. ;;
  239. esac