annotate vext @ 529:2cc8700975db

Update vext
author Chris Cannam
date Fri, 06 Oct 2017 13:28:52 +0100
parents 9fc762aafd01
children
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@529 14 hasher=
Chris@529 15 local_install=
Chris@529 16 if [ -w "$mydir" ]; then
Chris@529 17 if echo | sha256sum >/dev/null 2>&1 ; then
Chris@529 18 hasher=sha256sum
Chris@529 19 local_install=true
Chris@529 20 elif echo | shasum >/dev/null 2>&1 ; then
Chris@529 21 hasher=shasum
Chris@529 22 local_install=true
Chris@529 23 else
Chris@529 24 echo "WARNING: sha256sum or shasum program not found" 1>&2
Chris@529 25 fi
Chris@529 26 fi
Chris@529 27
Chris@529 28 if [ -n "$local_install" ]; then
Chris@529 29 hash=$(echo "$sml" | cat "$program" - | $hasher | cut -c1-16)
Chris@529 30 gen_sml=$mydir/.vext-$hash.sml
Chris@529 31 gen_out=$mydir/.vext-$hash.bin
Chris@529 32 trap 'rm -f $gen_sml' 0
Chris@529 33 else
Chris@529 34 gen_sml=$(mktemp /tmp/vext-XXXXXXXX.sml)
Chris@529 35 gen_out=$(mktemp /tmp/vext-XXXXXXXX.bin)
Chris@529 36 trap 'rm -f $gen_sml $gen_out' 0
Chris@529 37 fi
Chris@529 38
Chris@529 39 if [ -x "$gen_out" ]; then
Chris@529 40 exec "$gen_out" "$@"
Chris@529 41 fi
Chris@529 42
Chris@523 43 # We need one of Poly/ML, SML/NJ, or MLton. Since we're running a
Chris@523 44 # single-file SML program as if it were a script, our order of
Chris@529 45 # preference is based on startup speed, except in the local_install
Chris@529 46 # case where we retain a persistent binary.
Chris@523 47
Chris@523 48 if [ -z "$sml" ]; then
Chris@529 49 if [ -n "$local_install" ] && mlton 2>&1 | grep -q 'MLton'; then
Chris@529 50 sml="mlton"
Chris@529 51 elif sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then
Chris@523 52 sml="smlnj"
Chris@523 53 # We would prefer Poly/ML to SML/NJ, except that Poly v5.7 has a
Chris@523 54 # nasty bug that occasionally causes it to deadlock on startup.
Chris@523 55 # That appears to be fixed in their repo, so we could promote it
Chris@523 56 # up the order again at some point in future
Chris@523 57 elif echo | poly -v 2>/dev/null | grep -q 'Poly/ML'; then
Chris@523 58 sml="poly"
Chris@523 59 elif mlton 2>&1 | grep -q 'MLton'; then
Chris@523 60 sml="mlton"
Chris@523 61 else cat 1>&2 <<EOF
Chris@523 62
Chris@523 63 ERROR: No supported SML compiler or interpreter found
Chris@523 64 EOF
Chris@529 65 cat 1>&2 <<EOF
Chris@523 66
Chris@523 67 The Vext external source code manager needs a Standard ML (SML)
Chris@523 68 compiler or interpreter to run.
Chris@523 69
Chris@523 70 Please ensure you have one of the following SML implementations
Chris@523 71 installed and present in your PATH, and try again.
Chris@523 72
Chris@523 73 1. Standard ML of New Jersey
Chris@523 74 - often found in a distribution package called: smlnj
Chris@523 75 - executable name: sml
Chris@523 76
Chris@523 77 2. Poly/ML
Chris@523 78 - often found in a distribution package called: polyml
Chris@523 79 - executable name: poly
Chris@523 80
Chris@523 81 3. MLton
Chris@523 82 - often found in a distribution package called: mlton
Chris@523 83 - executable name: mlton
Chris@523 84
Chris@523 85 EOF
Chris@523 86 exit 2
Chris@523 87 fi
Chris@523 88 fi
Chris@523 89
Chris@523 90 arglist=""
Chris@523 91 for arg in "$@"; do
Chris@523 92 if [ -n "$arglist" ]; then arglist="$arglist,"; fi
Chris@529 93 if echo "$arg" | grep -q '["'"'"']' ; then
Chris@523 94 arglist="$arglist\"usage\""
Chris@523 95 else
Chris@523 96 arglist="$arglist\"$arg\""
Chris@523 97 fi
Chris@523 98 done
Chris@523 99
Chris@523 100 case "$sml" in
Chris@529 101 poly)
Chris@529 102 if [ -n "$local_install" ] && polyc --help >/dev/null 2>&1 ; then
Chris@529 103 if [ ! -x "$gen_out" ]; then
Chris@529 104 polyc -o "$gen_out" "$program"
Chris@529 105 fi
Chris@529 106 "$gen_out" "$@"
Chris@529 107 else
Chris@529 108 echo 'use "'"$program"'"; vext ['"$arglist"'];' |
Chris@529 109 poly -q --error-exit
Chris@529 110 fi ;;
Chris@523 111 mlton)
Chris@529 112 if [ ! -x "$gen_out" ]; then
Chris@529 113 echo "[Precompiling Vext binary...]" 1>&2
Chris@529 114 echo "val _ = main ()" | cat "$program" - > "$gen_sml"
Chris@529 115 mlton -output "$gen_out" "$gen_sml"
Chris@529 116 fi
Chris@529 117 "$gen_out" "$@" ;;
Chris@523 118 smlnj)
Chris@523 119 cat "$program" | (
Chris@523 120 cat <<EOF
Chris@523 121 val smlrun__cp =
Chris@523 122 let val x = !Control.Print.out in
Chris@523 123 Control.Print.out := { say = fn _ => (), flush = fn () => () };
Chris@523 124 x
Chris@523 125 end;
Chris@523 126 val smlrun__prev = ref "";
Chris@523 127 Control.Print.out := {
Chris@523 128 say = fn s =>
Chris@523 129 (if String.isSubstring " Error" s
Chris@523 130 then (Control.Print.out := smlrun__cp;
Chris@523 131 (#say smlrun__cp) (!smlrun__prev);
Chris@523 132 (#say smlrun__cp) s)
Chris@523 133 else (smlrun__prev := s; ())),
Chris@523 134 flush = fn s => ()
Chris@523 135 };
Chris@523 136 EOF
Chris@523 137 cat -
Chris@523 138 cat <<EOF
Chris@523 139 val _ = vext [$arglist];
Chris@523 140 val _ = OS.Process.exit (OS.Process.success);
Chris@523 141 EOF
Chris@529 142 ) > "$gen_sml"
Chris@529 143 CM_VERBOSE=false sml "$gen_sml" ;;
Chris@523 144 *)
Chris@529 145 echo "ERROR: Unknown SML implementation name: $sml" 1>&2;
Chris@523 146 exit 2 ;;
Chris@523 147 esac
Chris@523 148