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