Skip to content

Commit

Permalink
Eliminate runtime bash dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ii8 committed Nov 8, 2023
1 parent 38eb9de commit 8fc267e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 76 deletions.
1 change: 0 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ $ make PREFIX=/opt/mlton install
* http://gcc.gnu.org/[GCC] or http://clang.llvm.org[Clang] (The C compiler must support `-std=gnu11`.)
* http://gmplib.org[GMP] (GNU Multiple Precision arithmetic library)
* http://savannah.gnu.org/projects/make[GNU Make]
* http://www.gnu.org/software/bash/[GNU Bash]
* miscellaneous Unix utilities (`bzip2`, `gzip`, `sed`, `tar`, ...)

=== Binary Package
Expand Down
163 changes: 90 additions & 73 deletions bin/mlton-script
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh

# This script calls MLton.

Expand All @@ -17,30 +17,14 @@ GMP_LIB_DIR=

set -e

dir=`dirname "$0"`
lib=`cd "$dir/$LIB_REL_BIN" && pwd`
dir=$(dirname "$0")
lib=$(cd "$dir/$LIB_REL_BIN" && pwd)

declare -a rargs
case "$1" in
@MLton)
shift
while [ "$#" -gt 0 -a "$1" != "--" ]; do
rargs[${#rargs[@]}]="$1"
shift
done
if [ "$#" -gt 0 -a "$1" == "--" ]; then
shift
else
echo '@MLton missing --'
exit 1
fi
;;
esac

doitMLton () {
mlton_mlton="$lib/mlton-compile$EXE"
if [ -x "$mlton_mlton" ]; then
exec "$mlton_mlton" @MLton ram-slop 0.5 "${rargs[@]}" -- "$@"
exec "$mlton_mlton" "$@"
fi
}
doitSMLNJ () {
Expand All @@ -65,67 +49,100 @@ doitMLKit () {
fi
}

doit () {
doitMLton "$@"
doitSMLNJ "$@"
doitPolyML "$@"
doitMLKit "$@"
echo 'Unable to run MLton. Check that lib is set properly.' >&2
exit 1
}

if [ -n "$GMP_INC_DIR" ]; then
gmpCCOpts="-cc-opt -I$GMP_INC_DIR"
gmpCCOpts="-cc-opt -I$GMP_INC_DIR"
fi
if [ -n "$GMP_LIB_DIR" ]; then
gmpLinkOpts="-link-opt -L$GMP_LIB_DIR -target-link-opt netbsd -Wl,-R$GMP_LIB_DIR"
gmpLinkOpts="-link-opt -L$GMP_LIB_DIR -target-link-opt netbsd -Wl,-R$GMP_LIB_DIR"
fi

# "Legacy" Pass Manager; LLVM < 13.0
llvmOptOpt="-mem2reg -O2"
# "New" Pass Manager; LLVM >= 13.0
llvmOptOpt="--passes=function(mem2reg),default<O2>"

doit "$lib" \
-ar-script "$lib/static-library" \
-cc "$CC" \
-cc-opt '-std=gnu11 -fno-common' \
-cc-opt '-O1 -fno-strict-aliasing' \
-cc-opt '-foptimize-sibling-calls' \
-cc-opt '-w' \
-cc-opt-quote "-I$lib/include" \
$gmpCCOpts $gmpLinkOpts \
-llvm-llc-opt '-O2' \
-llvm-opt-opt "$llvmOptOpt" \
-mlb-path-var 'SML_LIB $(LIB_MLTON_DIR)/sml' \
-target-as-opt amd64 '-m64' \
-target-as-opt x86 '-m32' \
-target-cc-opt aix '-maix64' \
-target-cc-opt alpha \
'-mieee -mbwx -mtune=ev6 -mfp-rounding-mode=d' \
-target-cc-opt amd64 '-m64' \
-target-cc-opt amd64-darwin '-arch x86_64' \
-target-cc-opt arm64-darwin '-arch arm64' \
-target-cc-opt powerpc-darwin '-arch ppc' \
-target-cc-opt powerpc64-darwin '-arch ppc64' \
-target-cc-opt ia64-hpux "-mlp64" \
-target-cc-opt ia64 "-mtune=itanium2" \
-target-cc-opt sparc '-m32 -mcpu=v8 -Wa,-xarch=v8plusa' \
-target-cc-opt x86 '-m32' \
-target-link-opt aix '-maix64' \
-target-link-opt alpha \
'-mieee -mbwx -mtune=ev6 -mfp-rounding-mode=d' \
-target-link-opt amd64 '-m64' \
-target-link-opt amd64-darwin '-arch x86_64' \
-target-link-opt arm64-darwin '-arch arm64' \
-target-link-opt powerpc-darwin '-arch ppc' \
-target-link-opt powerpc64-darwin '-arch ppc64' \
-target-link-opt ia64-hpux "-mlp64" \
-target-link-opt linux '-Wl,-znoexecstack' \
-target-link-opt mingw \
'-lws2_32 -lkernel32 -lpsapi -lnetapi32 -lwinmm' \
-target-link-opt mingw '-Wl,--enable-stdcall-fixup' \
-target-link-opt solaris '-lnsl -lsocket -lrt' \
-target-link-opt x86 '-m32' \
-profile-exclude '\$\(SML_LIB\)' \
"$@"

# insert @MLton runtime arguments
case "$1" in
@MLton)
shift
;;
*)
set -- -- "$@"
;;
esac
set -- @MLton \
ram-slop 0.5 \
"$@"

# insert compile arguments
looking='yes'
for arg in "$@"; do
set -- "$@" "$arg"
if [ "$arg" = '--' ] && [ "$looking" = 'yes' ]; then
looking='no'
set -- "$@" "$lib" \
-ar-script "$lib/static-library" \
-cc "$CC" \
-cc-opt '-std=gnu11 -fno-common' \
-cc-opt '-O1 -fno-strict-aliasing' \
-cc-opt '-foptimize-sibling-calls' \
-cc-opt '-w' \
-cc-opt-quote "-I$lib/include" \
$gmpCCOpts $gmpLinkOpts \
-llvm-llc-opt '-O2' \
-llvm-opt-opt "$llvmOptOpt" \
-mlb-path-var 'SML_LIB $(LIB_MLTON_DIR)/sml' \
-target-as-opt amd64 '-m64' \
-target-as-opt x86 '-m32' \
-target-cc-opt aix '-maix64' \
-target-cc-opt alpha \
'-mieee -mbwx -mtune=ev6 -mfp-rounding-mode=d' \
-target-cc-opt amd64 '-m64' \
-target-cc-opt amd64-darwin '-arch x86_64' \
-target-cc-opt arm64-darwin '-arch arm64' \
-target-cc-opt powerpc-darwin '-arch ppc' \
-target-cc-opt powerpc64-darwin '-arch ppc64' \
-target-cc-opt ia64-hpux "-mlp64" \
-target-cc-opt ia64 "-mtune=itanium2" \
-target-cc-opt sparc '-m32 -mcpu=v8 -Wa,-xarch=v8plusa' \
-target-cc-opt x86 '-m32' \
-target-link-opt aix '-maix64' \
-target-link-opt alpha \
'-mieee -mbwx -mtune=ev6 -mfp-rounding-mode=d' \
-target-link-opt amd64 '-m64' \
-target-link-opt amd64-darwin '-arch x86_64' \
-target-link-opt arm64-darwin '-arch arm64' \
-target-link-opt powerpc-darwin '-arch ppc' \
-target-link-opt powerpc64-darwin '-arch ppc64' \
-target-link-opt ia64-hpux "-mlp64" \
-target-link-opt linux '-Wl,-znoexecstack' \
-target-link-opt mingw \
'-lws2_32 -lkernel32 -lpsapi -lnetapi32 -lwinmm' \
-target-link-opt mingw '-Wl,--enable-stdcall-fixup' \
-target-link-opt solaris '-lnsl -lsocket -lrt' \
-target-link-opt x86 '-m32' \
-profile-exclude '\$\(SML_LIB\)'
fi
shift
done
if [ "$looking" = 'yes' ]; then
echo '@MLton missing --'
exit 1
fi

doitMLton "$@"

# drop @MLton runtime arguments
while [ "$1" != "--" ]; do
shift
done
shift

doitSMLNJ "$@"
doitPolyML "$@"
doitMLKit "$@"

echo 'Unable to run MLton. Check that lib is set properly.' >&2
exit 1
4 changes: 2 additions & 2 deletions bin/static-library
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env bash
#!/bin/sh

# This script creates a static library (archive).
# It is invoked as: static-library TARGET OS OUTPUT objects* libraries*
Expand Down Expand Up @@ -39,7 +39,7 @@ if "$partialLink"; then
if [ "$os" = "darwin" ]; then
"${target}ld" -r -o "$output.o" "$@"
# The osx linker already makes hidden symbols local
elif [ "$os" = "mingw" -o "$os" = "cygwin" ]; then
elif [ "$os" = "mingw" ] || [ "$os" = "cygwin" ]; then
# Link allowing _address of stdcall function fixups
# Preserve the export list (.drectve section)
"${target}ld" -r --unique=.drectve --enable-stdcall-fixup -o "$output.o" "$@"
Expand Down

0 comments on commit 8fc267e

Please sign in to comment.