annotate vext @ 1781:89cf48de5969

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