tiny fix in porting docs I just spotted
[ghc-hetmet.git] / docs / building / building.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3    "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4   <!ENTITY hacking SYSTEM "../../HACKING">
5 ]>
6
7 <article id="building-guide">
8
9 <articleinfo>
10
11 <title>Building and developing GHC</title>
12 <author><othername>The GHC Team</othername></author>
13 <address><email>glasgow-haskell-&lcub;users,bugs&rcub;@haskell.org</email></address>
14
15     <abstract>
16       <para>This Guide is primarily aimed at those who want to build and/or
17         hack on GHC.  It describes how to get started with building GHC on your
18         machine, and how to tweak the settings to get the kind of build you
19         want.  It also describes the inner workings of the build system, so you
20         can extend it, modify it, and use it to build your code.</para>
21
22       <para>The bulk of this guide applies to building on Unix
23       systems; see <xref linkend="winbuild"/> for Windows notes.</para>
24     </abstract>
25
26 </articleinfo>
27
28
29   <sect1 id="sec-getting">
30     <title>Getting the sources</title>
31     
32     <para>You can get your hands on the GHC sources in two ways:</para>
33
34     <variablelist>
35
36       <varlistentry>
37         <term><indexterm><primary>Source
38         distributions</primary></indexterm>Source distributions</term>
39         <listitem>
40           <para>You have a supported platform, but (a)&nbsp;you like
41           the warm fuzzy feeling of compiling things yourself;
42           (b)&nbsp;you want to build something ``extra&rdquo;&mdash;e.g., a
43           set of libraries with strictness-analysis turned off; or
44           (c)&nbsp;you want to hack on GHC yourself.</para>
45
46           <para>A source distribution contains complete sources for
47           GHC.  Not only that, but the more awkward
48           machine-independent steps are done for you.  For example, if
49           you don't have
50           <command>happy</command><indexterm><primary>happy</primary></indexterm>
51           you'll find it convenient that the source distribution
52           contains the result of running <command>happy</command> on
53           the parser specifications.  If you don't want to alter the
54           parser then this saves you having to find and install
55           <command>happy</command>. You will still need a working
56           version of GHC (version 5.x or later) on your machine in
57           order to compile (most of) the sources, however.</para>
58         </listitem>
59       </varlistentry>
60
61       <varlistentry>
62         <term>The darcs repository.<indexterm><primary>darcs repository</primary></indexterm></term>
63         <listitem>
64           <para>We make releases infrequently.  If you want more
65           up-to-the minute (but less tested) source code then you need
66           to get access to our darcs repository.</para>
67
68           <para>Information on accessing the darcs repository is on
69             the wiki: <ulink
70             url="http://hackage.haskell.org/trac/ghc/wiki/GhcDarcs"
71             />.</para>
72
73           <para>The repository holds source code only. It holds no
74           mechanically generated files at all.  So if you check out a
75           source tree from darcs you will need to install every utility
76           so that you can build all the derived files from
77           scratch.</para>
78         </listitem>
79       </varlistentry>
80     </variablelist>
81   </sect1>
82
83   <sect1 id="sec-build-checks">
84     <title>Things to check before you start</title>
85
86     <para>Here's a list of things to check before you get
87     started.</para>
88
89     <orderedlist>
90
91       <listitem><para><indexterm><primary>Disk space needed</primary></indexterm>Disk
92         space needed: from about 100Mb for a basic GHC
93         build, up to probably 500Mb for a GHC build with everything
94         included (libraries built several different ways,
95         etc.).</para>
96       </listitem>
97
98       <listitem>
99         <para>Use an appropriate machine / operating system.  <ulink
100         url="http://hackage.haskell.org/trac/ghc/wiki/Platforms">GHC
101         Platform Support</ulink> lists the currently supported
102         platforms; if yours isn't amongst these then you can try
103         porting GHC (see <xref linkend="sec-porting-ghc"/>).</para>
104         </listitem>
105
106       <listitem>
107         <para>Be sure that the &ldquo;pre-supposed&rdquo; utilities are
108         installed.  <xref linkend="sec-pre-supposed"/>
109         elaborates.</para>
110       </listitem>
111
112       <listitem>
113         <para>If you have any problem when building or installing the
114         Glasgow tools, please check the &ldquo;known pitfalls&rdquo; (<xref
115         linkend="sec-build-pitfalls"/>).  Also check the FAQ for the
116         version you're building, which is part of the User's Guide and
117         available on the <ulink url="http://www.haskell.org/ghc/" >GHC web
118         site</ulink>.</para>
119
120         <indexterm><primary>bugs</primary><secondary>known</secondary></indexterm>
121
122         <para>If you feel there is still some shortcoming in our
123         procedure or instructions, please report it.</para>
124
125         <para>For GHC, please see the <ulink
126         url="http://www.haskell.org/ghc/docs/latest/set/bug-reporting.html">bug-reporting
127         section of the GHC Users' Guide</ulink>, to maximise the
128         usefulness of your report.</para>
129
130         <indexterm><primary>bugs</primary><secondary>seporting</secondary></indexterm>
131         <para>If in doubt, please send a message to
132         <email>glasgow-haskell-bugs@haskell.org</email>.
133         <indexterm><primary>bugs</primary><secondary>mailing
134         list</secondary></indexterm></para>
135       </listitem>
136     </orderedlist>
137   </sect1>
138
139   <sect1 id="sec-pre-supposed">
140     <title>Installing pre-supposed utilities</title>
141
142     <indexterm><primary>pre-supposed utilities</primary></indexterm>
143     <indexterm><primary>utilities, pre-supposed</primary></indexterm>
144
145     <para>Here are the gory details about some utility programs you
146     may need; <command>perl</command>, <command>gcc</command> and
147     <command>happy</command> are the only important
148     ones. (PVM<indexterm><primary>PVM</primary></indexterm> is
149     important if you're going for Parallel Haskell.)  The
150     <command>configure</command><indexterm><primary>configure</primary></indexterm>
151     script will tell you if you are missing something.</para>
152
153     <variablelist>
154
155       <varlistentry>
156         <term>GHC
157           <indexterm><primary>pre-supposed: GHC</primary></indexterm>
158           <indexterm><primary>GHC, pre-supposed</primary></indexterm>
159         </term>
160         <listitem>
161           <para>GHC is required to build GHC, because GHC itself is
162           written in Haskell, and uses GHC extensions.  It is possible
163           to build GHC using just a C compiler, and indeed some
164           distributions of GHC do just that, but it isn't the best
165           supported method, and you may encounter difficulties.  Full
166           instructions are in <xref linkend="sec-porting-ghc"/>.</para>
167
168           <para>GHC can be built using either an earlier released
169           version of GHC (currently 5.04 and later are supported), or
170           bootstrapped using a GHC built from exactly the same
171           sources.  Note that this means you cannot in general build
172           GHC using an arbitrary development snapshot, or a build from
173           say last week.  It might work, it might not - we don't
174           guarantee anything.  To be on the safe side, start your
175           build using the most recently released stable version of
176           GHC.</para>
177         </listitem>
178       </varlistentry>
179
180       <varlistentry>
181         <term>Perl
182           <indexterm><primary>pre-supposed: Perl</primary></indexterm>
183           <indexterm><primary>Perl, pre-supposed</primary></indexterm>
184         </term>
185         <listitem>
186           <para><emphasis>You have to have Perl to proceed!</emphasis>
187           Perl version 5 at least is required.  GHC has been known to
188           tickle bugs in Perl, so if you find that Perl crashes when
189           running GHC try updating (or downgrading) your Perl
190           installation.  Versions of Perl before 5.6 have been known to have
191           various bugs tickled by GHC, so the configure script
192           will look for version 5.6 or later.</para>
193
194           <para>For Win32 platforms, you should use the binary
195           supplied in the InstallShield (copy it to
196           <filename>/bin</filename>).  The Cygwin-supplied Perl seems
197           not to work.</para>
198
199           <para>Perl should be put somewhere so that it can be invoked
200           by the <literal>&num;!</literal> script-invoking
201           mechanism. The full pathname may need to be less than 32
202           characters long on some systems.</para>
203         </listitem>
204       </varlistentry>
205
206       <varlistentry>
207         <term>GNU C (<command>gcc</command>)
208           <indexterm><primary>pre-supposed: GCC (GNU C compiler)</primary></indexterm>
209           <indexterm><primary>GCC (GNU C compiler), pre-supposed</primary></indexterm>
210         </term>
211         <listitem>
212           <para>Most GCC versions should work with the most recent GHC
213             sources.  Expect trouble if you use a recent GCC with
214             an older GHC, though (trouble in the form of mis-compiled code,
215             link errors, and errors from the <literal>ghc-asm</literal>
216             script).</para>
217
218           <para>If your GCC dies with &ldquo;internal error&rdquo; on
219           some GHC source file, please let us know, so we can report
220           it and get things improved.  (Exception: on x86
221           boxes&mdash;you may need to fiddle with GHC's
222           <option>-monly-N-regs</option> option; see the User's
223           Guide)</para>
224         </listitem>
225       </varlistentry>
226
227       <varlistentry>
228         <term>GNU Make
229           <indexterm><primary>make</primary><secondary>GNU</secondary></indexterm>
230         </term>
231         <listitem>
232           <para>The GHC build system makes heavy use of features
233           specific to GNU <command>make</command>, so you must have
234           this installed in order to build GHC.</para>
235
236           <para>NB. it has been reported that version 3.79 no longer
237           works to build GHC, and 3.80 is required.</para>
238         </listitem>
239       </varlistentry>
240
241       <varlistentry>
242         <term><ulink url="http://www.haskell.org/happy">Happy</ulink>
243           <indexterm><primary>Happy</primary></indexterm>
244         </term>
245         <listitem>
246           <para>Happy is a parser generator tool for Haskell, and is
247           used to generate GHC's parsers.</para>
248
249           <para>If you start from a source tarball of GHC (i.e. not a darcs
250             checkout), then you don't need Happy, because we supply the
251             pre-processed versions of the Happy parsers.  If you intend to
252             modify the compiler and/or you're using a darcs checkout, then you
253             need Happy.</para>
254
255           <para>Happy version 1.15 is currently required to build GHC.
256           Grab a copy from <ulink
257           url="http://www.haskell.org/happy/">Happy's Web
258           Page</ulink>.</para>
259         </listitem>
260       </varlistentry>
261
262       <varlistentry>
263         <term>Alex
264           <indexterm><primary>Alex</primary></indexterm>
265         </term>
266         <listitem>
267           <para>Alex is a lexical-analyser generator for Haskell,
268           which GHC uses to generate its lexer.</para>
269
270           <para>Like Happy, you don't need Alex if you're building GHC from a
271             source tarball, but you do need it if you're modifying GHC and/or
272             building a darcs checkout.</para>
273
274           <para>Alex is
275           written in Haskell and is a project in the darcs repository.
276           Alex distributions are available from <ulink url="http://www.haskell.org/alex/">Alex's Web
277           Page</ulink>.</para>
278         </listitem>
279       </varlistentry>
280
281       <varlistentry>
282         <term>autoconf
283           <indexterm><primary>pre-supposed: autoconf</primary></indexterm>
284           <indexterm><primary>autoconf, pre-supposed</primary></indexterm>
285         </term>
286         <listitem>
287           <para>GNU autoconf is needed if you intend to build from the
288           darcs sources, it is <emphasis>not</emphasis> needed if you
289           just intend to build a standard source distribution.</para>
290
291           <para>Version 2.52 or later of the autoconf package is required.
292           NB. version 2.13 will no longer work, as of GHC version
293           6.1.</para>
294
295           <para><command>autoreconf</command> (from the autoconf package)
296           recursively builds <command>configure</command> scripts from
297           the corresponding <filename>configure.ac</filename> and
298           <filename>aclocal.m4</filename> files.  If you modify one of
299           the latter files, you'll need <command>autoreconf</command> to
300           rebuild the corresponding <filename>configure</filename>.</para>
301         </listitem>
302       </varlistentry>
303
304       <varlistentry>
305         <term><command>sed</command>
306           <indexterm><primary>pre-supposed: sed</primary></indexterm>
307           <indexterm><primary>sed, pre-supposed</primary></indexterm>
308         </term>
309         <listitem>
310           <para>You need a working <command>sed</command> if you are
311           going to build from sources.  The build-configuration stuff
312           needs it.  GNU sed version 2.0.4 is no good!  It has a bug
313           in it that is tickled by the build-configuration.  2.0.5 is
314           OK. Others are probably OK too (assuming we don't create too
315           elaborate configure scripts.)</para>
316         </listitem>
317       </varlistentry>
318     </variablelist>
319
320     <sect2 id="pre-supposed-gph-tools">
321       <title>Tools for building parallel GHC (GPH)</title>
322
323       <variablelist>
324         <varlistentry>
325           <term>PVM version 3:
326           <indexterm><primary>pre-supposed: PVM3 (Parallel Virtual Machine)</primary></indexterm>
327             <indexterm><primary>PVM3 (Parallel Virtual Machine), pre-supposed</primary></indexterm>
328           </term>
329           <listitem>
330             <para>PVM is the Parallel Virtual Machine on which
331             Parallel Haskell programs run.  (You only need this if you
332             plan to run Parallel Haskell.  Concurrent Haskell, which
333             runs concurrent threads on a uniprocessor doesn't need
334             it.)  Underneath PVM, you can have (for example) a network
335             of workstations (slow) or a multiprocessor box
336             (faster).</para>
337
338             <para>The current version of PVM is 3.3.11; we use 3.3.7.
339             It is readily available on the net; I think I got it from
340             <literal>research.att.com</literal>, in
341             <filename>netlib</filename>.</para>
342
343             <para>A PVM installation is slightly quirky, but easy to
344             do.  Just follow the <filename>Readme</filename>
345             instructions.</para>
346           </listitem>
347         </varlistentry>
348
349         <varlistentry>
350           <term><command>bash</command>:
351             <indexterm><primary>bash, presupposed (Parallel Haskell only)</primary></indexterm>
352           </term>
353           <listitem>
354             <para>Sadly, the <command>gr2ps</command> script, used to
355             convert &ldquo;parallelism profiles&rdquo; to PostScript,
356             is written in Bash (GNU's Bourne Again shell).  This bug
357             will be fixed (someday).</para>
358           </listitem>
359         </varlistentry>
360       </variablelist>
361
362       <para>More tools are required if you want to format the
363       documentation that comes with GHC.  See <xref
364       linkend="building-docs"/>.</para>
365     </sect2>
366   </sect1>
367
368   <sect1 id="sec-building-from-source">
369     <title>Building from source</title>
370
371     <indexterm><primary>Building from source</primary></indexterm>
372     <indexterm><primary>Source, building from</primary></indexterm>
373
374     <para>&ldquo;I just want to build it!&rdquo;</para>
375
376     <para>No problem.  This recipe should build and install a working GHC with
377       all the default settings.  (unless you're
378       on Windows, in which case go to <xref linkend="winbuild" />).</para>
379
380 <screen>$ autoreconf<footnote><para>not necessary if you started from a source tarball</para>
381       </footnote>
382 $ ./configure
383 $ make
384 $ make install</screen>
385
386       <para>For GHC, this will do a 2-stage bootstrap build of the
387       compiler, with profiling libraries, and install the
388       results in the default location (under <filename>/usr/local</filename> on
389       Unix, for example).</para>
390
391     <para>The <literal>configure</literal> script is a standard GNU
392       <literal>autoconf</literal> script, and accepts the usual options for
393       changing install locations and the like.  Run
394       <literal>./configure&nbsp;--help</literal> for a list of options.</para>
395
396       <para>If you want to do anything at all non-standard, or you
397       want to do some development, read on...</para>
398     </sect1>
399     
400     <sect1 id="quick-start">
401       <title>Quick start for GHC developers</title>
402       
403       <para>This section is a copy of the file
404         <literal>HACKING</literal> from the GHC source tree.  It describes
405         how to get started with setting up your build tree for developing GHC
406         or its libraries, and how to start building.</para>
407
408 <screen>     
409 &hacking;
410       </screen>
411     </sect1>
412
413   <sect1 id="sec-working-with-the-build-system">
414     <title>Working with the build system</title>
415     
416     <para>This rest of this guide is intended for duffers like me, who
417     aren't really interested in Makefiles and systems configurations,
418     but who need a mental model of the interlocking pieces so that
419     they can make them work, extend them consistently when adding new
420     software, and lay hands on them gently when they don't
421     work.</para>
422
423     <sect2>
424       <title>History</title>
425
426       <para>First, a historical note.  The GHC build system used to be
427       called "fptools": a generic build system used to build multiple
428       projects (GHC, Happy, GreenCard, H/Direct, etc.).  It had a
429       concept of the generic project-independent parts, and
430       project-specific parts that resided in a project
431       subdirectory.</para>
432
433       <para>Nowadays, most of these other projects are using <ulink
434       url="http://www.haskell.org/cabal/">Cabal</ulink>, or have faded
435       away, and GHC is the only regular user of the fptools build
436       system.  We decided therefore to simplify the situation for
437       developers, and specialise the build system for GHC.  This
438       resulted in a simpler organisation of the source tree and the
439       build system, which hopefully makes the whole thing easier to
440       understand.</para>
441
442       <para>You might find old comments that refer to "projects" or
443       "fptools" in the documentation and/or source; please let us know
444       if you do.</para>
445     </sect2>
446
447     <sect2>
448       <title>Build trees</title>
449       <indexterm><primary>build trees</primary></indexterm>
450       <indexterm><primary>link trees, for building</primary></indexterm>
451
452       <para>If you just want to build the software once on a single
453       platform, then your source tree can also be your build tree, and
454       you can skip the rest of this section.</para>
455
456       <para>We often want to build multiple versions of our software
457       for different architectures, or with different options
458       (e.g. profiling).  It's very desirable to share a single copy of
459       the source code among all these builds.</para>
460
461       <para>So for every source tree we have zero or more
462       <emphasis>build trees</emphasis>.  Each build tree is initially
463       an exact copy of the source tree, except that each file is a
464       symbolic link to the source file, rather than being a copy of
465       the source file.  There are &ldquo;standard&rdquo; Unix
466       utilities that make such copies, so standard that they go by
467       different names:
468       <command>lndir</command><indexterm><primary>lndir</primary></indexterm>,
469       <command>mkshadowdir</command><indexterm><primary>mkshadowdir</primary></indexterm>
470       are two (If you don't have either, the source distribution
471       includes sources for the X11
472       <command>lndir</command>&mdash;check out
473       <filename>utils/lndir</filename>). See <xref
474       linkend="sec-storysofar"/> for a typical invocation.</para>
475
476       <para>The build tree does not need to be anywhere near the
477       source tree in the file system.  Indeed, one advantage of
478       separating the build tree from the source is that the build tree
479       can be placed in a non-backed-up partition, saving your systems
480       support people from backing up untold megabytes of
481       easily-regenerated, and rapidly-changing, gubbins.  The golden
482       rule is that (with a single exception&mdash;<xref
483       linkend="sec-build-config"/>) <emphasis>absolutely everything in
484       the build tree is either a symbolic link to the source tree, or
485       else is mechanically generated</emphasis>.  It should be
486       perfectly OK for your build tree to vanish overnight; an hour or
487       two compiling and you're on the road again.</para>
488
489       <para>You need to be a bit careful, though, that any new files
490       you create (if you do any development work) are in the source
491       tree, not a build tree!</para>
492
493       <para>Remember, that the source files in the build tree are
494       <emphasis>symbolic links</emphasis> to the files in the source
495       tree.  (The build tree soon accumulates lots of built files like
496       <filename>Foo.o</filename>, as well.)  You can
497       <emphasis>delete</emphasis> a source file from the build tree
498       without affecting the source tree (though it's an odd thing to
499       do).  On the other hand, if you <emphasis>edit</emphasis> a
500       source file from the build tree, you'll edit the source-tree
501       file directly.  (You can set up Emacs so that if you edit a
502       source file from the build tree, Emacs will silently create an
503       edited copy of the source file in the build tree, leaving the
504       source file unchanged; but the danger is that you think you've
505       edited the source file whereas actually all you've done is edit
506       the build-tree copy.  More commonly you do want to edit the
507       source file.)</para>
508
509       <para>Like the source tree, the top level of your build tree
510       must be (a linked copy of) the root directory of the GHC source
511       tree..  Inside Makefiles, the root of your build tree is called
512       <constant>&dollar;(GHC&lowbar;TOP)</constant><indexterm><primary>GHC&lowbar;TOP</primary></indexterm>.
513       In the rest of this document path names are relative to
514       <constant>&dollar;(GHC&lowbar;TOP)</constant> unless
515       otherwise stated.  For example, the file
516       <filename>mk/target.mk</filename> is actually
517       <filename>&dollar;(GHC&lowbar;TOP)/mk/target.mk</filename>.</para>
518     </sect2>
519
520     <sect2 id="sec-build-config">
521       <title>Getting the build you want</title>
522
523       <para>When you build GHC you will be compiling code on a
524       particular <emphasis>host platform</emphasis>, to run on a
525       particular <emphasis>target platform</emphasis> (usually the
526       same as the host
527       platform)<indexterm><primary>platform</primary></indexterm>.
528       The difficulty is that there are minor differences between
529       different platforms; minor, but enough that the code needs to be
530       a bit different for each.  There are some big differences too:
531       for a different architecture we need to build GHC with a
532       different native-code generator.</para>
533
534       <para>There are also knobs you can turn to control how the
535       software is built.  For example, you might want to build GHC
536       optimised (so that it runs fast) or unoptimised (so that you can
537       compile it fast after you've modified it.  Or, you might want to
538       compile it with debugging on (so that extra consistency-checking
539       code gets included) or off.  And so on.</para>
540
541       <para>All of this stuff is called the
542       <emphasis>configuration</emphasis> of your build.  You set the
543       configuration using a three-step process.</para>
544
545       <variablelist>
546         <varlistentry>
547           <term>Step 1: get ready for configuration.</term>
548           <listitem>
549             <para>NOTE: if you're starting from a source distribution,
550             rather than darcs sources, you can skip this step.</para>
551
552             <para>Change directory to
553             <constant>&dollar;(GHC&lowbar;TOP)</constant> and
554             issue the command</para>
555 <screen>$ autoreconf</screen>
556             <indexterm><primary>autoreconf</primary></indexterm>
557             <para>(with no arguments). This GNU program (recursively) converts
558             <filename>&dollar;(GHC&lowbar;TOP)/configure.ac</filename> and
559             <filename>&dollar;(GHC&lowbar;TOP)/aclocal.m4</filename>
560             to a shell script called
561             <filename>&dollar;(GHC&lowbar;TOP)/configure</filename>.
562               If <command>autoreconf</command> bleats that it can't write the file <filename>configure</filename>,
563               then delete the latter and try again.  Note that you must use <command>autoreconf</command>,
564               and not the old <command>autoconf</command>!  If you erroneously use the latter, you'll get 
565               a message like "No rule to make target 'mk/config.h.in'".
566             </para>
567
568             <para>Some parts of the source tree, particularly
569             libraries, have their own configure script.
570             <command>autoreconf</command> takes care of that, too, so all you have
571              to do is calling <command>autoreconf</command> in the top-level directory
572             <filename>&dollar;(GHC&lowbar;TOP)</filename>.</para>
573
574             <para>These steps are completely platform-independent; they just mean
575             that the human-written files (<filename>configure.ac</filename> and
576             <filename>aclocal.m4</filename>) can be short, although the resulting
577             files (the <command>configure</command> shell scripts and the C header
578             template <filename>mk/config.h.in</filename>) are long.</para>
579           </listitem>
580         </varlistentry>
581
582         <varlistentry>
583           <term>Step 2: system configuration.</term>
584           <listitem>
585             <para>Runs the newly-created <command>configure</command>
586             script, thus:</para>
587
588 <screen>$ ./configure <optional><parameter>args</parameter></optional></screen>
589
590             <para><command>configure</command>'s mission is to scurry
591             round your computer working out what architecture it has,
592             what operating system, whether it has the
593             <function>vfork</function> system call, where
594             <command>tar</command> is kept, whether
595             <command>gcc</command> is available, where various obscure
596             <literal>&num;include</literal> files are, whether it's a
597             leap year, and what the systems manager had for lunch.  It
598             communicates these snippets of information in two
599             ways:</para>
600
601             <itemizedlist>
602               <listitem>
603                 
604                 <para>It translates
605                 <filename>mk/config.mk.in</filename><indexterm><primary>config.mk.in</primary></indexterm>
606                 to
607                 <filename>mk/config.mk</filename><indexterm><primary>config.mk</primary></indexterm>,
608                 substituting for things between
609                 &ldquo;<literal>@</literal>&rdquo; brackets.  So,
610                 &ldquo;<literal>@HaveGcc@</literal>&rdquo; will be
611                 replaced by &ldquo;<literal>YES</literal>&rdquo; or
612                 &ldquo;<literal>NO</literal>&rdquo; depending on what
613                 <command>configure</command> finds.
614                 <filename>mk/config.mk</filename> is included by every
615                 Makefile (directly or indirectly), so the
616                 configuration information is thereby communicated to
617                 all Makefiles.</para>
618                 </listitem>
619
620               <listitem>
621                 <para> It translates
622                 <filename>mk/config.h.in</filename><indexterm><primary>config.h.in</primary></indexterm>
623                 to
624                 <filename>mk/config.h</filename><indexterm><primary>config.h</primary></indexterm>.
625                 The latter is <literal>&num;include</literal>d by
626                 various C programs, which can thereby make use of
627                 configuration information.</para>
628               </listitem>
629             </itemizedlist>
630
631             <para><command>configure</command> takes some optional
632             arguments.  Use <literal>./configure --help</literal> to
633             get a list of the available arguments.  Here are some of
634             the ones you might need:</para>
635
636             <variablelist>
637               <varlistentry>
638                 <term><literal>--with-ghc=<parameter>path</parameter></literal>
639                   <indexterm><primary><literal>--with-ghc</literal></primary></indexterm>
640                 </term>
641                 <listitem>
642                   <para>Specifies the path to an installed GHC which
643                   you would like to use.  This compiler will be used
644                   for compiling GHC-specific code (eg. GHC itself).
645                   This option <emphasis>cannot</emphasis> be specified
646                   using <filename>build.mk</filename> (see later),
647                   because <command>configure</command> needs to
648                   auto-detect the version of GHC you're using.  The
649                   default is to look for a compiler named
650                   <literal>ghc</literal> in your path.</para>
651                 </listitem>
652               </varlistentry>
653               
654               <varlistentry>
655                 <term><literal>--with-hc=<parameter>path</parameter></literal>
656                   <indexterm><primary><literal>--with-hc</literal></primary></indexterm>
657                 </term>
658                 <listitem>
659                   <para>Specifies the path to any installed Haskell
660                   compiler.  This compiler will be used for compiling
661                   generic Haskell code.  The default is to use
662                   <literal>ghc</literal>. (NOTE: I'm not sure it
663                   actually works to specify a compiler other than GHC
664                   here; unless you really know what you're doing I
665                   suggest not using this option at all.)</para>
666                 </listitem>
667               </varlistentry>
668               
669               <varlistentry>
670                 <term><literal>--with-gcc=<parameter>path</parameter></literal>
671                   <indexterm><primary><literal>--with-gcc</literal></primary></indexterm>
672                 </term>
673                 <listitem>
674                   <para>Specifies the path to the installed GCC. This
675                   compiler will be used to compile all C files,
676                   <emphasis>except</emphasis> any generated by the
677                   installed Haskell compiler, which will have its own
678                   idea of which C compiler (if any) to use.  The
679                   default is to use <literal>gcc</literal>.</para>
680                 </listitem>
681               </varlistentry>
682             </variablelist>
683           </listitem>
684         </varlistentry>
685         
686         <varlistentry>
687           <term>Step 3: build configuration.</term>
688           <listitem>
689             <para>Next, you say how this build of
690             GHC is to differ from the standard
691             defaults by creating a new file
692             <filename>mk/build.mk</filename><indexterm><primary>build.mk</primary></indexterm>
693             <emphasis>in the build tree</emphasis>.  This file is the
694             one and only file you edit in the build tree, precisely
695             because it says how this build differs from the source.
696             (Just in case your build tree does die, you might want to
697             keep a private directory of <filename>build.mk</filename>
698             files, and use a symbolic link in each build tree to point
699             to the appropriate one.)  So
700             <filename>mk/build.mk</filename> never exists in the
701             source tree&mdash;you create one in each build tree from
702             the template.  We'll discuss what to put in it
703             shortly.</para>
704           </listitem>
705         </varlistentry>
706       </variablelist>
707
708       <para>And that's it for configuration. Simple, eh?</para>
709
710       <para>What do you put in your build-specific configuration file
711       <filename>mk/build.mk</filename>?  <emphasis>For almost all
712       purposes all you will do is put make variable definitions that
713       override those in</emphasis>
714       <filename>mk/config.mk.in</filename>.  The whole point of
715       <filename>mk/config.mk.in</filename>&mdash;and its derived
716       counterpart <filename>mk/config.mk</filename>&mdash;is to define
717       the build configuration. It is heavily commented, as you will
718       see if you look at it.  So generally, what you do is look at
719       <filename>mk/config.mk.in</filename>, and add definitions in
720       <filename>mk/build.mk</filename> that override any of the
721       <filename>config.mk</filename> definitions that you want to
722       change.  (The override occurs because the main boilerplate file,
723       <filename>mk/boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>,
724       includes <filename>build.mk</filename> after
725       <filename>config.mk</filename>.)</para>
726
727      <para>For your convenience, there's a file called
728      <filename>build.mk.sample</filename> that can serve as a starting
729      point for your <filename>build.mk</filename>.</para>
730
731       <para>For example, <filename>config.mk.in</filename> contains
732       the definition:</para>
733
734 <programlisting>GhcHcOpts=-Rghc-timing</programlisting>
735
736       <para>The accompanying comment explains that this is the list of
737       flags passed to GHC when building GHC itself.  For doing
738       development, it is wise to add <literal>-DDEBUG</literal>, to
739       enable debugging code.  So you would add the following to
740       <filename>build.mk</filename>:</para>
741       
742 <programlisting>GhcHcOpts += -DDEBUG</programlisting>
743
744       <para>GNU <command>make</command> allows existing definitions to
745       have new text appended using the &ldquo;<literal>+=</literal>&rdquo;
746       operator, which is quite a convenient feature.</para>
747
748       <para>Haskell compilations by default have <literal>-O</literal>
749       turned on, by virtue of this setting from
750       <filename>config.mk</filename>:</para>
751
752 <programlisting>SRC_HC_OPTS += -H16m -O</programlisting>
753
754       <para><literal>SRC_HC_OPTS</literal> means "options for HC from
755       the source tree", where HC stands for Haskell Compiler.
756       <literal>SRC_HC_OPTS</literal> are added to every Haskell
757       compilation.  To turn off optimisation, you could add this to
758       <filename>build.mk</filename>:</para>
759
760 <programlisting>SRC_HC_OPTS = -H16m -O0</programlisting>
761
762       <para>Or you could just add <literal>-O0</literal> to
763       <literal>GhcHcOpts</literal> to turn off optimisation for the
764       compiler.  See <xref linkend="quick-start" /> for some more
765       suggestions.</para>
766
767       <para>When reading <filename>config.mk.in</filename>, remember
768       that anything between &ldquo;@...@&rdquo; signs is going to be substituted
769       by <command>configure</command> later.  You
770       <emphasis>can</emphasis> override the resulting definition if
771       you want, but you need to be a bit surer what you are doing.
772       For example, there's a line that says:</para>
773
774 <programlisting>TAR = @TarCmd@</programlisting>
775
776       <para>This defines the Make variables <constant>TAR</constant>
777       to the pathname for a <command>tar</command> that
778       <command>configure</command> finds somewhere.  If you have your
779       own pet <command>tar</command> you want to use instead, that's
780       fine. Just add this line to <filename>mk/build.mk</filename>:</para>
781
782 <programlisting>TAR = mytar</programlisting>
783
784       <para>You do not <emphasis>have</emphasis> to have a
785       <filename>mk/build.mk</filename> file at all; if you don't,
786       you'll get all the default settings from
787       <filename>mk/config.mk.in</filename>.</para>
788
789       <para>You can also use <filename>build.mk</filename> to override
790       anything that <command>configure</command> got wrong.  One place
791       where this happens often is with the definition of
792       <constant>GHC&lowbar;TOP&lowbar;ABS</constant>: this
793       variable is supposed to be the canonical path to the top of your
794       source tree, but if your system uses an automounter then the
795       correct directory is hard to find automatically.  If you find
796       that <command>configure</command> has got it wrong, just put the
797       correct definition in <filename>build.mk</filename>.</para>
798     </sect2>
799
800     <sect2 id="sec-storysofar">
801       <title>The story so far</title>
802
803       <para>Let's summarise the steps you need to carry to get
804       yourself a fully-configured build tree from scratch.</para>
805
806       <orderedlist>
807         <listitem>
808           <para> Get your source tree from somewhere (darcs repository
809           or source distribution).  Say you call the root directory
810           <filename>myghc</filename> (it does not have to be
811           called <filename>ghc</filename>).</para>
812         </listitem>
813
814         <listitem>
815           <para>(Optional) Use <command>lndir</command> or
816           <command>mkshadowdir</command> to create a build tree.</para>
817
818 <screen>$ cd myghc
819 $ mkshadowdir . /scratch/joe-bloggs/myghc-x86</screen>
820
821           <para>(N.B. <command>mkshadowdir</command>'s first argument
822           is taken relative to its second.) You probably want to give
823           the build tree a name that suggests its main defining
824           characteristic (in your mind at least), in case you later
825           add others.</para>
826         </listitem>
827
828         <listitem>
829           <para>Change directory to the build tree.  Everything is
830           going to happen there now.</para>
831
832 <screen>$ cd /scratch/joe-bloggs/myghc-x86</screen>
833
834         </listitem>
835
836         <listitem>
837           <para>Prepare for system configuration:</para>
838
839 <screen>$ autoreconf</screen>
840
841           <para>(You can skip this step if you are starting from a
842           source distribution, and you already have
843           <filename>configure</filename> and
844           <filename>mk/config.h.in</filename>.)</para>
845         </listitem>
846
847         <listitem>
848           <para>Do system configuration:</para>
849
850 <screen>$ ./configure</screen>
851
852           <para>Don't forget to check whether you need to add any
853           arguments to <literal>configure</literal>; for example, a
854           common requirement is to specify which GHC to use with
855           <option>--with-ghc=<replaceable>ghc</replaceable></option>.</para>
856         </listitem>
857
858         <listitem>
859           <para>Create the file <filename>mk/build.mk</filename>,
860           adding definitions for your desired configuration
861           options.</para>
862         </listitem>
863       </orderedlist>
864
865       <para>You can make subsequent changes to
866       <filename>mk/build.mk</filename> as often as you like.  You do
867       not have to run any further configuration programs to make these
868       changes take effect. In theory you should, however, say
869       <command>make clean; make</command>, because configuration
870       option changes could affect anything&mdash;but in practice you
871       are likely to know what's affected.</para>
872     </sect2>
873
874     <sect2>
875       <title>Making things</title>
876
877       <para>At this point you have made yourself a fully-configured
878       build tree, so you are ready to start building real
879       things.</para>
880
881       <para>The first thing you need to know is that <emphasis>you
882       must use GNU <command>make</command></emphasis>.  On some
883       systems (eg. FreeBSD) this is called <command>gmake</command>,
884       whereas on others it is the standard <command>make</command>
885       command.  In this document we will always refer to it as
886       <command>make</command>; please substitute with
887       <command>gmake</command> if your system requires it.  If you use
888       a the wrong <command>make</command> you will get all sorts of
889       error messages (but no damage) because the GHC
890       <command>Makefiles</command> use GNU <command>make</command>'s
891       facilities extensively.</para>
892
893       <para>To just build the whole thing, <command>cd</command> to
894       the top of your build tree and type <command>make</command>.
895       This will prepare the tree and build the various parts in the
896       correct order, resulting in a complete build of GHC that can
897       even be used directly from the tree, without being installed
898       first.</para>
899     </sect2>
900
901     <sect2 id="sec-bootstrapping">
902       <title>Bootstrapping GHC</title>
903
904       <para>GHC requires a 2-stage bootstrap in order to provide 
905       full functionality, including GHCi.  By a 2-stage bootstrap, we
906       mean that the compiler is built once using the installed GHC,
907       and then again using the compiler built in the first stage.  You
908       can also build a stage 3 compiler, but this normally isn't
909       necessary except to verify that the stage 2 compiler is working
910       properly.</para>
911
912       <para>Note that when doing a bootstrap, the stage 1 compiler
913       must be built, followed by the runtime system and libraries, and
914       then the stage 2 compiler.  The correct ordering is implemented
915       by the top-level <filename>Makefile</filename>, so if you want
916       everything to work automatically it's best to start
917       <command>make</command> from the top of the tree.  The top-level
918       <filename>Makefile</filename> is set up to do a 2-stage
919       bootstrap by default (when you say <command>make</command>).
920       Some other targets it supports are:</para>
921
922       <variablelist>
923         <varlistentry>
924           <term>stage1</term>
925           <listitem>
926             <para>Build everything as normal, including the stage 1
927             compiler.</para>
928           </listitem>
929         </varlistentry>
930
931         <varlistentry>
932           <term>stage2</term>
933           <listitem>
934             <para>Build the stage 2 compiler only.</para>
935           </listitem>
936         </varlistentry>
937
938         <varlistentry>
939           <term>stage3</term>
940           <listitem>
941             <para>Build the stage 3 compiler only.</para>
942           </listitem>
943         </varlistentry>
944
945         <varlistentry>
946           <term>bootstrap</term> <term>bootstrap2</term>
947           <listitem>
948             <para>Build stage 1 followed by stage 2.</para>
949           </listitem>
950         </varlistentry>
951
952         <varlistentry>
953           <term>bootstrap3</term>
954           <listitem>
955             <para>Build stages 1, 2 and 3.</para>
956           </listitem>
957         </varlistentry>
958
959         <varlistentry>
960           <term>install</term>
961           <listitem>
962             <para>Install everything, including the compiler built in
963             stage 2.  To override the stage, say <literal>make install
964             stage=<replaceable>n</replaceable></literal> where
965             <replaceable>n</replaceable> is the stage to install.</para>
966           </listitem>
967         </varlistentry>
968
969         <varlistentry>
970           <term><literal>binary-dist</literal></term>
971           <listitem>
972             <para>make a binary distribution.  This is the target we
973             use to build the binary distributions of GHC.</para>
974           </listitem>
975         </varlistentry>
976
977         <varlistentry>
978           <term><literal>dist</literal></term>
979           <listitem>
980             <para>make a source distribution.  Note that this target
981             does &ldquo;make distclean&rdquo; as part of its work;
982             don't use it if you want to keep what you've built.</para>
983           </listitem>
984         </varlistentry>
985       </variablelist>
986
987       <para>The top-level <filename>Makefile</filename> also arranges
988       to do the appropriate <literal>make boot</literal> steps (see
989       below) before actually building anything.</para>
990
991       <para>The <literal>stage1</literal>, <literal>stage2</literal>
992       and <literal>stage3</literal> targets also work in the
993       <literal>compiler</literal> directory, but don't forget that
994       each stage requires its own <literal>make boot</literal> step:
995       for example, you must do</para>
996
997       <screen>$ make boot stage=2</screen>
998
999       <para>before <literal>make stage2</literal> in
1000       <literal>compiler</literal>.</para>
1001     </sect2>
1002
1003     <sect2 id="sec-standard-targets">
1004       <title>Standard Targets</title>
1005       <indexterm><primary>targets, standard makefile</primary></indexterm>
1006       <indexterm><primary>makefile targets</primary></indexterm>
1007
1008       <para>In any directory you should be able to make the following:</para>
1009
1010       <variablelist>
1011         <varlistentry>
1012           <term><literal>boot</literal></term>
1013           <listitem>
1014             <para>does the one-off preparation required to get ready
1015             for the real work.  Notably, it does <command>make
1016             depend</command> in all directories that contain programs.
1017             It also builds the necessary tools for compilation to
1018             proceed.</para>
1019
1020             <para>Invoking the <literal>boot</literal> target
1021             explicitly is not normally necessary.  From the top-level
1022             directory, invoking <literal>make</literal> causes
1023             <literal>make boot</literal> to be invoked in various
1024             subdirectories first, in the right order.  Unless you
1025             really know what you are doing, it is best to always say
1026             <literal>make</literal> from the top level first.</para>
1027
1028             <para>If you're working in a subdirectory somewhere and
1029             need to update the dependencies, <literal>make
1030             boot</literal> is a good way to do it.</para>
1031           </listitem>
1032         </varlistentry>
1033
1034         <varlistentry>
1035           <term><literal>all</literal></term>
1036           <listitem>
1037             <para>makes all the final target(s) for this Makefile.
1038             Depending on which directory you are in a &ldquo;final
1039             target&rdquo; may be an executable program, a library
1040             archive, a shell script, or a Postscript file.  Typing
1041             <command>make</command> alone is generally the same as
1042             typing <command>make all</command>.</para>
1043           </listitem>
1044         </varlistentry>
1045
1046         <varlistentry>
1047           <term><literal>install</literal></term>
1048           <listitem>
1049             <para>installs the things built by <literal>all</literal>
1050             (except for the documentation).  Where does it install
1051             them?  That is specified by
1052             <filename>mk/config.mk.in</filename>; you can override it
1053             in <filename>mk/build.mk</filename>, or by running
1054             <command>configure</command> with command-line arguments
1055             like <literal>--bindir=/home/simonpj/bin</literal>; see
1056             <literal>./configure --help</literal> for the full
1057             details.</para>
1058           </listitem>
1059         </varlistentry>
1060
1061         <varlistentry>
1062           <term><literal>install-docs</literal></term>
1063           <listitem>
1064             <para>installs the documentation. Otherwise behaves just
1065             like <literal>install</literal>.</para>
1066           </listitem>
1067         </varlistentry>
1068
1069         <varlistentry>
1070           <term><literal>uninstall</literal></term>
1071           <listitem>
1072             <para>reverses the effect of
1073             <literal>install</literal> (WARNING: probably doesn't work).</para>
1074           </listitem>
1075         </varlistentry>
1076
1077         <varlistentry>
1078           <term><literal>clean</literal></term>
1079           <listitem>
1080             <para>Delete all files from the current directory that are
1081             normally created by building the program.  Don't delete
1082             the files that record the configuration, or files
1083             generated by <command>make boot</command>.  Also preserve
1084             files that could be made by building, but normally aren't
1085             because the distribution comes with them.</para>
1086           </listitem>
1087         </varlistentry>
1088
1089         <varlistentry>
1090           <term><literal>distclean</literal></term>
1091           <listitem>
1092             <para>Delete all files from the current directory that are
1093             created by configuring or building the program. If you
1094             have unpacked the source and built the program without
1095             creating any other files, <literal>make
1096             distclean</literal> should leave only the files that were
1097             in the distribution.</para>
1098           </listitem>
1099         </varlistentry>
1100
1101         <varlistentry>
1102           <term><literal>mostlyclean</literal></term>
1103           <listitem>
1104             <para>Like <literal>clean</literal>, but may refrain from
1105             deleting a few files that people normally don't want to
1106             recompile.</para>
1107           </listitem>
1108         </varlistentry>
1109
1110         <varlistentry>
1111           <term><literal>maintainer-clean</literal></term>
1112           <listitem>
1113             <para>Delete everything from the current directory that
1114             can be reconstructed with this Makefile.  This typically
1115             includes everything deleted by
1116             <literal>distclean</literal>, plus more: C source files
1117             produced by Bison, tags tables, Info files, and so
1118             on.</para>
1119
1120             <para>One exception, however: <literal>make
1121             maintainer-clean</literal> should not delete
1122             <filename>configure</filename> even if
1123             <filename>configure</filename> can be remade using a rule
1124             in the <filename>Makefile</filename>. More generally,
1125             <literal>make maintainer-clean</literal> should not delete
1126             anything that needs to exist in order to run
1127             <filename>configure</filename> and then begin to build the
1128             program.</para>
1129
1130             <para>After a <literal>maintainer-clean</literal>, a
1131             <literal>configure</literal> will be necessary before
1132             building again.</para>
1133           </listitem>
1134         </varlistentry>
1135       </variablelist>
1136
1137       <para>All of these standard targets automatically recurse into
1138       sub-directories.  Certain other standard targets do not:</para>
1139
1140       <variablelist>
1141         <varlistentry>
1142           <term><literal>depend</literal></term>
1143           <listitem>
1144             <para>make a <filename>.depend</filename> file in each
1145             directory that needs it. This <filename>.depend</filename>
1146             file contains mechanically-generated dependency
1147             information; for example, suppose a directory contains a
1148             Haskell source module <filename>Foo.lhs</filename> which
1149             imports another module <literal>Baz</literal>.  Then the
1150             generated <filename>.depend</filename> file will contain
1151             the dependency:</para>
1152
1153 <programlisting>Foo.o : Baz.hi</programlisting>
1154
1155             <para>which says that the object file
1156             <filename>Foo.o</filename> depends on the interface file
1157             <filename>Baz.hi</filename> generated by compiling module
1158             <literal>Baz</literal>.  The <filename>.depend</filename>
1159             file is automatically included by every Makefile.</para>
1160           </listitem>
1161         </varlistentry>
1162       </variablelist>
1163
1164       <para>Some <filename>Makefile</filename>s have targets other
1165       than these.  You can discover them by looking in the
1166       <filename>Makefile</filename> itself.</para>
1167     </sect2>
1168
1169     <sect2>
1170       <title>Using GHC from the build tree</title>
1171
1172       <para>If you want to build GHC and just use it direct from the
1173       build tree without doing <literal>make install</literal> first,
1174       you can run the in-place driver script.  To run the stage 1
1175       compiler, use <filename>compiler/stage1/ghc-inplace</filename>,
1176       stage 2 is <filename>compiler/stage2/ghc-inplace</filename>, and
1177       so on.</para>
1178
1179       <para> Do <emphasis>NOT</emphasis> use
1180       <filename>compiler/stage1/ghc</filename>, or
1181       <filename>compiler/stage1/ghc-6.xx</filename>, as these are the
1182       scripts intended for installation, and contain hard-wired paths
1183       to the installed libraries, rather than the libraries in the
1184       build tree.</para>
1185     </sect2>
1186
1187     <sect2>
1188       <title>Fast Making</title>
1189
1190       <indexterm><primary>fastmake</primary></indexterm>
1191       <indexterm><primary>dependencies, omitting</primary></indexterm>
1192       <indexterm><primary>FAST, makefile variable</primary></indexterm>
1193
1194       <para>Sometimes the dependencies get in the way: if you've made
1195       a small change to one file, and you're absolutely sure that it
1196       won't affect anything else, but you know that
1197       <command>make</command> is going to rebuild everything anyway,
1198       the following hack may be useful:</para>
1199
1200 <screen>$ make FAST=YES</screen>
1201
1202       <para>This tells the make system to ignore dependencies and just
1203       build what you tell it to.  In other words, it's equivalent to
1204       temporarily removing the <filename>.depend</filename> file in
1205       the current directory (where <command>mkdependHS</command> and
1206       friends store their dependency information).</para>
1207
1208       <para>A bit of history: GHC used to come with a
1209       <command>fastmake</command> script that did the above job, but
1210       GNU make provides the features we need to do it without
1211       resorting to a script.  Also, we've found that fastmaking is
1212       less useful since the advent of GHC's recompilation checker (see
1213       the User's Guide section on "Separate Compilation").</para>
1214     </sect2>
1215   </sect1>
1216
1217   <sect1 id="sec-makefile-arch">
1218     <title>The <filename>Makefile</filename> architecture</title>
1219     <indexterm><primary>makefile architecture</primary></indexterm>
1220
1221     <para><command>make</command> is great if everything
1222     works&mdash;you type <command>make install</command> and lo! the
1223     right things get compiled and installed in the right places.  Our
1224     goal is to make this happen often, but somehow it often doesn't;
1225     instead some weird error message eventually emerges from the
1226     bowels of a directory you didn't know existed.</para>
1227
1228     <para>The purpose of this section is to give you a road-map to
1229     help you figure out what is going right and what is going
1230     wrong.</para>
1231
1232     <sect2>
1233       <title>Debugging</title>
1234       
1235       <para>Debugging <filename>Makefile</filename>s is something of a
1236       black art, but here's a couple of tricks that we find
1237       particularly useful.  The following command allows you to see
1238       the contents of any make variable in the context of the current
1239       <filename>Makefile</filename>:</para>
1240
1241 <screen>$ make show VALUE=HS_SRCS</screen>
1242
1243       <para>where you can replace <literal>HS_SRCS</literal> with the
1244       name of any variable you wish to see the value of.</para>
1245       
1246       <para>GNU make has a <option>-d</option> option which generates
1247       a dump of the decision procedure used to arrive at a conclusion
1248       about which files should be recompiled.  Sometimes useful for
1249       tracking down problems with superfluous or missing
1250       recompilations.</para>
1251     </sect2>
1252
1253     <sect2>
1254       <title>A small example</title>
1255
1256       <para>To get started, let us look at the
1257       <filename>Makefile</filename> for an imaginary small program,
1258       <literal>small</literal>.  Each program or library in the GHC
1259       source tree typically has its own directory, in this case we'll
1260       use <filename>&dollar;(GHC&lowbar;TOP)/small</filename>.
1261       Inside the <filename>small/</filename> directory there will be a
1262       <filename>Makefile</filename>, looking something like
1263       this:</para>
1264
1265 <indexterm><primary>Makefile, minimal</primary></indexterm>
1266
1267 <programlisting># Makefile for program "small"
1268 TOP = ..
1269 include $(TOP)/mk/boilerplate.mk
1270
1271 HS_PROG = small
1272
1273 include $(TOP)/target.mk</programlisting>
1274
1275       <para>this <filename>Makefile</filename> has three
1276       sections:</para>
1277
1278       <orderedlist>
1279         <listitem>
1280           <para>The first section includes
1281 <footnote>
1282 <para>
1283 One of the most important
1284 features of GNU <command>make</command> that we use is the ability for a <filename>Makefile</filename> to
1285 include another named file, very like <command>cpp</command>'s <literal>&num;include</literal>
1286 directive.
1287 </para>
1288 </footnote>
1289
1290           a file of &ldquo;boilerplate&rdquo; code from the top level
1291           <indexterm><primary>boilerplate.mk</primary></indexterm>).
1292           As its name suggests, <filename>boilerplate.mk</filename>
1293           consists of a large quantity of standard
1294           <filename>Makefile</filename> code.  We discuss this
1295           boilerplate in more detail in <xref linkend="sec-boiler"/>.
1296           <indexterm><primary>include, directive in
1297           Makefiles</primary></indexterm> <indexterm><primary>Makefile
1298           inclusion</primary></indexterm></para>
1299
1300           <para>Before the <literal>include</literal> statement, you
1301           must define the <command>make</command> variable
1302           <constant>TOP</constant><indexterm><primary>TOP</primary></indexterm>
1303           to be the top-level directory of the source tree, containing
1304           the <filename>mk</filename>
1305           directory in which the <filename>boilerplate.mk</filename>
1306           file is.  It is <emphasis>not</emphasis> OK to simply say</para>
1307
1308 <programlisting>include ../mk/boilerplate.mk  # NO NO NO</programlisting>
1309
1310           <para>Why?  Because the <filename>boilerplate.mk</filename>
1311           file needs to know where it is, so that it can, in turn,
1312           <literal>include</literal> other files.  (Unfortunately,
1313           when an <literal>include</literal>d file does an
1314           <literal>include</literal>, the filename is treated relative
1315           to the directory in which <command>make</command> is being
1316           run, not the directory in which the
1317           <literal>include</literal>d sits.)  In general,
1318           <emphasis>every file <filename>foo.mk</filename> assumes
1319           that
1320           <filename>&dollar;(TOP)/mk/foo.mk</filename>
1321           refers to itself.</emphasis> It is up to the
1322           <filename>Makefile</filename> doing the
1323           <literal>include</literal> to ensure this is the case.</para>
1324         </listitem>
1325
1326         <listitem>
1327           <para> The second section defines the standard
1328           <command>make</command> variable
1329           <constant>HS&lowbar;PROG</constant><indexterm><primary>HS&lowbar;PROG</primary></indexterm>
1330           (the executable binary to be built).  We will discuss in
1331           more detail what the &ldquo;standard variables&rdquo; are,
1332           and how they affect what happens, in <xref
1333           linkend="sec-targets"/>.</para>
1334         </listitem>
1335
1336         <listitem>
1337           <para>The last section includes a second file of standard
1338           code, called
1339           <filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>.
1340           It contains the rules that tell <command>make</command> how
1341           to make the standard targets (<xref
1342           linkend="sec-standard-targets"/>).  Why, you ask, can't this
1343           standard code be part of
1344           <filename>boilerplate.mk</filename>?  Good question.  We
1345           discuss the reason later, in <xref
1346           linkend="sec-boiler-arch"/>.</para>
1347
1348           <para>You do not <emphasis>have</emphasis> to
1349           <literal>include</literal> the
1350           <filename>target.mk</filename> file.  Instead, you can write
1351           rules of your own for all the standard targets.  Usually,
1352           though, you will find quite a big payoff from using the
1353           canned rules in <filename>target.mk</filename>; the price
1354           tag is that you have to understand what canned rules get
1355           enabled, and what they do (<xref
1356           linkend="sec-targets"/>).</para>
1357         </listitem>
1358       </orderedlist>
1359
1360       <para>In our example <filename>Makefile</filename>, most of the
1361       work is done by the two <literal>include</literal>d files.  When
1362       you say <command>make all</command>, the following things
1363       happen:</para>
1364
1365       <itemizedlist>
1366         <listitem>
1367           <para><command>make</command> looks in the current directory
1368           to see what source files it can find
1369           (eg. <filename>Foo.hs</filename>,
1370           <filename>Baz.c</filename>), and from that it figures out
1371           what object files need to be built
1372           (eg. <filename>Foo.o</filename>,
1373           <filename>Baz.o</filename>).  Because source files are found
1374           and used automatically, omitting them from a program or
1375           library has to be done manually (see
1376           <literal>EXCLUDED_SRCS</literal> in <xref
1377           linkend="sec-boiler" />).</para>
1378         </listitem>
1379
1380         <listitem>
1381           <para>It uses a boilerplate pattern rule to compile
1382           <filename>Foo.hs</filename> to <filename>Foo.o</filename>
1383           using a Haskell compiler.  (Which one?  That is set in the
1384           build configuration.)</para>
1385         </listitem>
1386
1387         <listitem>
1388           <para>It uses another standard pattern rule to compile
1389           <filename>Baz.c</filename> to <filename>Baz.o</filename>,
1390           using a C compiler.  (Ditto.)</para>
1391         </listitem>
1392
1393         <listitem>
1394           <para>It links the resulting <filename>.o</filename> files
1395           together to make <literal>small</literal>, using the Haskell
1396           compiler to do the link step.  (Why not use
1397           <command>ld</command>?  Because the Haskell compiler knows
1398           what standard libraries to link in.  How did
1399           <command>make</command> know to use the Haskell compiler to
1400           do the link, rather than the C compiler?  Because we set the
1401           variable <constant>HS&lowbar;PROG</constant> rather than
1402           <constant>C&lowbar;PROG</constant>.)</para>
1403         </listitem>
1404       </itemizedlist>
1405
1406       <para>All <filename>Makefile</filename>s should follow the above
1407       three-section format.</para>
1408     </sect2>
1409
1410     <sect2 id="sec-boiler-arch">
1411       <title>Boilerplate architecture</title>
1412       <indexterm><primary>boilerplate architecture</primary></indexterm>
1413
1414       <para>Every <filename>Makefile</filename> includes a
1415       <filename>boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>
1416       file at the top, and
1417       <filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>
1418       file at the bottom.  In this section we discuss what is in these
1419       files, and why there have to be two of them.  In general:</para>
1420
1421       <itemizedlist>
1422         <listitem>
1423           <para><filename>boilerplate.mk</filename> consists of:</para>
1424
1425           <itemizedlist>
1426             <listitem>
1427               <para><emphasis>Definitions of millions of
1428               <command>make</command> variables</emphasis> that
1429               collectively specify the build configuration.  Examples:
1430               <constant>HC&lowbar;OPTS</constant><indexterm><primary>HC&lowbar;OPTS</primary></indexterm>,
1431               the options to feed to the Haskell compiler;
1432               <constant>NoFibSubDirs</constant><indexterm><primary>NoFibSubDirs</primary></indexterm>,
1433               the sub-directories to enable within the
1434               <literal>nofib</literal> project;
1435               <constant>GhcWithHc</constant><indexterm><primary>GhcWithHc</primary></indexterm>,
1436               the name of the Haskell compiler to use when compiling
1437               GHC in the <literal>ghc</literal> project.</para>
1438             </listitem>
1439
1440             <listitem>
1441               <para><emphasis>Standard pattern rules</emphasis> that
1442               tell <command>make</command> how to construct one file
1443               from another.</para>
1444             </listitem>
1445           </itemizedlist>
1446
1447           <para><filename>boilerplate.mk</filename> needs to be
1448           <literal>include</literal>d at the <emphasis>top</emphasis>
1449           of each <filename>Makefile</filename>, so that the user can
1450           replace the boilerplate definitions or pattern rules by
1451           simply giving a new definition or pattern rule in the
1452           <filename>Makefile</filename>.  <command>make</command>
1453           simply takes the last definition as the definitive one.</para>
1454
1455           <para>Instead of <emphasis>replacing</emphasis> boilerplate
1456           definitions, it is also quite common to
1457           <emphasis>augment</emphasis> them. For example, a
1458           <filename>Makefile</filename> might say:</para>
1459
1460 <programlisting>SRC_HC_OPTS += -O</programlisting>
1461
1462           <para>thereby adding &ldquo;<option>-O</option>&rdquo; to
1463           the end of
1464           <constant>SRC&lowbar;HC&lowbar;OPTS</constant><indexterm><primary>SRC&lowbar;HC&lowbar;OPTS</primary></indexterm>.</para>
1465         </listitem>
1466
1467         <listitem>
1468           <para><filename>target.mk</filename> contains
1469           <command>make</command> rules for the standard targets
1470           described in <xref linkend="sec-standard-targets"/>.  These
1471           rules are selectively included, depending on the setting of
1472           certain <command>make</command> variables.  These variables
1473           are usually set in the middle section of the
1474           <filename>Makefile</filename> between the two
1475           <literal>include</literal>s.</para>
1476
1477           <para><filename>target.mk</filename> must be included at the
1478           end (rather than being part of
1479           <filename>boilerplate.mk</filename>) for several tiresome
1480           reasons:</para>
1481
1482           <itemizedlist>
1483             <listitem>
1484
1485               <para><command>make</command> commits target and
1486               dependency lists earlier than it should.  For example,
1487               <filename>target.mk</filename> has a rule that looks
1488               like this:</para>
1489
1490 <programlisting>$(HS_PROG) : $(OBJS)
1491       $(HC) $(LD_OPTS) $&#60; -o $@</programlisting>
1492
1493               <para>If this rule was in
1494               <filename>boilerplate.mk</filename> then
1495               <constant>&dollar;(HS&lowbar;PROG)</constant><indexterm><primary>HS&lowbar;PROG</primary></indexterm>
1496               and
1497               <constant>&dollar;(OBJS)</constant><indexterm><primary>OBJS</primary></indexterm>
1498               would not have their final values at the moment
1499               <command>make</command> encountered the rule.  Alas,
1500               <command>make</command> takes a snapshot of their
1501               current values, and wires that snapshot into the rule.
1502               (In contrast, the commands executed when the rule
1503               &ldquo;fires&rdquo; are only substituted at the moment
1504               of firing.)  So, the rule must follow the definitions
1505               given in the <filename>Makefile</filename> itself.</para>
1506             </listitem>
1507
1508             <listitem>
1509               <para>Unlike pattern rules, ordinary rules cannot be
1510               overriden or replaced by subsequent rules for the same
1511               target (at least, not without an error message).
1512               Including ordinary rules in
1513               <filename>boilerplate.mk</filename> would prevent the
1514               user from writing rules for specific targets in specific
1515               cases.</para>
1516             </listitem>
1517
1518             <listitem>
1519               <para>There are a couple of other reasons I've
1520               forgotten, but it doesn't matter too much.</para>
1521             </listitem>
1522           </itemizedlist>
1523         </listitem>
1524       </itemizedlist>
1525     </sect2>
1526
1527     <sect2 id="sec-boiler">
1528       <title>The <filename>mk/boilerplate.mk</filename> file</title>
1529       <indexterm><primary>boilerplate.mk</primary></indexterm>
1530
1531       <para>If you look at
1532       <filename>&dollar;(GHC&lowbar;TOP)/mk/boilerplate.mk</filename>
1533       you will find that it consists of the following sections, each
1534       held in a separate file:</para>
1535
1536       <variablelist>
1537         <varlistentry>
1538           <term><filename>config.mk</filename>
1539             <indexterm><primary>config.mk</primary></indexterm>
1540           </term>
1541           <listitem>
1542             <para>is the build configuration file we discussed at
1543             length in <xref linkend="sec-build-config"/>.</para>
1544           </listitem>
1545         </varlistentry>
1546
1547         <varlistentry>
1548           <term><filename>paths.mk</filename>
1549             <indexterm><primary>paths.mk</primary></indexterm>
1550           </term>
1551           <listitem>
1552             <para>defines <command>make</command> variables for
1553             pathnames and file lists.  This file contains code for
1554             automatically compiling lists of source files and deriving
1555             lists of object files from those.  The results can be
1556             overriden in the <filename>Makefile</filename>, but in
1557             most cases the automatic setup should do the right
1558             thing.</para>
1559             
1560             <para>The following variables may be set in the
1561             <filename>Makefile</filename> to affect how the automatic
1562             source file search is done:</para>
1563
1564             <variablelist>
1565               <varlistentry>
1566                 <term><literal>ALL_DIRS</literal>
1567                   <indexterm><primary><literal>ALL_DIRS</literal></primary></indexterm>
1568                 </term>
1569                 <listitem>
1570                   <para>Set to a list of directories to search in
1571                   addition to the current directory for source
1572                   files.</para>
1573                 </listitem>
1574               </varlistentry>
1575
1576               <varlistentry>
1577                 <term><literal>EXCLUDED_SRCS</literal>
1578                   <indexterm><primary><literal>EXCLUDED_SRCS</literal></primary></indexterm>
1579                 </term>
1580                 <listitem>
1581                   <para>Set to a list of source files (relative to the
1582                   current directory) to omit from the automatic
1583                   search.  The source searching machinery is clever
1584                   enough to know that if you exclude a source file
1585                   from which other sources are derived, then the
1586                   derived sources should also be excluded.  For
1587                   example, if you set <literal>EXCLUDED_SRCS</literal>
1588                   to include <filename>Foo.y</filename>, then
1589                   <filename>Foo.hs</filename> will also be
1590                   excluded.</para>
1591                 </listitem>
1592               </varlistentry>
1593
1594               <varlistentry>
1595                 <term><literal>EXTRA_SRCS</literal>
1596                   <indexterm><primary><literal>EXTRA_SRCS</literal></primary></indexterm>
1597                 </term>
1598                   <listitem>
1599                   <para>Set to a list of extra source files (perhaps
1600                   in directories not listed in
1601                   <literal>ALL_DIRS</literal>) that should be
1602                   considered.</para>
1603                 </listitem>
1604               </varlistentry>
1605             </variablelist>
1606
1607             <para>The results of the automatic source file search are
1608             placed in the following make variables:</para>
1609
1610             <variablelist>
1611               <varlistentry>
1612                 <term><literal>SRCS</literal>
1613                   <indexterm><primary><literal>SRCS</literal></primary></indexterm>
1614                 </term>
1615                 <listitem>
1616                   <para>All source files found, sorted and without
1617                   duplicates, including those which might not exist
1618                   yet but will be derived from other existing sources.
1619                   <literal>SRCS</literal> <emphasis>can</emphasis> be
1620                   overriden if necessary, in which case the variables
1621                   below will follow suit.</para>
1622                 </listitem>
1623               </varlistentry>
1624
1625               <varlistentry>
1626                 <term><literal>HS_SRCS</literal>
1627                   <indexterm><primary><literal>HS_SRCS</literal></primary></indexterm>
1628                 </term>
1629                 <listitem>
1630                   <para>all Haskell source files in the current
1631                   directory, including those derived from other source
1632                   files (eg. Happy sources also give rise to Haskell
1633                   sources).</para>
1634                 </listitem>
1635               </varlistentry>
1636
1637               <varlistentry>
1638                 <term><literal>HS_OBJS</literal>
1639                   <indexterm><primary><literal>HS_OBJS</literal></primary></indexterm>
1640                 </term>
1641                 <listitem>
1642                   <para>Object files derived from
1643                   <literal>HS_SRCS</literal>.</para>
1644                 </listitem>
1645               </varlistentry>
1646
1647               <varlistentry>
1648                 <term><literal>HS_IFACES</literal>
1649                   <indexterm><primary><literal>HS_IFACES</literal></primary></indexterm>
1650                 </term>
1651                 <listitem>
1652                   <para>Interface files (<literal>.hi</literal> files)
1653                   derived from <literal>HS_SRCS</literal>.</para>
1654                 </listitem>
1655               </varlistentry>
1656
1657               <varlistentry>
1658                 <term><literal>C_SRCS</literal>
1659                 <indexterm><primary><literal>C_SRCS</literal></primary></indexterm>
1660                 </term>
1661                 <listitem>
1662                   <para>All C source files found.</para>
1663                 </listitem>
1664               </varlistentry>
1665
1666               <varlistentry>
1667                 <term><literal>C_OBJS</literal>
1668                 <indexterm><primary><literal>C_OBJS</literal></primary></indexterm>
1669                 </term>
1670                 <listitem>
1671                   <para>Object files derived from
1672                   <literal>C_SRCS</literal>.</para>
1673                 </listitem>
1674               </varlistentry>
1675
1676               <varlistentry>
1677                 <term><literal>SCRIPT_SRCS</literal>
1678                 <indexterm><primary><literal>SCRIPT_SRCS</literal></primary></indexterm>
1679                 </term>
1680                 <listitem>
1681                   <para>All script source files found
1682                   (<literal>.lprl</literal> files).</para>
1683                 </listitem>
1684               </varlistentry>
1685
1686               <varlistentry>
1687                 <term><literal>SCRIPT_OBJS</literal>
1688                 <indexterm><primary><literal>SCRIPT_OBJS</literal></primary></indexterm>
1689                 </term>
1690                 <listitem>
1691                   <para><quote>object</quote> files derived from
1692                   <literal>SCRIPT_SRCS</literal>
1693                   (<literal>.prl</literal> files).</para>
1694                 </listitem>
1695               </varlistentry>
1696
1697               <varlistentry>
1698                 <term><literal>HSC_SRCS</literal>
1699                 <indexterm><primary><literal>HSC_SRCS</literal></primary></indexterm>
1700                 </term>
1701                 <listitem>
1702                   <para>All <literal>hsc2hs</literal> source files
1703                   (<literal>.hsc</literal> files).</para>
1704                 </listitem>
1705               </varlistentry>
1706
1707               <varlistentry>
1708                 <term><literal>HAPPY_SRCS</literal>
1709                 <indexterm><primary><literal>HAPPY_SRCS</literal></primary></indexterm>
1710                 </term>
1711                 <listitem>
1712                   <para>All <literal>happy</literal> source files
1713                   (<literal>.y</literal> or <literal>.hy</literal> files).</para>
1714                 </listitem>
1715               </varlistentry>
1716
1717               <varlistentry>
1718                 <term><literal>OBJS</literal>
1719                 <indexterm><primary>OBJS</primary></indexterm>
1720                 </term>
1721                 <listitem>
1722                   <para>the concatenation of
1723                   <literal>&dollar;(HS_OBJS)</literal>,
1724                   <literal>&dollar;(C_OBJS)</literal>, and
1725                   <literal>&dollar;(SCRIPT_OBJS)</literal>.</para>
1726                 </listitem>
1727               </varlistentry>
1728             </variablelist>
1729
1730             <para>Any or all of these definitions can easily be
1731             overriden by giving new definitions in your
1732             <filename>Makefile</filename>.</para>
1733
1734             <para>What, exactly, does <filename>paths.mk</filename>
1735             consider a <quote>source file</quote> to be?  It's based
1736             on the file's suffix (e.g. <filename>.hs</filename>,
1737             <filename>.lhs</filename>, <filename>.c</filename>,
1738             <filename>.hy</filename>, etc), but this is the kind of
1739             detail that changes, so rather than enumerate the source
1740             suffices here the best thing to do is to look in
1741             <filename>paths.mk</filename>.</para>
1742           </listitem>
1743         </varlistentry>
1744
1745         <varlistentry>
1746           <term><filename>opts.mk</filename>
1747             <indexterm><primary>opts.mk</primary></indexterm>
1748           </term>
1749           <listitem>
1750             <para>defines <command>make</command> variables for option
1751             strings to pass to each program. For example, it defines
1752             <constant>HC&lowbar;OPTS</constant><indexterm><primary>HC&lowbar;OPTS</primary></indexterm>,
1753             the option strings to pass to the Haskell compiler.  See
1754             <xref linkend="sec-suffix"/>.</para>
1755           </listitem>
1756         </varlistentry>
1757
1758         <varlistentry>
1759           <term><filename>suffix.mk</filename>
1760             <indexterm><primary>suffix.mk</primary></indexterm>
1761           </term>
1762           <listitem>
1763             <para>defines standard pattern rules&mdash;see <xref
1764             linkend="sec-suffix"/>.</para>
1765           </listitem>
1766         </varlistentry>
1767       </variablelist>
1768
1769       <para>Any of the variables and pattern rules defined by the
1770       boilerplate file can easily be overridden in any particular
1771       <filename>Makefile</filename>, because the boilerplate
1772       <literal>include</literal> comes first.  Definitions after this
1773       <literal>include</literal> directive simply override the default
1774       ones in <filename>boilerplate.mk</filename>.</para>
1775     </sect2>
1776
1777     <sect2 id="sec-platforms">
1778       <title>Platform settings</title>
1779       <indexterm><primary>Platform settings</primary>
1780       </indexterm>
1781
1782       <para>There are three platforms of interest when building GHC:</para>
1783       
1784       <variablelist>
1785         <varlistentry>
1786           <term>The <emphasis>build</emphasis> platform</term>
1787           <listitem>
1788             <para>The platform on which we are doing this build.</para>
1789           </listitem>
1790         </varlistentry>
1791
1792         <varlistentry>
1793           <term>The <emphasis>host</emphasis> platform</term>
1794           <listitem>
1795             <para>The platform on which these binaries will run.</para>
1796           </listitem>
1797         </varlistentry>
1798
1799         <varlistentry>
1800           <term>The <emphasis>target</emphasis> platform</term>
1801           <listitem>
1802             <para>The platform for which this compiler will generate code.</para>
1803           </listitem>
1804         </varlistentry>
1805       </variablelist>
1806       
1807       <para>These platforms are set when running the
1808         <literal>configure</literal> script, using the
1809         <option>--build</option>, <option>--host</option>, and
1810         <option>--target</option> options.  The <filename>mk/config.mk</filename>
1811         file defines several symbols related to the platform settings (see
1812         <filename>mk/config.mk</filename> for details).</para>
1813
1814       <para>We don't currently support build &amp; host being different, because
1815         the build process creates binaries that are both run during the build,
1816         and also installed.</para>
1817
1818       <para>If host and target are different, then we are building a
1819         cross-compiler.  For GHC, this means a compiler
1820         which will generate intermediate .hc files to port to the target
1821         architecture for bootstrapping.  The libraries and stage 2 compiler
1822         will be built as HC files for the target system (see <xref
1823           linkend="sec-porting-ghc" /> for details.</para>
1824
1825       <para>More details on when to use BUILD, HOST or TARGET can be found in
1826         the comments in <filename>config.mk</filename>.</para>
1827     </sect2>
1828
1829     <sect2 id="sec-suffix">
1830       <title>Pattern rules and options</title>
1831       <indexterm><primary>Pattern rules</primary></indexterm>
1832
1833       <para>The file
1834       <filename>suffix.mk</filename><indexterm><primary>suffix.mk</primary></indexterm>
1835       defines standard <emphasis>pattern rules</emphasis> that say how
1836       to build one kind of file from another, for example, how to
1837       build a <filename>.o</filename> file from a
1838       <filename>.c</filename> file.  (GNU <command>make</command>'s
1839       <emphasis>pattern rules</emphasis> are more powerful and easier
1840       to use than Unix <command>make</command>'s <emphasis>suffix
1841       rules</emphasis>.)</para>
1842
1843       <para>Almost all the rules look something like this:</para>
1844
1845 <programlisting>%.o : %.c
1846       $(RM) $@
1847       $(CC) $(CC_OPTS) -c $&#60; -o $@</programlisting>
1848
1849       <para>Here's how to understand the rule.  It says that
1850       <emphasis>something</emphasis><filename>.o</filename> (say
1851       <filename>Foo.o</filename>) can be built from
1852       <emphasis>something</emphasis><filename>.c</filename>
1853       (<filename>Foo.c</filename>), by invoking the C compiler (path
1854       name held in <constant>&dollar;(CC)</constant>), passing to it
1855       the options <constant>&dollar;(CC&lowbar;OPTS)</constant> and
1856       the rule's dependent file of the rule
1857       <literal>&dollar;&lt;</literal> (<filename>Foo.c</filename> in
1858       this case), and putting the result in the rule's target
1859       <literal>&dollar;@</literal> (<filename>Foo.o</filename> in this
1860       case).</para>
1861
1862       <para>Every program is held in a <command>make</command>
1863       variable defined in <filename>mk/config.mk</filename>&mdash;look
1864       in <filename>mk/config.mk</filename> for the complete list.  One
1865       important one is the Haskell compiler, which is called
1866       <constant>&dollar;(HC)</constant>.</para>
1867
1868       <para>Every program's options are are held in a
1869       <command>make</command> variables called
1870       <constant>&lt;prog&gt;&lowbar;OPTS</constant>.  the
1871       <constant>&lt;prog&gt;&lowbar;OPTS</constant> variables are
1872       defined in <filename>mk/opts.mk</filename>.  Almost all of them
1873       are defined like this:</para>
1874
1875 <programlisting>CC_OPTS = \
1876   $(SRC_CC_OPTS) $(WAY$(_way)_CC_OPTS) $($*_CC_OPTS) $(EXTRA_CC_OPTS)</programlisting>
1877
1878       <para>The four variables from which
1879        <constant>CC&lowbar;OPTS</constant> is built have the following
1880       meaning:</para>
1881
1882       <variablelist>
1883         <varlistentry>
1884           <term><constant>SRC&lowbar;CC&lowbar;OPTS</constant><indexterm><primary>SRC&lowbar;CC&lowbar;OPTS</primary></indexterm>:</term>
1885           <listitem>
1886             <para>options passed to all C compilations.</para>
1887           </listitem>
1888         </varlistentry>
1889
1890         <varlistentry>
1891           <term><constant>WAY&lowbar;&lt;way&gt;&lowbar;CC&lowbar;OPTS</constant>:</term>
1892           <listitem>
1893             <para>options passed to C compilations for way
1894             <literal>&lt;way&gt;</literal>. For example,
1895             <constant>WAY&lowbar;mp&lowbar;CC&lowbar;OPTS</constant>
1896             gives options to pass to the C compiler when compiling way
1897             <literal>mp</literal>.  The variable
1898             <constant>WAY&lowbar;CC&lowbar;OPTS</constant> holds
1899             options to pass to the C compiler when compiling the
1900             standard way.  (<xref linkend="sec-ways"/> dicusses
1901             multi-way compilation.)</para>
1902           </listitem>
1903         </varlistentry>
1904
1905         <varlistentry>
1906           <term><constant>&lt;module&gt;&lowbar;CC&lowbar;OPTS</constant>:</term>
1907           <listitem>
1908             <para>options to pass to the C compiler that are specific
1909             to module <literal>&lt;module&gt;</literal>.  For example,
1910             <constant>SMap&lowbar;CC&lowbar;OPTS</constant> gives the
1911             specific options to pass to the C compiler when compiling
1912             <filename>SMap.c</filename>.</para>
1913           </listitem>
1914         </varlistentry>
1915
1916         <varlistentry>
1917           <term><constant>EXTRA&lowbar;CC&lowbar;OPTS</constant><indexterm><primary>EXTRA&lowbar;CC&lowbar;OPTS</primary></indexterm>:</term>
1918           <listitem>
1919             <para>extra options to pass to all C compilations.  This
1920             is intended for command line use, thus:</para>
1921
1922 <screen>$ make libHS.a EXTRA_HC_OPTS="-v"</screen>
1923           </listitem>
1924         </varlistentry>
1925       </variablelist>
1926     </sect2>
1927
1928     <sect2 id="sec-targets">
1929       <title>The main <filename>mk/target.mk</filename> file</title>
1930       <indexterm><primary>target.mk</primary></indexterm>
1931
1932       <para><filename>target.mk</filename> contains canned rules for
1933       all the standard targets described in <xref
1934       linkend="sec-standard-targets"/>.  It is complicated by the fact
1935       that you don't want all of these rules to be active in every
1936       <filename>Makefile</filename>.  Rather than have a plethora of
1937       tiny files which you can include selectively, there is a single
1938       file, <filename>target.mk</filename>, which selectively includes
1939       rules based on whether you have defined certain variables in
1940       your <filename>Makefile</filename>.  This section explains what
1941       rules you get, what variables control them, and what the rules
1942       do.  Hopefully, you will also get enough of an idea of what is
1943       supposed to happen that you can read and understand any weird
1944       special cases yourself.</para>
1945
1946       <variablelist>
1947         <varlistentry>
1948           <term><constant>HS&lowbar;PROG</constant><indexterm><primary>HS&lowbar;PROG</primary></indexterm>.</term>
1949           <listitem>
1950             <para>If <constant>HS&lowbar;PROG</constant> is defined,
1951             you get rules with the following targets:</para>
1952
1953             <variablelist>
1954               <varlistentry>
1955                 <term><filename>HS&lowbar;PROG</filename><indexterm><primary>HS&lowbar;PROG</primary></indexterm></term>
1956                 <listitem>
1957                   <para>itself.  This rule links
1958                   <constant>&dollar;(OBJS)</constant> with the Haskell
1959                   runtime system to get an executable called
1960                   <constant>&dollar;(HS&lowbar;PROG)</constant>.</para>
1961                 </listitem>
1962               </varlistentry>
1963
1964               <varlistentry>
1965                 <term><literal>install</literal><indexterm><primary>install</primary></indexterm></term>
1966                 <listitem>
1967                   <para>installs
1968                   <constant>&dollar;(HS&lowbar;PROG)</constant> in
1969                   <constant>&dollar;(bindir)</constant>.</para>
1970                 </listitem>
1971               </varlistentry>
1972             </variablelist>
1973
1974           </listitem>
1975         </varlistentry>
1976
1977         <varlistentry>
1978           <term><constant>C&lowbar;PROG</constant><indexterm><primary>C&lowbar;PROG</primary></indexterm></term>
1979           <listitem>
1980             <para>is similar to <constant>HS&lowbar;PROG</constant>,
1981             except that the link step links
1982             <constant>&dollar;(C&lowbar;OBJS)</constant> with the C
1983             runtime system.</para>
1984           </listitem>
1985         </varlistentry>
1986
1987         <varlistentry>
1988           <term><constant>LIBRARY</constant><indexterm><primary>LIBRARY</primary></indexterm></term>
1989           <listitem>
1990             <para>is similar to <constant>HS&lowbar;PROG</constant>,
1991             except that it links
1992             <constant>&dollar;(LIB&lowbar;OBJS)</constant> to make the
1993             library archive <constant>&dollar;(LIBRARY)</constant>,
1994             and <literal>install</literal> installs it in
1995             <constant>&dollar;(libdir)</constant>.</para>
1996           </listitem>
1997         </varlistentry>
1998       </variablelist>
1999
2000       <para>Some rules are &ldquo;double-colon&rdquo; rules,
2001       thus</para>
2002
2003 <programlisting>install :: $(HS_PROG)
2004       ...how to install it...</programlisting>
2005
2006       <para>GNU <command>make</command> treats double-colon rules as
2007       separate entities.  If there are several double-colon rules for
2008       the same target it takes each in turn and fires it if its
2009       dependencies say to do so.  This means that you can, for
2010       example, define both <constant>HS&lowbar;PROG</constant> and
2011       <constant>LIBRARY</constant>, which will generate two rules for
2012       <literal>install</literal>.  When you type <command>make
2013       install</command> both rules will be fired, and both the program
2014       and the library will be installed, just as you wanted.</para>
2015     </sect2>
2016
2017     <sect2 id="sec-subdirs">
2018       <title>Recursion</title>
2019       <indexterm><primary>recursion, in makefiles</primary></indexterm>
2020       <indexterm><primary>Makefile, recursing into subdirectories</primary></indexterm>
2021
2022       <para>In leaf <filename>Makefile</filename>s the variable
2023       <constant>SUBDIRS</constant><indexterm><primary>SUBDIRS</primary></indexterm>
2024       is undefined.  In non-leaf <filename>Makefile</filename>s,
2025       <constant>SUBDIRS</constant> is set to the list of
2026       sub-directories that contain subordinate
2027       <filename>Makefile</filename>s.  <emphasis>It is up to you to
2028       set <constant>SUBDIRS</constant> in the
2029       <filename>Makefile</filename>.</emphasis> There is no automation
2030       here&mdash;<constant>SUBDIRS</constant> is too important to
2031       automate.</para>
2032
2033       <para>When <constant>SUBDIRS</constant> is defined,
2034       <filename>target.mk</filename> includes a rather neat rule for
2035       the standard targets (<xref linkend="sec-standard-targets"/> that
2036       simply invokes <command>make</command> recursively in each of
2037       the sub-directories.</para>
2038
2039       <para><emphasis>These recursive invocations are guaranteed to
2040       occur in the order in which the list of directories is specified
2041       in <constant>SUBDIRS</constant>. </emphasis>This guarantee can
2042       be important.  For example, when you say <command>make
2043       boot</command> it can be important that the recursive invocation
2044       of <command>make boot</command> is done in one sub-directory
2045       (the include files, say) before another (the source files).
2046       Generally, put the most independent sub-directory first, and the
2047       most dependent last.</para>
2048     </sect2>
2049
2050     <sect2 id="sec-ways">
2051       <title>Way management</title>
2052       <indexterm><primary>way management</primary></indexterm>
2053
2054       <para>We sometimes want to build essentially the same system in
2055       several different &ldquo;ways&rdquo;.  For example, we want to build GHC's
2056       <literal>Prelude</literal> libraries with and without profiling,
2057       so that there is an appropriately-built library archive to link
2058       with when the user compiles his program.  It would be possible
2059       to have a completely separate build tree for each such &ldquo;way&rdquo;,
2060       but it would be horribly bureaucratic, especially since often
2061       only parts of the build tree need to be constructed in multiple
2062       ways.</para>
2063
2064       <para>Instead, the
2065       <filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>
2066       contains some clever magic to allow you to build several
2067       versions of a system; and to control locally how many versions
2068       are built and how they differ.  This section explains the
2069       magic.</para>
2070
2071       <para>The files for a particular way are distinguished by
2072       munging the suffix.  The <quote>normal way</quote> is always
2073       built, and its files have the standard suffices
2074       <filename>.o</filename>, <filename>.hi</filename>, and so on.
2075       In addition, you can build one or more extra ways, each
2076       distinguished by a <emphasis>way tag</emphasis>.  The object
2077       files and interface files for one of these extra ways are
2078       distinguished by their suffix.  For example, way
2079       <literal>mp</literal> has files
2080       <filename>.mp&lowbar;o</filename> and
2081       <filename>.mp&lowbar;hi</filename>.  Library archives have their
2082       way tag the other side of the dot, for boring reasons; thus,
2083       <filename>libHS&lowbar;mp.a</filename>.</para>
2084
2085       <para>A <command>make</command> variable called
2086       <constant>way</constant> holds the current way tag.
2087       <emphasis><constant>way</constant> is only ever set on the
2088       command line of <command>make</command></emphasis> (usually in
2089       a recursive invocation of <command>make</command> by the
2090       system).  It is never set inside a
2091       <filename>Makefile</filename>.  So it is a global constant for
2092       any one invocation of <command>make</command>.  Two other
2093       <command>make</command> variables,
2094       <constant>way&lowbar;</constant> and
2095       <constant>&lowbar;way</constant> are immediately derived from
2096       <constant>&dollar;(way)</constant> and never altered.  If
2097       <constant>way</constant> is not set, then neither are
2098       <constant>way&lowbar;</constant> and
2099       <constant>&lowbar;way</constant>, and the invocation of
2100       <command>make</command> will build the <quote>normal
2101       way</quote>.  If <constant>way</constant> is set, then the other
2102       two variables are set in sympathy.  For example, if
2103       <constant>&dollar;(way)</constant> is &ldquo;<literal>mp</literal>&rdquo;,
2104       then <constant>way&lowbar;</constant> is set to
2105       &ldquo;<literal>mp&lowbar;</literal>&rdquo; and
2106       <constant>&lowbar;way</constant> is set to
2107       &ldquo;<literal>&lowbar;mp</literal>&rdquo;.  These three variables are
2108       then used when constructing file names.</para>
2109
2110       <para>So how does <command>make</command> ever get recursively
2111       invoked with <constant>way</constant> set?  There are two ways
2112       in which this happens:</para>
2113
2114       <itemizedlist>
2115         <listitem>
2116           <para>For some (but not all) of the standard targets, when
2117           in a leaf sub-directory, <command>make</command> is
2118           recursively invoked for each way tag in
2119           <constant>&dollar;(WAYS)</constant>.  You set
2120           <constant>WAYS</constant> in the
2121           <filename>Makefile</filename> to the list of way tags you
2122           want these targets built for.  The mechanism here is very
2123           much like the recursive invocation of
2124           <command>make</command> in sub-directories (<xref
2125           linkend="sec-subdirs"/>).  It is up to you to set
2126           <constant>WAYS</constant> in your
2127           <filename>Makefile</filename>; this is how you control what
2128           ways will get built.</para>
2129         </listitem>
2130
2131         <listitem>
2132           <para>For a useful collection of targets (such as
2133           <filename>libHS&lowbar;mp.a</filename>,
2134           <filename>Foo.mp&lowbar;o</filename>) there is a rule which
2135           recursively invokes <command>make</command> to make the
2136           specified target, setting the <constant>way</constant>
2137           variable.  So if you say <command>make
2138           Foo.mp&lowbar;o</command> you should see a recursive
2139           invocation <command>make Foo.mp&lowbar;o way=mp</command>,
2140           and <emphasis>in this recursive invocation the pattern rule
2141           for compiling a Haskell file into a <filename>.o</filename>
2142           file will match</emphasis>.  The key pattern rules (in
2143           <filename>suffix.mk</filename>) look like this:
2144
2145 <programlisting>%.$(way_)o : %.lhs
2146       $(HC) $(HC_OPTS) $&#60; -o $@</programlisting>
2147
2148           Neat, eh?</para>
2149         </listitem>
2150
2151         <listitem>
2152           <para>You can invoke <command>make</command> with a
2153           particular <literal>way</literal> setting yourself, in order
2154           to build files related to a particular
2155           <literal>way</literal> in the current directory.  eg.
2156
2157 <screen>$ make way=p</screen>
2158
2159           will build files for the profiling way only in the current
2160           directory. </para>
2161         </listitem>
2162       </itemizedlist>
2163     </sect2>
2164
2165     <sect2>
2166       <title>When the canned rule isn't right</title>
2167
2168       <para>Sometimes the canned rule just doesn't do the right thing.
2169       For example, in the <literal>nofib</literal> suite we want the
2170       link step to print out timing information.  The thing to do here
2171       is <emphasis>not</emphasis> to define
2172       <constant>HS&lowbar;PROG</constant> or
2173       <constant>C&lowbar;PROG</constant>, and instead define a special
2174       purpose rule in your own <filename>Makefile</filename>.  By
2175       using different variable names you will avoid the canned rules
2176       being included, and conflicting with yours.</para>
2177     </sect2>
2178   </sect1>
2179
2180   <sect1 id="building-docs">
2181     <title>Building the documentation</title>
2182
2183     <sect2 id="pre-supposed-doc-tools">
2184       <title>Tools for building the Documentation</title>
2185
2186       <para>The following additional tools are required if you want to
2187       format the documentation that comes with GHC:</para>
2188       
2189       <variablelist>
2190         <varlistentry>
2191           <term>DocBook
2192             <indexterm><primary>pre-supposed: DocBook</primary></indexterm>
2193             <indexterm><primary>DocBook, pre-supposed</primary></indexterm>
2194           </term>
2195           <listitem>
2196             <para>Much of our documentation is written in DocBook XML, instructions
2197             on installing and configuring the DocBook tools are below.</para>
2198           </listitem>
2199         </varlistentry>
2200
2201         <varlistentry>
2202           <term>TeX
2203             <indexterm><primary>pre-supposed: TeX</primary></indexterm>
2204             <indexterm><primary>TeX, pre-supposed</primary></indexterm>
2205           </term>
2206           <listitem>
2207             <para>A decent TeX distribution is required if you want to
2208             produce printable documentation.  We recomment teTeX,
2209             which includes just about everything you need.</para>
2210           </listitem>
2211         </varlistentry>
2212
2213         <varlistentry>
2214           <term>Haddock
2215             <indexterm><primary>Haddock</primary></indexterm>
2216           </term>
2217           <listitem>
2218             <para>Haddock is a Haskell documentation tool that we use
2219             for automatically generating documentation from the
2220             library source code.  To build documentation for the
2221             libraries (<literal>$(GHC&lowbar;TOP)/libraries</literal>) you
2222             should build and install Haddock.  Haddock requires GHC
2223             to build.</para>
2224           </listitem>
2225         </varlistentry>
2226       </variablelist>
2227     </sect2>
2228
2229     <sect2>
2230       <title>Installing the DocBook tools</title>
2231
2232       <sect3>
2233         <title>Installing the DocBook tools on Linux</title>
2234
2235         <para>If you're on a recent RedHat (7.0+) or SuSE (8.1+) system,
2236         you probably have working DocBook tools already installed. The
2237         configure script should detect your setup and you're away.</para>
2238
2239         <para>If you don't have DocBook tools installed, and you are
2240         using a system that can handle RPM packages, you can use <ulink
2241         url="http://rpmfind.net/">Rpmfind.net</ulink> to find suitable
2242         packages for your system. Search for the packages
2243         <literal>docbook-dtd</literal>,
2244         <literal>docbook-xsl-stylesheets</literal>,
2245         <literal>libxslt</literal>,
2246         <literal>libxml2</literal>,
2247         <literal>fop</literal>,
2248         <literal>xmltex</literal>, and
2249         <literal>dvips</literal>.</para>
2250       </sect3>
2251     
2252       <sect3>
2253         <title>Installing DocBook on FreeBSD</title>
2254
2255         <para>On FreeBSD systems, the easiest way to get DocBook up
2256         and running is to install it from the ports tree or a
2257         pre-compiled package (packages are available from your local
2258         FreeBSD mirror site).</para>
2259
2260         <para>To use the ports tree, do this:
2261 <screen>$ cd /usr/ports/textproc/docproj
2262 $ make install</screen>
2263         This installs the FreeBSD documentation project tools, which
2264         includes everything needed to format the GHC
2265         documentation.</para>
2266       </sect3>
2267
2268       <sect3>
2269         <title>Installing from binaries on Windows</title>
2270         
2271         <para>Probably the fastest route to a working DocBook environment on
2272         Windows is to install <ulink url="http://www.cygwin.com/">Cygwin</ulink>
2273         with the complete <literal>Doc</literal> category. If you are using
2274         <ulink url="http://www.mingw.org/">MinGW</ulink> for compilation, you
2275         have to help <command>configure</command> a little bit: Set the
2276         environment variables <envar>XmllintCmd</envar> and
2277         <envar>XsltprocCmd</envar> to the paths of the Cygwin executables
2278         <command>xmllint</command> and <command>xsltproc</command>,
2279         respectively, and set <envar>fp_cv_dir_docbook_xsl</envar> to the path
2280         of the directory where the XSL stylesheets are installed,
2281         e.g. <filename>c:/cygwin/usr/share/docbook-xsl</filename>.
2282         </para>
2283
2284         <para>If you want to build HTML Help, you have to install the
2285         <ulink url="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/htmlhelp/html/hworiHTMLHelpStartPage.asp">HTML Help SDK</ulink>,
2286         too, and make sure that <command>hhc</command> is in your <envar>PATH</envar>.</para>
2287       </sect3>
2288
2289     </sect2>
2290
2291     <sect2>
2292       <title>Configuring the DocBook tools</title>
2293
2294       <para>Once the DocBook tools are installed, the configure script
2295       will detect them and set up the build system accordingly. If you
2296       have a system that isn't supported, let us know, and we'll try
2297       to help.</para>
2298     </sect2>
2299
2300     <sect2>
2301       <title>Building the documentation</title>
2302
2303       <para>To build documentation in a certain format, you can
2304       say, for example,</para>
2305
2306 <screen>$ make html</screen>
2307
2308       <para>to build HTML documentation below the current directory.
2309       The available formats are: <literal>dvi</literal>,
2310       <literal>ps</literal>, <literal>pdf</literal>,
2311       <literal>html</literal>, and <literal>rtf</literal>.  Note that
2312       not all documentation can be built in all of these formats: HTML
2313       documentation is generally supported everywhere, and DocBook
2314       documentation might support the other formats (depending on what
2315       other tools you have installed).</para>
2316
2317       <para>All of these targets are recursive; that is, saying
2318       <literal>make html</literal> will make HTML docs for all the
2319       documents recursively below the current directory.</para>
2320
2321       <para>Because there are many different formats that the DocBook
2322       documentation can be generated in, you have to select which ones
2323       you want by setting the <literal>XMLDocWays</literal> variable
2324       to a list of them.  For example, in
2325       <filename>build.mk</filename> you might have a line:</para>
2326
2327 <screen>XMLDocWays = html ps</screen>
2328
2329       <para>This will cause the documentation to be built in the requested
2330       formats as part of the main build (the default is not to build
2331       any documentation at all).</para>
2332     </sect2>
2333
2334     <sect2>
2335       <title>Installing the documentation</title>
2336
2337       <para>To install the documentation, use:</para>
2338
2339 <screen>$ make install-docs</screen>
2340
2341       <para>This will install the documentation into
2342       <literal>$(datadir)</literal> (which defaults to
2343       <literal>$(prefix)/share</literal>).  The exception is HTML
2344       documentation, which goes into
2345       <literal>$(datadir)/html</literal>, to keep things tidy.</para>
2346
2347       <para>Note that unless you set <literal>$(XMLDocWays)</literal>
2348       to a list of formats, the <literal>install-docs</literal> target
2349       won't do anything for DocBook XML documentation.</para>
2350     </sect2>
2351
2352   </sect1>
2353     
2354
2355   <sect1 id="sec-porting-ghc">
2356     <title>Porting GHC</title>
2357
2358     <para>This section describes how to port GHC to a currenly
2359     unsupported platform.  To avoid confusion, when we say
2360     &ldquo;architecture&rdquo; we are referring to the processor, and
2361     we use the term &ldquo;platform&rdquo; to refer to the combination
2362     of architecture and operating system.</para>
2363
2364     <para>There are two distinct porting scenarios:</para>
2365
2366     <itemizedlist>
2367       <listitem>
2368         <para>Your platform is already supported, but you want to
2369         compile up GHC using just a C compiler.  This is a
2370         straightforward bootstrap from HC files, and is described in
2371         <xref linkend="sec-booting-from-hc" />.</para>
2372       </listitem>
2373       
2374       <listitem>
2375         <para>Your platform isn't supported by GHC.  You will need to
2376         do an <emphasis>unregisterised bootstrap</emphasis>, proceed
2377         to <xref linkend="unregisterised-porting"/>.</para>
2378       </listitem>
2379     </itemizedlist>
2380     
2381     <sect2 id="sec-booting-from-hc">
2382       <title>Booting/porting from C (<filename>.hc</filename>) files</title>
2383
2384       <indexterm><primary>building GHC from .hc files</primary></indexterm>
2385       <indexterm><primary>booting GHC from .hc files</primary></indexterm>
2386       <indexterm><primary>porting GHC</primary></indexterm>
2387
2388       <para>Bootstrapping GHC on a system without GHC already
2389       installed is achieved by taking the intermediate C files (known
2390       as HC files) from another GHC compilation, compiling them using gcc to
2391         get a working GHC.</para>
2392
2393       <para><emphasis>NOTE: GHC versions 5.xx were hard to bootstrap
2394       from C.  We recommend using GHC 6.0.1 or
2395       later.</emphasis></para>
2396
2397       <para>HC files are platform-dependent, so you have to get a set
2398         that were generated on <emphasis>the same platform</emphasis>.
2399         There may be some supplied on the GHC download page, otherwise
2400         you'll have to compile some up yourself.</para>
2401
2402       <para>The following steps should result in a working GHC build
2403       with full libraries:</para>
2404
2405       <itemizedlist>
2406         <listitem>
2407           <para>Make a set of HC files.  On an identical system with
2408           GHC already installed, get a GHC source tree and put the
2409           following in <literal>mk/build.mk</literal>:</para>
2410
2411 <programlisting>
2412 SRC_HC_OPTS     = -H32m -O -fasm -Rghc-timing -keep-hc-files
2413 GhcLibHcOpts    = -O
2414 GhcLibWays      =
2415 SplitObjs       = NO
2416 </programlisting>
2417
2418           <para>Build GHC as normal, and then <literal>make
2419           hc-file-bundle Project=ghc</literal> to creates the tar file
2420           containing the hc files.</para>
2421         </listitem>
2422
2423         <listitem>
2424           <para>On the target system, unpack the HC files on top of a
2425           fresh source tree (make sure the source tree version matches
2426           the version of the HC files <emphasis>exactly</emphasis>!).
2427           This will place matching <filename>.hc</filename> files next
2428           to the corresponding Haskell source
2429           (<filename>.hs</filename> or <filename>.lhs</filename>) in
2430           the compiler subdirectory <filename>ghc/compiler</filename>
2431           and in the libraries (subdirectories of
2432           <literal>libraries</literal>).</para>
2433         </listitem>
2434
2435         <listitem>
2436           <para>The actual build process is fully automated by the
2437           <filename>hc-build</filename> script located in the
2438           <filename>distrib</filename> directory.  If you eventually
2439           want to install GHC into the directory
2440           <replaceable>dir</replaceable>, the following
2441           command will execute the whole build process (it won't
2442           install yet):</para>
2443
2444 <screen>$ distrib/hc-build --prefix=<replaceable>dir</replaceable></screen>
2445 <indexterm><primary>--hc-build</primary></indexterm>
2446
2447           <para>By default, the installation directory is
2448           <filename>/usr/local</filename>.  If that is what you want,
2449           you may omit the argument to <filename>hc-build</filename>.
2450           Generally, any option given to <filename>hc-build</filename>
2451           is passed through to the configuration script
2452           <filename>configure</filename>.  If
2453           <filename>hc-build</filename> successfully completes the
2454           build process, you can install the resulting system, as
2455           normal, with</para>
2456
2457 <screen>$ make install</screen>
2458         </listitem>
2459       </itemizedlist>
2460     </sect2>
2461
2462     <sect2 id="unregisterised-porting">
2463       <title>Porting GHC to a new platform</title>
2464       
2465       <para>The first step in porting to a new platform is to get an
2466       <firstterm>unregisterised</firstterm> build working.  An
2467       unregisterised build is one that compiles via vanilla C only.
2468       By contrast, a registerised build uses the following
2469       architecture-specific hacks for speed:</para>
2470
2471       <itemizedlist>
2472         <listitem>
2473           <para>Global register variables: certain abstract machine
2474           <quote>registers</quote> are mapped to real machine
2475           registers, depending on how many machine registers are
2476           available (see
2477           <filename>ghc/includes/MachRegs.h</filename>).</para>
2478         </listitem>
2479
2480         <listitem>
2481           <para>Assembly-mangling: when compiling via C, we feed the
2482           assembly generated by gcc though a Perl script known as the
2483           <firstterm>mangler</firstterm> (see
2484           <filename>ghc/driver/mangler/ghc-asm.lprl</filename>).  The
2485           mangler rearranges the assembly to support tail-calls and
2486           various other optimisations.</para>
2487         </listitem>
2488       </itemizedlist>
2489
2490       <para>In an unregisterised build, neither of these hacks are
2491       used &mdash; the idea is that the C code generated by the
2492       compiler should compile using gcc only.  The lack of these
2493       optimisations costs about a factor of two in performance, but
2494       since unregisterised compilation is usually just a step on the
2495       way to a full registerised port, we don't mind too much.</para>
2496
2497       <para>You should go through this process even if your
2498       architecture is already has registerised support in GHC, but
2499       your OS currently isn't supported.  In this case you probably
2500       won't need to port any of the architecture-specific parts of the
2501       code, and you can proceed straight from the unregisterised build
2502       to build a registerised compiler.</para>
2503
2504       <para>Notes on GHC portability in general: we've tried to stick
2505       to writing portable code in most parts of the system, so it
2506       should compile on any POSIXish system with gcc, but in our
2507       experience most systems differ from the standards in one way or
2508       another.  Deal with any problems as they arise - if you get
2509       stuck, ask the experts on
2510       <email>glasgow-haskell-users@haskell.org</email>.</para>
2511         
2512       <para>Lots of useful information about the innards of GHC is
2513       available in the <ulink
2514       url="http://www.cse.unsw.edu.au/~chak/haskell/ghc/comm/">GHC
2515       Commentary</ulink>, which might be helpful if you run into some
2516       code which needs tweaking for your system.</para>
2517
2518       <sect3>
2519         <title>Cross-compiling to produce an unregisterised GHC</title>
2520
2521         <para>NOTE!  These instructions apply to GHC 6.4 and (hopefully)
2522           later.  If you need instructions for an earlier version of GHC, try
2523           to get hold of the version of this document that was current at the
2524           time.  It should be available from the appropriate download page on
2525           the <ulink
2526             url="http://www.haskell.org/ghc/">GHC&nbsp;homepage</ulink>.</para>  
2527
2528         <para>In this section, we explain how to bootstrap GHC on a
2529         new platform, using unregisterised intermediate C files.  We
2530         haven't put a great deal of effort into automating this
2531         process, for two reasons: it is done very rarely, and the
2532         process usually requires human intervention to cope with minor
2533         porting issues anyway.</para>
2534
2535         <para>The following step-by-step instructions should result in
2536         a fully working, albeit unregisterised, GHC.  Firstly, you
2537         need a machine that already has a working GHC (we'll call this
2538         the <firstterm>host</firstterm> machine), in order to
2539         cross-compile the intermediate C files that we will use to
2540         bootstrap the compiler on the <firstterm>target</firstterm>
2541         machine.</para>
2542
2543         <itemizedlist>
2544           <listitem>
2545             <para>On the target machine:</para>
2546
2547           <itemizedlist>
2548             <listitem>
2549               <para>Unpack a source tree (preferably a released
2550               version).  We will call the path to the root of this
2551               tree <replaceable>T</replaceable>.</para>
2552             </listitem>
2553
2554             <listitem>
2555 <screen>$ cd <replaceable>T</replaceable>
2556 $ ./configure --enable-hc-boot --enable-hc-boot-unregisterised</screen>
2557
2558               <para>You might need to update
2559               <filename>configure.in</filename> to recognise the new
2560               platform, and re-generate
2561               <filename>configure</filename> with
2562               <literal>autoreconf</literal>.</para>
2563             </listitem>
2564   
2565             <listitem>
2566 <screen>$ cd <replaceable>T</replaceable>/ghc/includes
2567 $ make</screen>
2568             </listitem>
2569           </itemizedlist>
2570           </listitem>
2571
2572           <listitem>
2573             <para>On the host machine:</para>
2574               
2575           <itemizedlist>
2576             <listitem>
2577               <para>Unpack a source tree (same released version).  Call
2578               this directory <replaceable>H</replaceable>.</para>
2579             </listitem>
2580  
2581             <listitem>
2582 <screen>$ cd <replaceable>H</replaceable>
2583 $ ./configure</screen>
2584             </listitem>
2585
2586             <listitem>
2587               <para>Create
2588               <filename><replaceable>H</replaceable>/mk/build.mk</filename>,
2589               with the following contents:</para>
2590
2591 <programlisting>GhcUnregisterised = YES
2592 GhcLibHcOpts = -O -fvia-C -keep-hc-files
2593 GhcRtsHcOpts = -keep-hc-files
2594 GhcLibWays =
2595 SplitObjs = NO
2596 GhcWithNativeCodeGen = NO
2597 GhcWithInterpreter = NO
2598 GhcStage1HcOpts = -O
2599 GhcStage2HcOpts = -O -fvia-C -keep-hc-files
2600 SRC_HC_OPTS += -H32m
2601 GhcBootLibs = YES</programlisting>
2602             </listitem>
2603
2604             <listitem>
2605               <para>Edit
2606               <filename><replaceable>H</replaceable>/mk/config.mk</filename>:</para>
2607               <itemizedlist>
2608                 <listitem>
2609                   <para>change <literal>TARGETPLATFORM</literal>
2610                   appropriately, and set the variables involving
2611                   <literal>TARGET</literal> or
2612                   <literal>Target</literal> to the correct values for
2613                   the target platform.  This step is necessary because
2614                   currently <literal>configure</literal> doesn't cope
2615                   with specifying different values for the
2616                   <literal>--host</literal> and
2617                   <literal>--target</literal> flags.</para>
2618                 </listitem>
2619                 <listitem>
2620                   <para>copy <literal>LeadingUnderscore</literal>
2621                   setting from target.</para>
2622                 </listitem>
2623               </itemizedlist>
2624             </listitem>
2625
2626             <listitem>
2627               <para>Copy
2628               <filename><replaceable>T</replaceable>/ghc/includes/ghcautoconf.h</filename>, <filename><replaceable>T</replaceable>/ghc/includes/DerivedConstants.h</filename>, and <filename><replaceable>T</replaceable>/ghc/includes/GHCConstants.h</filename>
2629               to
2630               <filename><replaceable>H</replaceable>/ghc/includes</filename>.
2631               Note that we are building on the host machine, using the
2632               target machine's configuration files.  This
2633               is so that the intermediate C files generated here will
2634               be suitable for compiling on the target system.</para>
2635             </listitem>
2636
2637               <listitem>
2638                 <para>Touch the generated configuration files, just to make
2639                 sure they don't get replaced during the build:</para>
2640 <screen>$ cd <filename><replaceable>H</replaceable></filename>/ghc/includes
2641 $ touch ghcautoconf.h DerivedConstants.h GHCConstants.h mkDerivedConstants.c
2642 $ touch mkDerivedConstantsHdr mkDerivedConstants.o mkGHCConstants mkGHCConstants.o</screen>
2643
2644                 <para>Note: it has been reported that these files still get
2645                   overwritten during the next stage.  We have installed a fix
2646                   for this in GHC 6.4.2, but if you are building a version
2647                   before that you need to watch out for these files getting
2648                   overwritte by the <literal>Makefile</literal> in
2649                   <literal>ghc/includes</literal>.  If your system supports
2650                   it, you might be able to prevent it by making them
2651                   immutable:</para>
2652 <screen>$ chflags uchg  ghc/includes/{ghcautoconf.h,DerivedConstants.h,GHCConstants.h}</screen>
2653               </listitem>
2654
2655             <listitem>
2656                 <para>Now build the compiler:</para>
2657 <screen>$ cd <replaceable>H</replaceable>/glafp-utils &amp;&amp; make boot &amp;&amp; make
2658 $ cd <replaceable>H</replaceable>/ghc &amp;&amp; make boot &amp;&amp; make</screen>
2659               <para>Don't worry if the build falls over in the RTS, we
2660               don't need the RTS yet.</para>
2661             </listitem>
2662
2663             <listitem>
2664 <screen>$ cd <replaceable>H</replaceable>/libraries
2665 $ make boot &amp;&amp; make</screen>
2666             </listitem>
2667
2668             <listitem>
2669 <screen>$ cd <replaceable>H</replaceable>/ghc/compiler
2670 $ make boot stage=2 &amp;&amp; make stage=2</screen>
2671             </listitem>
2672
2673             <listitem>
2674 <screen>$ cd <replaceable>H</replaceable>/compat
2675 $ make clean
2676 $ rm .depend
2677 $ make boot UseStage1=YES EXTRA_HC_OPTS='-O -fvia-C -keep-hc-files'
2678 $ cd <replaceable>H</replaceable>/utils
2679 $ make clean
2680 $ make -k UseStage1=YES EXTRA_HC_OPTS='-O -fvia-C -keep-hc-files'</screen>
2681             </listitem>
2682             
2683             <listitem>
2684 <screen>$ cd <replaceable>H</replaceable>
2685 $ make hc-file-bundle Project=Ghc</screen>
2686             </listitem>
2687
2688             <listitem>
2689               <para>copy
2690               <filename><replaceable>H</replaceable>/*-hc.tar.gz</filename>
2691               to <filename><replaceable>T</replaceable>/..</filename>.</para>
2692             </listitem>
2693           </itemizedlist>
2694           </listitem>
2695
2696           <listitem>
2697             <para>On the target machine:</para>
2698
2699             <para>At this stage we simply need to bootstrap a compiler
2700             from the intermediate C files we generated above.  The
2701             process of bootstrapping from C files is automated by the
2702             script in <literal>distrib/hc-build</literal>, and is
2703             described in <xref linkend="sec-booting-from-hc"/>.</para>
2704
2705 <screen>$ ./distrib/hc-build --enable-hc-boot-unregisterised</screen>
2706
2707             <para>However, since this is a bootstrap on a new machine,
2708             the automated process might not run to completion the
2709             first time.  For that reason, you might want to treat the
2710             <literal>hc-build</literal> script as a list of
2711             instructions to follow, rather than as a fully automated
2712             script.  This way you'll be able to restart the process
2713             part-way through if you need to fix anything on the
2714             way.</para>
2715
2716             <para>Don't bother with running
2717             <literal>make&nbsp;install</literal> in the newly
2718             bootstrapped tree; just use the compiler in that tree to
2719             build a fresh compiler from scratch, this time without
2720             booting from C files.  Before doing this, you might want
2721             to check that the bootstrapped compiler is generating
2722             working binaries:</para>
2723
2724 <screen>$ cat >hello.hs
2725 main = putStrLn "Hello World!\n"
2726 ^D
2727 $ <replaceable>T</replaceable>/ghc/compiler/ghc-inplace hello.hs -o hello
2728 $ ./hello
2729 Hello World!</screen>
2730
2731             <para>Once you have the unregisterised compiler up and
2732             running, you can use it to start a registerised port.  The
2733             following sections describe the various parts of the
2734             system that will need architecture-specific tweaks in
2735             order to get a registerised build going.</para>
2736
2737           </listitem>
2738         </itemizedlist>
2739       </sect3>
2740
2741       <sect3>
2742         <title>Porting the RTS</title>
2743         
2744         <para>The following files need architecture-specific code for a
2745         registerised build:</para>
2746
2747         <variablelist>
2748           <varlistentry>
2749             <term><filename>ghc/includes/MachRegs.h</filename>
2750             <indexterm><primary><filename>MachRegs.h</filename></primary></indexterm>
2751             </term>
2752             <listitem>
2753               <para>Defines the STG-register to machine-register
2754               mapping.  You need to know your platform's C calling
2755               convention, and which registers are generally available
2756               for mapping to global register variables.  There are
2757               plenty of useful comments in this file.</para>
2758             </listitem>
2759           </varlistentry>
2760           <varlistentry>
2761             <term><filename>ghc/includes/TailCalls.h</filename>
2762             <indexterm><primary><filename>TailCalls.h</filename></primary></indexterm>
2763             </term>
2764             <listitem>
2765               <para>Macros that cooperate with the mangler (see <xref
2766               linkend="sec-mangler"/>) to make proper tail-calls
2767               work.</para>
2768             </listitem>
2769           </varlistentry>
2770           <varlistentry>
2771             <term><filename>ghc/rts/Adjustor.c</filename>
2772             <indexterm><primary><filename>Adjustor.c</filename></primary></indexterm>
2773             </term>
2774             <listitem>
2775               <para>Support for
2776               <literal>foreign&nbsp;import&nbsp;"wrapper"</literal>
2777               (aka
2778               <literal>foreign&nbsp;export&nbsp;dynamic</literal>).
2779               Not essential for getting GHC bootstrapped, so this file
2780               can be deferred until later if necessary.</para>
2781             </listitem>
2782           </varlistentry>
2783           <varlistentry>
2784             <term><filename>ghc/rts/StgCRun.c</filename>
2785             <indexterm><primary><filename>StgCRun.c</filename></primary></indexterm>
2786             </term>
2787             <listitem>
2788               <para>The little assembly layer between the C world and
2789               the Haskell world.  See the comments and code for the
2790               other architectures in this file for pointers.</para>
2791             </listitem>
2792           </varlistentry>
2793           <varlistentry>
2794             <term><filename>ghc/rts/MBlock.h</filename>
2795               <indexterm><primary><filename>MBlock.h</filename></primary></indexterm>
2796             </term>
2797             <term><filename>ghc/rts/MBlock.c</filename>
2798               <indexterm><primary><filename>MBlock.c</filename></primary></indexterm>
2799             </term>
2800             <listitem>
2801               <para>These files are really OS-specific rather than
2802               architecture-specific.  In <filename>MBlock.h</filename>
2803               is specified the absolute location at which the RTS
2804               should try to allocate memory on your platform (try to
2805               find an area which doesn't conflict with code or dynamic
2806               libraries).  In <filename>Mblock.c</filename> you might
2807               need to tweak the call to <literal>mmap()</literal> for
2808               your OS.</para>
2809             </listitem>
2810           </varlistentry>
2811         </variablelist>
2812       </sect3>
2813
2814       <sect3 id="sec-mangler">
2815         <title>The mangler</title>
2816         
2817         <para>The mangler is an evil Perl-script
2818         (<filename>ghc/driver/mangler/ghc-asm.lprl</filename>) that
2819         rearranges the assembly code output from gcc to do two main
2820         things:</para>
2821
2822         <itemizedlist>
2823           <listitem>
2824             <para>Remove function prologues and epilogues, and all
2825             movement of the C stack pointer.  This is to support
2826             tail-calls: every code block in Haskell code ends in an
2827             explicit jump, so we don't want the C-stack overflowing
2828             while we're jumping around between code blocks.</para>
2829           </listitem>
2830           <listitem>
2831             <para>Move the <firstterm>info table</firstterm> for a
2832             closure next to the entry code for that closure.  In
2833             unregisterised code, info tables contain a pointer to the
2834             entry code, but in registerised compilation we arrange
2835             that the info table is shoved right up against the entry
2836             code, and addressed backwards from the entry code pointer
2837             (this saves a word in the info table and an extra
2838             indirection when jumping to the closure entry
2839             code).</para>
2840           </listitem>
2841         </itemizedlist>
2842
2843         <para>The mangler is abstracted to a certain extent over some
2844         architecture-specific things such as the particular assembler
2845         directives used to herald symbols.  Take a look at the
2846         definitions for other architectures and use these as a
2847         starting point.</para>
2848       </sect3>
2849
2850       <sect3>
2851         <title>The splitter</title>
2852
2853         <para>The splitter is another evil Perl script
2854         (<filename>ghc/driver/split/ghc-split.lprl</filename>).  It
2855         cooperates with the mangler to support object splitting.
2856         Object splitting is what happens when the
2857         <option>-split-objs</option> option is passed to GHC: the
2858         object file is split into many smaller objects.  This feature
2859         is used when building libraries, so that a program statically
2860         linked against the library will pull in less of the
2861         library.</para>
2862
2863         <para>The splitter has some platform-specific stuff; take a
2864         look and tweak it for your system.</para>
2865       </sect3>
2866
2867       <sect3>
2868         <title>The native code generator</title>
2869
2870         <para>The native code generator isn't essential to getting a
2871         registerised build going, but it's a desirable thing to have
2872         because it can cut compilation times in half.  The native code
2873         generator is described in some detail in the <ulink
2874         url="http://www.cse.unsw.edu.au/~chak/haskell/ghc/comm/">GHC
2875         commentary</ulink>.</para>
2876       </sect3>
2877
2878       <sect3>
2879         <title>GHCi</title>
2880
2881         <para>To support GHCi, you need to port the dynamic linker
2882         (<filename>$(GHC&lowbar;TOP)/rts/Linker.c</filename>).  The
2883         linker currently supports the ELF and PEi386 object file
2884         formats - if your platform uses one of these then things will
2885         be significantly easier.  The majority of Unix platforms use
2886         the ELF format these days.  Even so, there are some
2887         machine-specific parts of the ELF linker: for example, the
2888         code for resolving particular relocation types is
2889         machine-specific, so some porting of this code to your
2890         architecture and/or OS will probaly be necessary.</para>
2891         
2892         <para>If your system uses a different object file format, then
2893         you have to write a linker &mdash; good luck!</para>
2894       </sect3>
2895     </sect2>
2896
2897   </sect1>
2898
2899 <sect1 id="sec-build-pitfalls">
2900 <title>Known pitfalls in building Glasgow Haskell
2901
2902 <indexterm><primary>problems, building</primary></indexterm>
2903 <indexterm><primary>pitfalls, in building</primary></indexterm>
2904 <indexterm><primary>building pitfalls</primary></indexterm></title>
2905
2906 <para>
2907 WARNINGS about pitfalls and known &ldquo;problems&rdquo;:
2908 </para>
2909
2910 <para>
2911
2912 <orderedlist>
2913 <listitem>
2914
2915 <para>
2916 One difficulty that comes up from time to time is running out of space
2917 in <literal>TMPDIR</literal>.  (It is impossible for the configuration stuff to
2918 compensate for the vagaries of different sysadmin approaches to temp
2919 space.)
2920 <indexterm><primary>tmp, running out of space in</primary></indexterm>
2921
2922 The quickest way around it is <command>setenv TMPDIR /usr/tmp</command><indexterm><primary>TMPDIR</primary></indexterm> or
2923 even <command>setenv TMPDIR .</command> (or the equivalent incantation with your shell
2924 of choice).
2925
2926 The best way around it is to say
2927
2928 <programlisting>export TMPDIR=&#60;dir&#62;</programlisting>
2929
2930 in your <filename>build.mk</filename> file.  Then GHC and the other
2931 tools will use the appropriate directory in all cases.
2932
2933
2934 </para>
2935 </listitem>
2936 <listitem>
2937
2938 <para>
2939 In compiling some support-code bits, e.g., in <filename>ghc/rts/gmp</filename> and even
2940 in <filename>ghc/lib</filename>, you may get a few C-compiler warnings.  We think these
2941 are OK.
2942
2943 </para>
2944 </listitem>
2945 <listitem>
2946
2947 <para>
2948 When compiling via C, you'll sometimes get &ldquo;warning: assignment from
2949 incompatible pointer type&rdquo; out of GCC.  Harmless.
2950
2951 </para>
2952 </listitem>
2953 <listitem>
2954
2955 <para>
2956 Similarly, <command>ar</command>chiving warning messages like the following are not
2957 a problem:
2958
2959 <screen>ar: filename GlaIOMonad__1_2s.o truncated to GlaIOMonad_
2960 ar: filename GlaIOMonad__2_2s.o truncated to GlaIOMonad_
2961 ...</screen>
2962
2963
2964 </para>
2965 </listitem>
2966 <listitem>
2967
2968 <para>
2969  In compiling the compiler proper (in <filename>compiler/</filename>), you <emphasis>may</emphasis>
2970 get an &ldquo;Out of heap space&rdquo; error message.  These can vary with the
2971 vagaries of different systems, it seems.  The solution is simple:
2972
2973
2974 <itemizedlist>
2975 <listitem>
2976
2977 <para>
2978  If you're compiling with GHC 4.00 or later, then the
2979 <emphasis>maximum</emphasis> heap size must have been reached.  This
2980 is somewhat unlikely, since the maximum is set to 64M by default.
2981 Anyway, you can raise it with the
2982 <option>-optCrts-M&lt;size&gt;</option> flag (add this flag to
2983 <constant>&lt;module&gt;&lowbar;HC&lowbar;OPTS</constant>
2984 <command>make</command> variable in the appropriate
2985 <filename>Makefile</filename>).
2986
2987 </para>
2988 </listitem>
2989 <listitem>
2990
2991 <para>
2992  For GHC &#60; 4.00, add a suitable <option>-H</option> flag to the <filename>Makefile</filename>, as
2993 above.
2994
2995 </para>
2996 </listitem>
2997
2998 </itemizedlist>
2999
3000
3001 and try again: <command>make</command>.  (see <xref linkend="sec-suffix"/> for information about
3002 <constant>&lt;module&gt;&lowbar;HC&lowbar;OPTS</constant>.)
3003
3004 Alternatively, just cut to the chase:
3005
3006 <screen>$ cd ghc/compiler
3007 $ make EXTRA_HC_OPTS=-optCrts-M128M</screen>
3008
3009
3010 </para>
3011 </listitem>
3012 <listitem>
3013
3014 <para>
3015 If you try to compile some Haskell, and you get errors from GCC about
3016 lots of things from <filename>/usr/include/math.h</filename>, then your GCC was
3017 mis-installed.  <command>fixincludes</command> wasn't run when it should've been.
3018
3019 As <command>fixincludes</command> is now automagically run as part of GCC installation,
3020 this bug also suggests that you have an old GCC.
3021
3022
3023 </para>
3024 </listitem>
3025 <listitem>
3026
3027 <para>
3028 You <emphasis>may</emphasis> need to re-<command>ranlib</command><indexterm><primary>ranlib</primary></indexterm> your libraries (on Sun4s).
3029
3030
3031 <screen>$ cd $(libdir)/ghc-x.xx/sparc-sun-sunos4
3032 $ foreach i ( `find . -name '*.a' -print` ) # or other-shell equiv...
3033 ?    ranlib $i
3034 ?    # or, on some machines: ar s $i
3035 ? end</screen>
3036
3037
3038 We'd be interested to know if this is still necessary.
3039
3040
3041 </para>
3042 </listitem>
3043 <listitem>
3044
3045 <para>
3046 GHC's sources go through <command>cpp</command> before being compiled, and <command>cpp</command> varies
3047 a bit from one Unix to another.  One particular gotcha is macro calls
3048 like this:
3049
3050
3051 <programlisting>SLIT("Hello, world")</programlisting>
3052
3053
3054 Some <command>cpp</command>s treat the comma inside the string as separating two macro
3055 arguments, so you get
3056
3057
3058 <screen>:731: macro `SLIT' used with too many (2) args</screen>
3059
3060
3061 Alas, <command>cpp</command> doesn't tell you the offending file!
3062
3063 Workaround: don't put weird things in string args to <command>cpp</command> macros.
3064 </para>
3065 </listitem>
3066
3067 </orderedlist>
3068
3069 </para>
3070
3071 </sect1>
3072
3073
3074 <sect1 id="platforms"><title>Platforms, scripts, and file names</title>
3075 <para>
3076 GHC is designed both to be built, and to run, on both Unix and Windows.  This flexibility
3077 gives rise to a good deal of brain-bending detail, which we have tried to collect in this chapter.
3078 </para>
3079
3080 <sect2 id="cygwin-and-mingw"><title>Windows platforms: Cygwin, MSYS, and MinGW</title>
3081
3082 <para> The build system is built around Unix-y makefiles.  Because it's not native,
3083 the Windows situation for building GHC is particularly confusing.  This section
3084 tries to clarify, and to establish terminology.</para>
3085
3086 <sect3 id="ghc-mingw"><title>MinGW</title>
3087
3088 <para> <ulink url="http://www.mingw.org">MinGW (Minimalist GNU for Windows)</ulink> 
3089 is a collection of header
3090 files and import libraries that allow one to use <command>gcc</command> and produce
3091 native Win32 programs that do not rely on any third-party DLLs. The
3092 current set of tools include GNU Compiler Collection (<command>gcc</command>), GNU Binary
3093 Utilities (Binutils), GNU debugger (Gdb), GNU make, and a assorted
3094 other utilities. 
3095 </para>
3096
3097 <para> The down-side of MinGW is that the MinGW libraries do not support anything like the full
3098 Posix interface.  
3099 </para>
3100 </sect3>
3101
3102 <sect3 id="ghc-cygwin"><title>Cygwin and MSYS</title>
3103
3104 <para>You can't use the MinGW to <emphasis>build</emphasis> GHC, because MinGW doesn't have a shell,
3105 or the standard Unix commands such as <command>mv</command>, <command>rm</command>,
3106 <command>ls</command>, nor build-system stuff such as <command>make</command> and <command>darcs</command>.
3107 For that, there are two choices: <ulink url="http://www.cygwin.com">Cygwin</ulink> 
3108 and <ulink url="http://www.mingw.org/msys.shtml">MSYS</ulink>:
3109
3110 <itemizedlist>
3111 <listitem><para>
3112 Cygwin comes with compilation tools (<command>gcc</command>, <command>ld</command> and so on), which
3113 compile code that has access to all of Posix.  The price is that the executables must be 
3114 dynamically linked with the Cygwin DLL, so that <emphasis>you cannot run a Cywin-compiled program on a machine
3115 that doesn't have Cygwin</emphasis>.  Worse, Cygwin is a moving target.  The name of the main DLL, <literal>cygwin1.dll</literal>
3116 does not change, but the implementation certainly does.  Even the interfaces to functions
3117 it exports seem to change occasionally. </para>
3118 </listitem>
3119
3120 <listitem><para>
3121 MSYS is a fork of the Cygwin tree, so they
3122 are fundamentally similar.  However, MSYS is by design much smaller and simpler.  Access to the file system goes
3123 through fewer layers, so MSYS is quite a bit faster too.
3124 </para>
3125
3126 <para>Furthermore, MSYS provides no compilation tools; it relies instead on the MinGW tools. These
3127 compile binaries that run with no DLL support, on any Win32 system.
3128 However, MSYS does come with all the make-system tools, such as <command>make</command>, <command>autoconf</command>, 
3129 <command>darcs</command>, <command>ssh</command> etc.  To get these, you have to download the 
3130 MsysDTK (Developer Tool Kit) package, as well as the base MSYS package.
3131 </para>
3132 <para>MSYS does have a DLL, but it's only used by MSYS commands (<command>sh</command>, <command>rm</command>, 
3133 <command>ssh</command> and so on),
3134 not by programs compiled under MSYS.
3135 </para></listitem>
3136
3137 </itemizedlist>
3138
3139 </para>
3140 </sect3>
3141
3142 <sect3><title>Targeting MinGW</title>
3143
3144 <para>We want GHC to compile programs that work on any Win32 system.  Hence:
3145 <itemizedlist>
3146 <listitem><para>
3147 GHC does invoke a C compiler, assembler, linker and so on, but we ensure that it only
3148 invokes the MinGW tools, not the Cygwin ones.  That means that the programs GHC compiles
3149 will work on any system, but it also means that the programs GHC compiles do not have access
3150 to all of Posix.  In particular, they cannot import the (Haskell) Posix 
3151 library; they have to do
3152 their input output using standard Haskell I/O libraries, or native Win32 bindings.</para>
3153 <para> We will call a GHC that targets MinGW in this way <emphasis>GHC-mingw</emphasis>.</para>
3154 </listitem>
3155
3156 <listitem><para>
3157 To make the GHC distribution self-contained, the GHC distribution includes the MinGW <command>gcc</command>,
3158 <command>as</command>, <command>ld</command>, and a bunch of input/output libraries.  
3159 </para></listitem>
3160 </itemizedlist>
3161 So <emphasis>GHC targets MinGW</emphasis>, not Cygwin.
3162 It is in principle possible to build a version of GHC, <emphasis>GHC-cygwin</emphasis>, 
3163 that targets Cygwin instead.  The up-side of GHC-cygwin is
3164 that Haskell programs compiled by GHC-cygwin can import the (Haskell) Posix library.
3165 <emphasis>We do not support GHC-cygwin, however; it is beyond our resources.</emphasis>
3166 </para>
3167
3168 <para>While GHC <emphasis>targets</emphasis> MinGW, that says nothing about 
3169 how GHC is <emphasis>built</emphasis>.  We use both MSYS and Cygwin as build environments for
3170 GHC; both work fine, though MSYS is rather lighter weight.</para>
3171
3172 <para>In your build tree, you build a compiler called <command>ghc-inplace</command>.  It
3173 uses the <command>gcc</command> that you specify using the
3174 <option>--with-gcc</option> flag when you run
3175 <command>configure</command> (see below).
3176 The makefiles are careful to use <command>ghc-inplace</command> (not <command>gcc</command>)
3177 to compile any C files, so that it will in turn invoke the correct <command>gcc</command> rather that
3178 whatever one happens to be in your path.  However, the makefiles do use whatever <command>ld</command> 
3179 and <command>ar</command> happen to be in your path. This is a bit naughty, but (a) they are only
3180 used to glom together .o files into a bigger .o file, or a .a file, 
3181 so they don't ever get libraries (which would be bogus; they might be the wrong libraries), and (b)
3182 Cygwin and MinGW use the same .o file format.  So its ok.
3183 </para>
3184 </sect3>
3185
3186 <sect3><title> File names </title>
3187
3188 <para>Cygwin, MSYS, and the underlying Windows file system all understand file paths of form <literal>c:/tmp/foo</literal>.
3189 However:
3190 <itemizedlist>
3191 <listitem><para>
3192 MSYS programs understand <filename>/bin</filename>, <filename>/usr/bin</filename>, and map Windows's lettered drives as
3193 <filename>/c/tmp/foo</filename> etc.  The exact mount table is given in the doc subdirectory of the MSYS distribution.
3194 </para>
3195 <para> When it invokes a command, the MSYS shell sees whether the invoked binary lives in the MSYS <filename>/bin</filename>
3196 directory.  If so, it just invokes it.  If not, it assumes the program is no an MSYS program, and walks over the command-line
3197 arguments changing MSYS paths into native-compatible paths.  It does this inside sub-arguments and inside quotes. For example,
3198 if you invoke
3199 <programlisting>foogle -B/c/tmp/baz</programlisting>
3200 the MSYS shell will actually call <literal>foogle</literal> with argument <literal>-Bc:/tmp/baz</literal>.
3201 </para></listitem>
3202
3203 <listitem><para>
3204 Cygwin programs have a more complicated mount table, and map the lettered drives as <filename>/cygdrive/c/tmp/foo</filename>.
3205 </para>
3206 <para>The Cygwin shell does no argument processing when invoking non-Cygwin programs.
3207 </para></listitem>
3208 </itemizedlist>
3209 </para>
3210 </sect3>
3211
3212 <sect3><title>Crippled <command>ld</command></title>
3213
3214 <para>
3215 It turns out that on both Cygwin and MSYS, the <command>ld</command> has a
3216 limit of 32kbytes on its command line.  Especially when using split object
3217 files, the make system can emit calls to <command>ld</command> with thousands
3218 of files on it.  Then you may see something like this:
3219 <programlisting>
3220 (cd Graphics/Rendering/OpenGL/GL/QueryUtils_split &amp;&amp; /mingw/bin/ld -r -x -o ../QueryUtils.o *.o)
3221 /bin/sh: /mingw/bin/ld: Invalid argument
3222 </programlisting>
3223 The solution is either to switch off object file splitting (set
3224 <option>SplitObjs</option> to <literal>NO</literal> in your
3225 <filename>build.mk</filename>),
3226 or to make the module smaller.
3227 </para>
3228 </sect3>
3229
3230 <sect3><title>Host System vs Target System</title>
3231
3232 <para>
3233 In the source code you'll find various ifdefs looking like:
3234 <programlisting>#ifdef mingw32_HOST_OS
3235   ...blah blah...
3236 #endif</programlisting>
3237 and 
3238 <programlisting>#ifdef mingw32_TARGET_OS
3239   ...blah blah...
3240 #endif</programlisting>
3241 These macros are set by the configure script (via the file config.h).
3242 Which is which?  The criterion is this.  In the ifdefs in GHC's source code:
3243 <itemizedlist>
3244   <listitem>
3245     <para>The "host" system is the one on which GHC itself will be run.</para>
3246   </listitem>
3247   <listitem>
3248     <para>The "target" system is the one for which the program compiled by GHC will be run.</para>
3249   </listitem>
3250 </itemizedlist>
3251 For a stage-2 compiler, in which GHCi is available, the "host" and "target" systems must be the same.
3252 So then it doesn't really matter whether you use the HOST_OS or TARGET_OS cpp macros.
3253
3254 </para>
3255 </sect3>
3256
3257 </sect2>
3258
3259 <sect2><title>Wrapper scripts</title>
3260
3261 <para>
3262 Many programs, including GHC itself and hsc2hs, need to find associated binaries and libraries.
3263 For <emphasis>installed</emphasis> programs, the strategy depends on the platform.  We'll use
3264 GHC itself as an example:
3265 <itemizedlist>
3266   <listitem> <para>
3267   On Unix, the command <command>ghc</command> is a shell script, generated by adding installation
3268   paths to the front of the source file <filename>ghc.sh</filename>, 
3269   that invokes the real binary, passing "-B<emphasis>path</emphasis>" as an argument to tell <command>ghc</command>
3270   where to find its supporting files. 
3271   </para> </listitem>
3272
3273   <listitem> <para>
3274   On vanilla Windows, it turns out to be much harder to make reliable script to be run by the
3275   native Windows shell <command>cmd</command> (e.g. limits on the length
3276    of the command line).  So instead we invoke the GHC binary directly, with no -B flag.
3277   GHC uses the Windows <literal>getExecDir</literal> function to find where the executable is,
3278   and from that figures out where the supporting files are.
3279   </para> </listitem>
3280 </itemizedlist>
3281 (You can find the layout of GHC's supporting files in the
3282   section "Layout of installed files" of Section 2 of the GHC user guide.)
3283 </para>
3284 <para>
3285 Things work differently for <emphasis>in-place</emphasis> execution, where you want to
3286 execute a program that has just been built in a build tree. The difference is that the
3287 layout of the supporting files is different.
3288 In this case, whether on Windows or Unix, we always use a shell script. This works OK
3289 on Windows because the script is executed by MSYS or Cygwin, which don't have the
3290 shortcomings of the native Windows <command>cmd</command> shell.
3291 </para>
3292
3293 </sect2>
3294
3295 </sect1>
3296
3297 <sect1 id="winbuild"><title>Instructions for building under Windows</title>
3298
3299 <para>
3300 This section gives detailed instructions for how to build 
3301 GHC from source on your Windows machine. Similar instructions for
3302 installing and running GHC may be found in the user guide. In general,
3303 Win95/Win98 behave the same, and WinNT/Win2k behave the same.
3304 </para>
3305 <para>
3306 Make sure you read the preceding section on platforms (<xref linkend="platforms"/>)
3307 before reading section.
3308 You don't need Cygwin or MSYS to <emphasis>use</emphasis> GHC, 
3309 but you do need one or the other to <emphasis>build</emphasis> GHC.</para>
3310
3311
3312 <sect2 id="msys-install"><title>Installing and configuring MSYS</title>
3313
3314 <para>
3315 MSYS is a lightweight alternative to Cygwin.  
3316 You don't need MSYS to <emphasis>use</emphasis> GHC, 
3317 but you do need it or Cygwin to <emphasis>build</emphasis> GHC.
3318 Here's how to install MSYS.
3319 <itemizedlist>
3320 <listitem><para>
3321 Go to <ulink url="http://www.mingw.org/download.shtml">http://www.mingw.org/download.shtml</ulink> and 
3322 download the following (of course, the version numbers will differ):
3323 <itemizedlist>
3324   <listitem><para>The main MSYS package (binary is sufficient): <literal>MSYS-1.0.9.exe</literal>
3325   </para></listitem>
3326   <listitem><para>The MSYS developer's toolkit (binary is sufficient): <literal>msysDTK-1.0.1.exe</literal>.
3327                     This provides <command>make</command>, <command>autoconf</command>, 
3328                     <command>ssh</command> and probably more besides.
3329   </para></listitem>
3330 </itemizedlist>
3331 Run both executables (in the order given above) to install them.  I put them in <literal>c:/msys</literal>
3332 </para></listitem>
3333
3334 <listitem><para>
3335 Set the following environment variables
3336 <itemizedlist>
3337    <listitem><para><literal>PATH</literal>: add <literal>c:/msys/1.0/bin</literal> and 
3338                     <literal>c:/msys/1.0/local/bin</literal>
3339                     to your path.  (Of course, the version number may differ.)
3340                     MSYS mounts the former as both <literal>/bin</literal> and 
3341                     <literal>/usr/bin</literal> and the latter as <literal>/usr/local/bin</literal>.
3342   </para></listitem>
3343
3344   <listitem><para><literal>HOME</literal>: set to your home directory (e.g. <literal>c:/userid</literal>).
3345   This is where, among other things, <command>ssh</command> will look for your <literal>.ssh</literal> directory.
3346   </para></listitem>  
3347
3348   <listitem><para><literal>SHELL</literal>: set to <literal>c:/msys/1.0/bin/sh.exe</literal>
3349   </para></listitem>
3350
3351   <listitem><para><literal>CVS_RSH</literal>: set to <literal>c:/msys/1.0/bin/ssh.exe</literal>.  Only necessary if
3352               you are using CVS.
3353   </para></listitem>
3354
3355   <listitem><para><literal>MAKE_MODE</literal>: set to <literal>UNIX</literal>.  (I'm not certain this is necessary for MSYS.)
3356   </para></listitem>
3357
3358 </itemizedlist>
3359 </para></listitem>
3360
3361 <listitem><para>
3362 Check that the <literal>CYGWIN</literal> environment variable is <emphasis>not</emphasis> set.  It's a bad bug
3363 that MSYS is affected by this, but if you have CYGWIN set to "ntsec ntea", which is right for Cygwin, it
3364 causes the MSYS <command>ssh</command> to bogusly fail complaining that your <filename>.ssh/identity</filename>
3365 file has too-liberal permissinos.
3366 </para></listitem>
3367
3368 </itemizedlist>
3369 </para>
3370 <para>Here are some points to bear in mind when using MSYS:
3371 <itemizedlist>
3372 <listitem> <para> MSYS does some kind of special magic to binaries stored in 
3373 <filename>/bin</filename> and <filename>/usr/bin</filename>, which are by default both mapped
3374 to <filename>c:/msys/1.0/bin</filename> (assuming you installed MSYS in <filename>c:/msys</filename>).
3375 Do not put any other binaries (such as GHC or Alex) in this directory or its sub-directories: 
3376 they fail in mysterious ways.  However, it's fine to put other binaries in <filename>/usr/local/bin</filename>,
3377 which maps to <filename>c:/msys/1.0/local/bin</filename>.</para></listitem>
3378
3379 <listitem> <para> MSYS seems to implement symbolic links by copying, so sharing is lost.
3380 </para></listitem>
3381
3382 <listitem> <para>
3383 Win32 has a <command>find</command> command which is not the same as MSYS's find.
3384 You will probably discover that the Win32 <command>find</command> appears in your <constant>PATH</constant>
3385 before the MSYS one, because it's in the <emphasis>system</emphasis> <constant>PATH</constant> 
3386 environment variable, whereas you have probably modified the <emphasis>user</emphasis> <constant>PATH</constant> 
3387 variable.  You can always invoke <command>find</command> with an absolute path, or rename it.
3388 </para></listitem>
3389
3390 <listitem> <para>
3391 MSYS comes with <command>bzip</command>, and MSYS's <command>tar</command>'s <literal>-j</literal> 
3392 will bunzip an archive (e.g. <literal>tar xvjf foo.tar.bz2</literal>).  Useful when you get a
3393 bzip'd dump.</para></listitem>
3394
3395 </itemizedlist>
3396 </para>
3397 </sect2>
3398
3399 <sect2 id="install-cygwin"><title>Installing and configuring Cygwin</title>
3400
3401 <para> Install Cygwin from <ulink url="http://www.cygwin.com/">http://www.cygwin.com/</ulink>.
3402 The installation process is straightforward; we install it in
3403 <filename>c:/cygwin</filename>.</para>
3404 <para>
3405 You must install enough Cygwin <emphasis>packages</emphasis> to support
3406 building GHC.  If you miss out any of these, strange things will happen to you.   There are two ways to do this:
3407 <itemizedlist>
3408 <listitem><para>The direct, but laborious way is to 
3409 select all of the following packages in the installation dialogue:
3410               <command>cvs</command>, 
3411               <command>openssh</command>,
3412               <command>autoconf</command>,
3413               <command>binutils</command> (includes ld and (I think) ar),
3414               <command>gcc</command>,
3415               <command>flex</command>,
3416               <command>make</command>.
3417 To see thse packages, 
3418 click on the "View" button in the "Select Packages" 
3419 stage of Cygwin's installation dialogue, until the view says "Full".  The default view, which is
3420 "Category" isn't very helpful, and the "View" button is rather unobtrousive.
3421 </para>
3422 </listitem>
3423
3424 <listitem><para>The clever way is to point the Cygwin installer at the
3425 <command>ghc-depends</command> package, which is kept at <ulink
3426 url="http://haskell.org/ghc/cygwin">http://haskell.org/ghc/cygwin</ulink>.
3427 When the Cygwin installer asks you to "Choose a Download Site", choose one of
3428 the
3429 offered mirror sites; and then type "http://haskell.org/ghc/cygwin" into the
3430 "User URL" box and click "Add"; now two sites are selected. (The Cygwin
3431 installer remembers this for next time.)
3432 Click "Next".</para>
3433 <para>In the "Select Packages" dialogue box that follows, click the "+" sign by
3434 "Devel", scroll down to the end of the "Devel" packages, and choose
3435 <command>ghc-depends</command>.
3436 The package <command>ghc-depends</command> will not actually install anything itself, 
3437 but forces additional packages to be added by the Cygwin installer.
3438 </para>
3439 </listitem>
3440 </itemizedlist>
3441 </para>
3442
3443 <para> Now set the following user environment variables:
3444 <itemizedlist>
3445
3446 <listitem><para> Add <filename>c:/cygwin/bin</filename> and <filename>c:/cygwin/usr/bin</filename> to your 
3447 <constant>PATH</constant></para></listitem>
3448
3449 <listitem>
3450 <para>
3451 Set <constant>MAKE_MODE</constant> to <literal>UNIX</literal>. If you
3452 don't do this you get very weird messages when you type
3453 <command>make</command>, such as:
3454 <screen>/c: /c: No such file or directory</screen>
3455 </para>
3456 </listitem>
3457
3458 <listitem><para> Set <constant>SHELL</constant> to
3459 <filename>c:/cygwin/bin/bash</filename>. When you invoke a shell in Emacs, this
3460 <constant>SHELL</constant> is what you get.
3461 </para></listitem>
3462
3463 <listitem><para> Set <constant>HOME</constant> to point to your 
3464 home directory.  This is where, for example,
3465 <command>bash</command> will look for your <filename>.bashrc</filename>
3466 file.  Ditto <command>emacs</command> looking for <filename>.emacsrc</filename>
3467 </para></listitem>
3468 </itemizedlist>
3469 </para>
3470
3471 <para>Here are some things to be aware of when using Cygwin:
3472 <itemizedlist>
3473 <listitem> <para>Cygwin doesn't deal well with filenames that include
3474 spaces. "<filename>Program Files</filename>" and "<filename>Local files</filename>" are
3475 common gotchas.
3476 </para></listitem>
3477
3478 <listitem> <para> Cygwin implements a symbolic link as a text file with some
3479 magical text in it.  So other programs that don't use Cygwin's
3480 I/O libraries won't recognise such files as symlinks.  
3481 In particular, programs compiled by GHC are meant to be runnable
3482 without having Cygwin, so they don't use the Cygwin library, so
3483 they don't recognise symlinks.
3484 </para></listitem>
3485
3486 <listitem> <para>
3487 See the notes in <xref linkend="msys-install"/> about <command>find</command> and <command>bzip</command>,
3488 which apply to Cygwin too.
3489 </para></listitem>
3490
3491 <listitem>
3492 <para>
3493 Some script files used in the make system start with "<command>#!/bin/perl</command>",
3494 (and similarly for <command>sh</command>).  Notice the hardwired path!
3495 So you need to ensure that your <filename>/bin</filename> directory has at least
3496 <command>sh</command>, <command>perl</command>, and <command>cat</command> in it.
3497 All these come in Cygwin's <filename>bin</filename> directory, which you probably have
3498 installed as <filename>c:/cygwin/bin</filename>.  By default Cygwin mounts "<filename>/</filename>" as
3499 <filename>c:/cygwin</filename>, so if you just take the defaults it'll all work ok.
3500 (You can discover where your Cygwin
3501 root directory <filename>/</filename> is by typing <command>mount</command>.)
3502 Provided <filename>/bin</filename> points to the Cygwin <filename>bin</filename>
3503 directory, there's no need to copy anything.  If not, copy these binaries from the <filename>cygwin/bin</filename>
3504 directory (after fixing the <filename>sh.exe</filename> stuff mentioned in the previous bullet).
3505 </para>
3506 </listitem>
3507
3508 <listitem>
3509 <para>
3510 By default, cygwin provides the command shell <filename>ash</filename>
3511 as <filename>sh.exe</filename>.   It seems to be fine now, but in the past we
3512 saw build-system problems that turned out to be due to bugs in <filename>ash</filename>
3513 (to do with quoting and length of command lines).  On the other hand <filename>bash</filename> seems
3514 to be rock solid.
3515 If this happens to you (which it shouldn't), in <filename>cygwin/bin</filename>
3516 remove the supplied <filename>sh.exe</filename> (or rename it as <filename>ash.exe</filename>),
3517 and copy <filename>bash.exe</filename> to  <filename>sh.exe</filename>.
3518 You'll need to do this in Windows Explorer or the Windows <command>cmd</command> shell, because
3519 you can't rename a running program!
3520 </para>
3521 </listitem>
3522 </itemizedlist>
3523 </para>
3524
3525 </sect2>
3526
3527
3528 <sect2 id="configure-ssh"><title>Configuring SSH</title>
3529
3530 <para><command>ssh</command> comes with both Cygwin and MSYS. 
3531 (Cygwin note: you need to ask for package <command>openssh</command> (not ssh)
3532 in the Cygwin list of packages; or use the <command>ghc-depends</command>
3533 package -- see <xref linkend="install-cygwin"/>.)</para>
3534
3535 <para>There are several strange things about <command>ssh</command> on Windows that you need to know.
3536 <itemizedlist>
3537 <listitem>
3538 <para>
3539        The programs <command>ssh-keygen1</command>, <command>ssh1</command>, and <command>cvs</command>,
3540        seem to lock up <command>bash</command> entirely if they try to get user input (e.g. if
3541        they ask for a password).  To solve this, start up <filename>cmd.exe</filename> 
3542        and run it as follows:
3543 <screen>c:\tmp> set CYGWIN32=tty
3544 c:\tmp> c:/user/local/bin/ssh-keygen1</screen> </para>
3545 </listitem>
3546
3547 <listitem><para> (Cygwin-only problem, I think.)
3548 <command>ssh</command> needs to access your directory <filename>.ssh</filename>, in your home directory.  
3549 To determine your home directory <command>ssh</command> first looks in 
3550 <filename>c:/cygwin/etc/passwd</filename> (or wherever you have Cygwin installed).  If there's an entry
3551 there with your userid, it'll use that entry to determine your home directory, <emphasis>ignoring
3552 the setting of the environment variable $HOME</emphasis>.  If the home directory is
3553 bogus, <command>ssh</command> fails horribly.   The best way to see what is going on is to say
3554 <screen>ssh -v cvs.haskell.org</screen>
3555 which makes <command>ssh</command> print out information about its activity.
3556 </para>
3557 <para> You can fix this problem, either by correcting the home-directory field in 
3558 <filename>c:/cygwin/etc/passwd</filename>, or by simply deleting the entire entry for your userid. If
3559 you do that, <command>ssh</command> uses the $HOME environment variable instead.
3560 </para>
3561
3562 </listitem>
3563
3564 <listitem>
3565             <para>To protect your
3566             <literal>.ssh</literal> from access by anyone else,
3567             right-click your <literal>.ssh</literal> directory, and
3568             select <literal>Properties</literal>.  If you are not on
3569             the access control list, add yourself, and give yourself
3570             full permissions (the second panel).  Remove everyone else
3571             from the access control list.  Don't leave them there but
3572             deny them access, because 'they' may be a list that
3573             includes you!</para>
3574 </listitem>
3575
3576 <listitem>
3577             <para>In fact <command>ssh</command> 3.6.1 now seems to <emphasis>require</emphasis>
3578               you to have Unix permissions 600 (read/write for owner only) 
3579               on the <literal>.ssh/identity</literal> file, else it 
3580               bombs out.  For your local C drive, it seems that <literal>chmod 600 identity</literal> works,
3581               but on Windows NT/XP, it doesn't work on a network drive (exact dteails obscure).  
3582               The solution seems to be to set the $CYGWIN environment
3583               variable to "<literal>ntsec neta</literal>".  The $CYGWIN environment variable is discussed
3584               in <ulink url="http://cygwin.com/cygwin-ug-net/using-cygwinenv.html">the Cygwin User's Guide</ulink>,
3585               and there are more details in <ulink url="http://cygwin.com/faq/faq_4.html#SEC44">the Cygwin FAQ</ulink>.
3586               </para>
3587 </listitem>
3588 </itemizedlist>
3589 </para>
3590 </sect2>
3591
3592 <sect2><title>Other things you need to install</title>
3593
3594 <para>You have to install the following other things to build GHC, listed below.</para>
3595
3596 <para>On Windows you often install executables in directories with spaces, such as 
3597 "<filename>Program Files</filename>". However, the <literal>make</literal> system doesn't 
3598 deal with this situation (it'd have to do more quoting of binaries), so you are strongly advised
3599 to put binaries for all tools in places with no spaces in their path.
3600 On both MSYS and Cygwin, it's perfectly OK to install such programs in the standard Unixy places,
3601 <filename>/usr/local/bin</filename> and <filename>/usr/local/lib</filename>.  But it doesn't matter,
3602 provided they are in your path.
3603 <itemizedlist>
3604 <listitem>
3605 <para>
3606 Install an executable GHC, from <ulink url="http://www.haskell.org/ghc">http://www.haskell.org/ghc</ulink>.
3607 This is what you will use to compile GHC.  Add it in your
3608 <constant>PATH</constant>: the installer tells you the path element
3609 you need to add upon completion.
3610 </para>
3611 </listitem>
3612
3613 <listitem>
3614 <para>
3615 Install an executable Happy, from <ulink url="http://www.haskell.org/happy">http://www.haskell.org/happy</ulink>.
3616 Happy is a parser generator used to compile the Haskell grammar.  Under MSYS or Cygwin you can easily
3617 build it from the source distribution using
3618 <screen>$ ./configure
3619 $ make
3620 $ make install</screen>
3621 This should install it in <filename>/usr/local/bin</filename> (which maps to <filename>c:/msys/1.0/local/bin</filename>
3622 on MSYS).
3623 Make sure the installation directory is in your
3624 <constant>PATH</constant>.
3625 </para>
3626 </listitem>
3627
3628           <listitem>
3629             <para>Install an executable Alex.  This can be done by building from the
3630             source distribution in the same way as Happy.  Sources are
3631             available from <ulink
3632             url="http://www.haskell.org/alex">http://www.haskell.org/alex</ulink>.</para>
3633           </listitem>
3634
3635 <listitem>
3636 <para>GHC uses the <emphasis>mingw</emphasis> C compiler to
3637 generate code, so you have to install that (see <xref linkend="cygwin-and-mingw"/>). 
3638 Just pick up a mingw bundle at
3639 <ulink url="http://www.mingw.org/">http://www.mingw.org/</ulink>.
3640 We install it in <filename>c:/mingw</filename>.
3641 </para>
3642
3643 <para><emphasis>On MSYS</emphasis>, add <literal>c:/mingw/bin</literal> to your PATH. MSYS does not provide <command>gcc</command>,
3644 <command>ld</command>, <command>ar</command>, and so on, because it just uses the MinGW ones.  So you need them
3645 in your path.
3646 </para>
3647
3648 <para><emphasis>On Cygwin, do not</emphasis> add any of the <emphasis>mingw</emphasis> binaries to your  path.
3649 They are only going to get used by explicit access (via the --with-gcc flag you
3650 give to <command>configure</command> later).  If you do add them to your path
3651 you are likely to get into a mess because their names overlap with Cygwin
3652 binaries.
3653 On the other hand, you <emphasis>do</emphasis> need <command>ld</command>, <command>ar</command>
3654 (and perhaps one or two other things) in your path.  The Cygwin ones are fine,
3655 but you must have them; hence needing the  Cygwin binutils package.
3656 </para>
3657 </listitem>
3658
3659
3660 <listitem>
3661 <para>We use <command>emacs</command> a lot, so we install that too.
3662 When you are in <filename>$(GHC&lowbar;TOP)/compiler</filename>, you can use
3663 "<literal>make tags</literal>" to make a TAGS file for emacs.  That uses the utility
3664 <filename>$(GHC&lowbar;TOP)/ghc/utils/hasktags/hasktags</filename>, so you need to make that first.
3665 The most convenient way to do this is by going <literal>make boot</literal> in <filename>$(GHC&lowbar;TOP)/ghc</filename>.
3666 The <literal>make tags</literal> command also uses <command>etags</command>, which comes with <command>emacs</command>,
3667 so you will need to add <filename>emacs/bin</filename> to your <literal>PATH</literal>.
3668 </para>
3669 </listitem>
3670
3671           <listitem>
3672             <para>You might want to install GLUT in your MSYS/Cygwin
3673               installation, otherwise the GLUT package will not be built with
3674               GHC.</para>
3675           </listitem>
3676
3677 <listitem>
3678 <para> Finally, check out a copy of GHC sources from
3679 the darcs repository, following the instructions at <ulink url="http://hackage.haskell.org/trac/ghc/wiki/GhcDarcs" />.</para>
3680 </listitem>
3681 </itemizedlist>
3682 </para>
3683 </sect2>
3684
3685 <sect2><title>Building GHC</title>
3686
3687 <para>OK!  
3688 Now go read the documentation above on building from source (<xref linkend="sec-building-from-source"/>); 
3689 the bullets below only tell
3690 you about Windows-specific wrinkles.</para>
3691 <itemizedlist>
3692 <listitem>
3693 <para>
3694 If you used <command>autoconf</command> instead of <command>autoreconf</command>,
3695 you'll get an error when you run <filename>./configure</filename>:
3696 <screen>
3697 ...lots of stuff...
3698 creating mk/config.h
3699 mk/config.h is unchanged
3700 configuring in ghc
3701 running /bin/sh ./configure  --cache-file=.././config.cache --srcdir=.
3702 ./configure: ./configure: No such file or directory
3703 configure: error: ./configure failed for ghc</screen>
3704 </para>
3705 </listitem>
3706
3707 <listitem> <para><command>autoreconf</command> seems to create the file <filename>configure</filename>
3708 read-only.  So if you need to run autoreconf again (which I sometimes do for safety's sake),
3709 you get
3710 <screen>/usr/bin/autoconf: cannot create configure: permission denied</screen>
3711 Solution: delete <filename>configure</filename> first.
3712 </para></listitem>
3713
3714 <listitem>
3715   <para> 
3716     After <command>autoreconf</command> run <command>./configure</command> in
3717     <filename>$(GHC&lowbar;TOP)/</filename> thus:
3718
3719 <screen>$ ./configure --host=i386-unknown-mingw32 --with-gcc=c:/mingw/bin/gcc</screen>
3720 This is the point at which you specify that you are building GHC-mingw
3721 (see <xref linkend="ghc-mingw"/>). </para>
3722
3723 <para> Both these options are important! It's possible to get into
3724 trouble using the wrong C compiler!</para>
3725 <para>
3726 Furthermore, it's <emphasis>very important</emphasis> that you specify a 
3727 full MinGW path for <command>gcc</command>, not a Cygwin path, because GHC (which
3728 uses this path to invoke <command>gcc</command>) is a MinGW program and won't
3729 understand a Cygwin path.  For example, if you 
3730 say <literal>--with-gcc=/mingw/bin/gcc</literal>, it'll be interpreted as
3731 <filename>/cygdrive/c/mingw/bin/gcc</filename>, and GHC will fail the first
3732 time it tries to invoke it.   Worse, the failure comes with
3733 no error message whatsoever.  GHC simply fails silently when first invoked, 
3734 typically leaving you with this:
3735 <screen>make[4]: Leaving directory `/cygdrive/e/ghc-stage1/ghc/rts/gmp'
3736 ../../ghc/compiler/ghc-inplace -optc-mno-cygwin -optc-O 
3737   -optc-Wall -optc-W -optc-Wstrict-prototypes -optc-Wmissing-prototypes 
3738   -optc-Wmissing-declarations -optc-Winline -optc-Waggregate-return 
3739   -optc-Wbad-function-cast -optc-Wcast-align -optc-I../includes 
3740   -optc-I. -optc-Iparallel -optc-DCOMPILING_RTS 
3741   -optc-fomit-frame-pointer -O2 -static 
3742   -package-name rts -O -dcore-lint -c Adjustor.c -o Adjustor.o
3743 make[2]: *** [Adjustor.o] Error 1
3744 make[1]: *** [all] Error 1
3745 make[1]: Leaving directory `/cygdrive/e/ghc-stage1/ghc'
3746 make: *** [all] Error 1</screen>
3747 Be warned!
3748 </para>
3749
3750 <para>
3751 If you want to build GHC-cygwin (<xref linkend="ghc-cygwin"/>)
3752 you'll have to do something more like:
3753 <screen>$ ./configure --with-gcc=...the Cygwin gcc...</screen>
3754 </para>
3755 </listitem>
3756
3757 <listitem><para>
3758 If you are paranoid, delete <filename>config.cache</filename> if it exists.
3759 This file occasionally remembers out-of-date configuration information, which 
3760 can be really confusing.
3761 </para>
3762 </listitem>
3763
3764 <listitem><para> You almost certainly want to set
3765 <programlisting>SplitObjs = NO</programlisting>
3766 in your <filename>build.mk</filename> configuration file (see <xref linkend="sec-build-config"/>).
3767 This tells the build system not to split each library into a myriad of little object files, one
3768 for each function.  Doing so reduces binary sizes for statically-linked binaries, but on Windows
3769 it dramatically increases the time taken to build the libraries in the first place.
3770 </para>
3771 </listitem>
3772
3773 <listitem><para> Do not attempt to build the documentation.
3774 It needs all kinds of wierd Jade stuff that we haven't worked out for
3775 Win32.</para></listitem>
3776 </itemizedlist>
3777 </sect2>
3778
3779
3780 <sect2><title>A Windows build log using Cygwin</title>
3781
3782 <para>Here is a complete, from-scratch, log of all you need to build GHC using
3783 Cygwin, kindly provided by Claus Reinke.  It does not discuss alternative
3784 choices, but it gives a single path that works.</para>
3785 <programlisting>- Install some editor (vim, emacs, whatever)
3786
3787 - Install cygwin (http://www.cygwin.com)
3788     ; i used 1.5.16-1, installed in c:\cygwin
3789   - run 'setup.exe'
3790     Choose a Download Source:
3791         select 'download from internet';
3792     Select Root Install Directory:
3793         root dir: c:\cygwin; 
3794         install for: all users;
3795         default file type: unix
3796     Select Local Package Directory
3797         choose a spare temporary home
3798     Select Your Internet Connection
3799         Use IE5 settings
3800     Choose a Download Site
3801         Choose your preferred main mirror and
3802         Add 'http://www.haskell.org/ghc/cygwin'
3803     Select Packages
3804         In addition to 'Base' (default install), 
3805         select 'Devel->ghc-depends'
3806
3807 - Install mingw (http://www.mingw.org/)
3808     ; i used MinGW-3.1.0-1.exe
3809     ; installed in c:\mingw
3810   - you probably want to add GLUT 
3811     ; (http://www.xmission.com/~nate/glut.html)
3812     ; i used glut-3.7.3-mingw32.tar
3813
3814 - Get recent binary snapshot of ghc-6.4.1 for mingw 
3815     ; (http://www.haskell.org/ghc/dist/stable/dist/)
3816   - unpack in c:/ghc
3817   - add C:\ghc\ghc-6.4.1\bin to %PATH%
3818     (Start->Control Panel->System->Advanced->Environment Variables)
3819
3820 - Get darcs version of ghc
3821     ; also, subscribe to cvs-all@haskell.org, or follow the mailing list
3822     ; archive, in case you checkout a version with problems
3823     ; http://www.haskell.org//pipermail/cvs-all/
3824   - mkdir c:/ghc-build; cd c:/ghc-build
3825     ; (or whereever you want your darcs tree to be)
3826   - darcs get http://darcs.haskell.org/ghc
3827   - cd ghc
3828   - chmod +x darcs-all
3829   - ./darcs-all get
3830
3831 - Build ghc, using cygwin and mingw, targetting mingw
3832   - export PATH=/cygdrive/c/ghc/ghc-6.4.1:$PATH
3833     ; for haddock, alex, happy (*)
3834   - export PATH=/cygdrive/c/mingw/bin:$PATH
3835     ; without, we pick up some cygwin tools at best!
3836   - cd c:/ghc-build
3837     ; (if you aren't there already)
3838   - autoreconf
3839   - ./configure --host=i386-unknown-mingw32 --with-gcc=C:/Mingw/bin/gcc.exe
3840     ; we use cygwin, but build for windows
3841   - cp mk/build.mk.sample mk/build.mk
3842   - in mk/build.mk:
3843     add line:       SplitObjs = NO
3844         (MSYS seems slow when there are zillions of object files)
3845     uncomment line: BuildFlavour = perf
3846         (or BuildFlavour = devel, if you are doing development)
3847     add line:       BIN_DIST=1
3848   - make 2>&amp;1 | tee make.log
3849     ; always useful to have a log around
3850
3851 - Package up binary distribution
3852   - make binary-dist Project=Ghc 2>&amp;1 | tee make-bin-dist.log
3853     ; always useful to have a log around
3854   - cd ghc-6.5
3855   - chmod +x ../distrib/prep-bin-dist-mingw
3856     ; if you're happy with the script's contents (*)
3857   - ../distrib/prep-bin-dist-mingw
3858     ; then tar up, unpack where wanted, and enjoy</programlisting>
3859 </sect2>
3860 </sect1>
3861
3862 <index/>
3863
3864 </article>