annotate repoint @ 125:34e428693f5d vext

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