<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 9.0.0rc1">
<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 20210117</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 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.</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Build the MLton runtime system for the target using a remote
machine.
</p>
</li>
<li>
<p>
Install or build the appropriate cross-compiler toolchain on the
host so that the C compiler(s) can compile to 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 the MLton runtime system for the target, use the
<span class="monospaced">remote-add-cross-target</span> goal of the root <span class="monospaced">Makefile</span>; see comments in
the root <span class="monospaced">Makefile</span>.  The <span class="monospaced">remote-add-cross-target</span> goal uses <span class="monospaced">ssh</span> 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&#8217;t currently
feasible to build the runtime system on the host machine using a
cross-compiler toolchain.</p></div>
<div class="paragraph"><p>Here is an example adding a <span class="monospaced">sparc-sun-solaris</span> cross-compile target
using the remote machine <span class="monospaced">blade</span>:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% make CROSS_TARGET=sparc-sun-solaris REMOTE_MACHINE=blade remote-add-cross-target</pre>
</div></div>
<div class="paragraph"><p>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,</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>mlton -target sparc-sun-solaris 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 on a Sparc
Solaris 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 a cross-compiler toolchain 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>
