| 1 | #!/bin/sh |
| 2 | # The Mac's tests, make mac-check on an Apple Silicon Mac (PLAN.md 5): |
| 3 | # test/mac/X.raz compiled for darwin-arm64 by the cross compiler, which |
| 4 | # src/cross.sh builds in the linux/amd64 container, into build/mac/X; |
| 5 | # then each run here, natively, and what it prints and its status |
| 6 | # compared, as test/check.sh compares a test's: |
| 7 | # X.expect its standard output (optional; empty if missing) |
| 8 | # X.status its exit status (optional; 0 if missing) |
| 9 | # Then conform/run's programs the same way, into build/mac/conform, |
| 10 | # with their arguments (X.args): until the port is whole some fail, |
| 11 | # which are counted and listed in build/mac/conform.fail, not failures |
| 12 | # of the run. A few of conform/run are not for this target, each named |
| 13 | # in notHere with the reason, and skipped. |
| 14 | # Then the library: each module's own main, its test, as the target |
| 15 | # takes its file (Name.darwin-arm64.raz, Name.darwin.raz, Name.arm64.raz |
| 16 | # or Name.raz), built with -Werror into build/mac/lib and run there, |
| 17 | # passing if it ends with status 0, as raztest has it; and test/libc's |
| 18 | # programs, over Sys, the C one, into build/mac/libc, compared as |
| 19 | # test/check.sh compares them (X.expect, X.status, and X.stderr with |
| 20 | # each address 0x?). These count as test/mac's do, but those named in |
| 21 | # libNotHere, with the reason, which are skipped. |
| 22 | # |
| 23 | # test/mac.sh build in the container: the cross compiler, then the programs |
| 24 | # test/mac.sh run on the Mac: each program, run with a timeout |
| 25 | # |
| 26 | # A test with a C file, test/mac/X.c, links the library that file makes: |
| 27 | # run builds it by cc into build/mac/libX.dylib, where the program finds |
| 28 | # it by @executable_path (Exe.darwin's libPath), and, built as a program |
| 29 | # with -DMAIN, it must print what X.expect holds, as C itself says it. |
| 30 | # Then lldb, if there is one, must find a line (test/lldb.sh). |
| 31 | # |
| 32 | # build/mac is made anew each time: macOS keeps a file's code signature |
| 33 | # while the file is open or cached, and kills a program whose file was |
| 34 | # written again in place with another one. |
| 35 | set -e |
| 36 | out=build/mac |
| 37 | # conform/run's programs that are not for darwin-arm64, and why |
| 38 | notHere='SystemImport: SYSTEM.SYSCALL is Linux'"'"'s only (report 6.8)' |
| 39 | # the library's tests and test/libc's programs that are not for it, and why |
| 40 | libNotHere='test/libc/ExecveC: reads what signals sh ignores and catches in /proc/self/status, which is Linux'"'"'s' |
| 41 | |
| 42 | # The file of module $1 the target takes, as the compiler looks for it |
| 43 | libFile() { |
| 44 | for f in lib/$1.darwin-arm64.raz lib/$1.darwin.raz lib/$1.arm64.raz lib/$1.raz; do |
| 45 | [ -f $f ] && { echo $f; return; } |
| 46 | done |
| 47 | } |
| 48 | |
| 49 | # The library's modules that have a test, by the files the target takes |
| 50 | libTests() { |
| 51 | for m in $(ls lib/*.raz | sed 's|lib/||; s|\..*||' | sort -u); do |
| 52 | f=$(libFile $m) |
| 53 | [ -n "$f" ] && grep -q '^proc main\*' $f && echo $f |
| 54 | done |
| 55 | } |
| 56 | |
| 57 | # Runs $out/$1/$2 with a timeout, in its directory, from the arguments |
| 58 | # on: its status, its output in $2.out and $2.err |
| 59 | runIn() { |
| 60 | dir=$1; prog=$2; shift 2 |
| 61 | (cd $out/$dir && perl -e 'alarm 60; exec @ARGV' ./$prog "$@" > $prog.out 2>$prog.err) || return $? |
| 62 | } |
| 63 | case $1 in |
| 64 | build) |
| 65 | src/cross.sh darwin-arm64 |
| 66 | rm -rf $out |
| 67 | mkdir -p $out |
| 68 | for f in test/mac/*.raz; do |
| 69 | name=$(basename $f .raz) |
| 70 | build/cross/darwin-arm64/raz -o $out/$name $f 2>$out/$name.cerr || true |
| 71 | done |
| 72 | mkdir -p $out/conform |
| 73 | for f in conform/run/*.raz; do |
| 74 | name=$(basename $f .raz) |
| 75 | build/cross/darwin-arm64/raz -I conform/run/mods -o $out/conform/$name $f \ |
| 76 | 2>$out/conform/$name.cerr || true |
| 77 | done |
| 78 | mkdir -p $out/lib $out/libc |
| 79 | for f in $(libTests); do |
| 80 | name=$(basename $f .raz); name=${name%%.*} |
| 81 | build/cross/darwin-arm64/raz -Werror -o $out/lib/$name $f 2>$out/lib/$name.cerr || true |
| 82 | done |
| 83 | for f in test/libc/*.raz; do |
| 84 | name=$(basename $f .raz) |
| 85 | build/cross/darwin-arm64/raz -Werror -I test/libc/mods -o $out/libc/$name $f \ |
| 86 | 2>$out/libc/$name.cerr || true |
| 87 | done |
| 88 | ;; |
| 89 | run) |
| 90 | pass=0 |
| 91 | fail=0 |
| 92 | for f in test/mac/*.c; do |
| 93 | name=$(basename $f .c) |
| 94 | cc -dynamiclib -o $out/lib$name.dylib $f |
| 95 | cc -DMAIN -o $out/$name.cmain $f |
| 96 | if ! $out/$name.cmain | cmp -s - test/mac/$name.expect; then |
| 97 | echo "FAIL $f: C's own output differs from $name.expect"; fail=$((fail + 1)) |
| 98 | fi |
| 99 | done |
| 100 | for f in test/mac/*.raz; do |
| 101 | name=$(basename $f .raz) |
| 102 | want=0 |
| 103 | [ -f test/mac/$name.status ] && want=$(cat test/mac/$name.status) |
| 104 | if [ ! -x $out/$name ]; then |
| 105 | echo "FAIL $f: doesn't compile: $(cat $out/$name.cerr 2>/dev/null)"; fail=$((fail + 1)); continue |
| 106 | fi |
| 107 | # perl's alarm, as macOS has no timeout |
| 108 | status=0 |
| 109 | perl -e 'alarm 10; exec @ARGV' ./$out/$name > $out/$name.out 2>$out/$name.err || status=$? |
| 110 | if [ -f test/mac/$name.expect ]; then exp=test/mac/$name.expect; else exp=/dev/null; fi |
| 111 | if [ $status != $want ]; then |
| 112 | echo "FAIL $f: status $status, not $want"; fail=$((fail + 1)) |
| 113 | elif ! cmp -s $out/$name.out $exp; then |
| 114 | echo "FAIL $f: output differs"; fail=$((fail + 1)) |
| 115 | else |
| 116 | pass=$((pass + 1)) |
| 117 | fi |
| 118 | done |
| 119 | echo "$pass passed, $fail failed" |
| 120 | # conform/run, counted: why each fails goes to conform.fail |
| 121 | c=$out/conform |
| 122 | cpass=0 |
| 123 | cfail=0 |
| 124 | cskip=0 |
| 125 | : > $c.fail |
| 126 | for f in conform/run/*.raz; do |
| 127 | name=$(basename $f .raz) |
| 128 | if echo "$notHere" | grep -q "^$name:"; then cskip=$((cskip + 1)); continue; fi |
| 129 | want=0 |
| 130 | [ -f conform/run/$name.status ] && want=$(cat conform/run/$name.status) |
| 131 | if [ ! -x $c/$name ]; then |
| 132 | echo "$name: doesn't compile: $(grep -m1 error $c/$name.cerr)" >> $c.fail |
| 133 | cfail=$((cfail + 1)); continue |
| 134 | fi |
| 135 | set -- |
| 136 | if [ -f conform/run/$name.args ]; then |
| 137 | while IFS= read -r a; do set -- "$@" "$a"; done < conform/run/$name.args |
| 138 | fi |
| 139 | status=0 |
| 140 | perl -e 'alarm 10; exec @ARGV' ./$c/$name "$@" > $c/$name.out 2>$c/$name.err || status=$? |
| 141 | if [ $status != $want ]; then |
| 142 | echo "$name: status $status, not $want: $(head -1 $c/$name.err)" >> $c.fail |
| 143 | cfail=$((cfail + 1)) |
| 144 | elif ! cmp -s $c/$name.out conform/run/$name.expect; then |
| 145 | echo "$name: output differs" >> $c.fail |
| 146 | cfail=$((cfail + 1)) |
| 147 | else |
| 148 | cpass=$((cpass + 1)) |
| 149 | fi |
| 150 | done |
| 151 | echo "conform/run: $cpass of $((cpass + cfail)) passed, $cskip not for this target; the others in $c.fail" |
| 152 | # the library's tests, and test/libc's programs |
| 153 | lpass=0 |
| 154 | lfail=0 |
| 155 | lskip=0 |
| 156 | for f in $(libTests); do |
| 157 | name=$(basename $f .raz); name=${name%%.*} |
| 158 | if echo "$libNotHere" | grep -q "^lib/$name:"; then lskip=$((lskip + 1)); continue; fi |
| 159 | if [ ! -x $out/lib/$name ]; then |
| 160 | echo "FAIL $f: doesn't compile: $(head -3 $out/lib/$name.cerr)"; lfail=$((lfail + 1)); continue |
| 161 | fi |
| 162 | status=0 |
| 163 | runIn lib $name || status=$? |
| 164 | if [ $status != 0 ]; then |
| 165 | echo "FAIL $f: status $status"; tail -5 $out/lib/$name.err; lfail=$((lfail + 1)) |
| 166 | else |
| 167 | lpass=$((lpass + 1)) |
| 168 | fi |
| 169 | done |
| 170 | for f in test/libc/*.raz; do |
| 171 | name=$(basename $f .raz) |
| 172 | if echo "$libNotHere" | grep -q "^test/libc/$name:"; then lskip=$((lskip + 1)); continue; fi |
| 173 | if [ ! -x $out/libc/$name ]; then |
| 174 | echo "FAIL $f: doesn't compile: $(head -3 $out/libc/$name.cerr)"; lfail=$((lfail + 1)); continue |
| 175 | fi |
| 176 | want=0 |
| 177 | [ -f test/libc/$name.status ] && want=$(cat test/libc/$name.status) |
| 178 | # from the repository's root, as a message names the file so |
| 179 | status=0 |
| 180 | perl -e 'alarm 60; exec @ARGV' ./$out/libc/$name > $out/libc/$name.out 2>$out/libc/$name.err || status=$? |
| 181 | if [ -f test/libc/$name.expect ]; then exp=test/libc/$name.expect; else exp=/dev/null; fi |
| 182 | if [ $status != $want ]; then |
| 183 | echo "FAIL $f: status $status, not $want"; head -3 $out/libc/$name.err; lfail=$((lfail + 1)) |
| 184 | elif ! cmp -s $out/libc/$name.out $exp; then |
| 185 | echo "FAIL $f: output differs"; diff $out/libc/$name.out $exp | head -5; lfail=$((lfail + 1)) |
| 186 | elif [ -f test/libc/$name.stderr ] && ! sed 's/0x[0-9a-f]*/0x?/g' $out/libc/$name.err | |
| 187 | cmp -s - test/libc/$name.stderr; then |
| 188 | echo "FAIL $f: standard error differs"; diff $out/libc/$name.err test/libc/$name.stderr | head -5 |
| 189 | lfail=$((lfail + 1)) |
| 190 | else |
| 191 | lpass=$((lpass + 1)) |
| 192 | fi |
| 193 | done |
| 194 | echo "the library and test/libc: $lpass passed, $lfail failed, $lskip not for this target" |
| 195 | if command -v lldb > /dev/null; then test/lldb.sh || fail=$((fail + 1)); fi |
| 196 | [ $fail = 0 ] && [ $lfail = 0 ] |
| 197 | ;; |
| 198 | *) |
| 199 | echo "usage: test/mac.sh build|run" >&2 |
| 200 | exit 2 |
| 201 | ;; |
| 202 | esac |