Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / localhost / CrossCompiling
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="generator" content="AsciiDoc 8.6.9">
6 <title>CrossCompiling</title>
7 <link rel="stylesheet" href="./asciidoc.css" type="text/css">
8 <link rel="stylesheet" href="./pygments.css" type="text/css">
9
10
11 <script type="text/javascript" src="./asciidoc.js"></script>
12 <script type="text/javascript">
13 /*<![CDATA[*/
14 asciidoc.install();
15 /*]]>*/
16 </script>
17 <link rel="stylesheet" href="./mlton.css" type="text/css">
18 </head>
19 <body class="article">
20 <div id="banner">
21 <div id="banner-home">
22 <a href="./Home">MLton 20180207</a>
23 </div>
24 </div>
25 <div id="header">
26 <h1>CrossCompiling</h1>
27 </div>
28 <div id="content">
29 <div id="preamble">
30 <div class="sectionbody">
31 <div class="paragraph"><p>MLton&#8217;s <span class="monospaced">-target</span> flag directs MLton to cross compile an application
32 for another platform. By default, MLton is only able to compile for
33 the machine it is running on. In order to use MLton as a cross
34 compiler, you need to do two things.</p></div>
35 <div class="olist arabic"><ol class="arabic">
36 <li>
37 <p>
38 Install the GCC cross-compiler tools on the host so that GCC can
39 compile to the target.
40 </p>
41 </li>
42 <li>
43 <p>
44 Cross compile the MLton runtime system to build the runtime
45 libraries for the target.
46 </p>
47 </li>
48 </ol></div>
49 <div class="paragraph"><p>To make the terminology clear, we refer to the <em>host</em> as the machine
50 MLton is running on and the <em>target</em> as the machine that MLton is
51 compiling for.</p></div>
52 <div class="paragraph"><p>To build a GCC cross-compiler toolset on the host, you can use the
53 script <span class="monospaced">bin/build-cross-gcc</span>, available in the MLton sources, as a
54 template. The value of the <span class="monospaced">target</span> variable in that script is
55 important, since that is what you will pass to MLton&#8217;s <span class="monospaced">-target</span> flag.
56 Once you have the toolset built, you should be able to test it by
57 cross compiling a simple hello world program on your host machine.</p></div>
58 <div class="listingblock">
59 <div class="content monospaced">
60 <pre>% gcc -b i386-pc-cygwin -o hello-world hello-world.c</pre>
61 </div></div>
62 <div class="paragraph"><p>You should now be able to run <span class="monospaced">hello-world</span> on the target machine, in
63 this case, a Cygwin machine.</p></div>
64 <div class="paragraph"><p>Next, you must cross compile the MLton runtime system and inform MLton
65 of the availability of the new target. The script <span class="monospaced">bin/add-cross</span>
66 from the MLton sources will help you do this. Please read the
67 comments at the top of the script. Here is a sample run adding a
68 Solaris cross compiler.</p></div>
69 <div class="listingblock">
70 <div class="content monospaced">
71 <pre>% add-cross sparc-sun-solaris sun blade
72 Making runtime.
73 Building print-constants executable.
74 Running print-constants on blade.</pre>
75 </div></div>
76 <div class="paragraph"><p>Running <span class="monospaced">add-cross</span> uses <span class="monospaced">ssh</span> to compile the runtime on the target
77 machine and to create <span class="monospaced">print-constants</span>, which prints out all of the
78 constants that MLton needs in order to implement the
79 <a href="BasisLibrary">Basis Library</a>. The script runs <span class="monospaced">print-constants</span> on
80 the target machine (<span class="monospaced">blade</span> in this case), and saves the output.</p></div>
81 <div class="paragraph"><p>Once you have done all this, you should be able to cross compile SML
82 applications. For example,</p></div>
83 <div class="listingblock">
84 <div class="content monospaced">
85 <pre>mlton -target i386-pc-cygwin hello-world.sml</pre>
86 </div></div>
87 <div class="paragraph"><p>will create <span class="monospaced">hello-world</span>, which you should be able to run from a
88 Cygwin shell on your Windows machine.</p></div>
89 </div>
90 </div>
91 <div class="sect1">
92 <h2 id="_cross_compiling_alternatives">Cross-compiling alternatives</h2>
93 <div class="sectionbody">
94 <div class="paragraph"><p>Building and maintaining cross-compiling <span class="monospaced">gcc</span>'s is complex. You may
95 find it simpler to use <span class="monospaced">mlton -keep g</span> to generate the files on the
96 host, then copy the files to the target, and then use <span class="monospaced">gcc</span> or <span class="monospaced">mlton</span>
97 on the target to compile the files.</p></div>
98 </div>
99 </div>
100 </div>
101 <div id="footnotes"><hr></div>
102 <div id="footer">
103 <div id="footer-text">
104 </div>
105 <div id="footer-badges">
106 </div>
107 </div>
108 </body>
109 </html>