<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>CrossCompiling - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton 20051202</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      CrossCompiling
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "Index">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
MLton's <tt>-target</tt> 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. 
    <ol type="1">

    <li>
<p>
Install the GCC cross-compiler tools on the host so that GCC can  compile to the target. 
</p>
</li>
    <li class="gap">
<p>
Cross compile the MLton runtime system to build the runtime  libraries for the target. 
</p>
</li>

    </ol>


<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>
<p>
To build a GCC cross-compiler toolset on the host, you can use the script <tt>bin/build-cross-gcc</tt>, available in the MLton sources, as a template.  The value of the <tt>target</tt> variable in that script is important, since that is what you will pass to MLton's <tt>-target</tt> 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. 
<pre>% gcc -b i386-pc-cygwin -o hello-world hello-world.c
</pre>You should now be able to run <tt>hello-world</tt> on the target machine, in this case, a Cygwin machine. 
</p>
<p>
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</tt> 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. 
<pre>% add-cross sparc-sun-solaris sun blade
Making runtime.
Building print-constants executable.
Running print-constants on blade.
</pre>Running <tt>add-cross</tt> uses <tt>ssh</tt> to compile the runtime on the target machine and to create <tt>print-constants</tt>, which prints out all of the constants that MLton needs in order to implement the <a href="BasisLibrary">Basis Library</a>.  The script runs <tt>print-constants</tt> on the target machine (<tt>blade</tt> in this case), and saves the output. 
</p>
<p>
Once you have done all this, you should be able to cross compile SML applications.  For example, 
<pre>mlton -target i386-pc-cygwin hello-world.sml
</pre>will create <tt>hello-world</tt>, which you should be able to run from a Cygwin shell on your Windows machine. 
</p>
<h2 id="head-80fca97412735573fcad1a4be39280262e6f73d5">Cross-compiling alternatives</h2>
<p>
Building and maintaining cross-compiling <tt>gcc</tt>'s is complex.  You may find it simpler to use <tt>mlton&nbsp;-keep&nbsp;g</tt> to generate the files on the host, then copy the files to the target, and then use <tt>gcc</tt> or <tt>mlton</tt> on the target to compile the files. 
</p>
</div>



<p>
<hr>
Last edited on 2005-12-02 04:19:16 by <span title="ppp-71-139-183-221.dsl.snfc21.pacbell.net"><a href="StephenWeeks">StephenWeeks</a></span>.
</body></html>
