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