annotate vext @ 523:9fc762aafd01

Switch to Vext
author Chris Cannam
date Mon, 10 Jul 2017 19:27:26 +0100
parents
children 2cc8700975db
rev   line source
Chris@523 1 #!/bin/bash
Chris@523 2
Chris@523 3 # Disable shellcheck warnings for useless-use-of-cat. UUOC is good
Chris@523 4 # practice, not bad: clearer, safer, less error-prone.
Chris@523 5 # shellcheck disable=SC2002
Chris@523 6
Chris@523 7 sml="$VEXT_SML"
Chris@523 8
Chris@523 9 set -eu
Chris@523 10
Chris@523 11 mydir=$(dirname "$0")
Chris@523 12 program="$mydir/vext.sml"
Chris@523 13
Chris@523 14 # We need one of Poly/ML, SML/NJ, or MLton. Since we're running a
Chris@523 15 # single-file SML program as if it were a script, our order of
Chris@523 16 # preference is based on startup speed.
Chris@523 17
Chris@523 18 if [ -z "$sml" ]; then
Chris@523 19 if sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then
Chris@523 20 sml="smlnj"
Chris@523 21 # We would prefer Poly/ML to SML/NJ, except that Poly v5.7 has a
Chris@523 22 # nasty bug that occasionally causes it to deadlock on startup.
Chris@523 23 # That appears to be fixed in their repo, so we could promote it
Chris@523 24 # up the order again at some point in future
Chris@523 25 elif echo | poly -v 2>/dev/null | grep -q 'Poly/ML'; then
Chris@523 26 sml="poly"
Chris@523 27 elif mlton 2>&1 | grep -q 'MLton'; then
Chris@523 28 sml="mlton"
Chris@523 29 else cat 1>&2 <<EOF
Chris@523 30
Chris@523 31 ERROR: No supported SML compiler or interpreter found
Chris@523 32 EOF
Chris@523 33 cat <<EOF
Chris@523 34
Chris@523 35 The Vext external source code manager needs a Standard ML (SML)
Chris@523 36 compiler or interpreter to run.
Chris@523 37
Chris@523 38 Please ensure you have one of the following SML implementations
Chris@523 39 installed and present in your PATH, and try again.
Chris@523 40
Chris@523 41 1. Standard ML of New Jersey
Chris@523 42 - often found in a distribution package called: smlnj
Chris@523 43 - executable name: sml
Chris@523 44
Chris@523 45 2. Poly/ML
Chris@523 46 - often found in a distribution package called: polyml
Chris@523 47 - executable name: poly
Chris@523 48
Chris@523 49 3. MLton
Chris@523 50 - often found in a distribution package called: mlton
Chris@523 51 - executable name: mlton
Chris@523 52
Chris@523 53 EOF
Chris@523 54 exit 2
Chris@523 55 fi
Chris@523 56 fi
Chris@523 57
Chris@523 58 tmp_sml=$(mktemp /tmp/vext-XXXXXXXX.sml)
Chris@523 59 tmp_out=$(mktemp /tmp/vext-XXXXXXXX.bin)
Chris@523 60
Chris@523 61 trap 'rm -f $tmp_sml $tmp_out' 0
Chris@523 62
Chris@523 63 arglist=""
Chris@523 64 for arg in "$@"; do
Chris@523 65 if [ -n "$arglist" ]; then arglist="$arglist,"; fi
Chris@523 66 if echo "$arg" | grep -q '[^a-z]' ; then
Chris@523 67 arglist="$arglist\"usage\""
Chris@523 68 else
Chris@523 69 arglist="$arglist\"$arg\""
Chris@523 70 fi
Chris@523 71 done
Chris@523 72
Chris@523 73 case "$sml" in
Chris@523 74 poly) echo 'use "'"$program"'"; vext ['"$arglist"'];' |
Chris@523 75 poly -q --error-exit ;;
Chris@523 76 mlton)
Chris@523 77 cat "$program" > "$tmp_sml"
Chris@523 78 echo 'val _ = main ()' >> "$tmp_sml"
Chris@523 79 mlton -output "$tmp_out" "$tmp_sml"
Chris@523 80 "$tmp_out" "$@" ;;
Chris@523 81 smlnj)
Chris@523 82 cat "$program" | (
Chris@523 83 cat <<EOF
Chris@523 84 val smlrun__cp =
Chris@523 85 let val x = !Control.Print.out in
Chris@523 86 Control.Print.out := { say = fn _ => (), flush = fn () => () };
Chris@523 87 x
Chris@523 88 end;
Chris@523 89 val smlrun__prev = ref "";
Chris@523 90 Control.Print.out := {
Chris@523 91 say = fn s =>
Chris@523 92 (if String.isSubstring " Error" s
Chris@523 93 then (Control.Print.out := smlrun__cp;
Chris@523 94 (#say smlrun__cp) (!smlrun__prev);
Chris@523 95 (#say smlrun__cp) s)
Chris@523 96 else (smlrun__prev := s; ())),
Chris@523 97 flush = fn s => ()
Chris@523 98 };
Chris@523 99 EOF
Chris@523 100 cat -
Chris@523 101 cat <<EOF
Chris@523 102 val _ = vext [$arglist];
Chris@523 103 val _ = OS.Process.exit (OS.Process.success);
Chris@523 104 EOF
Chris@523 105 ) > "$tmp_sml"
Chris@523 106 CM_VERBOSE=false sml "$tmp_sml" ;;
Chris@523 107 *)
Chris@523 108 echo "Unknown SML implementation name: $sml";
Chris@523 109 exit 2 ;;
Chris@523 110 esac
Chris@523 111