[project @ 2004-02-05 09:46:22 by simonpj]
[ghc-hetmet.git] / docs / building / building.sgml
1 <!DOCTYPE Article PUBLIC "-//OASIS//DTD DocBook V3.1//EN">
2
3 <Article id="building-guide">
4
5 <ArtHeader>
6
7 <Title>Building the Glasgow Functional Programming Tools Suite</Title>
8 <Author><OtherName>The GHC Team</OtherName></Author>
9 <Address><Email>glasgow-haskell-&lcub;users,bugs&rcub;@haskell.org</Email></Address>
10 <PubDate>November 2001</PubDate>
11
12     <abstract>
13       <para>The Glasgow fptools suite is a collection of Functional
14       Programming related tools, including the Glasgow Haskell
15       Compiler (GHC).  The source code for the whole suite is kept in
16       a single CVS repository and shares a common build and
17       installation system.</para>
18
19       <para>This guide is intended for people who want to build or
20       modify programs from the Glasgow <Literal>fptools</Literal>
21       suite (as distinct from those who merely want to
22       <Emphasis>run</Emphasis> them). Installation instructions are
23       now provided in the user guide.</para>
24
25       <para>The bulk of this guide applies to building on Unix
26       systems; see <XRef LinkEnd="winbuild"> for Windows notes.</para>
27     </abstract>
28
29   </artheader>
30
31
32   <sect1 id="sec-getting">
33     <title>Getting the sources</title>
34     
35     <para>You can get your hands on the <literal>fptools</literal>
36     in two ways:</para>
37
38     <variablelist>
39
40       <varlistentry>
41         <term><indexterm><primary>Source
42         distributions</primary></indexterm>Source distributions</term>
43         <listitem>
44           <para>You have a supported platform, but (a)&nbsp;you like
45           the warm fuzzy feeling of compiling things yourself;
46           (b)&nbsp;you want to build something ``extra&rdquo;&mdash;e.g., a
47           set of libraries with strictness-analysis turned off; or
48           (c)&nbsp;you want to hack on GHC yourself.</para>
49
50           <para>A source distribution contains complete sources for
51           one or more projects in the <literal>fptools</literal>
52           suite.  Not only that, but the more awkward
53           machine-independent steps are done for you.  For example, if
54           you don't have
55           <command>happy</command><indexterm><primary>happy</primary></indexterm>
56           you'll find it convenient that the source distribution
57           contains the result of running <command>happy</command> on
58           the parser specifications.  If you don't want to alter the
59           parser then this saves you having to find and install
60           <command>happy</command>. You will still need a working
61           version of GHC (version 5.x or later) on your machine in
62           order to compile (most of) the sources, however.</para>
63         </listitem>
64       </varlistentry>
65
66       <varlistentry>
67         <term>The CVS repository.</term>
68         <indexterm><primary>CVS repository</primary>
69         </indexterm>
70         <listitem>
71           <para>We make releases infrequently.  If you want more
72           up-to-the minute (but less tested) source code then you need
73           to get access to our CVS repository.</para>
74
75           <para>All the <literal>fptools</literal> source code is held
76           in a CVS repository. CVS is a pretty good source-code
77           control system, and best of all it works over the
78           network.</para>
79
80           <para>The repository holds source code only. It holds no
81           mechanically generated files at all.  So if you check out a
82           source tree from CVS you will need to install every utility
83           so that you can build all the derived files from
84           scratch.</para>
85
86           <para>More information about our CVS repository can be found
87           in <xref linkend="sec-cvs">.</para>
88         </listitem>
89       </varlistentry>
90     </variablelist>
91
92     <para>If you are going to do any building from sources (either
93     from a source distribution or the CVS repository) then you need to
94     read all of this manual in detail.</para>
95   </sect1>
96
97   <sect1 id="sec-cvs">
98     <title>Using the CVS repository</title>
99
100     <para>We use <ulink url="http://www.cvshome.org/">CVS</ulink> (Concurrent Version System) to keep track of our
101     sources for various software projects. CVS lets several people
102     work on the same software at the same time, allowing changes to be
103     checked in incrementally. </para>
104
105     <para>This section is a set of guidelines for how to use our CVS
106     repository, and will probably evolve in time. The main thing to
107     remember is that most mistakes can be undone, but if there's
108     anything you're not sure about feel free to bug the local CVS
109     meister (namely Jeff Lewis
110     <email>jlewis@galconn.com</email>). </para>
111
112     <sect2 id="cvs-access">
113       <title>Getting access to the CVS Repository</title>
114
115       <para>You can access the repository in one of two ways:
116       read-only (<xref linkend="cvs-read-only">), or read-write (<xref
117       linkend="cvs-read-write">).</para>
118
119       <sect3 id="cvs-read-only">
120         <title>Remote Read-only CVS Access</title>
121
122         <para>Read-only access is available to anyone - there's no
123         need to ask us first.  With read-only CVS access you can do
124         anything except commit changes to the repository.  You can
125         make changes to your local tree, and still use CVS's merge
126         facility to keep your tree up to date, and you can generate
127         patches using 'cvs diff' in order to send to us for
128         inclusion. </para>
129
130         <para>To get read-only access to the repository:</para>
131
132         <orderedlist>
133           <listitem>
134             <para>Make sure that <application>cvs</application> is
135             installed on your machine.</para>
136           </listitem>
137           <listitem>
138             <para>Set your <literal>$CVSROOT</literal> environment variable to
139             <literal>:pserver:anoncvs@glass.cse.ogi.edu:/cvs</literal></para>
140             <para>If you set <literal>$CVSROOT</literal> in a shell script, be sure not to
141               have any trailing spaces on that line, otherwise CVS will respond with 
142               a perplexing message like
143               <programlisting>
144                 /cvs : no such repository
145           </programlisting></para>
146           </listitem>
147           <listitem>
148             <para>Run the command</para>
149 <programlisting>
150     $ cvs login
151 </programlisting>
152             <para>The password is simply <literal>cvs</literal>.  This
153             sets up a file in your home directory called
154             <literal>.cvspass</literal>, which squirrels away the
155             dummy password, so you only need to do this step once.</para>
156           </listitem>
157
158           <listitem>
159             <para>Now go to <xref linkend="cvs-first">.</para>
160           </listitem>
161         </orderedlist>
162       </sect3>
163
164       <sect3 id="cvs-read-write">
165         <title>Remote Read-Write CVS Access</title>
166
167         <para>We generally supply read-write access to folk doing
168         serious development on some part of the source tree, when
169         going through us would be a pain. If you're developing some
170         feature, or think you have the time and inclination to fix
171         bugs in our sources, feel free to ask for read-write
172         access. There is a certain amount of responsibility that goes
173         with commit privileges; we are more likely to grant you access
174         if you've demonstrated your competence by sending us patches
175         via mail in the past.</para>
176
177         <para>To get remote read-write CVS access, you need to do the
178         following steps.</para>
179
180         <orderedlist>
181           <listitem>
182             <para>Make sure that <literal>cvs</literal> and
183             <literal>ssh</literal> are both installed on your
184             machine.</para>
185           </listitem>
186
187           <listitem>
188             <para>Generate a DSA private-key/public-key pair, thus:</para>
189 <screen>
190      $ ssh-keygen -d
191 </screen>
192             <para>(<literal>ssh-keygen</literal> comes with
193             <literal>ssh</literal>.)  Running <literal>ssh-keygen
194             -d</literal> creates the private and public keys in
195             <literal>$HOME/.ssh/id_dsa</literal> and
196             <literal>$HOME/.ssh/id_dsa.pub</literal> respectively
197             (assuming you accept the standard defaults).</para>
198
199             <para><literal>ssh-keygen -d</literal> will only work if
200             you have Version 2 <literal>ssh</literal> installed; it
201             will fail harmlessly otherwise.  If you only have Version
202             1 you can instead generate an RSA key pair using plain</para>
203 <screen>
204     $ ssh-keygen
205 </screen>
206
207             <para>Doing so creates the private and public RSA keys in
208             <literal>$HOME/.ssh/identity</literal> and
209             <literal>$HOME/.ssh/identity.pub</literal>
210             respectively.</para>
211
212             <para>[Deprecated.]  Incidentally, you can force a Version
213             2 <literal>ssh</literal> to use the Version 1 protocol by
214             creating <literal>$HOME/config</literal> with the
215             following in it:</para>
216 <screen>
217    BatchMode Yes
218
219    Host cvs.haskell.org
220    Protocol 1
221 </screen>
222
223             <para>In both cases, <literal>ssh-keygen</literal> will
224             ask for a <firstterm>passphrase</firstterm>.  The
225             passphrase is a password that protects your private key.
226             In response to the 'Enter passphrase' question, you can
227             either:</para>
228             <itemizedlist>
229               <listitem>
230                 <para>[Recommended.]  Enter a passphrase, which you
231                 will quote each time you use CVS.
232                 <literal>ssh-agent</literal> makes this entirely
233                 un-tiresome.</para>
234               </listitem>
235               <listitem>
236                 <para>[Deprecated.] Just hit return (i.e. use an empty
237                 passphrase); then you won't need to quote the
238                 passphrase when using CVS.  The downside is that
239                 anyone who can see into your <literal>.ssh</literal>
240                 directory, and thereby get your private key, can mess
241                 up the repository.  So you must keep the
242                 <literal>.ssh</literal> directory with draconian
243                 no-access permissions.</para>
244               </listitem>
245             </itemizedlist>
246
247
248        <para>
249        <emphasis>Windows users: see the notes in <xref linkend="configure-ssh"> about <command>ssh</command> wrinkles!</emphasis>
250          </para>
251
252
253           </listitem>
254
255           <listitem>
256             <para>Send a message to to the CVS repository
257             administrator (currently Jeff Lewis
258             <email>jeff@galconn.com</email>), containing:</para>
259             <itemizedlist>
260               <listitem>
261                 <para>Your desired user-name.</para>
262               </listitem>
263               <listitem>
264                 <para>Your <literal>.ssh/id_dsa.pub</literal> (or
265                 <literal>.ssh/identity.pub</literal>).</para>
266               </listitem>
267             </itemizedlist>
268             <para>He will set up your account.</para>
269           </listitem>
270
271           <listitem>
272             <para>Set the following environment variables:</para>
273            <ItemizedList>
274            <listitem>
275            <para>
276            <constant>$HOME</constant>: points to your home directory.  This is where CVS
277            will look for its <filename>.cvsrc</filename> file.
278            </para>
279            </listitem>
280
281            <listitem>
282            <para>
283            <constant>$CVS_RSH</constant> to <filename>ssh</filename>
284            </para>
285            <para>[Windows users.] Setting your <literal>CVS_RSH</literal> to
286             <literal>ssh</literal> assumes that your CVS client
287             understands how to execute shell script
288             (&quot;#!&quot;s,really), which is what
289             <literal>ssh</literal> is. This may not be the case on
290             Win32 platforms, so in that case set <literal>CVS_RSH</literal> to
291             <literal>ssh1</literal>.</para>
292            </listitem>
293
294              <listitem>
295                 <para><literal>$CVSROOT</literal> to
296                 <literal>:ext:</literal><replaceable>your-username</replaceable>
297                 <literal>@cvs.haskell.org:/home/cvs/root</literal>
298                 where <replaceable>your-username</replaceable> is your user name on
299                 <literal>cvs.haskell.org</literal>.
300                 </para>
301         <para>The <literal>CVSROOT</literal> environment variable will
302         be recorded in the checked-out tree, so you don't need to set
303         this every time. </para>
304
305              </listitem>
306
307         <listitem>
308         <para>
309         <constant>$CVSEDITOR</constant>: <filename>bin/gnuclient.exe</filename> 
310         if you want to use an Emacs buffer for typing in those long commit messages.
311         </para>
312         </listitem>
313
314         <listitem>
315         <para>
316         <constant>$SHELL</constant>: To use bash as the shell in Emacs, you need to
317         set this to point to <filename>bash.exe</filename>.
318         </para>
319         </listitem>
320
321        </ItemizedList>
322
323
324           </listitem>
325
326           <listitem>
327           <para>
328           Put the following in <filename>$HOME/.cvsrc</filename>:
329           </para>
330           
331           <ProgramListing>
332           checkout -P
333           release -d
334           update -P
335           diff -u
336           </ProgramListing>
337           
338           <para>
339           These are the default options for the specified CVS commands,
340           and represent better defaults than the usual ones.  (Feel
341           free to change them.)
342           </para>
343           
344           <para>
345           [Windows users.]  Filenames starting with <filename>.</filename> were illegal in 
346           the 8.3 DOS filesystem, but that restriction should have
347           been lifted by now (i.e., you're using VFAT or later filesystems.) If
348           you're still having problems creating it, don't worry; <filename>.cvsrc</filename> is entirely
349           optional.
350           </para>
351           </listitem>
352
353         </orderedlist>
354
355
356         <para>[Experts.]  Once your account is set up, you can get
357         access from other machines without bothering Jeff, thus:</para>
358         <orderedlist>
359           <listitem>
360             <para>Generate a public/private key pair on the new
361             machine.</para>
362           </listitem>
363           <listitem>
364             <para>Use ssh to log in to
365             <literal>cvs.haskell.org</literal>, from your old
366             machine.</para>
367           </listitem>
368           <listitem>
369             <para>Add the public key for the new machine to the file
370             <literal>$HOME/ssh/authorized_keys</literal> on
371             <literal>cvs.haskell.org</literal>.
372             (<literal>authorized_keys2</literal>, I think, for Version
373             2 protocol.)</para>
374           </listitem>
375           <listitem>
376             <para>Make sure that the new version of
377             <literal>authorized_keys</literal> still has 600 file
378             permissions.</para>
379           </listitem>
380         </orderedlist>
381       </sect3>
382     </sect2>
383
384
385
386     <sect2 id="cvs-first">
387       <title>Checking Out a Source Tree</title>
388
389       <itemizedlist>
390         <listitem>
391           <para>Make sure you set your <literal>CVSROOT</literal>
392           environment variable according to either of the remote
393           methods above. The Approved Way to check out a source tree
394           is as follows:</para>
395
396 <screen>
397     $ cvs checkout fpconfig
398 </screen>
399
400           <para>At this point you have a new directory called
401           <literal>fptools</literal> which contains the basic stuff
402           for the fptools suite, including the configuration files and
403           some other junk. </para>
404
405 <para>[Windows users.]  The following messages appear to be harmless:
406 <Screen>
407 setsockopt IPTOS_LOWDELAY: Invalid argument
408 setsockopt IPTOS_THROUGHPUT: Invalid argument
409 </Screen>
410 </para>
411
412
413           <para>You can call the fptools directory whatever you like,
414           CVS won't mind: </para>
415           
416 <screen>
417     $ mv fptools <replaceable>directory</replaceable>
418 </screen>
419
420           <para> NB: after you've read the CVS manual you might be
421           tempted to try</para>
422 <screen>
423     $ cvs checkout -d <replaceable>directory</replaceable> fpconfig
424 </screen>
425
426           <para>instead of checking out <literal>fpconfig</literal>
427           and then renaming it.  But this doesn't work, and will
428           result in checking out the entire repository instead of just
429           the <literal>fpconfig</literal> bit.</para>
430 <screen>
431     $ cd <replaceable>directory</replaceable>
432     $ cvs checkout ghc hslibs libraries
433 </screen>
434
435           <para>The second command here checks out the relevant
436           modules you want to work on. For a GHC build, for instance,
437           you need at least the <literal>ghc</literal>,
438           <literal>hslibs</literal> and <literal>libraries</literal>
439           modules (for a full list of the projects available, see
440           <xref linkend="projects">).</para>
441
442           <para>Remember that if you do not have
443           <literal>happy</literal> and/or <literal>Alex</literal>
444           installed, you need to check them out as well.</para>
445         </listitem>
446       </itemizedlist>
447     </sect2>
448
449     <sect2 id="cvs-committing">
450       <title>Committing Changes</title>
451
452       <para>This is only if you have read-write access to the
453       repository. For anoncvs users, CVS will issue a &quot;read-only
454       repository&quot; error if you try to commit changes.</para>
455
456       <itemizedlist>
457         <listitem>
458           <para>Build the software, if necessary. Unless you're just
459           working on documentation, you'll probably want to build the
460           software in order to test any changes you make.</para>
461         </listitem>
462
463         <listitem>
464           <para>Make changes. Preferably small ones first.</para>
465         </listitem>
466
467         <listitem>
468           <para>Test them. You can see exactly what changes you've
469           made by using the <literal>cvs diff</literal> command:</para>
470 <screen>
471 $ cvs diff
472 </screen>
473           <para>lists all the changes (using the
474           <literal>diff</literal> command) in and below the current
475           directory. In emacs, <literal>C-c C-v =</literal> runs
476           <literal>cvs diff</literal> on the current buffer and shows
477           you the results.</para>
478         </listitem>
479
480         <listitem>
481           <para>If you changed something in the 
482           <literal>fptools/libraries</literal> subdirectories, also run
483           <literal>make html</literal> to check if the documentation can
484           be generated successfully, too.</para>
485         </listitem>
486
487         <listitem>
488           <para>Before checking in a change, you need to update your
489           source tree:</para>
490
491 <screen>
492 $ cd fptools
493 $ cvs update
494 </screen>
495           <para>This pulls in any changes that other people have made,
496           and merges them with yours. If there are any conflicts, CVS
497           will tell you, and you'll have to resolve them before you
498           can check your changes in. The documentation describes what
499           to do in the event of a conflict.</para>
500
501           <para>It's not always necessary to do a full cvs update
502           before checking in a change, since CVS will always tell you
503           if you try to check in a file that someone else has changed.
504           However, you should still update at regular intervals to
505           avoid making changes that don't work in conjuction with
506           changes that someone else made. Keeping an eye on what goes
507           by on the mailing list can help here.</para>
508         </listitem>
509
510         <listitem>
511           <para>When you're happy that your change isn't going to
512           break anything, check it in. For a one-file change:</para>
513
514 <screen>
515 $ cvs commit <replaceable>filename</replaceable>
516 </screen>
517
518           <para>CVS will then pop up an editor for you to enter a
519           &quot;commit message&quot;, this is just a short description
520           of what your change does, and will be kept in the history of
521           the file.</para>
522
523           <para>If you're using emacs, simply load up the file into a
524           buffer and type <literal>C-x C-q</literal>, and emacs will
525           prompt for a commit message and then check in the file for
526           you.</para>
527
528           <para>For a multiple-file change, things are a bit
529           trickier. There are several ways to do this, but this is the
530           way I find easiest. First type the commit message into a
531           temporary file. Then either</para>
532
533 <screen>
534 $ cvs commit -F <replaceable>commit-message</replaceable> <replaceable>file_1</replaceable> .... <replaceable>file_n</replaceable>
535 </screen>
536
537           <para>or, if nothing else has changed in this part of the
538           source tree, </para>
539
540 <screen>
541 $ cvs commit -F <replaceable>commit-message</replaceable> <replaceable>directory</replaceable>
542 </screen>
543
544           <para>where <replaceable>directory</replaceable> is a common
545           parent directory for all your changes, and
546           <replaceable>commit-message</replaceable> is the name of the
547           file containing the commit message.</para>
548
549           <para>Shortly afterwards, you'll get some mail from the
550           relevant mailing list saying which files changed, and giving
551           the commit message. For a multiple-file change, you should
552           still get only <emphasis>one</emphasis> message.</para>
553         </listitem>
554       </itemizedlist>
555     </sect2>
556
557     <sect2 id="cvs-update">
558       <title>Updating Your Source Tree</title>
559
560       <para>It can be tempting to cvs update just part of a source
561       tree to bring in some changes that someone else has made, or
562       before committing your own changes. This is NOT RECOMMENDED!
563       Quite often changes in one part of the tree are dependent on
564       changes in another part of the tree (the
565       <literal>mk/*.mk</literal> files are a good example where
566       problems crop up quite often). Having an inconsistent tree is a
567       major cause of headaches. </para>
568
569       <para>So, to avoid a lot of hassle, follow this recipe for
570       updating your tree:</para>
571
572 <screen>
573 $ cd fptools
574 $ cvs update -P 2&gt;&amp;1 | tee log</screen>
575
576       <para>Look at the log file, and fix any conflicts (denoted by a
577       <quote>C</quote> in the first column).  New directories may have
578       appeared in the repository; CVS doesn't check these out by
579       default, so to get new directories you have to explicitly do
580 <screen>
581 $ cvs update -d</screen>
582       in each project subdirectory.  Don't do this at the top level,
583       because then <emphasis>all</emphasis> the projects will be
584       checked out.</para>
585
586       <para>If you're using multiple build trees, then for every build
587       tree you have pointing at this source tree, you need to update
588       the links in case any new files have appeared: </para>
589
590 <screen>
591 $ cd <replaceable>build-tree</replaceable>
592 $ lndir <replaceable>source-tree</replaceable>
593 </screen>
594
595       <para>Some files might have been removed, so you need to remove
596       the links pointing to these non-existent files:</para>
597
598 <screen>
599 $ find . -xtype l -exec rm '{}' \;
600 </screen>
601
602       <para>To be <emphasis>really</emphasis> safe, you should do
603       </para>
604
605 <screen>$ gmake all</screen>
606
607       <para>from the top-level, to update the dependencies and build
608       any changed files. </para>
609     </sect2>
610
611     <sect2 id="cvs-tags">
612       <title>GHC Tag Policy</title>
613
614       <para>If you want to check out a particular version of GHC,
615       you'll need to know how we tag versions in the repository.  The
616       policy (as of 4.04) is:</para>
617
618       <itemizedlist>
619         <listitem>
620           <para>The tree is branched before every major release.  The
621           branch tag is <literal>ghc-x-xx-branch</literal>, where
622           <literal>x-xx</literal> is the version number of the release
623           with the <literal>'.'</literal> replaced by a
624           <literal>'-'</literal>.  For example, the 4.04 release lives
625           on <literal>ghc-4-04-branch</literal>.</para>
626         </listitem>
627
628         <listitem>
629           <para>The release itself is tagged with
630           <literal>ghc-x-xx</literal> (on the branch).  eg. 4.06 is
631           called <literal>ghc-4-06</literal>.</para>
632         </listitem>
633
634         <listitem>
635           <para>We didn't always follow these guidelines, so to see
636           what tags there are for previous versions, do <literal>cvs
637           log</literal> on a file that's been around for a while (like
638           <literal>fptools/ghc/README</literal>).</para>
639         </listitem>
640       </itemizedlist>
641
642       <para>So, to check out a fresh GHC 4.06 tree you would
643       do:</para>
644
645 <screen>
646      $ cvs co -r ghc-4-06 fpconfig
647      $ cd fptools
648      $ cvs co -r ghc-4-06 ghc hslibs
649 </screen>
650     </sect2>
651
652     <sect2 id="cvs-hints">
653       <title>General Hints</title>
654
655       <itemizedlist>
656         <listitem>
657           <para>As a general rule: commit changes in small units,
658           preferably addressing one issue or implementing a single
659           feature.  Provide a descriptive log message so that the
660           repository records exactly which changes were required to
661           implement a given feature/fix a bug. I've found this
662           <emphasis>very</emphasis> useful in the past for finding out
663           when a particular bug was introduced: you can just wind back
664           the CVS tree until the bug disappears.</para>
665         </listitem>
666
667         <listitem>
668           <para>Keep the sources at least *buildable* at any given
669           time. No doubt bugs will creep in, but it's quite easy to
670           ensure that any change made at least leaves the tree in a
671           buildable state. We do nightly builds of GHC to keep an eye
672           on what things work/don't work each day and how we're doing
673           in relation to previous verions. This idea is truely wrecked
674           if the compiler won't build in the first place!</para>
675         </listitem>
676
677         <listitem>
678           <para>To check out extra bits into an already-checked-out
679           tree, use the following procedure.  Suppose you have a
680           checked-out fptools tree containing just ghc, and you want
681           to add nofib to it:</para>
682
683 <screen>
684 $ cd fptools
685 $ cvs checkout nofib
686 </screen>
687
688           <para>or: </para>
689
690 <screen>
691 $ cd fptools
692 $ cvs update -d nofib
693 </screen>
694           
695           <para>(the -d flag tells update to create a new
696           directory). If you just want part of the nofib suite, you
697           can do </para>
698
699 <screen>
700 $ cd fptools
701 $ cvs checkout nofib/spectral
702 </screen>
703
704           <para>This works because <literal>nofib</literal> is a
705           module in its own right, and spectral is a subdirectory of
706           the nofib module. The path argument to checkout must always
707           start with a module name. There's no equivalent form of this
708           command using <literal>update</literal>.</para>
709         </listitem>
710       </itemizedlist>
711     </sect2>
712   </sect1>
713
714   <sect1 id="projects">
715     <title>What projects are there?</title>
716
717     <para>The <literal>fptools</literal> suite consists of several
718     <firstterm>projects</firstterm>, most of which can be downloaded,
719     built and installed individually.  Each project corresponds to a
720     subdirectory in the source tree, and if checking out from CVS then
721     each project can be checked out individually by sitting in the top
722     level of your source tree and typing <command>cvs checkout
723     <replaceable>project</replaceable></command>.</para>
724
725     <para>Here is a list of the projects currently available:</para>
726
727     <variablelist>
728       <varlistentry>
729         <term><literal>alex</literal></term>
730         <indexterm><primary><literal>alex</literal></primary>
731         <secondary>project</secondary></indexterm>
732         <listitem>
733           <para>The <ulink
734           url="http://www.haskell.org/alex/">Alex</ulink> lexical
735           analyser generator for Haskell.</para>
736         </listitem>
737       </varlistentry>
738
739       <varlistentry>
740         <term><literal>ghc</literal></term>
741         <indexterm><primary><literal>ghc</literal></primary>
742         <secondary>project</secondary></indexterm>
743         <listitem>
744           <para>The <ulink url="http://www.haskell.org/ghc/">Glasgow
745           Haskell Compiler</ulink> (minus libraries).  Absolutely
746           required for building GHC.</para>
747         </listitem>
748       </varlistentry>
749
750       <varlistentry>
751         <term><literal>glafp-utils</literal></term>
752         <indexterm><primary><literal>glafp-utils</literal></primary><secondary>project</secondary></indexterm>
753         <listitem>
754           <para>Utility programs, some of which are used by the
755           build/installation system.  Required for pretty much
756           everything.</para>
757         </listitem>
758       </varlistentry>
759
760       <varlistentry>
761         <term><literal>greencard</literal></term>
762         <indexterm><primary><literal>greencard</literal></primary><secondary>project</secondary></indexterm>
763         <listitem>
764           <para>The <ulink
765           url="http://www.haskell.org/greencard/">GreenCard</ulink>
766           system for generating Haskell foreign function
767           interfaces.</para>
768         </listitem>
769       </varlistentry>
770
771       <varlistentry>
772         <term><literal>haggis</literal></term>
773         <indexterm><primary><literal>haggis</literal></primary><secondary>project</secondary></indexterm>
774         <listitem>
775           <para>The <ulink
776           url="http://www.dcs.gla.ac.uk/fp/software/haggis/">Haggis</ulink>
777           Haskell GUI framework.</para>
778         </listitem>
779       </varlistentry>
780
781       <varlistentry>
782         <term><literal>haddock</literal></term>
783         <indexterm><primary><literal>haddock</literal></primary><secondary>project</secondary></indexterm>
784         <listitem>
785           <para>The <ulink
786           url="http://www.haskell.org/haddock/">Haddock</ulink>
787           documentation tool.</para>
788         </listitem>
789       </varlistentry>
790
791       <varlistentry>
792         <term><literal>happy</literal></term>
793         <indexterm><primary><literal>happy</literal></primary><secondary>project</secondary></indexterm>
794         <listitem>
795           <para>The <ulink
796           url="http://www.haskell.org/happy/">Happy</ulink> Parser
797           generator.</para>
798         </listitem>
799       </varlistentry>
800
801       <varlistentry>
802         <term><literal>hdirect</literal></term>
803         <indexterm><primary><literal>hdirect</literal></primary><secondary>project</secondary></indexterm>
804         <listitem>
805           <para>The <ulink
806           url="http://www.haskell.org/hdirect/">H/Direct</ulink>
807           Haskell interoperability tool.</para>
808         </listitem>
809       </varlistentry>
810
811       <varlistentry>
812         <term><literal>hood</literal></term>
813         <indexterm><primary><literal>hood</literal></primary><secondary>project</secondary></indexterm>
814         <listitem>
815           <para>The <ulink url="http://www.haskell.org/hood/">Haskell
816           Object Observation Debugger</ulink>.</para>
817         </listitem>
818       </varlistentry>
819
820       <varlistentry>
821         <term><literal>hslibs</literal></term>
822         <indexterm><primary><literal>hslibs</literal></primary><secondary>project</secondary></indexterm>
823         <listitem>
824           <para>Supplemental libraries for GHC
825           (<emphasis>required</emphasis> for building GHC).</para>
826         </listitem>
827       </varlistentry>
828
829       <varlistentry>
830         <term><literal>libraries</literal></term>
831         <indexterm><primary><literal></literal></primary><secondary>project</secondary></indexterm>
832         <listitem>
833           <para>Hierarchical Haskell library suite
834           (<emphasis>required</emphasis> for building GHC).</para>
835         </listitem>
836       </varlistentry>
837
838       <varlistentry>
839         <term><literal>mhms</literal></term>
840         <indexterm><primary><literal></literal></primary><secondary>project</secondary></indexterm>
841         <listitem>
842           <para>The Modular Haskell Metric System.</para>
843         </listitem>
844       </varlistentry>
845
846       <varlistentry>
847         <term><literal>nofib</literal></term>
848         <indexterm><primary><literal>nofib</literal></primary><secondary>project</secondary></indexterm>
849         <listitem>
850           <para>The NoFib suite: A collection of Haskell programs used
851           primarily for benchmarking.</para>
852         </listitem>
853       </varlistentry>
854
855       <varlistentry>
856         <term><literal>testsuite</literal></term>
857         <indexterm><primary><literal>testsuite</literal></primary><secondary>project</secondary></indexterm>
858         <listitem>
859           <para>A testing framework, including GHC's regression test
860           suite.</para>
861         </listitem>
862       </varlistentry>
863     </variablelist>
864
865     <para>So, to build GHC you need at least the
866     <literal>ghc</literal>, <literal>libraries</literal> and
867     <literal>hslibs</literal> projects (a GHC source distribution will
868     already include the bits you need).</para>
869   </sect1>
870
871   <sect1 id="sec-build-checks">
872     <title>Things to check before you start</title>
873
874     <para>Here's a list of things to check before you get
875     started.</para>
876
877     <orderedlist>
878
879       <listitem>
880         <indexterm><primary>Disk space needed</primary></indexterm>
881         <para>Disk space needed: from about 100Mb for a basic GHC
882         build, up to probably 500Mb for a GHC build with everything
883         included (libraries built several different ways,
884         etc.).</para>
885       </listitem>
886
887       <listitem>
888         <para>Use an appropriate machine / operating system.  <xref
889         linkend="sec-port-info"> lists the supported platforms; if
890         yours isn't amongst these then you can try porting GHC (see
891         <xref linkend="sec-porting-ghc">).</para>
892       </listitem>
893
894       <listitem>
895         <para>Be sure that the &ldquo;pre-supposed&rdquo; utilities are
896         installed.  <Xref LinkEnd="sec-pre-supposed">
897         elaborates.</para>
898       </listitem>
899
900       <listitem>
901         <para>If you have any problem when building or installing the
902         Glasgow tools, please check the &ldquo;known pitfalls&rdquo; (<Xref
903         LinkEnd="sec-build-pitfalls">).  Also check the FAQ for the
904         version you're building, which is part of the User's Guide and
905         available on the <ulink URL="http://www.haskell.org/ghc/" >GHC web
906         site</ulink>.</para>
907
908         <indexterm><primary>bugs</primary><secondary>known</secondary></indexterm>
909
910         <para>If you feel there is still some shortcoming in our
911         procedure or instructions, please report it.</para>
912
913         <para>For GHC, please see the <ulink
914         url="http://www.haskell.org/ghc/docs/latest/set/bug-reporting.html">bug-reporting
915         section of the GHC Users' Guide</ulink>, to maximise the
916         usefulness of your report.</para>
917
918         <indexterm><primary>bugs</primary><secondary>seporting</secondary></indexterm>
919         <para>If in doubt, please send a message to
920         <email>glasgow-haskell-bugs@haskell.org</email>.
921         <indexterm><primary>bugs</primary><secondary>mailing
922         list</secondary></indexterm></para>
923       </listitem>
924     </orderedlist>
925   </sect1>
926
927   <sect1 id="sec-port-info">
928     <title>What machines the Glasgow tools run on</title>
929
930 <indexterm><primary>ports</primary><secondary>GHC</secondary></indexterm>
931 <indexterm><primary>GHC</primary><secondary>ports</secondary></indexterm>
932 <indexterm><primary>platforms</primary><secondary>supported</secondary></indexterm>
933
934     <para>The main question is whether or not the Haskell compiler
935     (GHC) runs on your platform.</para>
936
937     <para>A &ldquo;platform&rdquo; is a
938     architecture/manufacturer/operating-system combination, such as
939     <literal>sparc-sun-solaris2</literal>.  Other common ones are
940     <literal>alpha-dec-osf2</literal>,
941     <literal>hppa1.1-hp-hpux9</literal>,
942     <literal>i386-unknown-linux</literal>,
943     <literal>i386-unknown-solaris2</literal>,
944     <literal>i386-unknown-freebsd</literal>,
945     <literal>i386-unknown-cygwin32</literal>,
946     <literal>m68k-sun-sunos4</literal>,
947     <literal>mips-sgi-irix5</literal>,
948     <literal>sparc-sun-sunos4</literal>,
949     <literal>sparc-sun-solaris2</literal>,
950     <literal>powerpc-ibm-aix</literal>.</para>
951
952     <para>Some libraries may only work on a limited number of
953     platforms; for example, a sockets library is of no use unless the
954     operating system supports the underlying BSDisms.</para>
955
956     <sect2>
957       <title>What platforms the Haskell compiler (GHC) runs on</title>
958
959       <indexterm><primary>fully-supported platforms</primary></indexterm>
960       <indexterm><primary>native-code generator</primary></indexterm>
961       <indexterm><primary>registerised ports</primary></indexterm>
962       <indexterm><primary>unregisterised ports</primary></indexterm>
963
964       <para>The GHC hierarchy of Porting Goodness: (a)&nbsp;Best is a
965       native-code generator; (b)&nbsp;next best is a
966       &ldquo;registerised&rdquo; port; (c)&nbsp;the bare minimum is an
967       &ldquo;unregisterised&rdquo; port.
968       (&ldquo;Unregisterised&rdquo; is so terrible that we won't say
969       more about it).</para>
970
971       <para>We use Sparcs running Solaris 2.7 and x86 boxes running
972       FreeBSD and Linux, so those are the best supported platforms,
973       unsurprisingly.</para>
974
975       <para>Here's everything that's known about GHC ports.  We
976       identify platforms by their &ldquo;canonical&rdquo;
977       CPU/Manufacturer/OS triple.</para>
978
979       <variablelist>
980         <varlistentry>
981           <term>alpha-dec-{osf,linux,freebsd,openbsd,netbsd}:</term>
982           <indexterm><primary>alpha-dec-osf</primary></indexterm>
983           <indexterm><primary>alpha-dec-linux</primary></indexterm>
984           <indexterm><primary>alpha-dec-freebsd</primary></indexterm>
985           <indexterm><primary>alpha-dec-openbsd</primary></indexterm>
986           <indexterm><primary>alpha-dec-netbsd</primary></indexterm>
987           
988           <listitem>
989             <para>The OSF port is currently working (as of GHC version
990             5.02.1) and well supported.  The native code generator is
991             currently non-working.  Other operating systems will
992             require some minor porting.</para>
993           </listitem>
994         </varlistentry>
995
996         <varlistentry>
997           <term>sparc-sun-sunos4</term>
998           <indexterm><primary>sparc-sun-sunos4</primary></indexterm>
999           <listitem>
1000             <para>Probably works with minor tweaks, hasn't been tested
1001             for a while.</para>
1002           </listitem>
1003         </varlistentry>
1004
1005         <varlistentry>
1006           <term>sparc-sun-solaris2</term>
1007           <indexterm><primary>sparc-sun-solaris2</primary></indexterm>
1008           <listitem>
1009             <para>Fully supported (at least for Solaris 2.7 and 2.6),
1010             including native-code generator.</para>
1011           </listitem>
1012         </varlistentry>
1013
1014         <varlistentry>
1015           <term>sparc-unknown-openbsd</term>
1016           <indexterm><primary>sparc-unknown-openbsd</primary></indexterm>
1017           <listitem>
1018             <para>Supported, including native-code generator. The
1019             same should also be true of NetBSD</para>
1020           </listitem>
1021         </varlistentry>
1022
1023         <varlistentry>
1024           <term>hppa1.1-hp-hpux (HP-PA boxes running HPUX 9.x)</term>
1025           <indexterm><primary>hppa1.1-hp-hpux</primary></indexterm>
1026           <listitem>
1027             <para>A registerised port is available for version 4.08,
1028             but GHC hasn't been built on that platform since (as far
1029             as we know).  No native-code generator.</para>
1030           </listitem>
1031         </varlistentry>
1032
1033         <varlistentry>
1034           <term>i386-unknown-linux (PCs running Linux, ELF binary format)</term>
1035           <indexterm><primary>i386-*-linux</primary></indexterm>
1036           <listitem>
1037             <para>GHC works registerised and has a native code
1038             generator.  You <Emphasis>must</Emphasis> have GCC 2.7.x
1039             or later.  NOTE about <literal>glibc</literal> versions:
1040             GHC binaries built on a system running <literal>glibc
1041             2.0</literal> won't work on a system running
1042             <literal>glibc 2.1</literal>, and vice versa.  In general,
1043             don't expect compatibility between
1044             <literal>glibc</literal> versions, even if the shared
1045             library version hasn't changed.</para>
1046           </listitem>
1047         </varlistentry>
1048
1049         <varlistentry>
1050           <term>i386-unknown-freebsd (PCs running FreeBSD 2.2 or
1051           higher)</term>
1052           <indexterm><primary>i386-unknown-freebsd</primary></indexterm>
1053           <listitem>
1054             <para>GHC works registerised.  Pre-built packages are
1055             available in the native package format, so if you just
1056             need binaries you're better off just installing the
1057             package (it might even be on your installation
1058             CD!).</para>
1059           </listitem>
1060         </varlistentry>
1061
1062         <varlistentry>
1063           <term>i386-unknown-openbsd (PCs running OpenBSD)</term>
1064           <indexterm><primary>i386-unknown-openbsd</primary></indexterm> 
1065           <listitem>
1066             <para>Supported, with native code generator.  Packages are
1067             available through the ports system in the native package
1068             format.</para>
1069           </listitem>
1070         </varlistentry>
1071
1072         <varlistentry>
1073           <term>i386-unknown-netbsd (PCs running NetBSD and
1074             OpenBSD)</term>
1075             <indexterm><primary>i386-unknown-netbsd</primary></indexterm>
1076           <listitem>
1077             <para>Will require some minor porting effort, but should
1078             work registerised.</para>
1079           </listitem>
1080         </varlistentry>
1081
1082         <varlistentry>
1083           <term>i386-unknown-mingw32 (PCs running Windows)</term>
1084           <indexterm><primary>i386-unknown-mingw32</primary></indexterm>
1085           <listitem>
1086             <para>Fully supported under Win9x, WinNT, Win2k, and
1087             WinXP.  Includes a native code generator.  Building from
1088             source requires a recent <ulink
1089             url="http://www.cygwin.com/">Cygwin</ulink> distribution
1090             to be installed.</para>
1091           </listitem>
1092         </varlistentry>
1093
1094         <varlistentry>
1095           <term>ia64-unknown-linux</term>
1096           <indexterm><primary>ia64-unknown-linux</primary></indexterm>
1097           <listitem>
1098             <para>Supported, except there is no native code
1099             generator.</para>
1100           </listitem>
1101         </varlistentry>
1102
1103         <varlistentry>
1104           <term>x86_64-unknown-linux</term>
1105           <indexterm><primary>x86_64-unknown-linux</primary></indexterm>
1106           <listitem>
1107             <para>GHC currently works unregisterised.  A registerised
1108             port is in progress.</para>
1109           </listitem>
1110         </varlistentry>
1111
1112         <varlistentry>
1113           <term>mips-sgi-irix5</term>
1114           <indexterm><primary>mips-sgi-irix[5-6]</primary></indexterm>
1115           <listitem>
1116             <para>Port has worked in the past, but hasn't been tested
1117             for some time (and will certainly have rotted in various
1118             ways).  As usual, we don't have access to machines and
1119             there hasn't been an overwhelming demand for this port,
1120             but feel free to get in touch.</para>
1121           </listitem>
1122         </varlistentry>
1123
1124         <varlistentry>
1125           <term>mips64-sgi-irix6</term>
1126           <indexterm><primary>mips-sgi-irix6</primary></indexterm>
1127           <listitem>
1128             <para>GHC currently works unregisterised.</para>
1129           </listitem>
1130         </varlistentry>
1131
1132         <varlistentry>
1133           <term>powerpc-ibm-aix</term>
1134           <indexterm><primary>powerpc-ibm-aix</primary></indexterm>
1135           <listitem>
1136             <para>Port currently doesn't work, needs some minimal
1137             porting effort.  As usual, we don't have access to
1138             machines and there hasn't been an overwhelming demand for
1139             this port, but feel free to get in touch.</para>
1140           </listitem>
1141         </varlistentry>
1142
1143         <varlistentry>
1144           <term>powerpc-apple-darwin</term>
1145           <indexterm><primary>powerpc-apple-darwin</primary></indexterm> 
1146           <listitem>
1147             <para>Supported registerised.  Native code generator is
1148             almost working.</para>
1149           </listitem>
1150         </varlistentry>
1151
1152         <varlistentry>
1153           <term>powerpc-apple-linux</term>
1154           <indexterm><primary>powerpc-apple-linux</primary></indexterm> 
1155           <listitem>
1156             <para>Not supported (yet).</para>
1157           </listitem>
1158         </varlistentry>
1159       </variablelist>
1160
1161       <para>Various other systems have had GHC ported to them in the
1162       distant past, including various Motorola 68k boxes.  The 68k
1163       support still remains, but porting to one of these systems will
1164       certainly be a non-trivial task.</para>
1165     </sect2>
1166
1167     <sect2>
1168       <title>What machines the other tools run on</title>
1169
1170       <para>Unless you hear otherwise, the other tools work if GHC
1171       works.</para>
1172     </sect2>
1173   </sect1>
1174
1175
1176   <sect1 id="sec-pre-supposed">
1177     <title>Installing pre-supposed utilities</title>
1178
1179     <indexterm><primary>pre-supposed utilities</primary></indexterm>
1180     <indexterm><primary>utilities, pre-supposed</primary></indexterm>
1181
1182     <para>Here are the gory details about some utility programs you
1183     may need; <command>perl</command>, <command>gcc</command> and
1184     <command>happy</command> are the only important
1185     ones. (PVM<indexterm><primary>PVM</primary></indexterm> is
1186     important if you're going for Parallel Haskell.)  The
1187     <command>configure</command><indexterm><primary>configure</primary></indexterm>
1188     script will tell you if you are missing something.</para>
1189
1190     <variablelist>
1191
1192       <varlistentry>
1193         <term>GHC</term>
1194         <indexterm><primary>pre-supposed: GHC</primary></indexterm>
1195         <indexterm><primary>GHC, pre-supposed</primary></indexterm>
1196         <listitem>
1197           <para>GHC is required to build many of the tools, including
1198           GHC itself.  If you need to port GHC to your platform
1199           because there isn't a binary distribution of GHC available,
1200           then see <xref linkend="sec-porting-ghc">.</para>
1201
1202           <para>Which version of GHC you need will depend on the
1203           packages you intend to build.  GHC itself will normally
1204           build using one of several older versions of itself - check
1205           the announcement or release notes for details.</para>
1206         </listitem>
1207       </varlistentry>
1208
1209       <varlistentry>
1210         <term>Perl</term>
1211         <indexterm><primary>pre-supposed: Perl</primary></indexterm>
1212         <indexterm><primary>Perl, pre-supposed</primary></indexterm>
1213         <listitem>
1214           <para><emphasis>You have to have Perl to proceed!</emphasis>
1215           Perl version 5 at least is required.  GHC has been known to
1216           tickle bugs in Perl, so if you find that Perl crashes when
1217           running GHC try updating (or downgrading) your Perl
1218           installation.  Versions of Perl that we use and are known to
1219           be fairly stable are 5.005 and 5.6.1.</para>
1220
1221           <para>For Win32 platforms, you should use the binary
1222           supplied in the InstallShield (copy it to
1223           <filename>/bin</filename>).  The Cygwin-supplied Perl seems
1224           not to work.</para>
1225
1226           <para>Perl should be put somewhere so that it can be invoked
1227           by the <literal>&num;!</literal> script-invoking
1228           mechanism. The full pathname may need to be less than 32
1229           characters long on some systems.</para>
1230         </listitem>
1231       </varlistentry>
1232
1233       <varlistentry>
1234         <term>GNU C (<command>gcc</command>)</term>
1235         <indexterm><primary>pre-supposed: GCC (GNU C
1236         compiler)</primary></indexterm> <indexterm><primary>GCC (GNU C
1237         compiler), pre-supposed</primary></indexterm>
1238         <listitem>
1239           <para>We recommend using GCC version 2.95.2 on all
1240           platforms.  Failing that, version 2.7.2 is stable on most
1241           platforms.  Earlier versions of GCC can be assumed not to
1242           work, and versions in between 2.7.2 and 2.95.2 (including
1243           <command>egcs</command>) have varying degrees of stability
1244           depending on the platform.</para>
1245
1246           <para>GCC 3.2 is currently known to have problems building
1247           GHC on Sparc, but is stable on x86.</para>
1248           
1249           <para>If your GCC dies with &ldquo;internal error&rdquo; on
1250           some GHC source file, please let us know, so we can report
1251           it and get things improved.  (Exception: on x86
1252           boxes&mdash;you may need to fiddle with GHC's
1253           <option>-monly-N-regs</option> option; see the User's
1254           Guide)</para>
1255         </listitem>
1256       </varlistentry>
1257
1258       <varlistentry>
1259         <term>GNU Make</term>
1260         <indexterm><primary>make</primary><secondary>GNU</secondary>
1261         </indexterm>
1262         <listitem>
1263           <para>The fptools build system makes heavy use of features
1264           specific to GNU <command>make</command>, so you must have
1265           this installed in order to build any of the fptools
1266           suite.</para>
1267         </listitem>
1268       </varlistentry>
1269
1270       <varlistentry>
1271         <term>Happy</term>
1272         <indexterm><primary>Happy</primary></indexterm>
1273         <listitem>
1274           <para>Happy is a parser generator tool for Haskell, and is
1275           used to generate GHC's parsers.  Happy is written in
1276           Haskell, and is a project in the CVS repository
1277           (<literal>fptools/happy</literal>).  It can be built from
1278           source, but bear in mind that you'll need GHC installed in
1279           order to build it.  To avoid the chicken/egg problem,
1280           install a binary distribution of either Happy or GHC to get
1281           started.  Happy distributions are available from <ulink
1282           url="http://www.haskell.org/happy/">Happy's Web
1283           Page</ulink>.</para>
1284         </listitem>
1285       </varlistentry>
1286
1287       <varlistentry>
1288         <term>Alex</term>
1289         <indexterm><primary>Alex</primary></indexterm>
1290         <listitem>
1291           <para>Alex is a lexical-analyser generator for Haskell,
1292           which GHC uses to generate its lexer.  Like Happy, Alex is
1293           written in Haskell and is a project in the CVS repository.
1294           Alex distributions are available from <ulink
1295           url="http://www.haskell.org/alex/">Alex's Web
1296           Page</ulink>.</para>
1297         </listitem>
1298       </varlistentry>
1299
1300       <varlistentry>
1301         <term>Autoreconf</term>
1302         <indexterm><primary>pre-supposed: Autoreconf</primary></indexterm>
1303         <indexterm><primary>Autoreconf, pre-supposed</primary></indexterm>
1304         <listitem>
1305           <para>GNU Autoreconf is needed if you intend to build from the
1306           CVS sources, it is <emphasis>not</emphasis> needed if you
1307           just intend to build a standard source distribution.</para>
1308
1309           <para>Version 2.52 or later of autoreconf is required.
1310           NB. vesrion 2.13 will no longer work, as of GHC version
1311           6.1.</para>
1312
1313           <para>Autoreconf builds the <command>configure</command>
1314           script from <filename>configure.ac</filename> and
1315           <filename>aclocal.m4</filename>.  If you modify either of
1316           these files, you'll need <command>autoreconf</command> to
1317           rebuild <filename>configure</filename>.</para>
1318         </listitem>
1319       </varlistentry>
1320
1321       <varlistentry>
1322         <term><command>sed</command></term>
1323         <indexterm><primary>pre-supposed: sed</primary></indexterm>
1324         <indexterm><primary>sed, pre-supposed</primary></indexterm>
1325         <listitem>
1326           <para>You need a working <command>sed</command> if you are
1327           going to build from sources.  The build-configuration stuff
1328           needs it.  GNU sed version 2.0.4 is no good!  It has a bug
1329           in it that is tickled by the build-configuration.  2.0.5 is
1330           OK. Others are probably OK too (assuming we don't create too
1331           elaborate configure scripts.)</para>
1332         </listitem>
1333       </varlistentry>
1334     </variablelist>
1335
1336     <para>One <literal>fptools</literal> project is worth a quick note
1337     at this point, because it is useful for all the others:
1338     <literal>glafp-utils</literal> contains several utilities which
1339     aren't particularly Glasgow-ish, but Occasionally Indispensable.
1340     Like <command>lndir</command> for creating symbolic link
1341     trees.</para>
1342
1343     <sect2 id="pre-supposed-gph-tools">
1344       <title>Tools for building parallel GHC (GPH)</title>
1345
1346       <variablelist>
1347         <varlistentry>
1348           <term>PVM version 3:</term>
1349           <indexterm><primary>pre-supposed: PVM3 (Parallel Virtual Machine)</primary></indexterm>
1350           <indexterm><primary>PVM3 (Parallel Virtual Machine), pre-supposed</primary></indexterm>
1351           <listitem>
1352             <para>PVM is the Parallel Virtual Machine on which
1353             Parallel Haskell programs run.  (You only need this if you
1354             plan to run Parallel Haskell.  Concurrent Haskell, which
1355             runs concurrent threads on a uniprocessor doesn't need
1356             it.)  Underneath PVM, you can have (for example) a network
1357             of workstations (slow) or a multiprocessor box
1358             (faster).</para>
1359
1360             <para>The current version of PVM is 3.3.11; we use 3.3.7.
1361             It is readily available on the net; I think I got it from
1362             <literal>research.att.com</literal>, in
1363             <filename>netlib</filename>.</para>
1364
1365             <para>A PVM installation is slightly quirky, but easy to
1366             do.  Just follow the <filename>Readme</filename>
1367             instructions.</para>
1368           </listitem>
1369         </varlistentry>
1370
1371         <varlistentry>
1372           <term><command>bash</command>:</term>
1373           <indexterm><primary>bash, presupposed (Parallel Haskell only)</primary></indexterm>
1374           <listitem>
1375             <para>Sadly, the <command>gr2ps</command> script, used to
1376             convert &ldquo;parallelism profiles&rdquo; to PostScript,
1377             is written in Bash (GNU's Bourne Again shell).  This bug
1378             will be fixed (someday).</para>
1379           </listitem>
1380         </varlistentry>
1381       </variablelist>
1382     </sect2>
1383
1384     <sect2 id="pre-supposed-other-tools">
1385       <title>Other useful tools</title>
1386
1387       <variablelist>
1388         <varlistentry>
1389           <term>Flex</term>
1390           <indexterm><primary>pre-supposed: flex</primary></indexterm> 
1391           <indexterm><primary>flex, pre-supposed</primary></indexterm>
1392           <listitem>
1393             <para>This is a quite-a-bit-better-than-Lex lexer.  Used
1394             to build a couple of utilities in
1395             <literal>glafp-utils</literal>.  Depending on your
1396             operating system, the supplied <command>lex</command> may
1397             or may not work; you should get the GNU version.</para>
1398           </listitem>
1399         </varlistentry>
1400       </variablelist>
1401
1402       <para>More tools are required if you want to format the documentation
1403       that comes with GHC and other fptools projects.  See <xref
1404       linkend="building-docs">.</para>
1405     </sect2>
1406   </sect1>
1407
1408   <sect1 id="sec-building-from-source">
1409     <title>Building from source</title>
1410
1411     <indexterm><primary>Building from source</primary></indexterm>
1412     <indexterm><primary>Source, building from</primary></indexterm>
1413
1414     <para>You've been rash enough to want to build some of the Glasgow
1415     Functional Programming tools (GHC, Happy, nofib, etc.) from
1416     source.  You've slurped the source, from the CVS repository or
1417     from a source distribution, and now you're sitting looking at a
1418     huge mound of bits, wondering what to do next.</para>
1419
1420     <para>Gingerly, you type <command>make</command>.  Wrong
1421     already!</para>
1422
1423     <para>This rest of this guide is intended for duffers like me, who
1424     aren't really interested in Makefiles and systems configurations,
1425     but who need a mental model of the interlocking pieces so that
1426     they can make them work, extend them consistently when adding new
1427     software, and lay hands on them gently when they don't
1428     work.</para>
1429
1430     <sect2 id="quick-start">
1431       <title>Quick Start</title>
1432
1433       <para>If you are starting from a source distribution, and just
1434       want a completely standard build, then the following should
1435       work:</para>
1436
1437 <screen>$ autoreconf
1438 $ ./configure
1439 $ make
1440 $ make install
1441 </screen>
1442
1443       <para>For GHC, this will do a 2-stage bootstrap build of the
1444       compiler, with profiling libraries, and install the
1445       results.</para>
1446
1447       <para>If you want to do anything at all non-standard, or you
1448       want to do some development, read on...</para>
1449     </sect2>
1450
1451     <sect2 id="sec-source-tree">
1452       <title>Your source tree</title>
1453
1454       <para>The source code is held in your <emphasis>source
1455       tree</emphasis>.  The root directory of your source tree
1456       <emphasis>must</emphasis> contain the following directories and
1457       files:</para>
1458
1459       <itemizedlist>
1460         <listitem>
1461           <para><filename>Makefile</filename>: the root
1462           Makefile.</para>
1463         </listitem>
1464
1465         <listitem>
1466           <para><filename>mk/</filename>: the directory that contains
1467           the main Makefile code, shared by all the
1468           <literal>fptools</literal> software.</para>
1469         </listitem>
1470
1471         <listitem>
1472           <para><filename>configure.ac</filename>,
1473           <filename>config.sub</filename>,
1474           <filename>config.guess</filename>: these files support the
1475           configuration process.</para>
1476         </listitem>
1477
1478         <listitem>
1479           <para><filename>install-sh</filename>.</para>
1480         </listitem>
1481       </itemizedlist>
1482
1483       <para>All the other directories are individual
1484       <emphasis>projects</emphasis> of the <literal>fptools</literal>
1485       system&mdash;for example, the Glasgow Haskell Compiler
1486       (<literal>ghc</literal>), the Happy parser generator
1487       (<literal>happy</literal>), the <literal>nofib</literal>
1488       benchmark suite, and so on.  You can have zero or more of these.
1489       Needless to say, some of them are needed to build others.</para>
1490
1491       <para>The important thing to remember is that even if you want
1492       only one project (<literal>happy</literal>, say), you must have
1493       a source tree whose root directory contains
1494       <filename>Makefile</filename>, <filename>mk/</filename>,
1495       <filename>configure.ac</filename>, and the project(s) you want
1496       (<filename>happy/</filename> in this case).  You cannot get by
1497       with just the <filename>happy/</filename> directory.</para>
1498     </sect2>
1499
1500     <sect2>
1501       <title>Build trees</title>
1502       <indexterm><primary>build trees</primary></indexterm>
1503       <indexterm><primary>link trees, for building</primary></indexterm>
1504
1505       <para>If you just want to build the software once on a single
1506       platform, then your source tree can also be your build tree, and
1507       you can skip the rest of this section.</para>
1508
1509       <para>We often want to build multiple versions of our software
1510       for different architectures, or with different options
1511       (e.g. profiling).  It's very desirable to share a single copy of
1512       the source code among all these builds.</para>
1513
1514       <para>So for every source tree we have zero or more
1515       <emphasis>build trees</emphasis>.  Each build tree is initially
1516       an exact copy of the source tree, except that each file is a
1517       symbolic link to the source file, rather than being a copy of
1518       the source file.  There are &ldquo;standard&rdquo; Unix
1519       utilities that make such copies, so standard that they go by
1520       different names:
1521       <command>lndir</command><indexterm><primary>lndir</primary></indexterm>,
1522       <command>mkshadowdir</command><indexterm><primary>mkshadowdir</primary></indexterm>
1523       are two (If you don't have either, the source distribution
1524       includes sources for the X11
1525       <command>lndir</command>&mdash;check out
1526       <filename>fptools/glafp-utils/lndir</filename>). See <Xref
1527       LinkEnd="sec-storysofar"> for a typical invocation.</para>
1528
1529       <para>The build tree does not need to be anywhere near the
1530       source tree in the file system.  Indeed, one advantage of
1531       separating the build tree from the source is that the build tree
1532       can be placed in a non-backed-up partition, saving your systems
1533       support people from backing up untold megabytes of
1534       easily-regenerated, and rapidly-changing, gubbins.  The golden
1535       rule is that (with a single exception&mdash;<XRef
1536       LinkEnd="sec-build-config">) <emphasis>absolutely everything in
1537       the build tree is either a symbolic link to the source tree, or
1538       else is mechanically generated</emphasis>.  It should be
1539       perfectly OK for your build tree to vanish overnight; an hour or
1540       two compiling and you're on the road again.</para>
1541
1542       <para>You need to be a bit careful, though, that any new files
1543       you create (if you do any development work) are in the source
1544       tree, not a build tree!</para>
1545
1546       <para>Remember, that the source files in the build tree are
1547       <emphasis>symbolic links</emphasis> to the files in the source
1548       tree.  (The build tree soon accumulates lots of built files like
1549       <filename>Foo.o</filename>, as well.)  You can
1550       <emphasis>delete</emphasis> a source file from the build tree
1551       without affecting the source tree (though it's an odd thing to
1552       do).  On the other hand, if you <emphasis>edit</emphasis> a
1553       source file from the build tree, you'll edit the source-tree
1554       file directly.  (You can set up Emacs so that if you edit a
1555       source file from the build tree, Emacs will silently create an
1556       edited copy of the source file in the build tree, leaving the
1557       source file unchanged; but the danger is that you think you've
1558       edited the source file whereas actually all you've done is edit
1559       the build-tree copy.  More commonly you do want to edit the
1560       source file.)</para>
1561
1562       <para>Like the source tree, the top level of your build tree
1563       must be (a linked copy of) the root directory of the
1564       <literal>fptools</literal> suite.  Inside Makefiles, the root of
1565       your build tree is called
1566       <constant>&dollar;(FPTOOLS&lowbar;TOP)</constant><indexterm><primary>FPTOOLS&lowbar;TOP</primary></indexterm>.
1567       In the rest of this document path names are relative to
1568       <constant>&dollar;(FPTOOLS&lowbar;TOP)</constant> unless
1569       otherwise stated.  For example, the file
1570       <filename>ghc/mk/target.mk</filename> is actually
1571       <filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/ghc/mk/target.mk</filename>.</para>
1572     </sect2>
1573
1574     <sect2 id="sec-build-config">
1575       <title>Getting the build you want</title>
1576
1577       <para>When you build <literal>fptools</literal> you will be
1578       compiling code on a particular <emphasis>host
1579       platform</emphasis>, to run on a particular <emphasis>target
1580       platform</emphasis> (usually the same as the host
1581       platform)<indexterm><primary>platform</primary></indexterm>.
1582       The difficulty is that there are minor differences between
1583       different platforms; minor, but enough that the code needs to be
1584       a bit different for each.  There are some big differences too:
1585       for a different architecture we need to build GHC with a
1586       different native-code generator.</para>
1587
1588       <para>There are also knobs you can turn to control how the
1589       <literal>fptools</literal> software is built.  For example, you
1590       might want to build GHC optimised (so that it runs fast) or
1591       unoptimised (so that you can compile it fast after you've
1592       modified it.  Or, you might want to compile it with debugging on
1593       (so that extra consistency-checking code gets included) or off.
1594       And so on.</para>
1595
1596       <para>All of this stuff is called the
1597       <emphasis>configuration</emphasis> of your build.  You set the
1598       configuration using a three-step process.</para>
1599
1600       <variablelist>
1601         <varlistentry>
1602           <term>Step 1: get ready for configuration.</term>
1603           <listitem>
1604             <para>NOTE: if you're starting from a source distribution,
1605             rather than CVS sources, you can skip this step.</para>
1606
1607             <para>Change directory to
1608             <constant>&dollar;(FPTOOLS&lowbar;TOP)</constant> and
1609             issue the command
1610 <ProgramListing>
1611 autoreconf
1612 </ProgramListing>
1613             <indexterm><primary>autoreconf</primary></indexterm>
1614             (with no arguments). This GNU program converts
1615             <filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/configure.ac</filename>
1616             to a shell script called
1617             <filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/configure</filename>.
1618               If <command>autoreconf</command> bleats that it can't write the file <filename>configure</filename>,
1619               then delete the latter and try again.  Note that you must use <command>autoreconf</command>,
1620               and not the old <command>autoconf</command>!  If you erroneously use the latter, you'll get 
1621               a message like "No rule to make target 'mk/config.h.in'".
1622             </para>
1623
1624             <para>Some projects, including GHC, have their own
1625             configure script.  If there's an
1626             <constant>&dollar;(FPTOOLS&lowbar;TOP)/&lt;project&gt;/configure.ac</constant>,
1627             then you need to run <command>autoreconf</command> in that
1628             directory too.</para>
1629
1630             <para>These steps are completely
1631             platform-independent; they just mean that the
1632             human-written file (<filename>configure.ac</filename>) can
1633             be short, although the resulting shell script,
1634             <command>configure</command>, and
1635             <filename>mk/config.h.in</filename>, are long.</para>
1636           </listitem>
1637         </varlistentry>
1638
1639         <varlistentry>
1640           <term>Step 2: system configuration.</term>
1641           <listitem>
1642             <para>Runs the newly-created <command>configure</command>
1643             script, thus:</para>
1644
1645 <ProgramListing>
1646 ./configure <optional><parameter>args</parameter></optional>
1647 </ProgramListing>
1648
1649             <para><command>configure</command>'s mission is to scurry
1650             round your computer working out what architecture it has,
1651             what operating system, whether it has the
1652             <Function>vfork</Function> system call, where
1653             <command>tar</command> is kept, whether
1654             <command>gcc</command> is available, where various obscure
1655             <literal>&num;include</literal> files are, whether it's a
1656             leap year, and what the systems manager had for lunch.  It
1657             communicates these snippets of information in two
1658             ways:</para>
1659
1660             <itemizedlist>
1661               <listitem>
1662                 
1663                 <para>It translates
1664                 <filename>mk/config.mk.in</filename><indexterm><primary>config.mk.in</primary></indexterm>
1665                 to
1666                 <filename>mk/config.mk</filename><indexterm><primary>config.mk</primary></indexterm>,
1667                 substituting for things between
1668                 &ldquo;<literal>@</literal>&rdquo; brackets.  So,
1669                 &ldquo;<literal>@HaveGcc@</literal>&rdquo; will be
1670                 replaced by &ldquo;<literal>YES</literal>&rdquo; or
1671                 &ldquo;<literal>NO</literal>&rdquo; depending on what
1672                 <command>configure</command> finds.
1673                 <filename>mk/config.mk</filename> is included by every
1674                 Makefile (directly or indirectly), so the
1675                 configuration information is thereby communicated to
1676                 all Makefiles.</para>
1677                 </listitem>
1678
1679               <listitem>
1680                 <para> It translates
1681                 <filename>mk/config.h.in</filename><indexterm><primary>config.h.in</primary></indexterm>
1682                 to
1683                 <filename>mk/config.h</filename><indexterm><primary>config.h</primary></indexterm>.
1684                 The latter is <literal>&num;include</literal>d by
1685                 various C programs, which can thereby make use of
1686                 configuration information.</para>
1687               </listitem>
1688             </itemizedlist>
1689
1690             <para><command>configure</command> takes some optional
1691             arguments.  Use <literal>./configure --help</literal> to
1692             get a list of the available arguments.  Here are some of
1693             the ones you might need:</para>
1694
1695             <variablelist>
1696               <varlistentry>
1697                 <term><literal>--with-ghc=<parameter>path</parameter></literal></term>
1698                 <indexterm><primary><literal>--with-ghc</literal></primary>
1699                 </indexterm>
1700                 <listitem>
1701                   <para>Specifies the path to an installed GHC which
1702                   you would like to use.  This compiler will be used
1703                   for compiling GHC-specific code (eg. GHC itself).
1704                   This option <emphasis>cannot</emphasis> be specified
1705                   using <filename>build.mk</filename> (see later),
1706                   because <command>configure</command> needs to
1707                   auto-detect the version of GHC you're using.  The
1708                   default is to look for a compiler named
1709                   <literal>ghc</literal> in your path.</para>
1710                 </listitem>
1711               </varlistentry>
1712               
1713               <varlistentry>
1714                 <term><literal>--with-hc=<parameter>path</parameter></literal></term>
1715                 <indexterm><primary><literal>--with-hc</literal></primary>
1716                 </indexterm>
1717                 <listitem>
1718                   <para>Specifies the path to any installed Haskell
1719                   compiler.  This compiler will be used for compiling
1720                   generic Haskell code.  The default is to use
1721                   <literal>ghc</literal>.</para>
1722                 </listitem>
1723               </varlistentry>
1724               
1725               <varlistentry>
1726                 <term><literal>--with-gcc=<parameter>path</parameter></literal></term>
1727                 <indexterm><primary><literal>--with-gcc</literal></primary>
1728                 </indexterm>
1729                 <listitem>
1730                   <para>Specifies the path to the installed GCC. This
1731                   compiler will be used to compile all C files,
1732                   <emphasis>except</emphasis> any generated by the
1733                   installed Haskell compiler, which will have its own
1734                   idea of which C compiler (if any) to use.  The
1735                   default is to use <literal>gcc</literal>.</para>
1736                 </listitem>
1737               </varlistentry>
1738             </variablelist>
1739             
1740             <para><command>configure</command> caches the results of
1741             its run in <filename>config.cache</filename>.  Quite often
1742             you don't want that; you're running
1743             <command>configure</command> a second time because
1744             something has changed.  In that case, simply delete
1745             <filename>config.cache</filename>.</para>
1746           </listitem>
1747         </varlistentry>
1748         
1749         <varlistentry>
1750           <term>Step 3: build configuration.</term>
1751           <listitem>
1752             <para>Next, you say how this build of
1753             <literal>fptools</literal> is to differ from the standard
1754             defaults by creating a new file
1755             <filename>mk/build.mk</filename><indexterm><primary>build.mk</primary></indexterm>
1756             <emphasis>in the build tree</emphasis>.  This file is the
1757             one and only file you edit in the build tree, precisely
1758             because it says how this build differs from the source.
1759             (Just in case your build tree does die, you might want to
1760             keep a private directory of <filename>build.mk</filename>
1761             files, and use a symbolic link in each build tree to point
1762             to the appropriate one.)  So
1763             <filename>mk/build.mk</filename> never exists in the
1764             source tree&mdash;you create one in each build tree from
1765             the template.  We'll discuss what to put in it
1766             shortly.</para>
1767           </listitem>
1768         </varlistentry>
1769       </variablelist>
1770
1771       <para>And that's it for configuration. Simple, eh?</para>
1772
1773       <para>What do you put in your build-specific configuration file
1774       <filename>mk/build.mk</filename>?  <emphasis>For almost all
1775       purposes all you will do is put make variable definitions that
1776       override those in</emphasis>
1777       <filename>mk/config.mk.in</filename>.  The whole point of
1778       <filename>mk/config.mk.in</filename>&mdash;and its derived
1779       counterpart <filename>mk/config.mk</filename>&mdash;is to define
1780       the build configuration. It is heavily commented, as you will
1781       see if you look at it.  So generally, what you do is look at
1782       <filename>mk/config.mk.in</filename>, and add definitions in
1783       <filename>mk/build.mk</filename> that override any of the
1784       <filename>config.mk</filename> definitions that you want to
1785       change.  (The override occurs because the main boilerplate file,
1786       <filename>mk/boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>,
1787       includes <filename>build.mk</filename> after
1788       <filename>config.mk</filename>.)</para>
1789
1790      <para>For your convenience, there's a file called <filename>build.mk.sample</filename>
1791      that can serve as a starting point for your <filename>build.mk</filename>.</para>
1792
1793       <para>For example, <filename>config.mk.in</filename> contains
1794       the definition:</para>
1795
1796 <ProgramListing>
1797 GhcHcOpts=-O -Rghc-timing
1798 </ProgramListing>
1799
1800       <para>The accompanying comment explains that this is the list of
1801       flags passed to GHC when building GHC itself.  For doing
1802       development, it is wise to add <literal>-DDEBUG</literal>, to
1803       enable debugging code.  So you would add the following to
1804       <filename>build.mk</filename>:</para>
1805       
1806       <para>or, if you prefer,</para>
1807
1808 <ProgramListing>
1809 GhcHcOpts += -DDEBUG
1810 </ProgramListing>
1811
1812       <para>GNU <command>make</command> allows existing definitions to
1813       have new text appended using the &ldquo;<literal>+=</literal>&rdquo;
1814       operator, which is quite a convenient feature.)</para>
1815
1816       <para>If you want to remove the <literal>-O</literal> as well (a
1817       good idea when developing, because the turn-around cycle gets a
1818       lot quicker), you can just override
1819       <literal>GhcLibHcOpts</literal> altogether:</para>
1820
1821 <ProgramListing>
1822 GhcHcOpts=-DDEBUG -Rghc-timing
1823 </ProgramListing>
1824
1825       <para>When reading <filename>config.mk.in</filename>, remember
1826       that anything between &ldquo;@...@&rdquo; signs is going to be substituted
1827       by <command>configure</command> later.  You
1828       <emphasis>can</emphasis> override the resulting definition if
1829       you want, but you need to be a bit surer what you are doing.
1830       For example, there's a line that says:</para>
1831
1832 <ProgramListing>
1833 TAR = @TarCmd@
1834 </ProgramListing>
1835
1836       <para>This defines the Make variables <constant>TAR</constant>
1837       to the pathname for a <command>tar</command> that
1838       <command>configure</command> finds somewhere.  If you have your
1839       own pet <command>tar</command> you want to use instead, that's
1840       fine. Just add this line to <filename>mk/build.mk</filename>:</para>
1841
1842 <ProgramListing>
1843 TAR = mytar
1844 </ProgramListing>
1845
1846       <para>You do not <emphasis>have</emphasis> to have a
1847       <filename>mk/build.mk</filename> file at all; if you don't,
1848       you'll get all the default settings from
1849       <filename>mk/config.mk.in</filename>.</para>
1850
1851       <para>You can also use <filename>build.mk</filename> to override
1852       anything that <command>configure</command> got wrong.  One place
1853       where this happens often is with the definition of
1854       <constant>FPTOOLS&lowbar;TOP&lowbar;ABS</constant>: this
1855       variable is supposed to be the canonical path to the top of your
1856       source tree, but if your system uses an automounter then the
1857       correct directory is hard to find automatically.  If you find
1858       that <command>configure</command> has got it wrong, just put the
1859       correct definition in <filename>build.mk</filename>.</para>
1860
1861     </sect2>
1862
1863     <sect2 id="sec-storysofar">
1864       <title>The story so far</title>
1865
1866       <para>Let's summarise the steps you need to carry to get
1867       yourself a fully-configured build tree from scratch.</para>
1868
1869       <orderedlist>
1870         <listitem>
1871           <para> Get your source tree from somewhere (CVS repository
1872           or source distribution).  Say you call the root directory
1873           <filename>myfptools</filename> (it does not have to be
1874           called <filename>fptools</filename>).  Make sure that you
1875           have the essential files (see <XRef
1876           LinkEnd="sec-source-tree">).</para>
1877         </listitem>
1878
1879         <listitem>
1880
1881           <para>(Optional) Use <command>lndir</command> or
1882           <command>mkshadowdir</command> to create a build tree.</para>
1883
1884 <programlisting>
1885 $ cd myfptools
1886 $ mkshadowdir . /scratch/joe-bloggs/myfptools-sun4
1887 </programlisting>
1888
1889           <para>(N.B. <command>mkshadowdir</command>'s first argument
1890           is taken relative to its second.) You probably want to give
1891           the build tree a name that suggests its main defining
1892           characteristic (in your mind at least), in case you later
1893           add others.</para>
1894         </listitem>
1895
1896         <listitem>
1897           <para>Change directory to the build tree.  Everything is
1898           going to happen there now.</para>
1899
1900 <programlisting>
1901 $ cd /scratch/joe-bloggs/myfptools-sun4
1902 </programlisting>
1903
1904         </listitem>
1905
1906         <listitem>
1907           <para>Prepare for system configuration:</para>
1908
1909 <programlisting>
1910 $ autoreconf
1911 </programlisting>
1912
1913           <para>(You can skip this step if you are starting from a
1914           source distribution, and you already have
1915           <filename>configure</filename> and
1916           <filename>mk/config.h.in</filename>.)</para>
1917
1918           <para>Some projects, including GHC itself, have their own
1919           configure scripts, so it is necessary to run autoreconf again
1920           in the appropriate subdirectories. eg:</para>
1921
1922 <programlisting>
1923 $ (cd ghc; autoreconf)
1924 </programlisting>
1925         </listitem>
1926
1927         <listitem>
1928           <para>Do system configuration:</para>
1929
1930 <programlisting>
1931 $ ./configure
1932 </programlisting>
1933
1934           <para>Don't forget to check whether you need to add any
1935           arguments to <literal>configure</literal>; for example, a
1936           common requirement is to specify which GHC to use with
1937           <option>--with-ghc=<replaceable>ghc</replaceable></option>.</para>
1938         </listitem>
1939
1940         <listitem>
1941           <para>Create the file <filename>mk/build.mk</filename>,
1942           adding definitions for your desired configuration
1943           options.</para>
1944
1945 <programlisting>
1946 $ emacs mk/build.mk
1947 </programlisting>
1948         </listitem>
1949       </orderedlist>
1950
1951       <para>You can make subsequent changes to
1952       <filename>mk/build.mk</filename> as often as you like.  You do
1953       not have to run any further configuration programs to make these
1954       changes take effect. In theory you should, however, say
1955       <command>gmake clean</command>, <command>gmake all</command>,
1956       because configuration option changes could affect
1957       anything&mdash;but in practice you are likely to know what's
1958       affected.</para>
1959     </sect2>
1960
1961     <sect2>
1962       <title>Making things</title>
1963
1964       <para>At this point you have made yourself a fully-configured
1965       build tree, so you are ready to start building real
1966       things.</para>
1967
1968       <para>The first thing you need to know is that <emphasis>you
1969       must use GNU <command>make</command>, usually called
1970       <command>gmake</command>, not standard Unix
1971       <command>make</command></emphasis>.  If you use standard Unix
1972       <command>make</command> you will get all sorts of error messages
1973       (but no damage) because the <literal>fptools</literal>
1974       <command>Makefiles</command> use GNU <command>make</command>'s
1975       facilities extensively.</para>
1976
1977       <para>To just build the whole thing, <command>cd</command> to
1978       the top of your <literal>fptools</literal> tree and type
1979       <command>gmake</command>.  This will prepare the tree and build
1980       the various projects in the correct order.</para>
1981     </sect2>
1982
1983     <sect2 id="sec-bootstrapping">
1984       <title>Bootstrapping GHC</title>
1985
1986       <para>GHC requires a 2-stage bootstrap in order to provide 
1987       full functionality, including GHCi.  By a 2-stage bootstrap, we
1988       mean that the compiler is built once using the installed GHC,
1989       and then again using the compiler built in the first stage.  You
1990       can also build a stage 3 compiler, but this normally isn't
1991       necessary except to verify that the stage 2 compiler is working
1992       properly.</para>
1993
1994       <para>Note that when doing a bootstrap, the stage 1 compiler
1995       must be built, followed by the runtime system and libraries, and
1996       then the stage 2 compiler.  The correct ordering is implemented
1997       by the top-level fptools <filename>Makefile</filename>, so if
1998       you want everything to work automatically it's best to start
1999       <command>make</command> from the top of the tree.  When building
2000       GHC, the top-level fptools <filename>Makefile</filename> is set
2001       up to do a 2-stage bootstrap by default (when you say
2002       <command>make</command>).  Some other targets it supports
2003       are:</para>
2004
2005       <variablelist>
2006         <varlistentry>
2007           <term>stage1</term>
2008           <listitem>
2009             <para>Build everything as normal, including the stage 1
2010             compiler.</para>
2011           </listitem>
2012         </varlistentry>
2013
2014         <varlistentry>
2015           <term>stage2</term>
2016           <listitem>
2017             <para>Build the stage 2 compiler only.</para>
2018           </listitem>
2019         </varlistentry>
2020
2021         <varlistentry>
2022           <term>stage3</term>
2023           <listitem>
2024             <para>Build the stage 3 compiler only.</para>
2025           </listitem>
2026         </varlistentry>
2027
2028         <varlistentry>
2029           <term>bootstrap</term> <term>bootstrap2</term>
2030           <listitem>
2031             <para>Build stage 1 followed by stage 2.</para>
2032           </listitem>
2033         </varlistentry>
2034
2035         <varlistentry>
2036           <term>bootstrap3</term>
2037           <listitem>
2038             <para>Build stages 1, 2 and 3.</para>
2039           </listitem>
2040         </varlistentry>
2041
2042         <varlistentry>
2043           <term>install</term>
2044           <listitem>
2045             <para>Install everything, including the compiler built in
2046             stage 2.  To override the stage, say <literal>make install
2047             stage=<replaceable>n</replaceable></literal> where
2048             <replaceable>n</replaceable> is the stage to install.</para>
2049           </listitem>
2050         </varlistentry>
2051       </variablelist>
2052
2053       <para>The top-level <filename>Makefile</filename> also arranges
2054       to do the appropriate <literal>make boot</literal> steps (see
2055       below) before actually building anything.</para>
2056
2057       <para>The <literal>stage1</literal>, <literal>stage2</literal>
2058       and <literal>stage3</literal> targets also work in the
2059       <literal>ghc/compiler</literal> directory, but don't forget that
2060       each stage requires its own <literal>make boot</literal> step:
2061       for example, you must do</para>
2062
2063       <screen>$ make boot stage=2</screen>
2064
2065       <para>before <literal>make stage2</literal> in
2066       <literal>ghc/compiler</literal>.</para>
2067     </sect2>
2068
2069     <sect2 id="sec-standard-targets">
2070       <title>Standard Targets</title>
2071       <indexterm><primary>targets, standard makefile</primary></indexterm>
2072       <indexterm><primary>makefile targets</primary></indexterm>
2073
2074       <para>In any directory you should be able to make the following:</para>
2075
2076       <variablelist>
2077         <varlistentry>
2078           <term><literal>boot</literal></term>
2079           <listitem>
2080             <para>does the one-off preparation required to get ready
2081             for the real work.  Notably, it does <command>gmake
2082             depend</command> in all directories that contain programs.
2083             It also builds the necessary tools for compilation to
2084             proceed.</para>
2085
2086             <para>Invoking the <literal>boot</literal> target
2087             explicitly is not normally necessary.  From the top-level
2088             <literal>fptools</literal> directory, invoking
2089             <literal>gmake</literal> causes <literal>gmake boot
2090             all</literal> to be invoked in each of the project
2091             subdirectories, in the order specified by
2092             <literal>&dollar;(AllTargets)</literal> in
2093             <literal>config.mk</literal>.</para>
2094
2095             <para>If you're working in a subdirectory somewhere and
2096             need to update the dependencies, <literal>gmake
2097             boot</literal> is a good way to do it.</para>
2098           </listitem>
2099         </varlistentry>
2100
2101         <varlistentry>
2102           <term><literal>all</literal></term>
2103           <listitem>
2104             <para>makes all the final target(s) for this Makefile.
2105             Depending on which directory you are in a &ldquo;final
2106             target&rdquo; may be an executable program, a library
2107             archive, a shell script, or a Postscript file.  Typing
2108             <command>gmake</command> alone is generally the same as
2109             typing <command>gmake all</command>.</para>
2110           </listitem>
2111         </varlistentry>
2112
2113         <varlistentry>
2114           <term><literal>install</literal></term>
2115           <listitem>
2116             <para>installs the things built by <literal>all</literal>
2117             (except for the documentation).  Where does it install
2118             them?  That is specified by
2119             <filename>mk/config.mk.in</filename>; you can override it
2120             in <filename>mk/build.mk</filename>, or by running
2121             <command>configure</command> with command-line arguments
2122             like <literal>--bindir=/home/simonpj/bin</literal>; see
2123             <literal>./configure --help</literal> for the full
2124             details.</para>
2125           </listitem>
2126         </varlistentry>
2127
2128         <varlistentry>
2129           <term><literal>install-docs</literal></term>
2130           <listitem>
2131             <para>installs the documentation. Otherwise behaves just
2132             like <literal>install</literal>.</para>
2133           </listitem>
2134         </varlistentry>
2135
2136         <varlistentry>
2137           <term><literal>uninstall</literal></term>
2138           <listitem>
2139             <para>reverses the effect of
2140             <literal>install</literal>.</para>
2141           </listitem>
2142         </varlistentry>
2143
2144         <varlistentry>
2145           <term><literal>clean</literal></term>
2146           <listitem>
2147             <para>Delete all files from the current directory that are
2148             normally created by building the program.  Don't delete
2149             the files that record the configuration, or files
2150             generated by <command>gmake boot</command>.  Also preserve
2151             files that could be made by building, but normally aren't
2152             because the distribution comes with them.</para>
2153           </listitem>
2154         </varlistentry>
2155
2156         <varlistentry>
2157           <term><literal>distclean</literal></term>
2158           <listitem>
2159             <para>Delete all files from the current directory that are
2160             created by configuring or building the program. If you
2161             have unpacked the source and built the program without
2162             creating any other files, <literal>make
2163             distclean</literal> should leave only the files that were
2164             in the distribution.</para>
2165           </listitem>
2166         </varlistentry>
2167
2168         <varlistentry>
2169           <term><literal>mostlyclean</literal></term>
2170           <listitem>
2171             <para>Like <literal>clean</literal>, but may refrain from
2172             deleting a few files that people normally don't want to
2173             recompile.</para>
2174           </listitem>
2175         </varlistentry>
2176
2177         <varlistentry>
2178           <term><literal>maintainer-clean</literal></term>
2179           <listitem>
2180             <para>Delete everything from the current directory that
2181             can be reconstructed with this Makefile.  This typically
2182             includes everything deleted by
2183             <literal>distclean</literal>, plus more: C source files
2184             produced by Bison, tags tables, Info files, and so
2185             on.</para>
2186
2187             <para>One exception, however: <literal>make
2188             maintainer-clean</literal> should not delete
2189             <filename>configure</filename> even if
2190             <filename>configure</filename> can be remade using a rule
2191             in the <filename>Makefile</filename>. More generally,
2192             <literal>make maintainer-clean</literal> should not delete
2193             anything that needs to exist in order to run
2194             <filename>configure</filename> and then begin to build the
2195             program.</para>
2196           </listitem>
2197         </varlistentry>
2198
2199         <varlistentry>
2200           <term><literal>check</literal></term>
2201           <listitem>
2202             <para>run the test suite.</para>
2203           </listitem>
2204         </varlistentry>
2205       </variablelist>
2206
2207       <para>All of these standard targets automatically recurse into
2208       sub-directories.  Certain other standard targets do not:</para>
2209
2210       <variablelist>
2211         <varlistentry>
2212           <term><literal>configure</literal></term>
2213           <listitem>
2214             <para>is only available in the root directory
2215             <constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>; it has
2216             been discussed in <XRef
2217             LinkEnd="sec-build-config">.</para>
2218           </listitem>
2219         </varlistentry>
2220
2221         <varlistentry>
2222           <term><literal>depend</literal></term>
2223           <listitem>
2224             <para>make a <filename>.depend</filename> file in each
2225             directory that needs it. This <filename>.depend</filename>
2226             file contains mechanically-generated dependency
2227             information; for example, suppose a directory contains a
2228             Haskell source module <filename>Foo.lhs</filename> which
2229             imports another module <literal>Baz</literal>.  Then the
2230             generated <filename>.depend</filename> file will contain
2231             the dependency:</para>
2232
2233 <ProgramListing>
2234 Foo.o : Baz.hi
2235 </ProgramListing>
2236
2237             <para>which says that the object file
2238             <filename>Foo.o</filename> depends on the interface file
2239             <filename>Baz.hi</filename> generated by compiling module
2240             <literal>Baz</literal>.  The <filename>.depend</filename>
2241             file is automatically included by every Makefile.</para>
2242           </listitem>
2243         </varlistentry>
2244
2245         <varlistentry>
2246           <term><literal>binary-dist</literal></term>
2247           <listitem>
2248             <para>make a binary distribution.  This is the target we
2249             use to build the binary distributions of GHC and
2250             Happy.</para>
2251           </listitem>
2252         </varlistentry>
2253
2254         <varlistentry>
2255           <term><literal>dist</literal></term>
2256           <listitem>
2257             <para>make a source distribution.  Note that this target
2258             does &ldquo;make distclean&rdquo; as part of its work;
2259             don't use it if you want to keep what you've built.</para>
2260           </listitem>
2261         </varlistentry>
2262       </variablelist>
2263
2264       <para>Most <filename>Makefile</filename>s have targets other
2265       than these.  You can discover them by looking in the
2266       <filename>Makefile</filename> itself.</para>
2267     </sect2>
2268
2269     <sect2>
2270       <title>Using a project from the build tree</title> 
2271
2272       <para>If you want to build GHC (say) and just use it direct from
2273       the build tree without doing <literal>make install</literal>
2274       first, you can run the in-place driver script:
2275       <filename>ghc/compiler/ghc-inplace</filename>.</para>
2276
2277       <para> Do <emphasis>NOT</emphasis> use
2278       <filename>ghc/compiler/ghc</filename>, or
2279       <filename>ghc/compiler/ghc-6.xx</filename>, as these are the
2280       scripts intended for installation, and contain hard-wired paths
2281       to the installed libraries, rather than the libraries in the
2282       build tree.</para>
2283
2284       <para>Happy can similarly be run from the build tree, using
2285       <filename>happy/src/happy-inplace</filename>, and similarly for
2286       Alex and Haddock.</para>
2287     </sect2>
2288
2289     <sect2>
2290       <title>Fast Making</title>
2291
2292       <indexterm><primary>fastmake</primary></indexterm>
2293       <indexterm><primary>dependencies, omitting</primary></indexterm>
2294       <indexterm><primary>FAST, makefile variable</primary></indexterm>
2295
2296       <para>Sometimes the dependencies get in the way: if you've made
2297       a small change to one file, and you're absolutely sure that it
2298       won't affect anything else, but you know that
2299       <command>make</command> is going to rebuild everything anyway,
2300       the following hack may be useful:</para>
2301
2302 <ProgramListing>
2303 gmake FAST=YES 
2304 </ProgramListing>
2305
2306       <para>This tells the make system to ignore dependencies and just
2307       build what you tell it to.  In other words, it's equivalent to
2308       temporarily removing the <filename>.depend</filename> file in
2309       the current directory (where <command>mkdependHS</command> and
2310       friends store their dependency information).</para>
2311
2312       <para>A bit of history: GHC used to come with a
2313       <command>fastmake</command> script that did the above job, but
2314       GNU make provides the features we need to do it without
2315       resorting to a script.  Also, we've found that fastmaking is
2316       less useful since the advent of GHC's recompilation checker (see
2317       the User's Guide section on "Separate Compilation").</para>
2318     </sect2>
2319   </sect1>
2320
2321   <sect1 id="sec-makefile-arch">
2322     <title>The <filename>Makefile</filename> architecture</title>
2323     <indexterm><primary>makefile architecture</primary></indexterm>
2324
2325     <para><command>make</command> is great if everything
2326     works&mdash;you type <command>gmake install</command> and lo! the
2327     right things get compiled and installed in the right places.  Our
2328     goal is to make this happen often, but somehow it often doesn't;
2329     instead some weird error message eventually emerges from the
2330     bowels of a directory you didn't know existed.</para>
2331
2332     <para>The purpose of this section is to give you a road-map to
2333     help you figure out what is going right and what is going
2334     wrong.</para>
2335
2336     <sect2>
2337       <title>Debugging</title>
2338       
2339       <para>Debugging <filename>Makefile</filename>s is something of a
2340       black art, but here's a couple of tricks that we find
2341       particularly useful.  The following command allows you to see
2342       the contents of any make variable in the context of the current
2343       <filename>Makefile</filename>:</para>
2344
2345 <screen>$  make show VALUE=HS_SRCS</screen>
2346
2347       <para>where you can replace <literal>HS_SRCS</literal> with the
2348       name of any variable you wish to see the value of.</para>
2349       
2350       <para>GNU make has a <option>-d</option> option which generates
2351       a dump of the decision procedure used to arrive at a conclusion
2352       about which files should be recompiled.  Sometimes useful for
2353       tracking down problems with superfluous or missing
2354       recompilations.</para>
2355     </sect2>
2356
2357     <sect2>
2358       <title>A small project</title>
2359
2360       <para>To get started, let us look at the
2361       <filename>Makefile</filename> for an imaginary small
2362       <literal>fptools</literal> project, <literal>small</literal>.
2363       Each project in <literal>fptools</literal> has its own directory
2364       in <constant>FPTOOLS&lowbar;TOP</constant>, so the
2365       <literal>small</literal> project will have its own directory
2366       <constant>FPOOLS&lowbar;TOP/small/</constant>.  Inside the
2367       <filename>small/</filename> directory there will be a
2368       <filename>Makefile</filename>, looking something like
2369       this:</para>
2370
2371 <indexterm><primary>Makefile, minimal</primary></indexterm>
2372
2373 <ProgramListing>
2374 #     Makefile for fptools project "small"
2375
2376 TOP = ..
2377 include $(TOP)/mk/boilerplate.mk
2378
2379 SRCS = $(wildcard *.lhs) $(wildcard *.c)
2380 HS_PROG = small
2381
2382 include $(TOP)/target.mk
2383 </ProgramListing>
2384
2385       <para>this <filename>Makefile</filename> has three
2386       sections:</para>
2387
2388       <orderedlist>
2389         <listitem>
2390           <para>The first section includes
2391 <footnote>
2392 <para>
2393 One of the most important
2394 features of GNU <command>make</command> that we use is the ability for a <filename>Makefile</filename> to
2395 include another named file, very like <command>cpp</command>'s <literal>&num;include</literal>
2396 directive.
2397 </para>
2398 </footnote>
2399
2400           a file of &ldquo;boilerplate&rdquo; code from the level
2401           above (which in this case will be
2402           <filename><constant>FPTOOLS&lowbar;TOP</constant>/mk/boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>).
2403           As its name suggests, <filename>boilerplate.mk</filename>
2404           consists of a large quantity of standard
2405           <filename>Makefile</filename> code.  We discuss this
2406           boilerplate in more detail in <XRef LinkEnd="sec-boiler">.
2407           <indexterm><primary>include, directive in
2408           Makefiles</primary></indexterm> <indexterm><primary>Makefile
2409           inclusion</primary></indexterm></para>
2410
2411           <para>Before the <literal>include</literal> statement, you
2412           must define the <command>make</command> variable
2413           <constant>TOP</constant><indexterm><primary>TOP</primary></indexterm>
2414           to be the directory containing the <filename>mk</filename>
2415           directory in which the <filename>boilerplate.mk</filename>
2416           file is.  It is <emphasis>not</emphasis> OK to simply say</para>
2417
2418 <ProgramListing>
2419 include ../mk/boilerplate.mk  # NO NO NO
2420 </ProgramListing>
2421
2422
2423           <para>Why?  Because the <filename>boilerplate.mk</filename>
2424           file needs to know where it is, so that it can, in turn,
2425           <literal>include</literal> other files.  (Unfortunately,
2426           when an <literal>include</literal>d file does an
2427           <literal>include</literal>, the filename is treated relative
2428           to the directory in which <command>gmake</command> is being
2429           run, not the directory in which the
2430           <literal>include</literal>d sits.)  In general,
2431           <emphasis>every file <filename>foo.mk</filename> assumes
2432           that
2433           <filename><constant>&dollar;(TOP)</constant>/mk/foo.mk</filename>
2434           refers to itself.</emphasis> It is up to the
2435           <filename>Makefile</filename> doing the
2436           <literal>include</literal> to ensure this is the case.</para>
2437
2438           <para>Files intended for inclusion in other
2439           <filename>Makefile</filename>s are written to have the
2440           following property: <emphasis>after
2441           <filename>foo.mk</filename> is <literal>include</literal>d,
2442           it leaves <constant>TOP</constant> containing the same value
2443           as it had just before the <literal>include</literal>
2444           statement</emphasis>.  In our example, this invariant
2445           guarantees that the <literal>include</literal> for
2446           <filename>target.mk</filename> will look in the same
2447           directory as that for <filename>boilerplate.mk</filename>.</para>
2448         </listitem>
2449
2450         <listitem>
2451           <para> The second section defines the following standard
2452           <command>make</command> variables:
2453           <constant>SRCS</constant><indexterm><primary>SRCS</primary></indexterm>
2454           (the source files from which is to be built), and
2455           <constant>HS&lowbar;PROG</constant><indexterm><primary>HS&lowbar;PROG</primary></indexterm>
2456           (the executable binary to be built).  We will discuss in
2457           more detail what the &ldquo;standard variables&rdquo; are,
2458           and how they affect what happens, in <XRef
2459           LinkEnd="sec-targets">.</para>
2460
2461           <para>The definition for <constant>SRCS</constant> uses the
2462           useful GNU <command>make</command> construct
2463           <literal>&dollar;(wildcard&nbsp;$pat$)</literal><indexterm><primary>wildcard</primary></indexterm>,
2464           which expands to a list of all the files matching the
2465           pattern <literal>pat</literal> in the current directory.  In
2466           this example, <constant>SRCS</constant> is set to the list
2467           of all the <filename>.lhs</filename> and
2468           <filename>.c</filename> files in the directory.  (Let's
2469           suppose there is one of each, <filename>Foo.lhs</filename>
2470           and <filename>Baz.c</filename>.)</para>
2471         </listitem>
2472
2473         <listitem>
2474           <para>The last section includes a second file of standard
2475           code, called
2476           <filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>.
2477           It contains the rules that tell <command>gmake</command> how
2478           to make the standard targets (<Xref
2479           LinkEnd="sec-standard-targets">).  Why, you ask, can't this
2480           standard code be part of
2481           <filename>boilerplate.mk</filename>?  Good question.  We
2482           discuss the reason later, in <Xref
2483           LinkEnd="sec-boiler-arch">.</para>
2484
2485           <para>You do not <emphasis>have</emphasis> to
2486           <literal>include</literal> the
2487           <filename>target.mk</filename> file.  Instead, you can write
2488           rules of your own for all the standard targets.  Usually,
2489           though, you will find quite a big payoff from using the
2490           canned rules in <filename>target.mk</filename>; the price
2491           tag is that you have to understand what canned rules get
2492           enabled, and what they do (<Xref
2493           LinkEnd="sec-targets">).</para>
2494         </listitem>
2495       </orderedlist>
2496
2497       <para>In our example <filename>Makefile</filename>, most of the
2498       work is done by the two <literal>include</literal>d files.  When
2499       you say <command>gmake all</command>, the following things
2500       happen:</para>
2501
2502       <itemizedlist>
2503         <listitem>
2504           <para><command>gmake</command> figures out that the object
2505           files are <filename>Foo.o</filename> and
2506           <filename>Baz.o</filename>.</para>
2507         </listitem>
2508
2509         <listitem>
2510           <para>It uses a boilerplate pattern rule to compile
2511           <filename>Foo.lhs</filename> to <filename>Foo.o</filename>
2512           using a Haskell compiler.  (Which one?  That is set in the
2513           build configuration.)</para>
2514         </listitem>
2515
2516         <listitem>
2517           <para>It uses another standard pattern rule to compile
2518           <filename>Baz.c</filename> to <filename>Baz.o</filename>,
2519           using a C compiler.  (Ditto.)</para>
2520         </listitem>
2521
2522         <listitem>
2523           <para>It links the resulting <filename>.o</filename> files
2524           together to make <literal>small</literal>, using the Haskell
2525           compiler to do the link step.  (Why not use
2526           <command>ld</command>?  Because the Haskell compiler knows
2527           what standard libraries to link in.  How did
2528           <command>gmake</command> know to use the Haskell compiler to
2529           do the link, rather than the C compiler?  Because we set the
2530           variable <constant>HS&lowbar;PROG</constant> rather than
2531           <constant>C&lowbar;PROG</constant>.)</para>
2532         </listitem>
2533       </itemizedlist>
2534
2535       <para>All <filename>Makefile</filename>s should follow the above
2536       three-section format.</para>
2537     </sect2>
2538
2539     <sect2>
2540       <title>A larger project</title>
2541
2542       <para>Larger projects are usually structured into a number of
2543       sub-directories, each of which has its own
2544       <filename>Makefile</filename>.  (In very large projects, this
2545       sub-structure might be iterated recursively, though that is
2546       rare.)  To give you the idea, here's part of the directory
2547       structure for the (rather large) GHC project:</para>
2548
2549 <Screen>
2550 $(FPTOOLS_TOP)/ghc/
2551   Makefile
2552   mk/
2553     boilerplate.mk
2554     rules.mk
2555    docs/
2556     Makefile
2557     ...source files for documentation...
2558    driver/
2559     Makefile
2560     ...source files for driver...
2561    compiler/
2562     Makefile
2563     parser/...source files for parser...
2564     renamer/...source files for renamer...
2565     ...etc...
2566 </Screen>
2567
2568       <para>The sub-directories <filename>docs</filename>,
2569       <filename>driver</filename>, <filename>compiler</filename>, and
2570       so on, each contains a sub-component of GHC, and each has its
2571       own <filename>Makefile</filename>.  There must also be a
2572       <filename>Makefile</filename> in
2573       <filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/ghc</filename>.
2574       It does most of its work by recursively invoking
2575       <command>gmake</command> on the <filename>Makefile</filename>s
2576       in the sub-directories.  We say that
2577       <filename>ghc/Makefile</filename> is a <emphasis>non-leaf
2578       <filename>Makefile</filename></emphasis>, because it does little
2579       except organise its children, while the
2580       <filename>Makefile</filename>s in the sub-directories are all
2581       <emphasis>leaf <filename>Makefile</filename>s</emphasis>.  (In
2582       principle the sub-directories might themselves contain a
2583       non-leaf <filename>Makefile</filename> and several
2584       sub-sub-directories, but that does not happen in GHC.)</para>
2585
2586       <para>The <filename>Makefile</filename> in
2587       <filename>ghc/compiler</filename> is considered a leaf
2588       <filename>Makefile</filename> even though the
2589       <filename>ghc/compiler</filename> has sub-directories, because
2590       these sub-directories do not themselves have
2591       <filename>Makefile</filename>s in them.  They are just used to
2592       structure the collection of modules that make up GHC, but all
2593       are managed by the single <filename>Makefile</filename> in
2594       <filename>ghc/compiler</filename>.</para>
2595
2596       <para>You will notice that <filename>ghc/</filename> also
2597       contains a directory <filename>ghc/mk/</filename>.  It contains
2598       GHC-specific <filename>Makefile</filename> boilerplate code.
2599       More precisely:</para>
2600
2601       <itemizedlist>
2602         <listitem>
2603           <para><filename>ghc/mk/boilerplate.mk</filename> is included
2604           at the top of <filename>ghc/Makefile</filename>, and of all
2605           the leaf <filename>Makefile</filename>s in the
2606           sub-directories.  It in turn <literal>include</literal>s the
2607           main boilerplate file
2608           <filename>mk/boilerplate.mk</filename>.</para>
2609         </listitem>
2610
2611         <listitem>
2612           <para><filename>ghc/mk/target.mk</filename> is
2613           <literal>include</literal>d at the bottom of
2614           <filename>ghc/Makefile</filename>, and of all the leaf
2615           <filename>Makefile</filename>s in the sub-directories.  It
2616           in turn <literal>include</literal>s the file
2617           <filename>mk/target.mk</filename>.</para>
2618         </listitem>
2619       </itemizedlist>
2620
2621       <para>So these two files are the place to look for GHC-wide
2622       customisation of the standard boilerplate.</para>
2623     </sect2>
2624
2625     <sect2 id="sec-boiler-arch">
2626       <title>Boilerplate architecture</title>
2627       <indexterm><primary>boilerplate architecture</primary></indexterm>
2628
2629       <para>Every <filename>Makefile</filename> includes a
2630       <filename>boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>
2631       file at the top, and
2632       <filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>
2633       file at the bottom.  In this section we discuss what is in these
2634       files, and why there have to be two of them.  In general:</para>
2635
2636       <itemizedlist>
2637         <listitem>
2638           <para><filename>boilerplate.mk</filename> consists of:</para>
2639
2640           <itemizedlist>
2641             <listitem>
2642               <para><emphasis>Definitions of millions of
2643               <command>make</command> variables</emphasis> that
2644               collectively specify the build configuration.  Examples:
2645               <constant>HC&lowbar;OPTS</constant><indexterm><primary>HC&lowbar;OPTS</primary></indexterm>,
2646               the options to feed to the Haskell compiler;
2647               <constant>NoFibSubDirs</constant><indexterm><primary>NoFibSubDirs</primary></indexterm>,
2648               the sub-directories to enable within the
2649               <literal>nofib</literal> project;
2650               <constant>GhcWithHc</constant><indexterm><primary>GhcWithHc</primary></indexterm>,
2651               the name of the Haskell compiler to use when compiling
2652               GHC in the <literal>ghc</literal> project.</para>
2653             </listitem>
2654
2655             <listitem>
2656               <para><emphasis>Standard pattern rules</emphasis> that
2657               tell <command>gmake</command> how to construct one file
2658               from another.</para>
2659             </listitem>
2660           </itemizedlist>
2661
2662           <para><filename>boilerplate.mk</filename> needs to be
2663           <literal>include</literal>d at the <emphasis>top</emphasis>
2664           of each <filename>Makefile</filename>, so that the user can
2665           replace the boilerplate definitions or pattern rules by
2666           simply giving a new definition or pattern rule in the
2667           <filename>Makefile</filename>.  <command>gmake</command>
2668           simply takes the last definition as the definitive one.</para>
2669
2670           <para>Instead of <emphasis>replacing</emphasis> boilerplate
2671           definitions, it is also quite common to
2672           <emphasis>augment</emphasis> them. For example, a
2673           <filename>Makefile</filename> might say:</para>
2674
2675 <ProgramListing>
2676 SRC_HC_OPTS += -O
2677 </ProgramListing>
2678
2679           <para>thereby adding &ldquo;<option>-O</option>&rdquo; to
2680           the end of
2681           <constant>SRC&lowbar;HC&lowbar;OPTS</constant><indexterm><primary>SRC&lowbar;HC&lowbar;OPTS</primary></indexterm>.</para>
2682         </listitem>
2683
2684         <listitem>
2685           <para><filename>target.mk</filename> contains
2686           <command>make</command> rules for the standard targets
2687           described in <Xref LinkEnd="sec-standard-targets">.  These
2688           rules are selectively included, depending on the setting of
2689           certain <command>make</command> variables.  These variables
2690           are usually set in the middle section of the
2691           <filename>Makefile</filename> between the two
2692           <literal>include</literal>s.</para>
2693
2694           <para><filename>target.mk</filename> must be included at the
2695           end (rather than being part of
2696           <filename>boilerplate.mk</filename>) for several tiresome
2697           reasons:</para>
2698
2699           <itemizedlist>
2700             <listitem>
2701
2702               <para><command>gmake</command> commits target and
2703               dependency lists earlier than it should.  For example,
2704               <FIlename>target.mk</FIlename> has a rule that looks
2705               like this:</para>
2706
2707 <ProgramListing>
2708 $(HS_PROG) : $(OBJS)
2709       $(HC) $(LD_OPTS) $&#60; -o $@
2710 </ProgramListing>
2711
2712               <para>If this rule was in
2713               <filename>boilerplate.mk</filename> then
2714               <constant>&dollar;(HS&lowbar;PROG)</constant><indexterm><primary>HS&lowbar;PROG</primary></indexterm>
2715               and
2716               <constant>&dollar;(OBJS)</constant><indexterm><primary>OBJS</primary></indexterm>
2717               would not have their final values at the moment
2718               <command>gmake</command> encountered the rule.  Alas,
2719               <command>gmake</command> takes a snapshot of their
2720               current values, and wires that snapshot into the rule.
2721               (In contrast, the commands executed when the rule
2722               &ldquo;fires&rdquo; are only substituted at the moment
2723               of firing.)  So, the rule must follow the definitions
2724               given in the <filename>Makefile</filename> itself.</para>
2725             </listitem>
2726
2727             <listitem>
2728               <para>Unlike pattern rules, ordinary rules cannot be
2729               overriden or replaced by subsequent rules for the same
2730               target (at least, not without an error message).
2731               Including ordinary rules in
2732               <filename>boilerplate.mk</filename> would prevent the
2733               user from writing rules for specific targets in specific
2734               cases.</para>
2735             </listitem>
2736
2737             <listitem>
2738               <para>There are a couple of other reasons I've
2739               forgotten, but it doesn't matter too much.</para>
2740             </listitem>
2741           </itemizedlist>
2742         </listitem>
2743       </itemizedlist>
2744     </sect2>
2745
2746     <sect2 id="sec-boiler">
2747       <title>The main <filename>mk/boilerplate.mk</filename> file</title>
2748       <indexterm><primary>boilerplate.mk</primary></indexterm>
2749
2750       <para>If you look at
2751       <filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/mk/boilerplate.mk</filename>
2752       you will find that it consists of the following sections, each
2753       held in a separate file:</para>
2754
2755       <variablelist>
2756         <varlistentry>
2757           <term><filename>config.mk</filename></term>
2758           <indexterm><primary>config.mk</primary></indexterm>
2759           <listitem>
2760             <para>is the build configuration file we discussed at
2761             length in <Xref LinkEnd="sec-build-config">.</para>
2762           </listitem>
2763         </varlistentry>
2764
2765         <varlistentry>
2766           <term><filename>paths.mk</filename></term>
2767           <indexterm><primary>paths.mk</primary></indexterm>
2768           <listitem>
2769             <para>defines <command>make</command> variables for
2770             pathnames and file lists.  This file contains code for
2771             automatically compiling lists of source files and deriving
2772             lists of object files from those.  The results can be
2773             overriden in the <filename>Makefile</filename>, but in
2774             most cases the automatic setup should do the right
2775             thing.</para>
2776             
2777             <para>The following variables may be set in the
2778             <filename>Makefile</filename> to affect how the automatic
2779             source file search is done:</para>
2780
2781             <variablelist>
2782               <varlistentry>
2783                 <term><literal>ALL_DIRS</literal></term>
2784                 <indexterm><primary><literal>ALL_DIRS</literal></primary>
2785                 </indexterm>
2786                 <listitem>
2787                   <para>Set to a list of directories to search in
2788                   addition to the current directory for source
2789                   files.</para>
2790                 </listitem>
2791               </varlistentry>
2792
2793               <varlistentry>
2794                 <term><literal>EXCLUDE_SRCS</literal></term>
2795                 <indexterm><primary><literal>EXCLUDE_SRCS</literal></primary>
2796                 </indexterm>
2797                 <listitem>
2798                   <para>Set to a list of source files (relative to the
2799                   current directory) to omit from the automatic
2800                   search.  The source searching machinery is clever
2801                   enough to know that if you exclude a source file
2802                   from which other sources are derived, then the
2803                   derived sources should also be excluded.  For
2804                   example, if you set <literal>EXCLUDED_SRCS</literal>
2805                   to include <filename>Foo.y</filename>, then
2806                   <filename>Foo.hs</filename> will also be
2807                   excluded.</para>
2808                 </listitem>
2809               </varlistentry>
2810
2811               <varlistentry>
2812                 <term><literal>EXTRA_SRCS</literal></term>
2813                 <indexterm><primary><literal>EXCLUDE_SRCS</literal></primary>
2814                 </indexterm>
2815                   <listitem>
2816                   <para>Set to a list of extra source files (perhaps
2817                   in directories not listed in
2818                   <literal>ALL_DIRS</literal>) that should be
2819                   considered.</para>
2820                 </listitem>
2821               </varlistentry>
2822             </variablelist>
2823
2824             <para>The results of the automatic source file search are
2825             placed in the following make variables:</para>
2826
2827             <variablelist>
2828               <varlistentry>
2829                 <term><literal>SRCS</literal></term>
2830                 <indexterm><primary><literal>SRCS</literal></primary></indexterm>
2831                 <listitem>
2832                   <para>All source files found, sorted and without
2833                   duplicates, including those which might not exist
2834                   yet but will be derived from other existing sources.
2835                   <literal>SRCS</literal> <emphasis>can</emphasis> be
2836                   overriden if necessary, in which case the variables
2837                   below will follow suit.</para>
2838                 </listitem>
2839               </varlistentry>
2840
2841               <varlistentry>
2842                 <term><literal>HS_SRCS</literal></term>
2843                 <indexterm><primary><literal>HS_SRCS</literal></primary></indexterm>
2844                 <listitem>
2845                   <para>all Haskell source files in the current
2846                   directory, including those derived from other source
2847                   files (eg. Happy sources also give rise to Haskell
2848                   sources).</para>
2849                 </listitem>
2850               </varlistentry>
2851
2852               <varlistentry>
2853                 <term><literal>HS_OBJS</literal></term>
2854                 <indexterm><primary><literal>HS_OBJS</literal></primary></indexterm>
2855                 <listitem>
2856                   <para>Object files derived from
2857                   <literal>HS_SRCS</literal>.</para>
2858                 </listitem>
2859               </varlistentry>
2860
2861               <varlistentry>
2862                 <term><literal>HS_IFACES</literal></term>
2863                 <indexterm><primary><literal>HS_IFACES</literal></primary></indexterm>
2864                 <listitem>
2865                   <para>Interface files (<literal>.hi</literal> files)
2866                   derived from <literal>HS_SRCS</literal>.</para>
2867                 </listitem>
2868               </varlistentry>
2869
2870               <varlistentry>
2871                 <term><literal>C_SRCS</literal></term>
2872                 <indexterm><primary><literal>C_SRCS</literal></primary></indexterm>
2873                 <listitem>
2874                   <para>All C source files found.</para>
2875                 </listitem>
2876               </varlistentry>
2877
2878               <varlistentry>
2879                 <term><literal>C_OBJS</literal></term>
2880                 <indexterm><primary><literal>C_OBJS</literal></primary></indexterm>
2881                 <listitem>
2882                   <para>Object files derived from
2883                   <literal>C_SRCS</literal>.</para>
2884                 </listitem>
2885               </varlistentry>
2886
2887               <varlistentry>
2888                 <term><literal>SCRIPT_SRCS</literal></term>
2889                 <indexterm><primary><literal>SCRIPT_SRCS</literal></primary></indexterm>
2890                 <listitem>
2891                   <para>All script source files found
2892                   (<literal>.lprl</literal> files).</para>
2893                 </listitem>
2894               </varlistentry>
2895
2896               <varlistentry>
2897                 <term><literal>SCRIPT_OBJS</literal></term>
2898                 <indexterm><primary><literal>SCRIPT_OBJS</literal></primary></indexterm>
2899                 <listitem>
2900                   <para><quote>object</quote> files derived from
2901                   <literal>SCRIPT_SRCS</literal>
2902                   (<literal>.prl</literal> files).</para>
2903                 </listitem>
2904               </varlistentry>
2905
2906               <varlistentry>
2907                 <term><literal>HSC_SRCS</literal></term>
2908                 <indexterm><primary><literal>HSC_SRCS</literal></primary></indexterm>
2909                 <listitem>
2910                   <para>All <literal>hsc2hs</literal> source files
2911                   (<literal>.hsc</literal> files).</para>
2912                 </listitem>
2913               </varlistentry>
2914
2915               <varlistentry>
2916                 <term><literal>HAPPY_SRCS</literal></term>
2917                 <indexterm><primary><literal>HAPPY_SRCS</literal></primary></indexterm>
2918                 <listitem>
2919                   <para>All <literal>happy</literal> source files
2920                   (<literal>.y</literal> or <literal>.hy</literal> files).</para>
2921                 </listitem>
2922               </varlistentry>
2923
2924               <varlistentry>
2925                 <term><literal>OBJS</literal></term>
2926                 <indexterm><primary>OBJS</primary></indexterm>
2927                 <listitem>
2928                   <para>the concatenation of
2929                   <literal>&dollar;(HS_OBJS)</literal>,
2930                   <literal>&dollar;(C_OBJS)</literal>, and
2931                   <literal>&dollar;(SCRIPT_OBJS)</literal>.</para>
2932                 </listitem>
2933               </varlistentry>
2934             </variablelist>
2935
2936             <para>Any or all of these definitions can easily be
2937             overriden by giving new definitions in your
2938             <filename>Makefile</filename>.</para>
2939
2940             <para>What, exactly, does <filename>paths.mk</filename>
2941             consider a <quote>source file</quote> to be?  It's based
2942             on the file's suffix (e.g. <filename>.hs</filename>,
2943             <filename>.lhs</filename>, <filename>.c</filename>,
2944             <filename>.hy</filename>, etc), but this is the kind of
2945             detail that changes, so rather than enumerate the source
2946             suffices here the best thing to do is to look in
2947             <filename>paths.mk</filename>.</para>
2948           </listitem>
2949         </varlistentry>
2950
2951         <varlistentry>
2952           <term><filename>opts.mk</filename></term>
2953           <indexterm><primary>opts.mk</primary></indexterm>
2954           <listitem>
2955             <para>defines <command>make</command> variables for option
2956             strings to pass to each program. For example, it defines
2957             <constant>HC&lowbar;OPTS</constant><indexterm><primary>HC&lowbar;OPTS</primary></indexterm>,
2958             the option strings to pass to the Haskell compiler.  See
2959             <Xref LinkEnd="sec-suffix">.</para>
2960           </listitem>
2961         </varlistentry>
2962
2963         <varlistentry>
2964           <term><filename>suffix.mk</filename></term>
2965           <indexterm><primary>suffix.mk</primary></indexterm>
2966           <listitem>
2967             <para>defines standard pattern rules&mdash;see <Xref
2968             LinkEnd="sec-suffix">.</para>
2969           </listitem>
2970         </varlistentry>
2971       </variablelist>
2972
2973       <para>Any of the variables and pattern rules defined by the
2974       boilerplate file can easily be overridden in any particular
2975       <filename>Makefile</filename>, because the boilerplate
2976       <literal>include</literal> comes first.  Definitions after this
2977       <literal>include</literal> directive simply override the default
2978       ones in <filename>boilerplate.mk</filename>.</para>
2979     </sect2>
2980
2981     <sect2 id="sec-suffix">
2982       <title>Pattern rules and options</title>
2983       <indexterm><primary>Pattern rules</primary></indexterm>
2984
2985       <para>The file
2986       <filename>suffix.mk</filename><indexterm><primary>suffix.mk</primary></indexterm>
2987       defines standard <emphasis>pattern rules</emphasis> that say how
2988       to build one kind of file from another, for example, how to
2989       build a <filename>.o</filename> file from a
2990       <filename>.c</filename> file.  (GNU <command>make</command>'s
2991       <emphasis>pattern rules</emphasis> are more powerful and easier
2992       to use than Unix <command>make</command>'s <emphasis>suffix
2993       rules</emphasis>.)</para>
2994
2995       <para>Almost all the rules look something like this:</para>
2996
2997 <ProgramListing>
2998 %.o : %.c
2999       $(RM) $@
3000       $(CC) $(CC_OPTS) -c $&#60; -o $@
3001 </ProgramListing>
3002
3003       <para>Here's how to understand the rule.  It says that
3004       <emphasis>something</emphasis><filename>.o</filename> (say
3005       <filename>Foo.o</filename>) can be built from
3006       <emphasis>something</emphasis><filename>.c</filename>
3007       (<filename>Foo.c</filename>), by invoking the C compiler (path
3008       name held in <constant>&dollar;(CC)</constant>), passing to it
3009       the options <constant>&dollar;(CC&lowbar;OPTS)</constant> and
3010       the rule's dependent file of the rule
3011       <literal>&dollar;&lt;</literal> (<filename>Foo.c</filename> in
3012       this case), and putting the result in the rule's target
3013       <literal>&dollar;@</literal> (<filename>Foo.o</filename> in this
3014       case).</para>
3015
3016       <para>Every program is held in a <command>make</command>
3017       variable defined in <filename>mk/config.mk</filename>&mdash;look
3018       in <filename>mk/config.mk</filename> for the complete list.  One
3019       important one is the Haskell compiler, which is called
3020       <constant>&dollar;(HC)</constant>.</para>
3021
3022       <para>Every program's options are are held in a
3023       <command>make</command> variables called
3024       <constant>&lt;prog&gt;&lowbar;OPTS</constant>.  the
3025       <constant>&lt;prog&gt;&lowbar;OPTS</constant> variables are
3026       defined in <filename>mk/opts.mk</filename>.  Almost all of them
3027       are defined like this:</para>
3028
3029 <ProgramListing>
3030 CC_OPTS = $(SRC_CC_OPTS) $(WAY$(_way)_CC_OPTS) $($*_CC_OPTS) $(EXTRA_CC_OPTS)
3031 </ProgramListing>
3032
3033       <para>The four variables from which
3034        <constant>CC&lowbar;OPTS</constant> is built have the following
3035       meaning:</para>
3036
3037       <variablelist>
3038         <varlistentry>
3039           <term><constant>SRC&lowbar;CC&lowbar;OPTS</constant><indexterm><primary>SRC&lowbar;CC&lowbar;OPTS</primary></indexterm>:</term>
3040           <listitem>
3041             <para>options passed to all C compilations.</para>
3042           </listitem>
3043         </varlistentry>
3044
3045         <varlistentry>
3046           <term><constant>WAY&lowbar;&lt;way&gt;&lowbar;CC&lowbar;OPTS</constant>:</term>
3047           <listitem>
3048             <para>options passed to C compilations for way
3049             <literal>&lt;way&gt;</literal>. For example,
3050             <constant>WAY&lowbar;mp&lowbar;CC&lowbar;OPTS</constant>
3051             gives options to pass to the C compiler when compiling way
3052             <literal>mp</literal>.  The variable
3053             <constant>WAY&lowbar;CC&lowbar;OPTS</constant> holds
3054             options to pass to the C compiler when compiling the
3055             standard way.  (<Xref LinkEnd="sec-ways"> dicusses
3056             multi-way compilation.)</para>
3057           </listitem>
3058         </varlistentry>
3059
3060         <varlistentry>
3061           <term><constant>&lt;module&gt;&lowbar;CC&lowbar;OPTS</constant>:</term>
3062           <listitem>
3063             <para>options to pass to the C compiler that are specific
3064             to module <literal>&lt;module&gt;</literal>.  For example,
3065             <constant>SMap&lowbar;CC&lowbar;OPTS</constant> gives the
3066             specific options to pass to the C compiler when compiling
3067             <filename>SMap.c</filename>.</para>
3068           </listitem>
3069         </varlistentry>
3070
3071         <varlistentry>
3072           <term><constant>EXTRA&lowbar;CC&lowbar;OPTS</constant><indexterm><primary>EXTRA&lowbar;CC&lowbar;OPTS</primary></indexterm>:</term>
3073           <listitem>
3074             <para>extra options to pass to all C compilations.  This
3075             is intended for command line use, thus:</para>
3076
3077 <ProgramListing>
3078 gmake libHS.a EXTRA_CC_OPTS="-v"
3079 </ProgramListing>
3080           </listitem>
3081         </varlistentry>
3082       </variablelist>
3083     </sect2>
3084
3085     <sect2 id="sec-targets">
3086       <title>The main <filename>mk/target.mk</filename> file</title>
3087       <indexterm><primary>target.mk</primary></indexterm>
3088
3089       <para><filename>target.mk</filename> contains canned rules for
3090       all the standard targets described in <Xref
3091       LinkEnd="sec-standard-targets">.  It is complicated by the fact
3092       that you don't want all of these rules to be active in every
3093       <filename>Makefile</filename>.  Rather than have a plethora of
3094       tiny files which you can include selectively, there is a single
3095       file, <filename>target.mk</filename>, which selectively includes
3096       rules based on whether you have defined certain variables in
3097       your <filename>Makefile</filename>.  This section explains what
3098       rules you get, what variables control them, and what the rules
3099       do.  Hopefully, you will also get enough of an idea of what is
3100       supposed to happen that you can read and understand any weird
3101       special cases yourself.</para>
3102
3103       <variablelist>
3104         <varlistentry>
3105           <term><constant>HS&lowbar;PROG</constant><indexterm><primary>HS&lowbar;PROG</primary></indexterm>.</term>
3106           <listitem>
3107             <para>If <constant>HS&lowbar;PROG</constant> is defined,
3108             you get rules with the following targets:</para>
3109
3110             <variablelist>
3111               <varlistentry>
3112                 <term><filename>HS&lowbar;PROG</filename><indexterm><primary>HS&lowbar;PROG</primary></indexterm></term>
3113                 <listitem>
3114                   <para>itself.  This rule links
3115                   <constant>&dollar;(OBJS)</constant> with the Haskell
3116                   runtime system to get an executable called
3117                   <constant>&dollar;(HS&lowbar;PROG)</constant>.</para>
3118                 </listitem>
3119               </varlistentry>
3120
3121               <varlistentry>
3122                 <term><literal>install</literal><indexterm><primary>install</primary></indexterm></term>
3123                 <listitem>
3124                   <para>installs
3125                   <constant>&dollar;(HS&lowbar;PROG)</constant> in
3126                   <constant>&dollar;(bindir)</constant>.</para>
3127                 </listitem>
3128               </varlistentry>
3129             </variablelist>
3130
3131           </listitem>
3132         </varlistentry>
3133
3134         <varlistentry>
3135           <term><constant>C&lowbar;PROG</constant><indexterm><primary>C&lowbar;PROG</primary></indexterm></term>
3136           <listitem>
3137             <para>is similar to <constant>HS&lowbar;PROG</constant>,
3138             except that the link step links
3139             <constant>&dollar;(C&lowbar;OBJS)</constant> with the C
3140             runtime system.</para>
3141           </listitem>
3142         </varlistentry>
3143
3144         <varlistentry>
3145           <term><constant>LIBRARY</constant><indexterm><primary>LIBRARY</primary></indexterm></term>
3146           <listitem>
3147             <para>is similar to <constant>HS&lowbar;PROG</constant>,
3148             except that it links
3149             <constant>&dollar;(LIB&lowbar;OBJS)</constant> to make the
3150             library archive <constant>&dollar;(LIBRARY)</constant>,
3151             and <literal>install</literal> installs it in
3152             <constant>&dollar;(libdir)</constant>.</para>
3153           </listitem>
3154         </varlistentry>
3155
3156         <varlistentry>
3157           <term><constant>LIB&lowbar;DATA</constant><indexterm><primary>LIB&lowbar;DATA</primary></indexterm></term>
3158           <listitem>
3159             <para>&hellip;</para>
3160           </listitem>
3161         </varlistentry>
3162
3163         <varlistentry>
3164           <term><constant>LIB&lowbar;EXEC</constant><indexterm><primary>LIB&lowbar;EXEC</primary></indexterm></term>
3165           <listitem>
3166             <para>&hellip;</para>
3167           </listitem>
3168         </varlistentry>
3169
3170         <varlistentry>
3171           <term><constant>HS&lowbar;SRCS</constant><indexterm><primary>HS&lowbar;SRCS</primary></indexterm>, <constant>C&lowbar;SRCS</constant><indexterm><primary>C&lowbar;SRCS</primary></indexterm>.</term>
3172           <listitem>
3173             <para>If <constant>HS&lowbar;SRCS</constant> is defined
3174             and non-empty, a rule for the target
3175             <literal>depend</literal> is included, which generates
3176             dependency information for Haskell programs.  Similarly
3177             for <constant>C&lowbar;SRCS</constant>.</para>
3178           </listitem>
3179         </varlistentry>
3180       </variablelist>
3181
3182       <para>All of these rules are &ldquo;double-colon&rdquo; rules,
3183       thus</para>
3184
3185 <ProgramListing>
3186 install :: $(HS_PROG)
3187       ...how to install it...
3188 </ProgramListing>
3189
3190       <para>GNU <command>make</command> treats double-colon rules as
3191       separate entities.  If there are several double-colon rules for
3192       the same target it takes each in turn and fires it if its
3193       dependencies say to do so.  This means that you can, for
3194       example, define both <constant>HS&lowbar;PROG</constant> and
3195       <constant>LIBRARY</constant>, which will generate two rules for
3196       <literal>install</literal>.  When you type <command>gmake
3197       install</command> both rules will be fired, and both the program
3198       and the library will be installed, just as you wanted.</para>
3199     </sect2>
3200
3201     <sect2 id="sec-subdirs">
3202       <title>Recursion</title>
3203       <indexterm><primary>recursion, in makefiles</primary></indexterm>
3204       <indexterm><primary>Makefile, recursing into subdirectories</primary></indexterm>
3205
3206       <para>In leaf <filename>Makefile</filename>s the variable
3207       <constant>SUBDIRS</constant><indexterm><primary>SUBDIRS</primary></indexterm>
3208       is undefined.  In non-leaf <filename>Makefile</filename>s,
3209       <constant>SUBDIRS</constant> is set to the list of
3210       sub-directories that contain subordinate
3211       <filename>Makefile</filename>s.  <emphasis>It is up to you to
3212       set <constant>SUBDIRS</constant> in the
3213       <filename>Makefile</filename>.</emphasis> There is no automation
3214       here&mdash;<constant>SUBDIRS</constant> is too important to
3215       automate.</para>
3216
3217       <para>When <constant>SUBDIRS</constant> is defined,
3218       <filename>target.mk</filename> includes a rather neat rule for
3219       the standard targets (<Xref LinkEnd="sec-standard-targets"> that
3220       simply invokes <command>make</command> recursively in each of
3221       the sub-directories.</para>
3222
3223       <para><emphasis>These recursive invocations are guaranteed to
3224       occur in the order in which the list of directories is specified
3225       in <constant>SUBDIRS</constant>. </emphasis>This guarantee can
3226       be important.  For example, when you say <command>gmake
3227       boot</command> it can be important that the recursive invocation
3228       of <command>make boot</command> is done in one sub-directory
3229       (the include files, say) before another (the source files).
3230       Generally, put the most independent sub-directory first, and the
3231       most dependent last.</para>
3232     </sect2>
3233
3234     <sect2 id="sec-ways">
3235       <title>Way management</title>
3236       <indexterm><primary>way management</primary></indexterm>
3237
3238       <para>We sometimes want to build essentially the same system in
3239       several different &ldquo;ways&rdquo;.  For example, we want to build GHC's
3240       <literal>Prelude</literal> libraries with and without profiling,
3241       so that there is an appropriately-built library archive to link
3242       with when the user compiles his program.  It would be possible
3243       to have a completely separate build tree for each such &ldquo;way&rdquo;,
3244       but it would be horribly bureaucratic, especially since often
3245       only parts of the build tree need to be constructed in multiple
3246       ways.</para>
3247
3248       <para>Instead, the
3249       <filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>
3250       contains some clever magic to allow you to build several
3251       versions of a system; and to control locally how many versions
3252       are built and how they differ.  This section explains the
3253       magic.</para>
3254
3255       <para>The files for a particular way are distinguished by
3256       munging the suffix.  The <quote>normal way</quote> is always
3257       built, and its files have the standard suffices
3258       <filename>.o</filename>, <filename>.hi</filename>, and so on.
3259       In addition, you can build one or more extra ways, each
3260       distinguished by a <emphasis>way tag</emphasis>.  The object
3261       files and interface files for one of these extra ways are
3262       distinguished by their suffix.  For example, way
3263       <literal>mp</literal> has files
3264       <filename>.mp&lowbar;o</filename> and
3265       <filename>.mp&lowbar;hi</filename>.  Library archives have their
3266       way tag the other side of the dot, for boring reasons; thus,
3267       <filename>libHS&lowbar;mp.a</filename>.</para>
3268
3269       <para>A <command>make</command> variable called
3270       <constant>way</constant> holds the current way tag.
3271       <emphasis><constant>way</constant> is only ever set on the
3272       command line of <command>gmake</command></emphasis> (usually in
3273       a recursive invocation of <command>gmake</command> by the
3274       system).  It is never set inside a
3275       <filename>Makefile</filename>.  So it is a global constant for
3276       any one invocation of <command>gmake</command>.  Two other
3277       <command>make</command> variables,
3278       <constant>way&lowbar;</constant> and
3279       <constant>&lowbar;way</constant> are immediately derived from
3280       <constant>&dollar;(way)</constant> and never altered.  If
3281       <constant>way</constant> is not set, then neither are
3282       <constant>way&lowbar;</constant> and
3283       <constant>&lowbar;way</constant>, and the invocation of
3284       <command>make</command> will build the <quote>normal
3285       way</quote>.  If <constant>way</constant> is set, then the other
3286       two variables are set in sympathy.  For example, if
3287       <constant>&dollar;(way)</constant> is &ldquo;<literal>mp</literal>&rdquo;,
3288       then <constant>way&lowbar;</constant> is set to
3289       &ldquo;<literal>mp&lowbar;</literal>&rdquo; and
3290       <constant>&lowbar;way</constant> is set to
3291       &ldquo;<literal>&lowbar;mp</literal>&rdquo;.  These three variables are
3292       then used when constructing file names.</para>
3293
3294       <para>So how does <command>make</command> ever get recursively
3295       invoked with <constant>way</constant> set?  There are two ways
3296       in which this happens:</para>
3297
3298       <itemizedlist>
3299         <listitem>
3300           <para>For some (but not all) of the standard targets, when
3301           in a leaf sub-directory, <command>make</command> is
3302           recursively invoked for each way tag in
3303           <constant>&dollar;(WAYS)</constant>.  You set
3304           <constant>WAYS</constant> in the
3305           <filename>Makefile</filename> to the list of way tags you
3306           want these targets built for.  The mechanism here is very
3307           much like the recursive invocation of
3308           <command>make</command> in sub-directories (<Xref
3309           LinkEnd="sec-subdirs">).  It is up to you to set
3310           <constant>WAYS</constant> in your
3311           <filename>Makefile</filename>; this is how you control what
3312           ways will get built.</para>
3313         </listitem>
3314
3315         <listitem>
3316           <para>For a useful collection of targets (such as
3317           <filename>libHS&lowbar;mp.a</filename>,
3318           <filename>Foo.mp&lowbar;o</filename>) there is a rule which
3319           recursively invokes <command>make</command> to make the
3320           specified target, setting the <constant>way</constant>
3321           variable.  So if you say <command>gmake
3322           Foo.mp&lowbar;o</command> you should see a recursive
3323           invocation <command>gmake Foo.mp&lowbar;o way=mp</command>,
3324           and <emphasis>in this recursive invocation the pattern rule
3325           for compiling a Haskell file into a <filename>.o</filename>
3326           file will match</emphasis>.  The key pattern rules (in
3327           <filename>suffix.mk</filename>) look like this:
3328
3329 <ProgramListing>
3330 %.$(way_)o : %.lhs
3331       $(HC) $(HC_OPTS) $&#60; -o $@
3332 </ProgramListing>
3333
3334           Neat, eh?</para>
3335         </listitem>
3336
3337         <listitem>
3338           <para>You can invoke <command>make</command> with a
3339           particular <literal>way</literal> setting yourself, in order
3340           to build files related to a particular
3341           <literal>way</literal> in the current directory.  eg.
3342
3343 <screen>
3344 $ make way=p
3345 </screen>
3346
3347           will build files for the profiling way only in the current
3348           directory. </para>
3349         </listitem>
3350       </itemizedlist>
3351     </sect2>
3352
3353     <sect2>
3354       <title>When the canned rule isn't right</title>
3355
3356       <para>Sometimes the canned rule just doesn't do the right thing.
3357       For example, in the <literal>nofib</literal> suite we want the
3358       link step to print out timing information.  The thing to do here
3359       is <emphasis>not</emphasis> to define
3360       <constant>HS&lowbar;PROG</constant> or
3361       <constant>C&lowbar;PROG</constant>, and instead define a special
3362       purpose rule in your own <filename>Makefile</filename>.  By
3363       using different variable names you will avoid the canned rules
3364       being included, and conflicting with yours.</para>
3365     </sect2>
3366   </sect1>
3367
3368   <sect1 id="building-docs">
3369     <title>Building the documentation</title>
3370
3371     <sect2 id="pre-supposed-doc-tools">
3372       <title>Tools for building the Documentation</title>
3373
3374       <para>The following additional tools are required if you want to
3375       format the documentation that comes with the
3376       <literal>fptools</literal> projects:</para>
3377       
3378       <variablelist>
3379         <varlistentry>
3380           <term>DocBook</term>
3381           <indexterm><primary>pre-supposed: DocBook</primary></indexterm>
3382           <indexterm><primary>DocBook, pre-supposed</primary></indexterm>
3383           <listitem>
3384             <para>Much of our documentation is written in SGML, using
3385             the DocBook DTD.  Instructions on installing and
3386             configuring the DocBook tools are below.</para>
3387           </listitem>
3388         </varlistentry>
3389
3390         <varlistentry>
3391           <term>TeX</term>
3392           <indexterm><primary>pre-supposed: TeX</primary></indexterm>
3393           <indexterm><primary>TeX, pre-supposed</primary></indexterm>
3394           <listitem>
3395             <para>A decent TeX distribution is required if you want to
3396             produce printable documentation.  We recomment teTeX,
3397             which includes just about everything you need.</para>
3398           </listitem>
3399         </varlistentry>
3400
3401         <varlistentry>
3402           <term>Haddock</term>
3403           <indexterm><primary>Haddock</primary>
3404           </indexterm>
3405           <listitem>
3406             <para>Haddock is a Haskell documentation tool that we use
3407             for automatically generating documentation from the
3408             library source code.  It is an <literal>fptools</literal>
3409             project in itself.  To build documentation for the
3410             libraries (<literal>fptools/libraries</literal>) you
3411             should check out and build Haddock in
3412             <literal>fptools/haddock</literal>.  Haddock requires GHC
3413             to build.</para>
3414           </listitem>
3415         </varlistentry>
3416       </variablelist>
3417     </sect2>
3418
3419     <sect2>
3420       <title>Installing the DocBook tools</title>
3421
3422       <sect3>
3423         <title>Installing the DocBook tools on Linux</title>
3424
3425         <para>If you're on a recent RedHat system (7.0+), you probably
3426         have working DocBook tools already installed.  The configure
3427         script should detect your setup and you're away.</para>
3428
3429         <para>If you don't have DocBook tools installed, and you are
3430         using a system that can handle RedHat RPM packages, you can
3431         probably use the <ULink
3432         URL="http://sourceware.cygnus.com/docbook-tools/">Cygnus
3433         DocBook tools</ULink>, which is the most shrink-wrapped SGML
3434         suite that we could find. You need all the RPMs except for
3435         psgml (i.e.  <Filename>docbook</Filename>,
3436         <Filename>jade</Filename>, <Filename>jadetex</Filename>,
3437         <Filename>sgmlcommon</Filename> and
3438         <Filename>stylesheets</Filename>). Note that most of these
3439         RPMs are architecture neutral, so are likely to be found in a
3440         <Filename>noarch</Filename> directory. The SuSE RPMs also
3441         work; the RedHat ones <Emphasis>don't</Emphasis> in RedHat 6.2
3442         (7.0 and later should be OK), but they are easy to fix: just
3443         make a symlink from
3444         <Filename>/usr/lib/sgml/stylesheets/nwalsh-modular/lib/dblib.dsl</Filename>
3445         to <Filename>/usr/lib/sgml/lib/dblib.dsl</Filename>. </para>
3446       </sect3>
3447     
3448       <sect3>
3449         <title>Installing DocBook on FreeBSD</title>
3450
3451         <para>On FreeBSD systems, the easiest way to get DocBook up
3452         and running is to install it from the ports tree or a
3453         pre-compiled package (packages are available from your local
3454         FreeBSD mirror site).</para>
3455
3456         <para>To use the ports tree, do this:
3457 <screen>
3458       $ cd /usr/ports/textproc/docproj
3459       $ make install
3460 </screen>
3461         This installs the FreeBSD documentation project tools, which
3462         includes everything needed to format the GHC
3463         documentation.</para>
3464       </sect3>
3465
3466       <sect3>
3467         <title>Installing from binaries on Windows</title>
3468         
3469         <Para>It's a good idea to use Norman Walsh's <ULink
3470         URL="http://nwalsh.com/docbook/dsssl/doc/install.html">installation
3471         notes</ULink> as a guide. You should get version 3.1 of
3472         DocBook, and note that his file <Filename>test.sgm</Filename>
3473         won't work, as it needs version 3.0. You should unpack Jade
3474         into <Filename>\Jade</Filename>, along with the entities,
3475         DocBook into <Filename>\docbook</Filename>, and the DocBook
3476         stylesheets into <Filename>\docbook\stylesheets</Filename> (so
3477         they actually end up in
3478         <Filename>\docbook\stylesheets\docbook</Filename>).</para>
3479       </Sect3>
3480
3481
3482       <sect3>
3483         <title>Installing the DocBook tools from source</title>
3484
3485         <sect4>
3486           <title>Jade</title>
3487
3488           <para>Install <ULink
3489           URL="http://openjade.sourceforge.net/">OpenJade</ULink>
3490           (Windows binaries are available as well as sources). If you
3491           want DVI, PS, or PDF then install JadeTeX from the
3492           <Filename>dsssl</Filename> subdirectory. (If you get the
3493           error:
3494
3495 <screen>
3496 ! LaTeX Error: Unknown option implicit=false' for package hyperref'.
3497 </screen>
3498
3499           your version of <Command>hyperref</Command> is out of date;
3500           download it from CTAN
3501           (<Filename>macros/latex/contrib/supported/hyperref</Filename>),
3502           and make it, ensuring that you have first removed or renamed
3503           your old copy. If you start getting file not found errors
3504           when making the test for <Command>hyperref</Command>, you
3505           can abort at that point and proceed straight to
3506           <Command>make install</Command>, or enter them as
3507           <Filename>../</Filename><Emphasis>filename</Emphasis>.)</para>
3508
3509           <para>Make links from <Filename>virtex</Filename> to
3510           <Filename>jadetex</Filename> and
3511           <Filename>pdfvirtex</Filename> to
3512           <Filename>pdfjadetex</Filename> (otherwise DVI, PostScript
3513           and PDF output will not work). Copy
3514           <Filename>dsssl/*.{dtd,dsl}</Filename> and
3515           <Filename>catalog</Filename> to
3516           <Filename>/usr/[local/]lib/sgml</Filename>.</para>
3517         </sect4>
3518
3519         <sect4>
3520           <title>DocBook and the DocBook stylesheets</title>
3521
3522           <para>Get a Zip of <ULink
3523           URL="http://www.oasis-open.org/docbook/sgml/3.1/index.html">DocBook</ULink>
3524           and install the contents in
3525           <Filename>/usr/[local/]/lib/sgml</Filename>.</para>
3526
3527           <para>Get the <ULink
3528           URL="http://nwalsh.com/docbook/dsssl/">DocBook
3529           stylesheets</ULink> and install in
3530           <Filename>/usr/[local/]lib/sgml/stylesheets</Filename>
3531           (thereby creating a subdirectory docbook). For indexing,
3532           copy or link <Filename>collateindex.pl</Filename> from the
3533           DocBook stylesheets archive in <Filename>bin</Filename> into
3534           a directory on your <Constant>PATH</Constant>.</para>
3535
3536           <para>Download the <ULink
3537           URL="http://www.oasis-open.org/cover/ISOEnts.zip">ISO
3538           entities</ULink> into
3539           <Filename>/usr/[local/]lib/sgml</Filename>.</para>
3540         </sect4>
3541       </sect3>
3542     </sect2>
3543
3544     <sect2>
3545       <title>Configuring the DocBook tools</title>
3546
3547       <Para>Once the DocBook tools are installed, the configure script
3548       will detect them and set up the build system accordingly. If you
3549       have a system that isn't supported, let us know, and we'll try
3550       to help.</para>
3551     </sect2>
3552
3553     <sect2>
3554       <title>Remaining problems</title>
3555
3556       <para>If you install from source, you'll get a pile of warnings
3557       of the form
3558
3559 <Screen>DTDDECL catalog entries are not supported</Screen>
3560
3561       every time you build anything. These can safely be ignored, but
3562       if you find them tedious you can get rid of them by removing all
3563       the <Constant>DTDDECL</Constant> entries from
3564       <Filename>docbook.cat</Filename>.</para>
3565     </sect2>
3566
3567     <sect2>
3568       <title>Building the documentation</title>
3569
3570       <para>To build documentation in a certain format, you can
3571       say, for example,</para>
3572
3573 <screen>
3574 $ make html
3575 </screen>
3576
3577       <para>to build HTML documentation below the current directory.
3578       The available formats are: <literal>dvi</literal>,
3579       <literal>ps</literal>, <literal>pdf</literal>,
3580       <literal>html</literal>, and <literal>rtf</literal>.  Note that
3581       not all documentation can be built in all of these formats: HTML
3582       documentation is generally supported everywhere, and DocBook
3583       documentation might support the other formats (depending on what
3584       other tools you have installed).</para>
3585
3586       <para>All of these targets are recursive; that is, saying
3587       <literal>make html</literal> will make HTML docs for all the
3588       documents recursively below the current directory.</para>
3589
3590       <para>Because there are many different formats that the DocBook
3591       documentation can be generated in, you have to select which ones
3592       you want by setting the <literal>SGMLDocWays</literal> variable
3593       to a list of them.  For example, in
3594       <filename>build.mk</filename> you might have a line:</para>
3595
3596 <screen>
3597 SGMLDocWays = html ps
3598 </screen>
3599
3600       <para>This will cause the documentation to be built in the requested
3601       formats as part of the main build (the default is not to build
3602       any documentation at all).</para>
3603     </sect2>
3604
3605     <sect2>
3606       <title>Installing the documentation</title>
3607
3608       <para>To install the documentation, use:</para>
3609
3610 <screen>
3611 $ make install-docs
3612 </screen>
3613
3614       <para>This will install the documentation into
3615       <literal>$(datadir)</literal> (which defaults to
3616       <literal>$(prefix)/share</literal>).  The exception is HTML
3617       documentation, which goes into
3618       <literal>$(datadir)/html</literal>, to keep things tidy.</para>
3619
3620       <para>Note that unless you set <literal>$(SGMLDocWays)</literal>
3621       to a list of formats, the <literal>install-docs</literal> target
3622       won't do anything for SGML documentation.</para>
3623     </sect2>
3624
3625   </sect1>
3626     
3627
3628   <sect1 id="sec-porting-ghc">
3629     <title>Porting GHC</title>
3630
3631     <para>This section describes how to port GHC to a currenly
3632     unsupported platform.  There are two distinct
3633     possibilities:</para>
3634
3635     <itemizedlist>
3636       <listitem>
3637         <para>The hardware architecture for your system is already
3638         supported by GHC, but you're running an OS that isn't
3639         supported (or perhaps has been supported in the past, but
3640         currently isn't).  This is the easiest type of porting job,
3641         but it still requires some careful bootstrapping.  Proceed to
3642         <xref linkend="sec-booting-from-hc">.</para>
3643       </listitem>
3644       
3645       <listitem>
3646         <para>Your system's hardware architecture isn't supported by
3647         GHC.  This will be a more difficult port (though by comparison
3648         perhaps not as difficult as porting gcc).  Proceed to <xref
3649         linkend="unregisterised-porting">.</para>
3650       </listitem>
3651     </itemizedlist>
3652     
3653     <sect2 id="sec-booting-from-hc">
3654       <title>Booting/porting from C (<filename>.hc</filename>) files</title>
3655
3656       <indexterm><primary>building GHC from .hc files</primary></indexterm>
3657       <indexterm><primary>booting GHC from .hc files</primary></indexterm>
3658       <indexterm><primary>porting GHC</primary></indexterm>
3659
3660       <para>Bootstrapping GHC on a system without GHC already
3661       installed is achieved by taking the intermediate C files (known
3662       as HC files) from a GHC compilation on a supported system to the
3663       target machine, and compiling them using gcc to get a working
3664       GHC.</para>
3665
3666       <para><emphasis>NOTE: GHC versions 5.xx were hard to bootstrap
3667       from C.  We recommend using GHC 6.0.1 or
3668       later.</emphasis></para>
3669
3670       <para>HC files are platform-dependent, so you have to get a set
3671       that were generated on similar hardware.  There may be some
3672       supplied on the GHC download page, otherwise you'll have to
3673       compile some up yourself, or start from
3674       <emphasis>unregisterised</emphasis> HC files - see <xref
3675       linkend="unregisterised-porting">.</para>
3676
3677       <para>The following steps should result in a working GHC build
3678       with full libraries:</para>
3679
3680       <itemizedlist>
3681         <listitem>
3682           <para>Unpack the HC files on top of a fresh source tree
3683           (make sure the source tree version matches the version of
3684           the HC files <emphasis>exactly</emphasis>!).  This will
3685           place matching <filename>.hc</filename> files next to the
3686           corresponding Haskell source (<filename>.hs</filename> or
3687           <filename>.lhs</filename>) in the compiler subdirectory
3688           <filename>ghc/compiler</filename> and in the libraries
3689           (subdirectories of <filename>hslibs</filename> and
3690           <literal>libraries</literal>).</para>
3691         </listitem>
3692
3693         <listitem>
3694           <para>The actual build process is fully automated by the
3695           <filename>hc-build</filename> script located in the
3696           <filename>distrib</filename> directory.  If you eventually
3697           want to install GHC into the directory
3698           <replaceable>dir</replaceable>, the following
3699           command will execute the whole build process (it won't
3700           install yet):</para>
3701
3702 <Screen>
3703 foo% distrib/hc-build --prefix=<replaceable>dir</replaceable>
3704 </Screen>
3705 <indexterm><primary>--hc-build</primary></indexterm>
3706
3707           <para>By default, the installation directory is
3708           <filename>/usr/local</filename>.  If that is what you want,
3709           you may omit the argument to <filename>hc-build</filename>.
3710           Generally, any option given to <filename>hc-build</filename>
3711           is passed through to the configuration script
3712           <filename>configure</filename>.  If
3713           <filename>hc-build</filename> successfully completes the
3714           build process, you can install the resulting system, as
3715           normal, with</para>
3716
3717 <Screen>
3718 foo% make install
3719 </Screen>
3720         </listitem>
3721       </itemizedlist>
3722     </sect2>
3723
3724     <sect2 id="unregisterised-porting">
3725       <title>Porting GHC to a new architecture</title>
3726       
3727       <para>The first step in porting to a new architecture is to get
3728       an <firstterm>unregisterised</firstterm> build working.  An
3729       unregisterised build is one that compiles via vanilla C only.
3730       By contrast, a registerised build uses the following
3731       architecture-specific hacks for speed:</para>
3732
3733       <itemizedlist>
3734         <listitem>
3735           <para>Global register variables: certain abstract machine
3736           <quote>registers</quote> are mapped to real machine
3737           registers, depending on how many machine registers are
3738           available (see
3739           <filename>ghc/includes/MachRegs.h</filename>).</para>
3740         </listitem>
3741
3742         <listitem>
3743           <para>Assembly-mangling: when compiling via C, we feed the
3744           assembly generated by gcc though a Perl script known as the
3745           <firstterm>mangler</firstterm> (see
3746           <filename>ghc/driver/mangler/ghc-asm.lprl</filename>).  The
3747           mangler rearranges the assembly to support tail-calls and
3748           various other optimisations.</para>
3749         </listitem>
3750       </itemizedlist>
3751
3752       <para>In an unregisterised build, neither of these hacks are
3753       used &mdash; the idea is that the C code generated by the
3754       compiler should compile using gcc only.  The lack of these
3755       optimisations costs about a factor of two in performance, but
3756       since unregisterised compilation is usually just a step on the
3757       way to a full registerised port, we don't mind too much.</para>
3758
3759       <para>Notes on GHC portability in general: we've tried to stick
3760       to writing portable code in most parts of the system, so it
3761       should compile on any POSIXish system with gcc, but in our
3762       experience most systems differ from the standards in one way or
3763       another.  Deal with any problems as they arise - if you get
3764       stuck, ask the experts on
3765       <email>glasgow-haskell-users@haskell.org</email>.</para>
3766         
3767       <para>Lots of useful information about the innards of GHC is
3768       available in the <ulink
3769       url="http://www.cse.unsw.edu.au/~chak/haskell/ghc/comm/">GHC
3770       Commentary</ulink>, which might be helpful if you run into some
3771       code which needs tweaking for your system.</para>
3772
3773       <sect3>
3774         <title>Cross-compiling to produce an unregisterised GHC</title>
3775
3776         <para>In this section, we explain how to bootstrap GHC on a
3777         new platform, using unregisterised intermediate C files.  We
3778         haven't put a great deal of effort into automating this
3779         process, for two reasons: it is done very rarely, and the
3780         process usually requires human intervention to cope with minor
3781         porting issues anyway.</para>
3782
3783         <para>The following step-by-step instructions should result in
3784         a fully working, albeit unregisterised, GHC.  Firstly, you
3785         need a machine that already has a working GHC (we'll call this
3786         the <firstterm>host</firstterm> machine), in order to
3787         cross-compile the intermediate C files that we will use to
3788         bootstrap the compiler on the <firstterm>target</firstterm>
3789         machine.</para>
3790
3791         <itemizedlist>
3792           <listitem>
3793             <para>On the target machine:</para>
3794
3795           <itemizedlist>
3796             <listitem>
3797               <para>Unpack a source tree (preferably a released
3798               version).  We will call the path to the root of this
3799               tree <replaceable>T</replaceable>.</para>
3800             </listitem>
3801
3802             <listitem>
3803 <screen>
3804 $ cd <replaceable>T</replaceable>
3805 $ ./configure --enable-hc-boot --enable-hc-boot-unregisterised
3806 </screen>
3807
3808               <para>You might need to update
3809               <filename>configure.in</filename> to recognise the new
3810               architecture, and re-generate
3811               <filename>configure</filename> with
3812               <literal>autoreconf</literal>.</para>
3813             </listitem>
3814   
3815             <listitem>
3816 <screen>
3817 $ cd <replaceable>T</replaceable>/ghc/includes
3818 $ make config.h
3819 </screen>
3820             </listitem>
3821           </itemizedlist>
3822           </listitem>
3823
3824           <listitem>
3825             <para>On the host machine:</para>
3826               
3827           <itemizedlist>
3828             <listitem>
3829               <para>Unpack a source tree (same released version).  Call
3830               this directory <replaceable>H</replaceable>.</para>
3831             </listitem>
3832  
3833             <listitem>
3834 <screen>
3835 $ cd <replaceable>H</replaceable>
3836 $ ./configure
3837 </screen>
3838             </listitem>
3839
3840             <listitem>
3841               <para>Create
3842               <filename><replaceable>H</replaceable>/mk/build.mk</filename>,
3843               with the following contents:</para>
3844
3845 <programlisting>
3846 GhcUnregisterised = YES
3847 GhcLibHcOpts = -O -H32m -keep-hc-files
3848 GhcLibWays =
3849 SplitObjs = NO
3850 GhcWithNativeCodeGen = NO
3851 GhcWithInterpreter = NO
3852 GhcStage1HcOpts = -O -H32m -fasm
3853 GhcStage2HcOpts = -O -fvia-C -keep-hc-files
3854 </programlisting>
3855             </listitem>
3856
3857             <listitem>
3858               <para>Edit
3859               <filename><replaceable>H</replaceable>/mk/config.mk</filename>:</para>
3860               <itemizedlist>
3861                 <listitem>
3862                   <para>change <literal>TARGETPLATFORM</literal>
3863                   appropriately, and set the variables involving
3864                   <literal>TARGET</literal> to the correct values for
3865                   the target platform.  This step is necessary because
3866                   currently <literal>configure</literal> doesn't cope
3867                   with specifying different values for the
3868                   <literal>--host</literal> and
3869                   <literal>--target</literal> flags.</para>
3870                 </listitem>
3871                 <listitem>
3872                   <para>copy <literal>LeadingUnderscore</literal>
3873                   setting from target.</para>
3874                 </listitem>
3875               </itemizedlist>
3876             </listitem>
3877
3878             <listitem>
3879               <para>Copy
3880               <filename><replaceable>T</replaceable>/ghc/includes/config.h</filename>
3881               to
3882               <filename><replaceable>H</replaceable>/ghc/includes</filename>.
3883               Note that we are building on the host machine, using the
3884               target machine's <literal>config.h</literal> file.  This
3885               is so that the intermediate C files generated here will
3886               be suitable for compiling on the target system.</para>
3887
3888             </listitem>
3889
3890               <listitem>
3891                 <para>Touch <literal>config.h</literal>, just to make
3892                 sure it doesn't get replaced during the build:</para>
3893 <screen>
3894 $ touch <replaceable>H</replaceable>/ghc/includes/config.h</screen>
3895               </listitem>
3896
3897             <listitem>
3898                 <para>Now build the compiler:</para>
3899 <screen>
3900 $ cd <replaceable>H</replaceable>/glafp-utils && make boot && make
3901 $ cd <replaceable>H</replaceable>/ghc && make boot && make
3902 </screen>
3903               <para>Don't worry if the build falls over in the RTS, we
3904               don't need the RTS yet.</para>
3905             </listitem>
3906
3907             <listitem>
3908 <screen>
3909 $ cd <replaceable>H</replaceable>/libraries
3910 $& make boot && make
3911 </screen>
3912             </listitem>
3913
3914             <listitem>
3915 <screen>
3916 $ cd <replaceable>H</replaceable>/ghc
3917 $ make boot stage=2 && make stage=2
3918 </screen>
3919             </listitem>
3920             
3921             <listitem>
3922               <screen>
3923 $ cd <replaceable>H</replaceable>/ghc/utils
3924 $ make clean
3925 $ make -k HC=<replaceable>H</replaceable>/ghc/compiler/stage1/ghc-inplace \
3926                 EXTRA_HC_OPTS='-O -fvia-C -keep-hc-files'
3927 </screen>
3928             </listitem>
3929             
3930             <listitem>
3931 <screen>
3932 $ cd <replaceable>H</replaceable>
3933 $ make hc-file-bundle Project=Ghc
3934 </screen>
3935             </listitem>
3936
3937             <listitem>
3938               <para>copy
3939               <filename><replaceable>H</replaceable>/*-hc.tar.gz</filename>
3940               to <filename><replaceable>T</replaceable>/..</filename>.</para>
3941             </listitem>
3942           </itemizedlist>
3943           </listitem>
3944
3945           <listitem>
3946             <para>On the target machine:</para>
3947
3948             <para>At this stage we simply need to bootstrap a compiler
3949             from the intermediate C files we generated above.  The
3950             process of bootstrapping from C files is automated by the
3951             script in <literal>distrib/hc-build</literal>, and is
3952             described in <xref linkend="sec-booting-from-hc">.</para>
3953
3954 <screen>
3955 $ ./distrib/hc-build --enable-hc-boot-unregisterised
3956 </screen>
3957
3958             <para>However, since this is a bootstrap on a new machine,
3959             the automated process might not run to completion the
3960             first time.  For that reason, you might want to treat the
3961             <literal>hc-build</literal> script as a list of
3962             instructions to follow, rather than as a fully automated
3963             script.  This way you'll be able to restart the process
3964             part-way through if you need to fix anything on the
3965             way.</para>
3966
3967             <para>Don't bother with running
3968             <literal>make&nbsp;install</literal> in the newly
3969             bootstrapped tree; just use the compiler in that tree to
3970             build a fresh compiler from scratch, this time without
3971             booting from C files.  Before doing this, you might want
3972             to check that the bootstrapped compiler is generating
3973             working binaries:</para>
3974
3975 <screen>
3976 $ cat >hello.hs
3977 main = putStrLn "Hello World!\n"
3978 ^D
3979 $ <replaceable>T</replaceable>/ghc/compiler/ghc-inplace hello.hs -o hello
3980 $ ./hello
3981 Hello World!
3982 </screen>
3983
3984             <para>Once you have the unregisterised compiler up and
3985             running, you can use it to start a registerised port.  The
3986             following sections describe the various parts of the
3987             system that will need architecture-specific tweaks in
3988             order to get a registerised build going.</para>
3989
3990           </listitem>
3991         </itemizedlist>
3992       </sect3>
3993
3994       <sect3>
3995         <title>Porting the RTS</title>
3996         
3997         <para>The following files need architecture-specific code for a
3998         registerised build:</para>
3999
4000         <variablelist>
4001           <varlistentry>
4002             <term><filename>ghc/includes/MachRegs.h</filename></term>
4003             <indexterm><primary><filename>MachRegs.h</filename></primary>
4004             </indexterm>
4005             <listitem>
4006               <para>Defines the STG-register to machine-register
4007               mapping.  You need to know your platform's C calling
4008               convention, and which registers are generally available
4009               for mapping to global register variables.  There are
4010               plenty of useful comments in this file.</para>
4011             </listitem>
4012           </varlistentry>
4013           <varlistentry>
4014             <term><filename>ghc/includes/TailCalls.h</filename></term>
4015             <indexterm><primary><filename>TailCalls.h</filename></primary>
4016             </indexterm>
4017             <listitem>
4018               <para>Macros that cooperate with the mangler (see <xref
4019               linkend="sec-mangler">) to make proper tail-calls
4020               work.</para>
4021             </listitem>
4022           </varlistentry>
4023           <varlistentry>
4024             <term><filename>ghc/rts/Adjustor.c</filename></term>
4025             <indexterm><primary><filename>Adjustor.c</filename></primary>
4026             </indexterm>
4027             <listitem>
4028               <para>Support for
4029               <literal>foreign&nbsp;import&nbsp;"wrapper"</literal>
4030               (aka
4031               <literal>foreign&nbsp;export&nbsp;dynamic</literal>).
4032               Not essential for getting GHC bootstrapped, so this file
4033               can be deferred until later if necessary.</para>
4034             </listitem>
4035           </varlistentry>
4036           <varlistentry>
4037             <term><filename>ghc/rts/StgCRun.c</filename></term>
4038             <indexterm><primary><filename>StgCRun.c</filename></primary>
4039             </indexterm>
4040             <listitem>
4041               <para>The little assembly layer between the C world and
4042               the Haskell world.  See the comments and code for the
4043               other architectures in this file for pointers.</para>
4044             </listitem>
4045           </varlistentry>
4046           <varlistentry>
4047             <term><filename>ghc/rts/MBlock.h</filename></term>
4048             <term><filename>ghc/rts/MBlock.c</filename></term>
4049             <indexterm><primary><filename>MBlock.h</filename></primary>
4050             </indexterm>
4051             <indexterm><primary><filename>MBlock.c</filename></primary>
4052             </indexterm>
4053             <listitem>
4054               <para>These files are really OS-specific rather than
4055               architecture-specific.  In <filename>MBlock.h</filename>
4056               is specified the absolute location at which the RTS
4057               should try to allocate memory on your platform (try to
4058               find an area which doesn't conflict with code or dynamic
4059               libraries).  In <filename>Mblock.c</filename> you might
4060               need to tweak the call to <literal>mmap()</literal> for
4061               your OS.</para>
4062             </listitem>
4063           </varlistentry>
4064         </variablelist>
4065       </sect3>
4066
4067       <sect3 id="sec-mangler">
4068         <title>The mangler</title>
4069         
4070         <para>The mangler is an evil Perl-script that rearranges the
4071         assembly code output from gcc to do two main things:</para>
4072
4073         <itemizedlist>
4074           <listitem>
4075             <para>Remove function prologues and epilogues, and all
4076             movement of the C stack pointer.  This is to support
4077             tail-calls: every code block in Haskell code ends in an
4078             explicit jump, so we don't want the C-stack overflowing
4079             while we're jumping around between code blocks.</para>
4080           </listitem>
4081           <listitem>
4082             <para>Move the <firstterm>info table</firstterm> for a
4083             closure next to the entry code for that closure.  In
4084             unregisterised code, info tables contain a pointer to the
4085             entry code, but in registerised compilation we arrange
4086             that the info table is shoved right up against the entry
4087             code, and addressed backwards from the entry code pointer
4088             (this saves a word in the info table and an extra
4089             indirection when jumping to the closure entry
4090             code).</para>
4091           </listitem>
4092         </itemizedlist>
4093
4094         <para>The mangler is abstracted to a certain extent over some
4095         architecture-specific things such as the particular assembler
4096         directives used to herald symbols.  Take a look at the
4097         definitions for other architectures and use these as a
4098         starting point.</para>
4099       </sect3>
4100
4101       <sect3>
4102         <title>The native code generator</title>
4103
4104         <para>The native code generator isn't essential to getting a
4105         registerised build going, but it's a desirable thing to have
4106         because it can cut compilation times in half.  The native code
4107         generator is described in some detail in the <ulink
4108         url="http://www.cse.unsw.edu.au/~chak/haskell/ghc/comm/">GHC
4109         commentary</ulink>.</para>
4110       </sect3>
4111
4112       <sect3>
4113         <title>GHCi</title>
4114
4115         <para>To support GHCi, you need to port the dynamic linker
4116         (<filename>fptools/ghc/rts/Linker.c</filename>).  The linker
4117         currently supports the ELF and PEi386 object file formats - if
4118         your platform uses one of these then things will be
4119         significantly easier.  The majority of Unix platforms use the
4120         ELF format these days.  Even so, there are some
4121         machine-specific parts of the ELF linker: for example, the
4122         code for resolving particular relocation types is
4123         machine-specific, so some porting of this code to your
4124         architecture will probaly be necessary.</para>
4125         
4126         <para>If your system uses a different object file format, then
4127         you have to write a linker &mdash; good luck!</para>
4128       </sect3>
4129     </sect2>
4130
4131   </sect1>
4132
4133 <sect1 id="sec-build-pitfalls">
4134 <title>Known pitfalls in building Glasgow Haskell
4135
4136 <indexterm><primary>problems, building</primary></indexterm>
4137 <indexterm><primary>pitfalls, in building</primary></indexterm>
4138 <indexterm><primary>building pitfalls</primary></indexterm></title>
4139
4140 <para>
4141 WARNINGS about pitfalls and known &ldquo;problems&rdquo;:
4142 </para>
4143
4144 <para>
4145
4146 <OrderedList>
4147 <listitem>
4148
4149 <para>
4150 One difficulty that comes up from time to time is running out of space
4151 in <literal>TMPDIR</literal>.  (It is impossible for the configuration stuff to
4152 compensate for the vagaries of different sysadmin approaches to temp
4153 space.)
4154 <indexterm><primary>tmp, running out of space in</primary></indexterm>
4155
4156 The quickest way around it is <command>setenv TMPDIR /usr/tmp</command><indexterm><primary>TMPDIR</primary></indexterm> or
4157 even <command>setenv TMPDIR .</command> (or the equivalent incantation with your shell
4158 of choice).
4159
4160 The best way around it is to say
4161
4162 <ProgramListing>
4163 export TMPDIR=&#60;dir&#62;
4164 </ProgramListing>
4165
4166 in your <filename>build.mk</filename> file.
4167 Then GHC and the other <literal>fptools</literal> programs will use the appropriate directory
4168 in all cases.
4169
4170
4171 </para>
4172 </listitem>
4173 <listitem>
4174
4175 <para>
4176 In compiling some support-code bits, e.g., in <filename>ghc/rts/gmp</filename> and even
4177 in <filename>ghc/lib</filename>, you may get a few C-compiler warnings.  We think these
4178 are OK.
4179
4180 </para>
4181 </listitem>
4182 <listitem>
4183
4184 <para>
4185 When compiling via C, you'll sometimes get &ldquo;warning: assignment from
4186 incompatible pointer type&rdquo; out of GCC.  Harmless.
4187
4188 </para>
4189 </listitem>
4190 <listitem>
4191
4192 <para>
4193 Similarly, <command>ar</command>chiving warning messages like the following are not
4194 a problem:
4195
4196 <Screen>
4197 ar: filename GlaIOMonad__1_2s.o truncated to GlaIOMonad_
4198 ar: filename GlaIOMonad__2_2s.o truncated to GlaIOMonad_
4199 ...
4200 </Screen>
4201
4202
4203 </para>
4204 </listitem>
4205 <listitem>
4206
4207 <para>
4208  In compiling the compiler proper (in <filename>compiler/</filename>), you <emphasis>may</emphasis>
4209 get an &ldquo;Out of heap space&rdquo; error message.  These can vary with the
4210 vagaries of different systems, it seems.  The solution is simple:
4211
4212
4213 <itemizedlist>
4214 <listitem>
4215
4216 <para>
4217  If you're compiling with GHC 4.00 or later, then the
4218 <emphasis>maximum</emphasis> heap size must have been reached.  This
4219 is somewhat unlikely, since the maximum is set to 64M by default.
4220 Anyway, you can raise it with the
4221 <option>-optCrts-M&lt;size&gt;</option> flag (add this flag to
4222 <constant>&lt;module&gt;&lowbar;HC&lowbar;OPTS</constant>
4223 <command>make</command> variable in the appropriate
4224 <filename>Makefile</filename>).
4225
4226 </para>
4227 </listitem>
4228 <listitem>
4229
4230 <para>
4231  For GHC &#60; 4.00, add a suitable <option>-H</option> flag to the <filename>Makefile</filename>, as
4232 above.
4233
4234 </para>
4235 </listitem>
4236
4237 </itemizedlist>
4238
4239
4240 and try again: <command>gmake</command>.  (see <Xref LinkEnd="sec-suffix"> for information about
4241 <constant>&lt;module&gt;&lowbar;HC&lowbar;OPTS</constant>.)
4242
4243 Alternatively, just cut to the chase:
4244
4245 <Screen>
4246 % cd ghc/compiler
4247 % make EXTRA_HC_OPTS=-optCrts-M128M
4248 </Screen>
4249
4250
4251 </para>
4252 </listitem>
4253 <listitem>
4254
4255 <para>
4256 If you try to compile some Haskell, and you get errors from GCC about
4257 lots of things from <filename>/usr/include/math.h</filename>, then your GCC was
4258 mis-installed.  <command>fixincludes</command> wasn't run when it should've been.
4259
4260 As <command>fixincludes</command> is now automagically run as part of GCC installation,
4261 this bug also suggests that you have an old GCC.
4262
4263
4264 </para>
4265 </listitem>
4266 <listitem>
4267
4268 <para>
4269 You <emphasis>may</emphasis> need to re-<command>ranlib</command><indexterm><primary>ranlib</primary></indexterm> your libraries (on Sun4s).
4270
4271
4272 <Screen>
4273 % cd $(libdir)/ghc-x.xx/sparc-sun-sunos4
4274 % foreach i ( `find . -name '*.a' -print` ) # or other-shell equiv...
4275 ?    ranlib $i
4276 ?    # or, on some machines: ar s $i
4277 ? end
4278 </Screen>
4279
4280
4281 We'd be interested to know if this is still necessary.
4282
4283
4284 </para>
4285 </listitem>
4286 <listitem>
4287
4288 <para>
4289 GHC's sources go through <command>cpp</command> before being compiled, and <command>cpp</command> varies
4290 a bit from one Unix to another.  One particular gotcha is macro calls
4291 like this:
4292
4293
4294 <ProgramListing>
4295 SLIT("Hello, world")
4296 </ProgramListing>
4297
4298
4299 Some <command>cpp</command>s treat the comma inside the string as separating two macro
4300 arguments, so you get
4301
4302
4303 <Screen>
4304 :731: macro `SLIT' used with too many (2) args
4305 </Screen>
4306
4307
4308 Alas, <command>cpp</command> doesn't tell you the offending file!
4309
4310 Workaround: don't put weird things in string args to <command>cpp</command> macros.
4311 </para>
4312 </listitem>
4313
4314 </OrderedList>
4315
4316 </para>
4317
4318 </sect1>
4319
4320
4321 <Sect1 id="winbuild"><Title>Notes for building under Windows</Title>
4322
4323 <para>
4324 This section summarises how to get the utilities you need on your
4325 Win95/98/NT/2000 machine to use CVS and build GHC. Similar notes for
4326 installing and running GHC may be found in the user guide. In general,
4327 Win95/Win98 behave the same, and WinNT/Win2k behave the same.
4328 You should read the GHC installation guide sections on Windows (in the user
4329 guide) before continuing to read these notes.
4330 </para>
4331
4332
4333 <sect2 id="cygwin-and-mingw"><Title>Cygwin and MinGW</Title>
4334
4335 <para> The Windows situation for building GHC is rather confusing.  This section
4336 tries to clarify, and to establish terminology.</para>
4337
4338 <sect3 id="ghc-mingw"><title>GHC-mingw</title>
4339
4340 <para> <ulink url="http://www.mingw.org">MinGW (Minimalist GNU for Windows)</ulink> 
4341 is a collection of header
4342 files and import libraries that allow one to use <command>gcc</command> and produce
4343 native Win32 programs that do not rely on any third-party DLLs. The
4344 current set of tools include GNU Compiler Collection (<command>gcc</command>), GNU Binary
4345 Utilities (Binutils), GNU debugger (Gdb), GNU make, and a assorted
4346 other utilities. 
4347 </para>
4348 <para>The GHC that we distribute includes, inside the distribution itself, the MinGW <command>gcc</command>,
4349 <command>as</command>, <command>ld</command>, and a bunch of input/output libraries.  
4350 GHC compiles Haskell to C (or to 
4351 assembly code), and then invokes these MinGW tools to generate an executable binary.
4352 The resulting binaries can run on any Win32 system.
4353 </para>
4354 <para> We will call a GHC that targets MinGW in this way <emphasis>GHC-mingw</emphasis>.</para>
4355
4356 <para> The down-side of GHC-mingw is that the MinGW libraries do not support anything like the full
4357 Posix interface.  So programs compiled with GHC-mingw cannot import the (Haskell) Posix 
4358 library; they have to do
4359 their input output using standard Haskell I/O libraries, or native Win32 bindings.
4360 </para>
4361 </sect3>
4362
4363 <sect3 id="ghc-cygwin"><title>GHC-cygwin</title>
4364
4365 <para>There <emphasis>is</emphasis> a way to get the full Posix interface, which is to use Cygwin.  
4366 <ulink url="http://www.cygwin.com">Cygwin</ulink> is a complete Unix simulation that runs on Win32.
4367 Cygwin comes with a shell, and all the usual Unix commands: <command>mv</command>, <command>rm</command>,
4368 <command>ls</command>, plus of course <command>gcc</command>, <command>ld</command> and so on.
4369 A C program compiled with the Cygwin <command>gcc</command> certainly can use all of Posix.
4370 </para>
4371 <para>So why doesn't GHC use the Cygwin <command>gcc</command> and libraries?  Because
4372 Cygwin comes with a DLL <emphasis>that must be linked with every runnable Cygwin-compiled program</emphasis>.
4373 A program compiled by the Cygwin tools cannot run at all unless Cygwin is installed. 
4374 If GHC targeted Cygwin, users would have to install Cygwin just to run the Haskell programs
4375 that GHC compiled; and the Cygwin DLL would have to be in the DLL load path.
4376 Worse, Cygwin is a moving target.  The name of the main DLL, <literal>cygwin1.dll</literal>
4377 does not change, but the implementation certainly does.  Even the interfaces to functions
4378 it exports seem to change occasionally. So programs compiled by GHC might only run with
4379 particular versions of Cygwin.  All of this seems very undesirable.
4380 </para>
4381 <para>
4382 Nevertheless, it is certainly possible to build a version of GHC that targets Cygwin;
4383 we will call that <emphasis>GHC-cygwin</emphasis>.  The up-side of GHC-cygwin is
4384 that Haskell programs compiled by GHC-cygwin can import the (Haskell) Posix library.
4385 </para>
4386 </sect3>
4387
4388 <sect3><title>HOST_OS vs TARGET_OS</title>
4389
4390 <para>
4391 In the source code you'll find various ifdefs looking like:
4392 <programlisting>
4393   #ifdef mingw32_HOST_OS
4394     ...blah blah...
4395   #endif
4396 </programlisting>
4397 and 
4398 <programlisting>
4399   #ifdef mingw32_TARGET_OS
4400     ...blah blah...
4401   #endif
4402 </programlisting>
4403 These macros are set by the configure script (via the file config.h).
4404 Which is which?  The criterion is this.  In the ifdefs in GHC's source code:
4405 <itemizedlist>
4406   <listitem> <para>
4407   The "host" system is the one on which GHC itself will be run.
4408   </para> </listitem>
4409   <listitem> <para>
4410   The "target" system is the one for which the program compiled by GHC will be run.
4411   </para> </listitem>
4412 </itemizedlist>
4413 For a stage-2 compiler, in which GHCi is available, the "host" and "target" systems must be the same.
4414 So then it doesn't really matter whether you use the HOST_OS or TARGET_OS cpp macros.
4415
4416 </para>
4417 </sect3>
4418
4419 <sect3><title>Summary</title>
4420
4421 <para>Notice that "GHC-mingw" means "GHC that <emphasis>targets</emphasis> MinGW".  It says nothing about 
4422 how that GHC was <emphasis>built</emphasis>.  It is entirely possible to have a GHC-mingw that was built
4423 by compiling GHC's Haskell sources with a GHC-cygwin, or vice versa.</para>
4424
4425 <para>We distribute only a GHC-mingw built by a GHC-mingw; supporting
4426 GHC-cygwin too is beyond our resources.  The GHC we distribute
4427 therefore does not require Cygwin to run, nor do the programs it
4428 compiles require Cygwin.</para>
4429
4430 <para>The instructions that follow describe how to build GHC-mingw. It is
4431 possible to build GHC-cygwin, but it's not a supported route, and the build system might
4432 be flaky.</para>
4433
4434 <para>In your build tree, you build a compiler called <Command>ghc-inplace</Command>.  It
4435 uses the <Command>gcc</Command> that you specify using the
4436 <option>--with-gcc</option> flag when you run
4437 <Command>configure</Command> (see below).
4438 The makefiles are careful to use <Command>ghc-inplace</Command> (not <Command>gcc</Command>)
4439 to compile any C files, so that it will in turn invoke the right <Command>gcc</Command> rather that
4440 whatever one happens to be in your path.  However, the makefiles do use whatever <Command>ld</Command> 
4441 and <Command>ar</Command> happen to be in your path. This is a bit naughty, but (a) they are only
4442 used to glom together .o files into a bigger .o file, or a .a file, 
4443 so they don't ever get libraries (which would be bogus; they might be the wrong libraries), and (b)
4444 Cygwin and Mingw use the same .o file format.  So its ok.
4445 </para>
4446 </sect3>
4447 </sect2>
4448
4449 <Sect2><Title>Installing and configuring Cygwin</Title>
4450
4451 <para>You don't need Cygwin to <emphasis>use</emphasis> GHC, 
4452 but you do need it to <emphasis>build</emphasis> GHC.</para>
4453
4454 <para> Install Cygwin from <ulink url="http://www.cygwin.com/">http://www.cygwin.com/</ulink>.
4455 The installation process is straightforward; we install it in <Filename>c:/cygwin</Filename>.
4456 During the installation dialogue, make sure that you select all of the following:
4457 <itemizedlist>
4458 <listitem><para>
4459               <command>cvs</command>, 
4460 </para></listitem>
4461 <listitem><para>
4462               <command>openssh</command>,
4463 </para></listitem>
4464 <listitem><para>
4465               <command>autoreconf</command>,
4466 </para></listitem>
4467 <listitem><para>
4468               <command>automake</command>,
4469 </para></listitem>
4470 <listitem><para>
4471               <command>binutils</command> (includes ld and (I think) ar),
4472 </para></listitem>
4473 <listitem><para>
4474               <command>gcc</command>,
4475 </para></listitem>
4476 <listitem><para>
4477               <command>flex</command>,
4478 </para></listitem>
4479 <listitem><para>
4480               <command>make</command>.
4481 </para></listitem>
4482 </itemizedlist>
4483 If you miss out any of these, strange things will happen to you.   To see thse packages, 
4484 click on the "View" button in the "Select Packages" 
4485 stage of Cygwin's installation dialogue, until the view says "Full".  The default view, which is
4486 "Category" isn't very helpful, and the "View" button is rather unobtrousive.
4487 </para>
4488 <para> Now set the following user environment variables:
4489 <itemizedlist>
4490
4491 <listitem><para> Add <filename>c:/cygwin/bin</filename> and <filename>c:/cygwin/usr/bin</filename> to your 
4492 <constant>PATH</constant></para></listitem>
4493
4494 <listitem>
4495 <para>
4496 Set <constant>MAKE_MODE</constant> to <Literal>UNIX</Literal>. If you
4497 don't do this you get very weird messages when you type
4498 <Command>make</Command>, such as:
4499 <Screen>
4500 /c: /c: No such file or directory
4501 </Screen>
4502 </para>
4503 </listitem>
4504
4505 <listitem><para> Set <constant>SHELL</constant> to
4506 <Filename>c:/cygwin/bin/bash</Filename>. When you invoke a shell in Emacs, this
4507 <constant>SHELL</constant> is what you get.
4508 </para></listitem>
4509
4510 <listitem><para> Set <constant>HOME</constant> to point to your 
4511 home directory.  This is where, for example,
4512 <command>bash</command> will look for your <filename>.bashrc</filename>
4513 file.  Ditto <command>emacs</command> looking for <filename>.emacsrc</filename>
4514 </para></listitem>
4515 </itemizedlist>
4516 </para>
4517
4518 <para>
4519 There are a few other things to do:
4520 <itemizedlist>
4521 <listitem>
4522 <para>
4523 By default, cygwin provides the command shell <filename>ash</filename>
4524 as <filename>sh.exe</filename>. We have often seen build-system problems that 
4525 turn out to be due to bugs in <filename>ash</filename>
4526 (to do with quoting
4527 and length of command lines).  On the other hand <filename>bash</filename> seems
4528 to be rock solid.
4529 So, in <filename>cygwin/bin</filename>
4530 remove the supplied <filename>sh.exe</filename> (or rename it as <filename>ash.exe</filename>),
4531 and copy <filename>bash.exe</filename> to  <filename>sh.exe</filename>.
4532 You'll need to do this in Windows Explorer or the Windows <command>cmd</command> shell, because
4533 you can't rename a running program!
4534 </para>
4535 </listitem>
4536
4537 <listitem>
4538 <para>
4539 Some script files used in the make system start with "<Command>#!/bin/perl</Command>",
4540 (and similarly for <Command>sh</Command>).  Notice the hardwired path!
4541 So you need to ensure that your <Filename>/bin</Filename> directory has the following
4542 binaries in it:
4543 <itemizedlist>
4544 <listitem> <para><Command>sh</Command></para></listitem>
4545 <listitem> <para><Command>perl</Command></para></listitem>
4546 <listitem> <para><Command>cat</Command></para></listitem>
4547 </itemizedlist>
4548 All these come in Cygwin's <Filename>bin</Filename> directory, which you probably have
4549 installed as <Filename>c:/cygwin/bin</Filename>.  By default Cygwin mounts "<Filename>/</Filename>" as
4550 <Filename>c:/cygwin</Filename>, so if you just take the defaults it'll all work ok.
4551 (You can discover where your Cygwin
4552 root directory <Filename>/</Filename> is by typing <Command>mount</Command>.)
4553 Provided <Filename>/bin</Filename> points to the Cygwin <Filename>bin</Filename>
4554 directory, there's no need to copy anything.  If not, copy these binaries from the <filename>cygwin/bin</filename>
4555 directory (after fixing the <filename>sh.exe</filename> stuff mentioned in the previous bullet).
4556 </para>
4557 </listitem>
4558 </itemizedlist>
4559 </para>
4560
4561 <para>Finally, here are some things to be aware of when using Cygwin:
4562 <itemizedlist>
4563 <listitem> <para>Cygwin doesn't deal well with filenames that include
4564 spaces. "<filename>Program Files</filename>" and "<filename>Local files</filename>" are
4565 common gotchas.
4566 </para></listitem>
4567
4568 <listitem> <para> Cygwin implements a symbolic link as a text file with some
4569 magical text in it.  So other programs that don't use Cygwin's
4570 I/O libraries won't recognise such files as symlinks.  
4571 In particular, programs compiled by GHC are meant to be runnable
4572 without having Cygwin, so they don't use the Cygwin library, so
4573 they don't recognise symlinks.
4574 </para></listitem>
4575
4576 <listitem> <para>
4577 Win32 has a <command>find</command> command which is not the same as Cygwin's find.
4578 You will probably discover that the Win32 <command>find</command> appears in your <constant>PATH</constant>
4579 before the Cygwin one, because it's in the <emphasis>system</emphasis> <constant>PATH</constant> 
4580 environment variable, whereas you have probably modified the <emphasis>user</emphasis> <constant>PATH</constant> 
4581 variable.  You can always invoke <command>find</command> with an absolute path, or rename it.
4582 </para></listitem>
4583 </itemizedlist>
4584 </para>
4585
4586 </Sect2>
4587
4588 <Sect2 id="configure-ssh"><Title>Configuring SSH</Title>
4589
4590 <para><command>ssh</command> comes with Cygwin, provided you remember to ask for it when
4591 you install Cygwin.  (If not, the installer lets you update easily.)  Look for <command>openssh</command> 
4592 (not ssh) in the Cygwin list of applications!</para>
4593
4594 <para>There are several strange things about <command>ssh</command> on Windows that you need to know.
4595 <itemizedlist>
4596 <listitem>
4597 <para>
4598        The programs <command>ssh-keygen1</command>, <command>ssh1</command>, and <command>cvs</command>,
4599        seem to lock up <command>bash</command> entirely if they try to get user input (e.g. if
4600        they ask for a password).  To solve this, start up <filename>cmd.exe</filename> 
4601        and run it as follows:
4602        <Screen>
4603        c:\tmp> set CYGWIN32=tty
4604        c:\tmp> c:/user/local/bin/ssh-keygen1
4605        </Screen> </para>
4606 </listitem>
4607
4608 <listitem><para>
4609 <command>ssh</command> needs to access your directory <filename>.ssh</filename>, in your home directory.  
4610 To determine your home directory <command>ssh</command> first looks in 
4611 <filename>c:/cygwin/etc/passwd</filename> (or wherever you have Cygwin installed).  If there's an entry
4612 there with your userid, it'll use that entry to determine your home directory, <emphasis>ignoring
4613 the setting of the environment variable $HOME</emphasis>.  If the home directory is
4614 bogus, <command>ssh</command> fails horribly.   The best way to see what is going on is to say
4615 <programlisting>
4616   ssh -v cvs.haskell.org
4617 </programlisting>
4618 which makes <command>ssh</command> print out information about its activity.
4619 </para>
4620 <para> You can fix this problem, either by correcting the home-directory field in 
4621 <filename>c:/cygwin/etc/passwd</filename>, or by simply deleting the entire entry for your userid. If
4622 you do that, <command>ssh</command> uses the $HOME environment variable instead.
4623 </para>
4624
4625 </listitem>
4626
4627 <listitem>
4628             <para>To protect your
4629             <literal>.ssh</literal> from access by anyone else,
4630             right-click your <literal>.ssh</literal> directory, and
4631             select <literal>Properties</literal>.  If you are not on
4632             the access control list, add yourself, and give yourself
4633             full permissions (the second panel).  Remove everyone else
4634             from the access control list.  Don't leave them there but
4635             deny them access, because 'they' may be a list that
4636             includes you!</para>
4637 </listitem>
4638
4639 <listitem>
4640             <para>In fact <command>ssh</command> 3.6.1 now seems to <emphasis>require</emphasis>
4641               you to have Unix permissions 600 (read/write for owner only) 
4642               on the <literal>.ssh/identity</literal> file, else it 
4643               bombs out.  For your local C drive, it seems that <literal>chmod 600 identity</literal> works,
4644               but on Windows NT/XP, it doesn't work on a network drive (exact dteails obscure).  
4645               The solution seems to be to set the $CYGWIN environment
4646               variable to "<literal>ntsec neta</literal>".  The $CYGWIN environment variable is discussed
4647               in <ulink url="http://cygwin.com/cygwin-ug-net/using-cygwinenv.html">the Cygwin User's Guide</ulink>,
4648               and there are more details in <ulink url="http://cygwin.com/faq/faq_4.html#SEC44">the Cygwin FAQ</ulink>.
4649               </para>
4650 </listitem>
4651 </itemizedlist>
4652 </para>
4653 </sect2>
4654
4655 <Sect2><Title>Other things you need to install</Title>
4656
4657 <para>You have to install the following other things to build GHC:
4658 <itemizedlist>
4659 <listitem>
4660 <para>
4661 Install an executable GHC, from <ulink url="http://www.haskell.org/ghc">http://www.haskell.org/ghc</ulink>.
4662 This is what you will use to compile GHC.  Add it in your
4663 <constant>PATH</constant>: the installer tells you the path element
4664 you need to add upon completion.
4665 </para>
4666 </listitem>
4667
4668 <listitem>
4669 <para>
4670 Install an executable Happy, from <ulink url="http://www.haskell.org/happy">http://www.haskell.org/happy</ulink>.
4671 Happy is a parser generator used to compile the Haskell grammar.  Add it in your
4672 <constant>PATH</constant>.
4673 </para>
4674 </listitem>
4675
4676           <listitem>
4677             <para>Install Alex.  This can be done by building from the
4678             source distribution in the usual way.  Sources are
4679             available from <ulink
4680             url="http://www.haskell.org/alex">http://www.haskell.org/alex</ulink>.</para>
4681           </listitem>
4682
4683 <listitem>
4684 <para>GHC uses the <emphasis>mingw</emphasis> C compiler to
4685 generate code, so you have to install that (see <xref linkend="cygwin-and-mingw">). 
4686 Just pick up a mingw bundle at
4687 <ulink url="http://www.mingw.org/">http://www.mingw.org/</ulink>.
4688 We install it in <filename>c:/mingw</filename>.
4689 </para>
4690 <para>Do <emphasis>not</emphasis> add any of the <emphasis>mingw</emphasis> binaries to your  path.
4691 They are only going to get used by explicit access (via the --with-gcc flag you
4692 give to <Command>configure</Command> later).  If you do add them to your path
4693 you are likely to get into a mess because their names overlap with Cygwin binaries.
4694 </para>
4695 </listitem>
4696
4697
4698 <listitem>
4699 <para>We use <command>emacs</command> a lot, so we install that too.
4700 When you are in <filename>fptools/ghc/compiler</filename>, you can use
4701 "<literal>make tags</literal>" to make a TAGS file for emacs.  That uses the utility
4702 <filename>fptools/ghc/utils/hasktags/hasktags</filename>, so you need to make that first.
4703 The most convenient way to do this is by going <literal>make boot</literal> in <filename>fptools/ghc</filename>.
4704 The <literal>make tags</literal> command also uses <command>etags</command>, which comes with <command>emacs</command>,
4705 so you will need to add <filename>emacs/bin</filename> to your <literal>PATH</literal>.
4706 </para>
4707 </listitem>
4708
4709
4710 <listitem>
4711 <para> Finally, check out a copy of GHC sources from
4712 the CVS repository, following the instructions above (<xref linkend="cvs-access">).
4713 </para>
4714 </listitem>
4715 </itemizedlist>
4716 </para>
4717 </sect2>
4718
4719 <Sect2><Title>Building GHC</Title>
4720
4721 <para>OK!  
4722 Now go read the documentation above on building from source (<xref linkend="sec-building-from-source">); 
4723 the bullets below only tell
4724 you about Windows-specific wrinkles.</para>
4725 <ItemizedList>
4726 <listitem>
4727 <para>
4728 Run <Command>autoreconf</Command> both in <filename>fptools</filename>
4729 and in <filename>fptools/ghc</filename>.  If you omit the latter step you'll
4730 get an error when you run <filename>./configure</filename>:
4731 <Screen>
4732 ...lots of stuff...
4733 creating mk/config.h
4734 mk/config.h is unchanged
4735 configuring in ghc
4736 running /bin/sh ./configure  --cache-file=.././config.cache --srcdir=.
4737 ./configure: ./configure: No such file or directory
4738 configure: error: ./configure failed for ghc
4739 </Screen>
4740 </para>
4741 </listitem>
4742
4743 <listitem> <para><command>autoreconf</command> seems to create the file <filename>configure</filename>
4744 read-only.  So if you need to run autoreconf again (which I sometimes do for safety's sake),
4745 you get
4746 <screen>
4747 /usr/bin/autoreconf: cannot create configure: permission denied
4748 </screen>
4749 Solution: delete <filename>configure</filename> first.
4750 </para></listitem>
4751
4752 <listitem>
4753 <para>
4754 You either need to add <filename>ghc</filename> to your
4755 <constant>PATH</constant> before you invoke
4756 <Command>configure</Command>, or use the <Command>configure</Command>
4757 option <option>--with-ghc=c:/ghc/ghc-some-version/bin/ghc</option>.
4758 </para>
4759 </listitem>
4760
4761 <listitem><para>
4762 If you are paranoid, delete <filename>config.cache</filename> if it exists.
4763 This file occasionally remembers out-of-date configuration information, which 
4764 can be really confusing.
4765 </para>
4766 </listitem>
4767
4768 <listitem>
4769   <para> 
4770     After <command>autoreconf</command> run <command>./configure</command> in
4771     <filename>fptools/</filename> thus:
4772
4773 <Screen>
4774   ./configure --host=i386-unknown-mingw32 --with-gcc=c:/mingw/bin/gcc
4775 </Screen>
4776 This is the point at which you specify that you are building GHC-mingw
4777 (see <xref linkend="ghc-mingw">). </para>
4778
4779 <para> Both these options are important! It's possible to get into
4780 trouble using the wrong C compiler!</para>
4781 <para>
4782 Furthermore, it's <emphasis>very important</emphasis> that you specify a 
4783 full MinGW path for <command>gcc</command>, not a Cygwin path, because GHC (which
4784 uses this path to invoke <command>gcc</command>) is a MinGW program and won't
4785 understand a Cygwin path.  For example, if you 
4786 say <literal>--with-gcc=/mingw/bin/gcc</literal>, it'll be interpreted as
4787 <filename>/cygdrive/c/mingw/bin/gcc</filename>, and GHC will fail the first
4788 time it tries to invoke it.   Worse, the failure comes with
4789 no error message whatsoever.  GHC simply fails silently when first invoked, 
4790 typically leaving you with this:
4791 <programlisting>
4792 make[4]: Leaving directory `/cygdrive/e/fptools-stage1/ghc/rts/gmp'
4793 ../../ghc/compiler/ghc-inplace -optc-mno-cygwin -optc-O 
4794   -optc-Wall -optc-W  -optc-Wstrict-prototypes -optc-Wmissing-prototypes 
4795   -optc-Wmissing-declarations -optc-Winline -optc-Waggregate-return 
4796   -optc-Wbad-function-cast -optc-Wcast-align -optc-I../includes 
4797   -optc-I. -optc-Iparallel -optc-DCOMPILING_RTS 
4798   -optc-fomit-frame-pointer -O2 -static 
4799   -package-name rts -O -dcore-lint -c Adjustor.c -o Adjustor.o
4800 make[2]: *** [Adjustor.o] Error 1
4801 make[1]: *** [all] Error 1
4802 make[1]: Leaving directory `/cygdrive/e/fptools-stage1/ghc'
4803 make: *** [all] Error 1
4804 </programlisting>
4805 Be warned!
4806 </para>
4807
4808 <para>
4809 If you want to build GHC-cygwin (<xref linkend="ghc-cygwin">)
4810 you'll have to do something more like:
4811 <Screen>
4812   ./configure --with-gcc=...the Cygwin gcc...
4813 </Screen>
4814 </para>
4815 </listitem>
4816
4817 <listitem><para> You almost certainly want to set
4818 <programlisting>
4819   SplitObjs = NO
4820 </programlisting>
4821 in your <filename>build.mk</filename> configuration file (see <xref linkend="sec-build-config">).
4822 This tells the build system not to split each library into a myriad of little object files, one
4823 for each function.  Doing so reduces binary sizes for statically-linked binaries, but on Windows
4824 it dramatically increases the time taken to build the libraries in the first place.
4825 </para>
4826 </listitem>
4827
4828 <listitem><para> Do not attempt to build the documentation.
4829 It needs all kinds of wierd Jade stuff that we haven't worked out for
4830 Win32.</para></listitem>
4831 </ItemizedList>
4832 </Sect2>
4833
4834
4835 </sect1>
4836
4837 </Article>