| 1 | #!/bin/sh |
| 2 | |
| 3 | # This script calls MLton. |
| 4 | |
| 5 | |
| 6 | LIB_REL_BIN="../lib/mlton" |
| 7 | |
| 8 | set -e |
| 9 | |
| 10 | dir=$(dirname "$0") |
| 11 | lib=$(cd "$dir/$LIB_REL_BIN" && pwd) |
| 12 | |
| 13 | # Find if "-target" is set, to read cross-specific variables. |
| 14 | # Default to "self". |
| 15 | TARGET=self |
| 16 | prev_arg= |
| 17 | for arg in "$@" |
| 18 | do |
| 19 | if [ "$prev_arg" = "-target" ]; then |
| 20 | TARGET="$arg" |
| 21 | fi |
| 22 | prev_arg="$arg" |
| 23 | done |
| 24 | |
| 25 | TARGET_VARS="$lib/targets/$TARGET/vars" |
| 26 | if [ ! -f "$TARGET_VARS" ]; then |
| 27 | echo "Unable to read $TARGET_VARS." >&2 |
| 28 | exit 1 |
| 29 | fi |
| 30 | . "$TARGET_VARS" |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | doitMLton () { |
| 36 | mlton_mlton="$lib/mlton-compile$EXE" |
| 37 | if [ -x "$mlton_mlton" ]; then |
| 38 | exec "$mlton_mlton" "$@" |
| 39 | fi |
| 40 | } |
| 41 | doitSMLNJ () { |
| 42 | SMLNJ="sml" |
| 43 | if [ -x "$(command -v "$SMLNJ")" ]; then |
| 44 | mlton_smlnj_heap="$lib/mlton-compile-smlnj.$("$SMLNJ" @SMLsuffix)" |
| 45 | if [ -s "$mlton_smlnj_heap" ]; then |
| 46 | exec "$SMLNJ" @SMLload="$mlton_smlnj_heap" "$@" |
| 47 | fi |
| 48 | fi |
| 49 | } |
| 50 | doitPolyML () { |
| 51 | mlton_polyml="$lib/mlton-compile-polyml$EXE" |
| 52 | if [ -x "$mlton_polyml" ]; then |
| 53 | exec "$mlton_polyml" "$@" |
| 54 | fi |
| 55 | } |
| 56 | doitMLKit () { |
| 57 | mlton_mlkit="$lib/mlton-compile-mlkit$EXE" |
| 58 | if [ -x "$mlton_mlkit" ]; then |
| 59 | exec "$mlton_mlkit" "$@" |
| 60 | fi |
| 61 | } |
| 62 | |
| 63 | |
| 64 | if [ -n "$GMP_INC_DIR" ]; then |
| 65 | gmpCCOpts="-cc-opt -I$GMP_INC_DIR" |
| 66 | fi |
| 67 | if [ -n "$GMP_LIB_DIR" ]; then |
| 68 | gmpLinkOpts="-link-opt -L$GMP_LIB_DIR -target-link-opt netbsd -Wl,-R$GMP_LIB_DIR" |
| 69 | fi |
| 70 | |
| 71 | # "Legacy" Pass Manager; LLVM <= 14.0 |
| 72 | llvmOptOpt="-mem2reg -O2" |
| 73 | # "New" Pass Manager; LLVM >= 13.0 |
| 74 | llvmOptOpt="--passes=function(mem2reg),default<O2>" |
| 75 | |
| 76 | |
| 77 | # insert @MLton runtime arguments |
| 78 | case "$1" in |
| 79 | @MLton) |
| 80 | shift |
| 81 | ;; |
| 82 | *) |
| 83 | set -- -- "$@" |
| 84 | ;; |
| 85 | esac |
| 86 | set -- @MLton \ |
| 87 | ram-slop 0.5 \ |
| 88 | "$@" |
| 89 | |
| 90 | # insert compile arguments |
| 91 | looking='yes' |
| 92 | for arg in "$@"; do |
| 93 | set -- "$@" "$arg" |
| 94 | if [ "$arg" = '--' ] && [ "$looking" = 'yes' ]; then |
| 95 | looking='no' |
| 96 | set -- "$@" "$lib" \ |
| 97 | -ar-script "$lib/static-library" \ |
| 98 | -cc "$CC" \ |
| 99 | -cc-opt '-std=gnu11 -fno-common' \ |
| 100 | -cc-opt '-O1 -fno-strict-aliasing' \ |
| 101 | -cc-opt '-foptimize-sibling-calls' \ |
| 102 | -cc-opt '-w' \ |
| 103 | -cc-opt-quote "-I$lib/include" \ |
| 104 | $gmpCCOpts $gmpLinkOpts \ |
| 105 | -llvm-llc-opt '-O2' \ |
| 106 | -llvm-opt-opt "$llvmOptOpt" \ |
| 107 | -mlb-path-var 'SML_LIB $(LIB_MLTON_DIR)/sml' \ |
| 108 | -target-as-opt amd64 '-m64' \ |
| 109 | -target-as-opt x86 '-m32' \ |
| 110 | -target-cc-opt aix '-maix64' \ |
| 111 | -target-cc-opt alpha \ |
| 112 | '-mieee -mbwx -mtune=ev6 -mfp-rounding-mode=d' \ |
| 113 | -target-cc-opt amd64 '-m64' \ |
| 114 | -target-cc-opt amd64-darwin '-arch x86_64' \ |
| 115 | -target-cc-opt arm64-darwin '-arch arm64' \ |
| 116 | -target-cc-opt powerpc-darwin '-arch ppc' \ |
| 117 | -target-cc-opt powerpc64-darwin '-arch ppc64' \ |
| 118 | -target-cc-opt ia64-hpux "-mlp64" \ |
| 119 | -target-cc-opt ia64 "-mtune=itanium2" \ |
| 120 | -target-cc-opt sparc '-m32 -mcpu=v8 -Wa,-xarch=v8plusa' \ |
| 121 | -target-cc-opt wasi '-D_WASI_EMULATED_SIGNAL' \ |
| 122 | -target-cc-opt wasi '-D_WASI_EMULATED_PROCESS_CLOCKS' \ |
| 123 | -target-cc-opt wasi '-D_WASI_EMULATED_GETPID' \ |
| 124 | -target-cc-opt x86 '-m32' \ |
| 125 | -target-link-opt aix '-maix64' \ |
| 126 | -target-link-opt alpha \ |
| 127 | '-mieee -mbwx -mtune=ev6 -mfp-rounding-mode=d' \ |
| 128 | -target-link-opt amd64 '-m64' \ |
| 129 | -target-link-opt amd64-darwin '-arch x86_64' \ |
| 130 | -target-link-opt arm64-darwin '-arch arm64' \ |
| 131 | -target-link-opt powerpc-darwin '-arch ppc' \ |
| 132 | -target-link-opt powerpc64-darwin '-arch ppc64' \ |
| 133 | -target-link-opt ia64-hpux "-mlp64" \ |
| 134 | -target-link-opt linux '-Wl,-znoexecstack' \ |
| 135 | -target-link-opt mingw \ |
| 136 | '-lws2_32 -lkernel32 -lpsapi -lnetapi32 -lwinmm' \ |
| 137 | -target-link-opt mingw '-Wl,--enable-stdcall-fixup' \ |
| 138 | -target-link-opt openbsd '-Wl,--no-execute-only' \ |
| 139 | -target-link-opt solaris '-lnsl -lsocket -lrt' \ |
| 140 | -target-link-opt wasi '-lwasi-emulated-signal' \ |
| 141 | -target-link-opt wasi '-lwasi-emulated-process-clocks' \ |
| 142 | -target-link-opt wasi '-lwasi-emulated-getpid' \ |
| 143 | -target-link-opt x86 '-m32' \ |
| 144 | -profile-exclude '\$\(SML_LIB\)' |
| 145 | fi |
| 146 | shift |
| 147 | done |
| 148 | if [ "$looking" = 'yes' ]; then |
| 149 | echo '@MLton missing --' |
| 150 | exit 1 |
| 151 | fi |
| 152 | |
| 153 | doitMLton "$@" |
| 154 | |
| 155 | # drop @MLton runtime arguments |
| 156 | while [ "$1" != "--" ]; do |
| 157 | shift |
| 158 | done |
| 159 | shift |
| 160 | |
| 161 | doitSMLNJ "$@" |
| 162 | doitPolyML "$@" |
| 163 | doitMLKit "$@" |
| 164 | |
| 165 | echo 'Unable to run MLton. Check that lib is set properly.' >&2 |
| 166 | exit 1 |