annotate vext @ 124:fc093b176444 vext

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