## MLton 20241230

# CrossCompiling

MLton’s `-target` flag directs MLton to cross compile a program for another platform. By default, MLton is only able to compile for the platform that it is running on. In order to use MLton as a cross compiler, you need to do two things.

1. Build the MLton runtime system for the target using a remote machine.

2. Install or build the appropriate cross-compiler toolchain on the host so that the C compiler(s) can compile to the target.

To make the terminology clear, we refer to the host as the machine MLton is running on and the target as the machine that MLton is compiling for.

To build the MLton runtime system for the target, use the `remote-add-cross` goal of the root `Makefile`; see comments in the root `Makefile`. The `remote-add-cross` goal uses `ssh` to build the runtime system on a remote machine. Building the runtime system requires compiling and executing programs that determine characteristics of the platform, which is why it isn’t currently feasible to build the runtime system on the host machine using a cross-compiler toolchain.

Here is an example adding a `sparc-sun-solaris` cross-compile target using the remote machine `blade`:

    % make CROSS_TARGET=sparc-sun-solaris REMOTE_MACHINE=blade remote-add-cross

Once you have both the MLton runtime system and a cross-compiler toolchain for the target, you should be able to cross compile SML programs. For example,

    mlton -target sparc-sun-solaris hello-world.sml

will create `hello-world`, which you should be able to run on a Sparc Solaris machine.

## Cross-compiling alternatives

Building and maintaining a cross-compiler toolchain is complex. You may find it simpler to use `mlton -keep g` to generate the files on the host, then copy the files to the target, and then use `gcc` or `mlton` on the target to compile the files.