annotate repoint @ 84:55a047986812 tip

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