annotate repoint @ 698:ee97c742d184 tip

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