paul@4 | 1 | #!/bin/sh |
paul@4 | 2 | |
paul@456 | 3 | # This tool runs the toolchain for each of the tests, optionally building and |
paul@456 | 4 | # running the test programs. |
paul@456 | 5 | # |
paul@918 | 6 | # Copyright (C) 2016, 2017, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@456 | 7 | # |
paul@456 | 8 | # This program is free software; you can redistribute it and/or modify it under |
paul@456 | 9 | # the terms of the GNU General Public License as published by the Free Software |
paul@456 | 10 | # Foundation; either version 3 of the License, or (at your option) any later |
paul@456 | 11 | # version. |
paul@456 | 12 | # |
paul@456 | 13 | # This program is distributed in the hope that it will be useful, but WITHOUT |
paul@456 | 14 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@456 | 15 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@456 | 16 | # details. |
paul@456 | 17 | # |
paul@456 | 18 | # You should have received a copy of the GNU General Public License along with |
paul@456 | 19 | # this program. If not, see <http://www.gnu.org/licenses/>. |
paul@456 | 20 | |
paul@456 | 21 | PROGNAME=$0 |
paul@456 | 22 | OPTION=$1 |
paul@918 | 23 | shift 1 |
paul@918 | 24 | MAKE_OPTIONS="$*" |
paul@456 | 25 | |
paul@442 | 26 | LPLC="./lplc" |
paul@613 | 27 | DATADIR="_main.lplc" |
paul@456 | 28 | TESTINPUT="_results/testinput.txt" |
paul@442 | 29 | |
paul@45 | 30 | # Expect failure from the "bad" tests. |
paul@45 | 31 | |
paul@45 | 32 | expect_failure() { |
paul@447 | 33 | echo "$FILENAME" | grep -q '_bad[._/]' |
paul@447 | 34 | return $? |
paul@45 | 35 | } |
paul@45 | 36 | |
paul@45 | 37 | # Check deduction output for type warnings, indicating that the program contains |
paul@45 | 38 | # errors. |
paul@45 | 39 | |
paul@45 | 40 | check_type_warnings() { |
paul@45 | 41 | |
paul@442 | 42 | if [ -e "$DATADIR/_deduced/type_warnings" ] && \ |
paul@442 | 43 | [ `stat -c %s "$DATADIR/_deduced/type_warnings"` -ne 0 ] ; then |
paul@45 | 44 | |
paul@45 | 45 | echo "Type warnings in deduced information." 1>&2 |
paul@45 | 46 | return 1 |
paul@45 | 47 | fi |
paul@45 | 48 | |
paul@45 | 49 | return 0 |
paul@45 | 50 | } |
paul@45 | 51 | |
paul@45 | 52 | # Main program. |
paul@45 | 53 | |
paul@456 | 54 | # Show help if requested. |
paul@456 | 55 | |
paul@456 | 56 | if [ "$OPTION" = '--help' ] ; then |
paul@456 | 57 | cat 1>&2 <<EOF |
paul@918 | 58 | Usage: $0 [ --build | --build-only | --run-built ] [ <make options> ] |
paul@456 | 59 | |
paul@456 | 60 | Run the toolchain over all tests in the tests directory. |
paul@323 | 61 | |
paul@456 | 62 | If --build is specified, the generated program code will be compiled and run, |
paul@456 | 63 | with the results collected in the _results directory. |
paul@456 | 64 | |
paul@456 | 65 | If --build-only is specified, the generated programs themselves will be |
paul@456 | 66 | collected in the _results directory, each taking their name from that of the |
paul@456 | 67 | main program file used as input. |
paul@456 | 68 | |
paul@456 | 69 | If --run-built is specified, any generated programs in the _results directory |
paul@456 | 70 | will be run and their output collected in the _results directory. |
paul@323 | 71 | |
paul@456 | 72 | By using --build-only on one system, copying the _results directory to another |
paul@456 | 73 | system, and then running this script with the --run-built option on the other |
paul@456 | 74 | system, it becomes possible to test the toolchain output on the other system |
paul@456 | 75 | without needing to actually use the toolchain on that system. This permits the |
paul@456 | 76 | testing of cross-compiled programs. |
paul@456 | 77 | |
paul@456 | 78 | For example, to build on one system but not run the generated programs: |
paul@456 | 79 | |
paul@456 | 80 | ARCH=mipsel-linux-gnu $PROGNAME --build-only |
paul@455 | 81 | |
paul@456 | 82 | And to run the generated programs on another system: |
paul@456 | 83 | |
paul@456 | 84 | $PROGNAME --run-built |
paul@456 | 85 | |
paul@456 | 86 | Of course, this script will need to be copied to the target system if it is not |
paul@456 | 87 | already available there. |
paul@456 | 88 | |
paul@456 | 89 | Build and output logs are stored in the _results directory with the .build and |
paul@456 | 90 | .out suffixes employed respectively, one of each kind for each generated |
paul@456 | 91 | program. |
paul@918 | 92 | |
paul@918 | 93 | The make options can be used to specify things like the number of processes |
paul@918 | 94 | employed to perform a build of each program. For example: |
paul@918 | 95 | |
paul@918 | 96 | $PROGNAME --build -j8 |
paul@456 | 97 | EOF |
paul@456 | 98 | exit 1 |
paul@323 | 99 | fi |
paul@323 | 100 | |
paul@455 | 101 | # If just running existing programs, do so now and exit. |
paul@455 | 102 | |
paul@455 | 103 | if [ "$OPTION" = '--run-built' ] ; then |
paul@455 | 104 | for FILENAME in _results/* ; do |
paul@455 | 105 | TESTNAME=`basename "$FILENAME"` |
paul@455 | 106 | |
paul@455 | 107 | # Skip non-program files. |
paul@455 | 108 | |
paul@455 | 109 | if [ ! -x "$FILENAME" ]; then |
paul@455 | 110 | continue |
paul@455 | 111 | fi |
paul@455 | 112 | |
paul@455 | 113 | echo "$FILENAME..." 1>&2 |
paul@455 | 114 | OUTLOG="_results/$TESTNAME.out" |
paul@465 | 115 | OUTCODE="_results/$TESTNAME.exitcode" |
paul@455 | 116 | |
paul@455 | 117 | echo " (run)..." 1>&2 |
paul@465 | 118 | "$FILENAME" > "$OUTLOG" < "$TESTINPUT" |
paul@465 | 119 | echo $? > "$OUTCODE" |
paul@455 | 120 | done |
paul@455 | 121 | |
paul@455 | 122 | exit 0 |
paul@455 | 123 | fi |
paul@331 | 124 | |
paul@456 | 125 | # Make any required results directory. |
paul@456 | 126 | |
paul@456 | 127 | if [ "$OPTION" = '--build' ] || [ "$OPTION" = '--build-only' ] ; then |
paul@456 | 128 | if [ ! -e "_results" ]; then |
paul@456 | 129 | mkdir "_results" |
paul@456 | 130 | else |
paul@456 | 131 | rm "_results/"* |
paul@456 | 132 | fi |
paul@456 | 133 | |
paul@456 | 134 | cp "tests/testinput.txt" "$TESTINPUT" |
paul@456 | 135 | fi |
paul@456 | 136 | |
paul@323 | 137 | # Perform each test. |
paul@323 | 138 | |
paul@4 | 139 | for FILENAME in tests/* ; do |
paul@323 | 140 | TESTNAME=`basename "$FILENAME" .py` |
paul@4 | 141 | |
paul@4 | 142 | # Detect tests in their own subdirectories. |
paul@4 | 143 | |
paul@4 | 144 | if [ -d "$FILENAME" ] ; then |
paul@4 | 145 | if [ -e "$FILENAME/main.py" ] ; then |
paul@4 | 146 | FILENAME="$FILENAME/main.py" |
paul@4 | 147 | else |
paul@4 | 148 | continue |
paul@4 | 149 | fi |
paul@4 | 150 | fi |
paul@4 | 151 | |
paul@331 | 152 | # Skip non-program files. |
paul@331 | 153 | |
paul@331 | 154 | if [ `basename "$FILENAME"` = "$TESTNAME" ]; then |
paul@331 | 155 | continue |
paul@331 | 156 | fi |
paul@331 | 157 | |
paul@644 | 158 | # Compile tests without an existing cache. |
paul@4 | 159 | |
paul@4 | 160 | echo "$FILENAME..." 1>&2 |
paul@442 | 161 | if ! "$LPLC" -c -r "$FILENAME" ; then |
paul@447 | 162 | if ! expect_failure ; then |
paul@275 | 163 | exit 1 |
paul@275 | 164 | else |
paul@275 | 165 | echo 1>&2 |
paul@275 | 166 | continue |
paul@275 | 167 | fi |
paul@275 | 168 | fi |
paul@39 | 169 | |
paul@39 | 170 | # Check for unresolved names in the cache. |
paul@39 | 171 | |
paul@39 | 172 | echo " (depends)..." 1>&2 |
paul@442 | 173 | for CACHEFILE in "$DATADIR/_cache/"* ; do |
paul@393 | 174 | STARTLINE=`grep -n '^deferred:' "$CACHEFILE" | cut -d: -f 1` |
paul@393 | 175 | if tail -n +$(($STARTLINE + 2)) "$CACHEFILE" | grep -q '<depends>' ; then |
paul@393 | 176 | echo "Unresolved names in the cache." 1>&2 |
paul@393 | 177 | exit 1 |
paul@393 | 178 | fi |
paul@393 | 179 | done |
paul@4 | 180 | |
paul@45 | 181 | # Check for type warnings in deduction output. |
paul@45 | 182 | |
paul@45 | 183 | echo " (warnings)..." 1>&2 |
paul@45 | 184 | if ! check_type_warnings ; then exit 1 ; fi |
paul@45 | 185 | |
paul@644 | 186 | # Compile tests with an existing cache. |
paul@4 | 187 | |
paul@39 | 188 | echo " (cached)..." 1>&2 |
paul@442 | 189 | if ! "$LPLC" -c "$FILENAME" ; then exit 1 ; fi |
paul@4 | 190 | |
paul@45 | 191 | echo " (warnings)..." 1>&2 |
paul@45 | 192 | if ! check_type_warnings ; then exit 1 ; fi |
paul@45 | 193 | |
paul@323 | 194 | # Build and run if appropriate. |
paul@323 | 195 | |
paul@455 | 196 | if [ "$OPTION" = '--build' ] || [ "$OPTION" = "--build-only" ] ; then |
paul@323 | 197 | BUILDLOG="_results/$TESTNAME.build" |
paul@323 | 198 | OUTLOG="_results/$TESTNAME.out" |
paul@465 | 199 | OUTCODE="_results/$TESTNAME.exitcode" |
paul@323 | 200 | |
paul@323 | 201 | echo " (build)..." 1>&2 |
paul@483 | 202 | if ! make -C "$DATADIR/_generated" clean > "$BUILDLOG" 2>&1 || \ |
paul@918 | 203 | ! make -C "$DATADIR/_generated" $MAKE_OPTIONS > "$BUILDLOG" 2>&1 ; then |
paul@323 | 204 | exit 1 |
paul@323 | 205 | fi |
paul@323 | 206 | |
paul@455 | 207 | if [ "$OPTION" = "--build-only" ]; then |
paul@455 | 208 | mv "$DATADIR/_generated/main" "_results/$TESTNAME" |
paul@455 | 209 | else |
paul@455 | 210 | echo " (run)..." 1>&2 |
paul@465 | 211 | "$DATADIR/_generated/main" > "$OUTLOG" < "$TESTINPUT" |
paul@465 | 212 | echo $? > "$OUTCODE" |
paul@323 | 213 | fi |
paul@323 | 214 | fi |
paul@323 | 215 | |
paul@45 | 216 | echo 1>&2 |
paul@4 | 217 | done |