Chris@523: #!/bin/bash Chris@523: Chris@523: # Disable shellcheck warnings for useless-use-of-cat. UUOC is good Chris@523: # practice, not bad: clearer, safer, less error-prone. Chris@523: # shellcheck disable=SC2002 Chris@523: Chris@531: sml="$REPOINT_SML" Chris@523: Chris@523: set -eu Chris@523: Chris@531: # avoid gussying up output Chris@531: export HGPLAIN=true Chris@531: Chris@523: mydir=$(dirname "$0") Chris@531: program="$mydir/repoint.sml" Chris@523: Chris@529: hasher= Chris@529: local_install= Chris@529: if [ -w "$mydir" ]; then Chris@529: if echo | sha256sum >/dev/null 2>&1 ; then Chris@529: hasher=sha256sum Chris@529: local_install=true Chris@529: elif echo | shasum >/dev/null 2>&1 ; then Chris@529: hasher=shasum Chris@529: local_install=true Chris@529: else Chris@529: echo "WARNING: sha256sum or shasum program not found" 1>&2 Chris@529: fi Chris@529: fi Chris@529: Chris@529: if [ -n "$local_install" ]; then Chris@529: hash=$(echo "$sml" | cat "$program" - | $hasher | cut -c1-16) Chris@531: gen_sml=$mydir/.repoint-$hash.sml Chris@531: gen_out=$mydir/.repoint-$hash.bin Chris@529: trap 'rm -f $gen_sml' 0 Chris@529: else Chris@531: gen_sml=$(mktemp /tmp/repoint-XXXXXXXX.sml) Chris@531: gen_out=$(mktemp /tmp/repoint-XXXXXXXX.bin) Chris@529: trap 'rm -f $gen_sml $gen_out' 0 Chris@529: fi Chris@529: Chris@529: if [ -x "$gen_out" ]; then Chris@529: exec "$gen_out" "$@" Chris@529: fi Chris@529: Chris@531: # We need one of Poly/ML, SML/NJ, MLton, or MLKit. Since we're running Chris@531: # a single-file SML program as if it were a script, our order of Chris@531: # preference is usually based on startup speed. An exception is the Chris@531: # local_install case, where we retain a persistent binary Chris@523: Chris@523: if [ -z "$sml" ]; then Chris@529: if [ -n "$local_install" ] && mlton 2>&1 | grep -q 'MLton'; then Chris@529: sml="mlton" Chris@529: elif sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then Chris@523: sml="smlnj" Chris@523: # We would prefer Poly/ML to SML/NJ, except that Poly v5.7 has a Chris@523: # nasty bug that occasionally causes it to deadlock on startup. Chris@531: # That is fixed in v5.7.1, so we could promote it up the order Chris@531: # again at some point in future Chris@523: elif echo | poly -v 2>/dev/null | grep -q 'Poly/ML'; then Chris@540: sml="polyml" Chris@523: elif mlton 2>&1 | grep -q 'MLton'; then Chris@523: sml="mlton" Chris@531: # MLKit is at the bottom because it leaves compiled files around Chris@531: # in an MLB subdir in the current directory Chris@531: elif mlkit 2>&1 | grep -q 'MLKit'; then Chris@531: sml="mlkit" Chris@523: else cat 1>&2 <&2 </dev/null 2>&1 ; then Chris@529: if [ ! -x "$gen_out" ]; then Chris@529: polyc -o "$gen_out" "$program" Chris@529: fi Chris@529: "$gen_out" "$@" Chris@529: else Chris@531: echo 'use "'"$program"'"; repoint ['"$arglist"'];' | Chris@529: poly -q --error-exit Chris@529: fi ;; Chris@523: mlton) Chris@529: if [ ! -x "$gen_out" ]; then Chris@531: echo "[Precompiling Repoint binary...]" 1>&2 Chris@529: echo "val _ = main ()" | cat "$program" - > "$gen_sml" Chris@529: mlton -output "$gen_out" "$gen_sml" Chris@529: fi Chris@529: "$gen_out" "$@" ;; Chris@531: mlkit) Chris@531: if [ ! -x "$gen_out" ]; then Chris@531: echo "[Precompiling Repoint binary...]" 1>&2 Chris@531: echo "val _ = main ()" | cat "$program" - > "$gen_sml" Chris@531: mlkit -output "$gen_out" "$gen_sml" Chris@531: fi Chris@531: "$gen_out" "$@" ;; Chris@523: smlnj) Chris@523: cat "$program" | ( Chris@523: cat < (), flush = fn () => () }; Chris@523: x Chris@523: end; Chris@523: val smlrun__prev = ref ""; Chris@523: Control.Print.out := { Chris@523: say = fn s => Chris@523: (if String.isSubstring " Error" s Chris@523: then (Control.Print.out := smlrun__cp; Chris@523: (#say smlrun__cp) (!smlrun__prev); Chris@523: (#say smlrun__cp) s) Chris@523: else (smlrun__prev := s; ())), Chris@523: flush = fn s => () Chris@523: }; Chris@523: EOF Chris@523: cat - Chris@523: cat < "$gen_sml" Chris@529: CM_VERBOSE=false sml "$gen_sml" ;; Chris@523: *) Chris@529: echo "ERROR: Unknown SML implementation name: $sml" 1>&2; Chris@523: exit 2 ;; Chris@523: esac Chris@523: