annotate vext @ 50:ec5b5a9adac2

Overhaul to use Vext for external repos
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 28 Jun 2017 10:49:26 +0100
parents
children 3a6ebb47393f
rev   line source
c@50 1 #!/bin/bash
c@50 2
c@50 3 # Disable shellcheck warnings for useless-use-of-cat. UUOC is good
c@50 4 # practice, not bad: clearer, safer, less error-prone.
c@50 5 # shellcheck disable=SC2002
c@50 6
c@50 7 sml="$VEXT_SML"
c@50 8
c@50 9 set -eu
c@50 10
c@50 11 mydir=$(dirname "$0")
c@50 12 program="$mydir/vext.sml"
c@50 13
c@50 14 # We need one of Poly/ML, SML/NJ, or MLton. Since we're running a
c@50 15 # single-file SML program as if it were a script, our order of
c@50 16 # preference is based on startup speed.
c@50 17
c@50 18 if [ -z "$sml" ]; then
c@50 19 if sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then
c@50 20 sml="smlnj"
c@50 21 # We would prefer Poly/ML to SML/NJ, except that Poly v5.7 has a
c@50 22 # nasty bug that occasionally causes it to deadlock on startup.
c@50 23 # That appears to be fixed in their repo, so we could promote it
c@50 24 # up the order again at some point in future
c@50 25 elif echo | poly -v 2>/dev/null | grep -q 'Poly/ML'; then
c@50 26 sml="poly"
c@50 27 elif mlton 2>&1 | grep -q 'MLton'; then
c@50 28 sml="mlton"
c@50 29 else cat 1>&2 <<EOF
c@50 30
c@50 31 ERROR: No supported SML compiler or interpreter found
c@50 32 EOF
c@50 33 cat <<EOF
c@50 34
c@50 35 The Vext external source code manager needs a Standard ML (SML)
c@50 36 compiler or interpreter to run.
c@50 37
c@50 38 Please ensure you have one of the following SML implementations
c@50 39 installed and present in your PATH, and try again.
c@50 40
c@50 41 1. Standard ML of New Jersey
c@50 42 - often found in a distribution package called: smlnj
c@50 43 - executable name: sml
c@50 44
c@50 45 2. Poly/ML
c@50 46 - often found in a distribution package called: polyml
c@50 47 - executable name: poly
c@50 48
c@50 49 3. MLton
c@50 50 - often found in a distribution package called: mlton
c@50 51 - executable name: mlton
c@50 52
c@50 53 EOF
c@50 54 exit 2
c@50 55 fi
c@50 56 fi
c@50 57
c@50 58 tmp_sml=$(mktemp /tmp/vext-XXXXXXXX.sml)
c@50 59 tmp_out=$(mktemp /tmp/vext-XXXXXXXX.bin)
c@50 60
c@50 61 trap 'rm -f $tmp_sml $tmp_out' 0
c@50 62
c@50 63 arglist=""
c@50 64 for arg in "$@"; do
c@50 65 if [ -n "$arglist" ]; then arglist="$arglist,"; fi
c@50 66 if echo "$arg" | grep -q '[^a-z]' ; then
c@50 67 arglist="$arglist\"usage\""
c@50 68 else
c@50 69 arglist="$arglist\"$arg\""
c@50 70 fi
c@50 71 done
c@50 72
c@50 73 case "$sml" in
c@50 74 poly) echo 'use "'"$program"'"; vext ['"$arglist"'];' |
c@50 75 poly -q --error-exit ;;
c@50 76 mlton)
c@50 77 cat "$program" > "$tmp_sml"
c@50 78 echo 'val _ = main ()' >> "$tmp_sml"
c@50 79 mlton -output "$tmp_out" "$tmp_sml"
c@50 80 "$tmp_out" "$@" ;;
c@50 81 smlnj)
c@50 82 cat "$program" | (
c@50 83 cat <<EOF
c@50 84 val smlrun__cp =
c@50 85 let val x = !Control.Print.out in
c@50 86 Control.Print.out := { say = fn _ => (), flush = fn () => () };
c@50 87 x
c@50 88 end;
c@50 89 val smlrun__prev = ref "";
c@50 90 Control.Print.out := {
c@50 91 say = fn s =>
c@50 92 (if String.isSubstring " Error" s
c@50 93 then (Control.Print.out := smlrun__cp;
c@50 94 (#say smlrun__cp) (!smlrun__prev);
c@50 95 (#say smlrun__cp) s)
c@50 96 else (smlrun__prev := s; ())),
c@50 97 flush = fn s => ()
c@50 98 };
c@50 99 EOF
c@50 100 cat -
c@50 101 cat <<EOF
c@50 102 val _ = vext [$arglist];
c@50 103 val _ = OS.Process.exit (OS.Process.success);
c@50 104 EOF
c@50 105 ) > "$tmp_sml"
c@50 106 CM_VERBOSE=false sml "$tmp_sml" ;;
c@50 107 *)
c@50 108 echo "Unknown SML implementation name: $sml";
c@50 109 exit 2 ;;
c@50 110 esac
c@50 111