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