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