1#! /bin/sh
2prefix="@prefix@"
3exec_prefix="@exec_prefix@"
4BINDIR="@bindir@"
5LINK="@CXX@"
6LIBDIR="@libdir@"
7LIBS="@dependentlibs@"
8CFLAGS="@polyc_CFLAGS@"
9
10DEFAULT_COMPILER="${BINDIR}/poly"
11COMPILER="${DEFAULT_COMPILER}"
12
13# Extra options for Windows.  config.status sets these conditionals to either "" or "#".
14
15@NATIVE_WINDOWS_FALSE@EXTRALDFLAGS=""
16@NATIVE_WINDOWS_TRUE@@ARCHX86_64_TRUE@EXTRALDFLAGS="-Wl,-u,WinMain"
17@NATIVE_WINDOWS_TRUE@@ARCHINTERPRET64_TRUE@EXTRALDFLAGS="-Wl,-u,WinMain"
18@NATIVE_WINDOWS_TRUE@@ARCHI386_TRUE@EXTRALDFLAGS="-Wl,-u,_WinMain@16 -Wl,--large-address-aware"
19@NATIVE_WINDOWS_TRUE@@ARCHINTERPRET_TRUE@EXTRALDFLAGS="-Wl,-u,_WinMain@16 -Wl,--large-address-aware"
20@NATIVE_WINDOWS_TRUE@@WINDOWSGUI_TRUE@EXTRALDFLAGS+=" -mwindows"
21@NATIVE_WINDOWS_TRUE@@WINDOWSGUI_FALSE@EXTRALDFLAGS+=" -mconsole"
22
23@NATIVE_WINDOWS_TRUE@SUFFIX="obj"
24@NATIVE_WINDOWS_FALSE@SUFFIX="o"
25
26# Msys passes the Windows TEMP in temp (lower case)
27# On other systems allow TMPDIR to override /tmp.
28@NATIVE_WINDOWS_TRUE@TEMPORARYDIR="${temp:-/tmp}"
29@NATIVE_WINDOWS_FALSE@TEMPORARYDIR="${TMPDIR:-/tmp}"
30
31# Extra options for Mac OS X
32@MACOSLDOPTS_TRUE@EXTRALDFLAGS="-Wl,-no_pie"
33
34TMPOBJFILE="${TEMPORARYDIR}/polyobj.$$.$SUFFIX"
35trap 'rm -f "$TMPOBJFILE"' 0
36
37compile()
38{
39    echo "val () = use (List.nth(CommandLine.arguments(), 2)); val () = PolyML.export(List.nth(CommandLine.arguments(), 3), main);" | "${COMPILER}" -q --error-exit  "$1" "$2"
40}
41
42link()
43{
44    if [ X"$2" = "X" ]
45    then
46        "${LINK}" ${EXTRALDFLAGS} ${CFLAGS} "$1" "-L${LIBDIR}" "-Wl,-rpath,${LIBDIR}" -lpolymain -lpolyml ${LIBS}
47    else
48        "${LINK}" ${EXTRALDFLAGS} ${CFLAGS} "$1" -o "$2" "-L${LIBDIR}" "-Wl,-rpath,${LIBDIR}" -lpolymain -lpolyml ${LIBS}
49    fi
50}
51
52printhelp()
53{
54    echo "Usage: polyc [OPTION]... [SOURCEFILE]"
55    echo Compile and link a Standard ML source file with Poly/ML.
56    echo
57    echo "   -b poly      Use 'poly' as compiler instead of ${DEFAULT_COMPILER}"
58    echo "   -c           Compile but do not link.  The object file is written to the source file with .$SUFFIX extension."
59    echo "   -o output    Write the executable file to 'output'"
60    echo "   --help       Write this text and exit"
61    exit
62}
63
64usage()
65{
66    echo "$1"
67    echo "Usage: polyc [OPTION]... [SOURCEFILE]"
68    exit 1
69}
70
71checkml()
72{
73    extension="${1##*.}"
74    case "$extension" in
75        sml|ML)
76             return 0 ;;
77        o|obj)
78             return 1;;
79        *)
80             test -r "$1" && file -b "$1" | grep -q text ;;
81    esac
82}
83
84sourcefile=""
85outputfile=""
86compileonly="no"
87
88while [ $# -gt 0 ]
89do
90    case "$1" in
91        --help)
92            printhelp ;;
93        -b)
94            shift
95            [ $# -eq 0 ] && usage "Expected file name after -b"
96            COMPILER="$1";;
97        -c) compileonly="yes";;
98        -o)
99            shift
100            [ $# -eq 0 ] && usage "Expected file name after -o"
101            outputfile="$1";;
102        *)
103            [ X"$sourcefile" = "X" ] || usage "Only one source file name allowed"
104            sourcefile="$1";;
105    esac
106    shift
107done
108
109[ X"$sourcefile" = "X" ] && usage "No input files"
110[ -r "$sourcefile" ] || usage "Error: $sourcefile: No such file"
111
112case "$compileonly" in
113     yes)
114	 if [ "x$outputfile" = "x" ]; then
115	     basename="${sourcefile##*/}"
116             outputfile="${basename%.*}.o"
117	 fi
118         compile "$sourcefile" "$outputfile"
119         ;;
120     no)
121         if checkml "$sourcefile"
122         then
123             compile "$sourcefile" "$TMPOBJFILE" && link "$TMPOBJFILE" "$outputfile"
124         else
125             link "$sourcefile" "$outputfile"
126         fi
127         ;;
128esac
129