comparison vext @ 529:2cc8700975db

Update vext
author Chris Cannam
date Fri, 06 Oct 2017 13:28:52 +0100
parents 9fc762aafd01
children
comparison
equal deleted inserted replaced
528:b6906ff30276 529:2cc8700975db
9 set -eu 9 set -eu
10 10
11 mydir=$(dirname "$0") 11 mydir=$(dirname "$0")
12 program="$mydir/vext.sml" 12 program="$mydir/vext.sml"
13 13
14 hasher=
15 local_install=
16 if [ -w "$mydir" ]; then
17 if echo | sha256sum >/dev/null 2>&1 ; then
18 hasher=sha256sum
19 local_install=true
20 elif echo | shasum >/dev/null 2>&1 ; then
21 hasher=shasum
22 local_install=true
23 else
24 echo "WARNING: sha256sum or shasum program not found" 1>&2
25 fi
26 fi
27
28 if [ -n "$local_install" ]; then
29 hash=$(echo "$sml" | cat "$program" - | $hasher | cut -c1-16)
30 gen_sml=$mydir/.vext-$hash.sml
31 gen_out=$mydir/.vext-$hash.bin
32 trap 'rm -f $gen_sml' 0
33 else
34 gen_sml=$(mktemp /tmp/vext-XXXXXXXX.sml)
35 gen_out=$(mktemp /tmp/vext-XXXXXXXX.bin)
36 trap 'rm -f $gen_sml $gen_out' 0
37 fi
38
39 if [ -x "$gen_out" ]; then
40 exec "$gen_out" "$@"
41 fi
42
14 # We need one of Poly/ML, SML/NJ, or MLton. Since we're running a 43 # We need one of Poly/ML, SML/NJ, or MLton. Since we're running a
15 # single-file SML program as if it were a script, our order of 44 # single-file SML program as if it were a script, our order of
16 # preference is based on startup speed. 45 # preference is based on startup speed, except in the local_install
46 # case where we retain a persistent binary.
17 47
18 if [ -z "$sml" ]; then 48 if [ -z "$sml" ]; then
19 if sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then 49 if [ -n "$local_install" ] && mlton 2>&1 | grep -q 'MLton'; then
50 sml="mlton"
51 elif sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then
20 sml="smlnj" 52 sml="smlnj"
21 # We would prefer Poly/ML to SML/NJ, except that Poly v5.7 has a 53 # We would prefer Poly/ML to SML/NJ, except that Poly v5.7 has a
22 # nasty bug that occasionally causes it to deadlock on startup. 54 # nasty bug that occasionally causes it to deadlock on startup.
23 # That appears to be fixed in their repo, so we could promote it 55 # That appears to be fixed in their repo, so we could promote it
24 # up the order again at some point in future 56 # up the order again at some point in future
28 sml="mlton" 60 sml="mlton"
29 else cat 1>&2 <<EOF 61 else cat 1>&2 <<EOF
30 62
31 ERROR: No supported SML compiler or interpreter found 63 ERROR: No supported SML compiler or interpreter found
32 EOF 64 EOF
33 cat <<EOF 65 cat 1>&2 <<EOF
34 66
35 The Vext external source code manager needs a Standard ML (SML) 67 The Vext external source code manager needs a Standard ML (SML)
36 compiler or interpreter to run. 68 compiler or interpreter to run.
37 69
38 Please ensure you have one of the following SML implementations 70 Please ensure you have one of the following SML implementations
53 EOF 85 EOF
54 exit 2 86 exit 2
55 fi 87 fi
56 fi 88 fi
57 89
58 tmp_sml=$(mktemp /tmp/vext-XXXXXXXX.sml)
59 tmp_out=$(mktemp /tmp/vext-XXXXXXXX.bin)
60
61 trap 'rm -f $tmp_sml $tmp_out' 0
62
63 arglist="" 90 arglist=""
64 for arg in "$@"; do 91 for arg in "$@"; do
65 if [ -n "$arglist" ]; then arglist="$arglist,"; fi 92 if [ -n "$arglist" ]; then arglist="$arglist,"; fi
66 if echo "$arg" | grep -q '[^a-z]' ; then 93 if echo "$arg" | grep -q '["'"'"']' ; then
67 arglist="$arglist\"usage\"" 94 arglist="$arglist\"usage\""
68 else 95 else
69 arglist="$arglist\"$arg\"" 96 arglist="$arglist\"$arg\""
70 fi 97 fi
71 done 98 done
72 99
73 case "$sml" in 100 case "$sml" in
74 poly) echo 'use "'"$program"'"; vext ['"$arglist"'];' | 101 poly)
75 poly -q --error-exit ;; 102 if [ -n "$local_install" ] && polyc --help >/dev/null 2>&1 ; then
103 if [ ! -x "$gen_out" ]; then
104 polyc -o "$gen_out" "$program"
105 fi
106 "$gen_out" "$@"
107 else
108 echo 'use "'"$program"'"; vext ['"$arglist"'];' |
109 poly -q --error-exit
110 fi ;;
76 mlton) 111 mlton)
77 cat "$program" > "$tmp_sml" 112 if [ ! -x "$gen_out" ]; then
78 echo 'val _ = main ()' >> "$tmp_sml" 113 echo "[Precompiling Vext binary...]" 1>&2
79 mlton -output "$tmp_out" "$tmp_sml" 114 echo "val _ = main ()" | cat "$program" - > "$gen_sml"
80 "$tmp_out" "$@" ;; 115 mlton -output "$gen_out" "$gen_sml"
116 fi
117 "$gen_out" "$@" ;;
81 smlnj) 118 smlnj)
82 cat "$program" | ( 119 cat "$program" | (
83 cat <<EOF 120 cat <<EOF
84 val smlrun__cp = 121 val smlrun__cp =
85 let val x = !Control.Print.out in 122 let val x = !Control.Print.out in
100 cat - 137 cat -
101 cat <<EOF 138 cat <<EOF
102 val _ = vext [$arglist]; 139 val _ = vext [$arglist];
103 val _ = OS.Process.exit (OS.Process.success); 140 val _ = OS.Process.exit (OS.Process.success);
104 EOF 141 EOF
105 ) > "$tmp_sml" 142 ) > "$gen_sml"
106 CM_VERBOSE=false sml "$tmp_sml" ;; 143 CM_VERBOSE=false sml "$gen_sml" ;;
107 *) 144 *)
108 echo "Unknown SML implementation name: $sml"; 145 echo "ERROR: Unknown SML implementation name: $sml" 1>&2;
109 exit 2 ;; 146 exit 2 ;;
110 esac 147 esac
111 148