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