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@122: sml="$VEXT_SML" Chris@122: Chris@122: set -eu Chris@122: Chris@122: mydir=$(dirname "$0") Chris@122: program="$mydir/vext.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@122: gen_sml=$mydir/.vext-$hash.sml Chris@122: gen_out=$mydir/.vext-$hash.bin Chris@122: trap 'rm -f $gen_sml' 0 Chris@122: else Chris@122: gen_sml=$(mktemp /tmp/vext-XXXXXXXX.sml) Chris@122: gen_out=$(mktemp /tmp/vext-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@122: # We need one of Poly/ML, SML/NJ, or MLton. Since we're running a Chris@122: # single-file SML program as if it were a script, our order of Chris@122: # preference is based on startup speed, except in the local_install Chris@122: # 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@122: # That appears to be fixed in their repo, so we could promote it Chris@122: # up the order again at some point in future Chris@122: elif echo | poly -v 2>/dev/null | grep -q 'Poly/ML'; then Chris@122: sml="poly" Chris@122: elif mlton 2>&1 | grep -q 'MLton'; then Chris@122: sml="mlton" 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@122: echo 'use "'"$program"'"; vext ['"$arglist"'];' | Chris@122: poly -q --error-exit Chris@122: fi ;; Chris@122: mlton) Chris@122: if [ ! -x "$gen_out" ]; then Chris@122: echo "[Precompiling Vext 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@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: