1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
\sec{Cross compiling}{cross-compiling}{Cross_compiling.html}
%
You can use the {\mlton}'s {\tt -target} flag to cross compile
applications. By default, {\mlton} is only able to compile for the
machine it is running on. In order to use {\mlton} as a cross
compiler, you need to do two things. To make the terminology clear,
we refer to the {\em host} as the machine {\mlton} is running on and
the {\em target} as the machine that {\mlton} is compiling for.
\begin{enumerate}
\item Install the GCC cross-compiler tools on the host so that GCC can
compile to the target.
\item Cross compile the {\mlton} runtime system to build the runtime
libraries for the target.
\end{enumerate}
To build a GCC cross-compiler toolset on the host, you can use the
script {\tt bin/build-cross-gcc}, available in the {\mlton} sources,
as a template. The value of the {\tt target} variable in that script
is important, since that is what you will pass to {\mlton}'s {\tt
-target} flag. Once you have the toolset built, you should be able to
test it by cross compiling a simple hello world program on your host
machine.
\begin{verbatim}
gcc -b i386-pc-cygwin -o hello-world hello-world.c
\end{verbatim}
You should now be able to run {\tt hello-world} on the target machine,
in this case, a Cygwin machine.
Next, you must cross compile the {\mlton} runtime system and inform
{\mlton} of the availability of the new target. The script {\tt
bin/add-cross} from the {\mlton} sources will help you do this.
Please read the comments at the top of the script. Here is a sample
run adding a Solaris cross compiler.
\begin{verbatim}
% add-cross sparc-sun-solaris sun blade
Making runtime.
Building print-constants executable.
Running print-constants on blade.
\end{verbatim}
Running {\tt add-cross} uses {\tt ssh} to compile the runtime on the
target machine and to create {\tt print-constants}, which prints out
all of the constants that {\mlton} needs in order to implement the
basis library. The script runs {\tt print-constants} on the target
machine ({\tt blade} in this case), and saves the output.
Once you have done all this, you should be able to cross compile SML
applications. For example,
\begin{verbatim}
mlton -target i386-pc-cygwin hello-world.sml
\end{verbatim}
will create {\tt hello-world}, which you should be able to run from a
Cygwin shell on your Windows machine.
%
\subsection{Cross compiling alternatives}
Building and maintaining cross compiling {\tt gcc}'s is complex. You
may find it simpler to use {\tt mlton -keep g} to generate the files
on the host, then copy the files to the target, and then use {\tt gcc}
or {\tt mlton} on the target to compile the files.
|