<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>CrossCompiling</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>CrossCompiling</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>MLton&#8217;s <span class="monospaced">-target</span> flag directs MLton to cross compile an application
for another platform.  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.</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Install the GCC cross-compiler tools on the host so that GCC can
compile to the target.
</p>
</li>
<li>
<p>
Cross compile the MLton runtime system to build the runtime
libraries for the target.
</p>
</li>
</ol></div>
<div class="paragraph"><p>To make the terminology clear, we refer to the <em>host</em> as the machine
MLton is running on and the <em>target</em> as the machine that MLton is
compiling for.</p></div>
<div class="paragraph"><p>To build a GCC cross-compiler toolset on the host, you can use the
script <span class="monospaced">bin/build-cross-gcc</span>, available in the MLton sources, as a
template.  The value of the <span class="monospaced">target</span> variable in that script is
important, since that is what you will pass to MLton&#8217;s <span class="monospaced">-target</span> 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.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% gcc -b i386-pc-cygwin -o hello-world hello-world.c</pre>
</div></div>
<div class="paragraph"><p>You should now be able to run <span class="monospaced">hello-world</span> on the target machine, in
this case, a Cygwin machine.</p></div>
<div class="paragraph"><p>Next, you must cross compile the MLton runtime system and inform MLton
of the availability of the new target.  The script <span class="monospaced">bin/add-cross</span>
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.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% add-cross sparc-sun-solaris sun blade
Making runtime.
Building print-constants executable.
Running print-constants on blade.</pre>
</div></div>
<div class="paragraph"><p>Running <span class="monospaced">add-cross</span> uses <span class="monospaced">ssh</span> to compile the runtime on the target
machine and to create <span class="monospaced">print-constants</span>, which prints out all of the
constants that MLton needs in order to implement the
<a href="BasisLibrary">Basis Library</a>.  The script runs <span class="monospaced">print-constants</span> on
the target machine (<span class="monospaced">blade</span> in this case), and saves the output.</p></div>
<div class="paragraph"><p>Once you have done all this, you should be able to cross compile SML
applications.  For example,</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>mlton -target i386-pc-cygwin hello-world.sml</pre>
</div></div>
<div class="paragraph"><p>will create <span class="monospaced">hello-world</span>, which you should be able to run from a
Cygwin shell on your Windows machine.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_cross_compiling_alternatives">Cross-compiling alternatives</h2>
<div class="sectionbody">
<div class="paragraph"><p>Building and maintaining cross-compiling <span class="monospaced">gcc</span>'s is complex.  You may
find it simpler to use <span class="monospaced">mlton -keep g</span> to generate the files on the
host, then copy the files to the target, and then use <span class="monospaced">gcc</span> or <span class="monospaced">mlton</span>
on the target to compile the files.</p></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
