view vext @ 1763:799c5e64d11b

Bump the version
author Chris Cannam
date Wed, 31 Jan 2018 11:36:29 +0000
parents bf4a7015033e
children 128c4544036d
line wrap: on
line source
#!/bin/bash

# Disable shellcheck warnings for useless-use-of-cat. UUOC is good
# practice, not bad: clearer, safer, less error-prone.
# shellcheck disable=SC2002

sml="$VEXT_SML"

set -eu

mydir=$(dirname "$0")
program="$mydir/vext.sml"

hasher=
local_install=
if [ -w "$mydir" ]; then
    if echo | sha256sum >/dev/null 2>&1 ; then
	hasher=sha256sum
        local_install=true
    elif echo | shasum >/dev/null 2>&1 ; then
	hasher=shasum
	local_install=true
    else
        echo "WARNING: sha256sum or shasum program not found" 1>&2
    fi
fi

if [ -n "$local_install" ]; then
    hash=$(echo "$sml" | cat "$program" - | $hasher | cut -c1-16)
    gen_sml=$mydir/.vext-$hash.sml
    gen_out=$mydir/.vext-$hash.bin
    trap 'rm -f $gen_sml' 0
else
    gen_sml=$(mktemp /tmp/vext-XXXXXXXX.sml)
    gen_out=$(mktemp /tmp/vext-XXXXXXXX.bin)
    trap 'rm -f $gen_sml $gen_out' 0
fi

if [ -x "$gen_out" ]; then
    exec "$gen_out" "$@"
fi

# We need one of Poly/ML, SML/NJ, or MLton. Since we're running a
# single-file SML program as if it were a script, our order of
# preference is based on startup speed, except in the local_install
# case where we retain a persistent binary.

if [ -z "$sml" ]; then
    if [ -n "$local_install" ] && mlton 2>&1 | grep -q 'MLton'; then
	sml="mlton"
    elif sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then
	sml="smlnj"
    # We would prefer Poly/ML to SML/NJ, except that Poly v5.7 has a
    # nasty bug that occasionally causes it to deadlock on startup.
    # That appears to be fixed in their repo, so we could promote it
    # up the order again at some point in future
    elif echo | poly -v 2>/dev/null | grep -q 'Poly/ML'; then
	sml="poly"
    elif mlton 2>&1 | grep -q 'MLton'; then
	sml="mlton"
    else cat 1>&2 <<EOF

ERROR: No supported SML compiler or interpreter found       
EOF
	cat 1>&2 <<EOF

  The Vext external source code manager needs a Standard ML (SML)
  compiler or interpreter to run.

  Please ensure you have one of the following SML implementations
  installed and present in your PATH, and try again.

    1. Standard ML of New Jersey
       - often found in a distribution package called: smlnj
       - executable name: sml

    2. Poly/ML
       - often found in a distribution package called: polyml
       - executable name: poly

    3. MLton
       - often found in a distribution package called: mlton
       - executable name: mlton

EOF
	exit 2
    fi
fi

arglist=""
for arg in "$@"; do
    if [ -n "$arglist" ]; then arglist="$arglist,"; fi
    if echo "$arg" | grep -q '["'"'"']' ; then
	arglist="$arglist\"usage\""
    else
	arglist="$arglist\"$arg\""
    fi
done

case "$sml" in
    poly)
        if [ -n "$local_install" ] && polyc --help >/dev/null 2>&1 ; then
            if [ ! -x "$gen_out" ]; then
                polyc -o "$gen_out" "$program"
            fi
	    "$gen_out" "$@"
        else
            echo 'use "'"$program"'"; vext ['"$arglist"'];' |
                poly -q --error-exit
        fi ;;
    mlton)
        if [ ! -x "$gen_out" ]; then
	    echo "[Precompiling Vext binary...]" 1>&2
	    echo "val _ = main ()" | cat "$program" - > "$gen_sml"
	    mlton -output "$gen_out" "$gen_sml"
        fi
	"$gen_out" "$@" ;;
    smlnj)
	cat "$program" | (
	    cat <<EOF
val smlrun__cp = 
    let val x = !Control.Print.out in
        Control.Print.out := { say = fn _ => (), flush = fn () => () };
        x
    end;
val smlrun__prev = ref "";
Control.Print.out := { 
    say = fn s => 
        (if String.isSubstring " Error" s
         then (Control.Print.out := smlrun__cp;
               (#say smlrun__cp) (!smlrun__prev);
               (#say smlrun__cp) s)
         else (smlrun__prev := s; ())),
    flush = fn s => ()
};
EOF
	    cat -
	    cat <<EOF
val _ = vext [$arglist];
val _ = OS.Process.exit (OS.Process.success);
EOF
            ) > "$gen_sml"
	CM_VERBOSE=false sml "$gen_sml" ;;
    *)
	echo "ERROR: Unknown SML implementation name: $sml" 1>&2;
	exit 2 ;;
esac