run_compile_tests.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/bash
  2. # Build and run the direct compiler harness (tests/CompileTest.mod).
  3. #
  4. # Same two-phase ISO link recipe as tpshell: a single monolithic invocation
  5. # trips gm2's pass-3 rollup on a compiler-size program, so generate the module
  6. # list first (-fgen-module-list -> /dev/null, rc=1 is expected) and then link
  7. # with -fuse-list.
  8. set -u
  9. D=/home/eric/Projets/Projets-Modula2/MyWork/TP3-comp/shell
  10. GM2=/home/eric/bin/Modula2/Gm2/bin/gm2
  11. cd "$D" || exit 9
  12. FLAGS="-fiso"
  13. # Is `compiletest` itself current? It links against the .o files, and `make`
  14. # only ever builds `tpshell` - so after an ordinary `make`, compiletest is
  15. # routinely OLDER than Compiler.o and still contains the previous compiler.
  16. # The check below used to look only at sources-versus-objects, which is blind
  17. # to exactly this: it saw "Compiler.mod is not newer than Compiler.o" and
  18. # skipped the rebuild, and the matrix then reported the previous compiler's
  19. # verdicts with total confidence. That is the worst kind of wrong, because a
  20. # real fix stays invisible and a test that should have gone red stays green.
  21. # It happened while bisecting the IF-handler bug, and it is the second time
  22. # this project has lost time to a stale harness.
  23. #
  24. # So compiletest is rebuilt whenever it is older than ANY input: the object
  25. # files it links, the sources those came from, and its own source.
  26. STALE=no
  27. for f in compiletest TextBuf.o Posix.o Compiler.o Runtime.o \
  28. tests/CompileTest.mod TextBuf.mod Posix.c Posix.def \
  29. Compiler.mod Runtime.mod Linker.mod; do
  30. if [ -e "$f" ] && [ compiletest -ot "$f" ]; then STALE=yes; break; fi
  31. done
  32. if [ "$STALE" = yes ]; then
  33. echo "compiletest is older than its inputs - it would report the OLD compiler"
  34. rm -f compiletest
  35. fi
  36. if [ ! -f TextBuf.o ] || [ ! -f Compiler.o ] || [ ! -f Posix.o ] \
  37. || [ ! -f Runtime.o ] \
  38. || [ TextBuf.mod -nt TextBuf.o ] || [ Compiler.mod -nt Compiler.o ] \
  39. || [ Runtime.mod -nt Runtime.o ] || [ Runtime.def -nt Runtime.o ] \
  40. || [ Compiler.def -nt Compiler.o ] \
  41. || [ Posix.c -nt Posix.o ] || [ Posix.def -nt Posix.o ]; then
  42. echo "building support modules (source newer than object)..."
  43. # Runtime is listed because Compiler now links against it: the blob is
  44. # copied into the code buffer and the TU_* offsets come from it. A
  45. # `make clean` (or a fresh checkout) removes every .o, so the C module has
  46. # to be built here too - it used to be assumed present, which made the
  47. # script fail with "cannot find Posix.o" after any clean.
  48. if [ ! -f Posix.o ] || [ Posix.c -nt Posix.o ] || [ Posix.def -nt Posix.o ]; then
  49. cc -c Posix.c >/tmp/ct_c_Posix 2>&1 \
  50. || { echo "COMPILE_FAIL Posix"; grep -m5 "error:" /tmp/ct_c_Posix; exit 1; }
  51. fi
  52. for m in TextBuf Compiler Runtime; do
  53. $GM2 $FLAGS -c $m.mod >/tmp/ct_c_$m 2>&1
  54. if [ $? -ne 0 ]; then
  55. echo "COMPILE_FAIL $m"
  56. grep -m5 -E "error:|Please submit" /tmp/ct_c_$m
  57. exit 1
  58. fi
  59. done
  60. echo " support modules rebuilt"
  61. fi
  62. rm -f tests/ct.lst compiletest
  63. $GM2 $FLAGS -fgen-module-list=tests/ct.lst -o /dev/null \
  64. tests/CompileTest.mod TextBuf.o Posix.o Compiler.o Runtime.o >/tmp/ct_p1 2>&1
  65. p1=$?
  66. $GM2 $FLAGS -fuse-list=tests/ct.lst -o compiletest \
  67. tests/CompileTest.mod TextBuf.o Posix.o Compiler.o Runtime.o >/tmp/ct_p2 2>&1
  68. p2=$?
  69. if [ $p2 -ne 0 ]; then
  70. echo "LINK_FAIL p1_rc=$p1 p2_rc=$p2"
  71. grep -E "error:|undefined" /tmp/ct_p2 | head -10
  72. exit 1
  73. fi
  74. echo "compiletest built (p1_rc=$p1, phase 1 rc=1 is the expected rollup)"
  75. FX=${1:-tests/fixtures}
  76. # ---- assert against tests/fixtures/expected.tsv ---------------------------
  77. # Previously this script only printed a verdict, so "17 of 23" was a number a
  78. # human counted by hand and a wrong error POSITION was invisible. Both the
  79. # verdict and the numbers are compared now, and the exit status is meaningful.
  80. EXP="$FX/expected.tsv"
  81. if [ ! -f "$EXP" ]; then
  82. echo "no expectations file $EXP - printing only"
  83. ls "$FX"/*.pas 2>/dev/null | ./compiletest
  84. exit 0
  85. fi
  86. RAW=$(mktemp) || exit 9
  87. ls "$FX"/*.pas 2>/dev/null | ./compiletest > "$RAW"
  88. # expected row for a fixture basename, normalised to single spaces
  89. # (only rows that actually carry a verdict count, so the wrapped comment
  90. # lines in expected.tsv can never be mistaken for data)
  91. expect_for() {
  92. awk -F'\t' -v n="$1" '
  93. ($2 == "OK" || $2 == "ERR") && $1 == n {
  94. out = ""
  95. for (i = 2; i <= NF; i++)
  96. out = (out == "") ? $i : out " " $i
  97. print out
  98. exit
  99. }
  100. ' "$EXP"
  101. }
  102. pass=0; fail=0
  103. while IFS= read -r base; do
  104. actual=$(grep -F "$base" "$RAW" \
  105. | head -1 \
  106. | tr -d '\r' \
  107. | sed -E 's/^[^ ]+[[:space:]]+//' \
  108. | tr -s ' ' \
  109. | sed -E 's/^OK code=([0-9]+) data=([0-9]+)$/OK \1 \2/;
  110. s/^ERROR ([0-9]+) at pos ([0-9]+)$/ERR \1 \2/')
  111. want=$(expect_for "$base" | tr -s ' ')
  112. if [ -z "$want" ]; then
  113. printf ' %-24s NO_EXPECTATION got: %s\n' "$base" "$actual"
  114. fail=$((fail + 1))
  115. elif [ "$actual" = "$want" ]; then
  116. printf ' %-24s PASS %s\n' "$base" "$actual"
  117. pass=$((pass + 1))
  118. else
  119. printf ' %-24s FAIL\n expected: %s\n actual: %s\n' \
  120. "$base" "$want" "$actual"
  121. fail=$((fail + 1))
  122. fi
  123. done <<EOF
  124. $(ls "$FX"/*.pas 2>/dev/null | xargs -n1 basename | sed 's/\.pas$//')
  125. EOF
  126. echo "----------------------------------------------------------------"
  127. echo "compile matrix: $pass passed, $fail failed (of $((pass + fail)))"
  128. rm -f "$RAW"
  129. [ "$fail" -eq 0 ] || exit 1