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