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