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