[project @ 2002-03-04 01:36:08 by sof]
[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 Glasgow <Literal>fptools</Literal> suite</title>
34
35     <para>Building the Glasgow tools <Emphasis>can</Emphasis> be
36     complicated, mostly because there are so many permutations of
37     what/why/how, e.g., ``Build Happy with HBC, everything else with
38     GHC, leave out profiling, and test it all on the `real' NoFib
39     programs.''  Yeeps!</para>
40
41     <para>Happily, such complications don't apply to most people.  A
42     few common ``strategies'' serve most purposes.  Pick one and
43     proceed as suggested:</para>
44
45 <VariableList>
46
47 <VarListEntry>
48 <term><indexterm><primary>Binary distribution</primary></indexterm>Binary distribution.</term>
49 <listitem>
50 <para>
51 If your only purpose is to install some of the
52 <Literal>fptools</Literal> suite then the easiest thing to do is to
53 get a binary distribution. In the binary distribution everything is
54 pre-compiled for your particular machine architecture and operating
55 system, so all you should have to do is install the binaries and
56 libraries in suitable places. The user guide describes how to do this.
57 </para>
58
59 <para>
60 A binary distribution may not work for you for two reasons.  First, we
61 may not have built the suite for the particular architecture/OS
62 platform you want. That may be due to lack of time and energy (in
63 which case you can get a source distribution and build from it; see
64 below).  Alternatively, it may be because we haven't yet ported the
65 suite to your architecture, in which case you are considerably worse
66 off.
67 </para>
68
69 <para>
70 The second reason a binary distribution may not be what you want is
71 if you want to read or modify the souce code.
72 </para>
73 </listitem></VarListEntry>
74 <VarListEntry>
75 <term><indexterm><primary>Source distribution</primary></indexterm>Source distribution.</term>
76 <listitem>
77 <para>
78 You have a supported
79 platform, but (a)&nbsp;you like the warm fuzzy feeling of compiling things
80 yourself; (b)&nbsp;you want to build something ``extra''&mdash;e.g., a set of
81 libraries with strictness-analysis turned off; or (c)&nbsp;you want to hack
82 on GHC yourself.
83 </para>
84
85 <para>
86 A source distribution contains complete sources for one or more
87 projects in the <Literal>fptools</Literal> suite.  Not only that, but
88 the more awkward machine-independent steps are done for you.  For
89 example, if you don't have
90 <Command>happy</Command><indexterm><primary>happy</primary></indexterm>
91 you'll find it convenient that the source distribution contains the
92 result of running <Command>happy</Command> on the parser
93 specifications.  If you don't want to alter the parser then this saves
94 you having to find and install <Command>happy</Command>. You will
95 still need a working version of GHC (preferably version 4.08+) on your
96 machine in order to compile (most of) the sources, however.
97 </para>
98
99 </listitem></VarListEntry>
100
101       <varlistentry>
102         <term>The CVS repository.</term>
103         <indexterm><primary>CVS repository</primary>
104         </indexterm>
105         <listitem>
106           <para>We make releases infrequently.  If you want more
107           up-to-the minute (but less tested) source code then you need
108           to get access to our CVS repository.</para>
109
110           <para>All the <Literal>fptools</Literal> source code is held
111           in a CVS repository. CVS is a pretty good source-code
112           control system, and best of all it works over the
113           network.</para>
114
115           <para>The repository holds source code only. It holds no
116           mechanically generated files at all.  So if you check out a
117           source tree from CVS you will need to install every utility
118           so that you can build all the derived files from
119           scratch.</para>
120
121           <para>More information about our CVS repository can be found
122           in <xref linkend="sec-cvs">.</para>
123
124         </listitem>
125       </varlistentry>
126
127       <varlistentry>
128         <term>Build GHC from intermediate C <Filename>.hc</Filename> files<indexterm><primary>hc files</primary></indexterm>:</term>
129         <listitem>
130           <para>You need a working GHC to use a source distribution.
131           What if you don't have a working GHC? Then you may be able
132           to bootstrap up from the intermediate C
133           (<filename>.hc</filename>) files that we provide.  Building
134           GHC on an unsupported platform falls into this category.
135           Beware: this route is not for the faint hearted!  Please see
136           <Xref LinkEnd="sec-booting-from-C">.</para>
137
138           <para>Once you have built GHC, you can build the other
139           Glasgow tools with it.</para>
140
141           <para>In theory, you can (could?) build GHC with another
142           Haskell compiler (e.g., HBC). We haven't tried to do this
143           for ages and it almost certainly doesn't work any more (for
144           tedious reasons).</para>
145         </listitem>
146       </varlistentry>
147     </variablelist>
148
149     <para>If you are going to do any building from sources (either
150     from a source distribution or the CVS repository) then you need to
151     read all of this manual in detail.</para>
152   </sect1>
153
154   <sect1 id="sec-cvs">
155     <title>Using the CVS repository</title>
156
157     <para>We use <ulink url="http://www.cvshome.org/">CVS</ulink> (Concurrent Version System) to keep track of our
158     sources for various software projects. CVS lets several people
159     work on the same software at the same time, allowing changes to be
160     checked in incrementally. </para>
161
162     <para>This section is a set of guidelines for how to use our CVS
163     repository, and will probably evolve in time. The main thing to
164     remember is that most mistakes can be undone, but if there's
165     anything you're not sure about feel free to bug the local CVS
166     meister (namely Jeff Lewis
167     <email>jlewis@galconn.com</email>). </para>
168
169     <sect2 id="cvs-access">
170       <title>Getting access to the CVS Repository</title>
171
172       <para>You can access the repository in one of two ways:
173       read-only (<xref linkend="cvs-read-only">), or read-write (<xref
174       linkend="cvs-read-write">).</para>
175
176       <sect3 id="cvs-read-only">
177         <title>Remote Read-only CVS Access</title>
178
179         <para>Read-only access is available to anyone - there's no
180         need to ask us first.  With read-only CVS access you can do
181         anything except commit changes to the repository.  You can
182         make changes to your local tree, and still use CVS's merge
183         facility to keep your tree up to date, and you can generate
184         patches using 'cvs diff' in order to send to us for
185         inclusion. </para>
186
187         <para>To get read-only access to the repository:</para>
188
189         <orderedlist>
190           <listitem>
191             <para>Make sure that <application>cvs</application> is
192             installed on your machine.</para>
193           </listitem>
194           <listitem>
195             <para>Set your <literal>$CVSROOT</literal> environment variable to
196             <literal>:pserver:anoncvs@glass.cse.ogi.edu:/cvs</literal></para>
197           </listitem>
198           <listitem>
199             <para>Run the command</para>
200 <programlisting>
201     $ cvs login
202 </programlisting>
203             <para>The password is simply <literal>cvs</literal>.  This
204             sets up a file in your home directory called
205             <literal>.cvspass</literal>, which squirrels away the
206             dummy password, so you only need to do this step once.</para>
207           </listitem>
208
209           <listitem>
210             <para>Now go to <xref linkend="cvs-first">.</para>
211           </listitem>
212         </orderedlist>
213       </sect3>
214
215       <sect3 id="cvs-read-write">
216         <title>Remote Read-Write CVS Access</title>
217
218         <para>We generally supply read-write access to folk doing
219         serious development on some part of the source tree, when
220         going through us would be a pain. If you're developing some
221         feature, or think you have the time and inclination to fix
222         bugs in our sources, feel free to ask for read-write
223         access. There is a certain amount of responsibility that goes
224         with commit privileges; we are more likely to grant you access
225         if you've demonstrated your competence by sending us patches
226         via mail in the past.</para>
227
228         <para>To get remote read-write CVS access, you need to do the
229         following steps.</para>
230
231         <orderedlist>
232           <listitem>
233             <para>Make sure that <literal>cvs</literal> and
234             <literal>ssh</literal> are both installed on your
235             machine.</para>
236           </listitem>
237
238           <listitem>
239             <para>Generate a DSA private-key/public-key pair, thus:</para>
240 <screen>
241      $ ssh-keygen -d
242 </screen>
243             <para>(<literal>ssh-keygen</literal> comes with
244             <literal>ssh</literal>.)  Running <literal>ssh-keygen
245             -d</literal> creates the private and public keys in
246             <literal>$HOME/.ssh/id_dsa</literal> and
247             <literal>$HOME/.ssh/id_dsa.pub</literal> respectively
248             (assuming you accept the standard defaults).</para>
249
250             <para><literal>ssh-keygen -d</literal> will only work if
251             you have Version 2 <literal>ssh</literal> installed; it
252             will fail harmlessly otherwise.  If you only have Version
253             1 you can instead generate an RSA key pair using plain</para>
254 <screen>
255     $ ssh-keygen
256 </screen>
257
258             <para>Doing so creates the private and public RSA keys in
259             <literal>$HOME/.ssh/identity</literal> and
260             <literal>$HOME/.ssh/identity.pub</literal>
261             respectively.</para>
262
263             <para>[Deprecated.]  Incidentally, you can force a Version
264             2 <literal>ssh</literal> to use the Version 1 protocol by
265             creating <literal>$HOME/config</literal> with the
266             following in it:</para>
267 <screen>
268    BatchMode Yes
269
270    Host cvs.haskell.org
271    Protocol 1
272 </screen>
273
274             <para>In both cases, <literal>ssh-keygen</literal> will
275             ask for a <firstterm>passphrase</firstterm>.  The
276             passphrase is a password that protects your private key.
277             In response to the 'Enter passphrase' question, you can
278             either:</para>
279             <itemizedlist>
280               <listitem>
281                 <para>[Recommended.]  Enter a passphrase, which you
282                 will quote each time you use CVS.
283                 <literal>ssh-agent</literal> makes this entirely
284                 un-tiresome.</para>
285               </listitem>
286               <listitem>
287                 <para>[Deprecated.] Just hit return (i.e. use an empty
288                 passphrase); then you won't need to quote the
289                 passphrase when using CVS.  The downside is that
290                 anyone who can see into your <literal>.ssh</literal>
291                 directory, and thereby get your private key, can mess
292                 up the repository.  So you must keep the
293                 <literal>.ssh</literal> directory with draconian
294                 no-access permissions.</para>
295               </listitem>
296             </itemizedlist>
297
298
299        <para>
300        [Windows users.] The programs <command>ssh-keygen1</command>, <command>ssh1</command>, and <command>cvs</command>,
301        seem to lock up <command>bash</command> entirely if they try to get user input (e.g. if
302        they ask for a password).  To solve this, start up <filename>cmd.exe</filename> 
303        and run it as follows:
304        <Screen>
305        c:\tmp> set CYGWIN32=tty
306        c:\tmp> c:/user/local/bin/ssh-keygen1
307        </Screen> </para>
308
309             <para>[Windows users.] To protect your
310             <literal>.ssh</literal> from access by anyone else,
311             right-click your <literal>.ssh</literal> directory, and
312             select <literal>Properties</literal>.  If you are not on
313             the access control list, add yourself, and give yourself
314             full permissions (the second panel).  Remove everyone else
315             from the access control list.  Don't leave them there but
316             deny them access, because 'they' may be a list that
317             includes you!</para>
318           </listitem>
319
320           <listitem>
321             <para>Send a message to to the CVS repository
322             administrator (currently Jeff Lewis
323             <email>jeff@galconn.com</email>), containing:</para>
324             <itemizedlist>
325               <listitem>
326                 <para>Your desired user-name.</para>
327               </listitem>
328               <listitem>
329                 <para>Your <literal>.ssh/id_dsa.pub</literal> (or
330                 <literal>.ssh/identity.pub</literal>).</para>
331               </listitem>
332             </itemizedlist>
333             <para>He will set up your account.</para>
334           </listitem>
335
336           <listitem>
337             <para>Set the following environment variables:</para>
338            <ItemizedList>
339            <listitem>
340            <para>
341            <constant>$HOME</constant>: points to your home directory.  This is where CVS
342            will look for its <filename>.cvsrc</filename> file.
343            </para>
344            </listitem>
345
346            <listitem>
347            <para>
348            <constant>$CVS_RSH</constant> to <filename>ssh</filename>
349            </para>
350            <para>[Windows users.] Setting your <literal>CVS_RSH</literal> to
351             <literal>ssh</literal> assumes that your CVS client
352             understands how to execute shell script
353             (&quot;#!&quot;s,really), which is what
354             <literal>ssh</literal> is. This may not be the case on
355             Win32 platforms, so in that case set <literal>CVS_RSH</literal> to
356             <literal>ssh1</literal>.</para>
357            </listitem>
358
359              <listitem>
360                 <para><literal>$CVSROOT</literal> to
361                 <literal>:ext:</literal><replaceable>your-username</replaceable>
362                 <literal>@cvs.haskell.org:/home/cvs/root</literal>
363                 where <replaceable>your-username</replaceable> is your user name on
364                 <literal>cvs.haskell.org</literal>.
365                 </para>
366         <para>The <literal>CVSROOT</literal> environment variable will
367         be recorded in the checked-out tree, so you don't need to set
368         this every time. </para>
369
370              </listitem>
371
372         <listitem>
373         <para>
374         <constant>$CVSEDITOR</constant>: <filename>bin/gnuclient.exe</filename> 
375         if you want to use an Emacs buffer for typing in those long commit messages.
376         </para>
377         </listitem>
378
379         <listitem>
380         <para>
381         <constant>$SHELL</constant>: To use bash as the shell in Emacs, you need to
382         set this to point to <Filename>bash.exe</Filename>.
383         </para>
384         </listitem>
385
386        </ItemizedList>
387
388
389           </listitem>
390
391           <listitem>
392           <para>
393           Put the following in <filename>$HOME/.cvsrc</filename>:
394           </para>
395           
396           <ProgramListing>
397           checkout -P
398           release -d
399           update -P
400           diff -u
401           </ProgramListing>
402           
403           <para>
404           These are the default options for the specified CVS commands,
405           and represent better defaults than the usual ones.  (Feel
406           free to change them.)
407           </para>
408           
409           <para>
410           [Windows users.]  Filenames starting with <filename>.</filename> were illegal in 
411           the 8.3 DOS filesystem, but that restriction should have
412           been lifted by now (i.e., you're using VFAT or later filesystems.) If
413           you're still having problems creating it, don't worry; <filename>.cvsrc</filename> is entirely
414           optional.
415           </para>
416           </listitem>
417
418         </orderedlist>
419
420
421         <para>[Experts.]  Once your account is set up, you can get
422         access from other machines without bothering Jeff, thus:</para>
423         <orderedlist>
424           <listitem>
425             <para>Generate a public/private key pair on the new
426             machine.</para>
427           </listitem>
428           <listitem>
429             <para>Use ssh to log in to
430             <literal>cvs.haskell.org</literal>, from your old
431             machine.</para>
432           </listitem>
433           <listitem>
434             <para>Add the public key for the new machine to the file
435             <literal>$HOME/ssh/authorized_keys</literal> on
436             <literal>cvs.haskell.org</literal>.
437             (<literal>authorized_keys2</literal>, I think, for Version
438             2 protocol.)</para>
439           </listitem>
440           <listitem>
441             <para>Make sure that the new version of
442             <literal>authorized_keys</literal> still has 600 file
443             permissions.</para>
444           </listitem>
445         </orderedlist>
446       </sect3>
447     </sect2>
448
449
450
451     <sect2 id="cvs-first">
452       <title>Checking Out a Source Tree</title>
453
454       <itemizedlist>
455         <listitem>
456           <para>Make sure you set your <literal>CVSROOT</literal>
457           environment variable according to either of the remote
458           methods above. The Approved Way to check out a source tree
459           is as follows:</para>
460
461 <screen>
462     $ cvs checkout fpconfig
463 </screen>
464
465           <para>At this point you have a new directory called
466           <literal>fptools</literal> which contains the basic stuff
467           for the fptools suite, including the configuration files and
468           some other junk. </para>
469
470 <para>[Windows users.]  The following messages appear to be harmless:
471 <Screen>
472 setsockopt IPTOS_LOWDELAY: Invalid argument
473 setsockopt IPTOS_THROUGHPUT: Invalid argument
474 </Screen>
475 </para>
476
477
478           <para>You can call the fptools directory whatever you like,
479           CVS won't mind: </para>
480           
481 <screen>
482     $ mv fptools <replaceable>directory</replaceable>
483 </screen>
484
485           <para> NB: after you've read the CVS manual you might be
486           tempted to try</para>
487 <screen>
488     $ cvs checkout -d <replaceable>directory</replaceable> fpconfig
489 </screen>
490
491           <para>instead of checking out <literal>fpconfig</literal>
492           and then renaming it.  But this doesn't work, and will
493           result in checking out the entire repository instead of just
494           the <literal>fpconfig</literal> bit.</para>
495 <screen>
496     $ cd <replaceable>directory</replaceable>
497     $ cvs checkout ghc hslibs
498 </screen>
499
500           <para>The second command here checks out the relevant
501           modules you want to work on. For a GHC build, for instance,
502           you need at least the <literal>ghc</literal> and
503           <literal>hslibs</literal> modules (for a full list of the
504           projects available, see <xref linkend="projects">).</para>
505         </listitem>
506       </itemizedlist>
507     </sect2>
508
509     <sect2 id="cvs-committing">
510       <title>Committing Changes</title>
511
512       <para>This is only if you have read-write access to the
513       repository. For anoncvs users, CVS will issue a &quot;read-only
514       repository&quot; error if you try to commit changes.</para>
515
516       <itemizedlist>
517         <listitem>
518           <para>Build the software, if necessary. Unless you're just
519           working on documentation, you'll probably want to build the
520           software in order to test any changes you make.</para>
521         </listitem>
522
523         <listitem>
524           <para>Make changes. Preferably small ones first.</para>
525         </listitem>
526
527         <listitem>
528           <para>Test them. You can see exactly what changes you've
529           made by using the <literal>cvs diff</literal> command:</para>
530 <screen>
531 $ cvs diff
532 </screen>
533           <para>lists all the changes (using the
534           <literal>diff</literal> command) in and below the current
535           directory. In emacs, <literal>C-c C-v =</literal> runs
536           <literal>cvs diff</literal> on the current buffer and shows
537           you the results.</para>
538         </listitem>
539
540       <listitem>
541           <para>Before checking in a change, you need to update your
542           source tree:</para>
543
544 <screen>
545 $ cd fptools
546 $ cvs update
547 </screen>
548           <para>This pulls in any changes that other people have made,
549           and merges them with yours. If there are any conflicts, CVS
550           will tell you, and you'll have to resolve them before you
551           can check your changes in. The documentation describes what
552           to do in the event of a conflict.</para>
553
554           <para>It's not always necessary to do a full cvs update
555           before checking in a change, since CVS will always tell you
556           if you try to check in a file that someone else has changed.
557           However, you should still update at regular intervals to
558           avoid making changes that don't work in conjuction with
559           changes that someone else made. Keeping an eye on what goes
560           by on the mailing list can help here.</para>
561         </listitem>
562
563         <listitem>
564           <para>When you're happy that your change isn't going to
565           break anything, check it in. For a one-file change:</para>
566
567 <screen>
568 $ cvs commit <replaceable>filename</replaceable>
569 </screen>
570
571           <para>CVS will then pop up an editor for you to enter a
572           &quot;commit message&quot;, this is just a short description
573           of what your change does, and will be kept in the history of
574           the file.</para>
575
576           <para>If you're using emacs, simply load up the file into a
577           buffer and type <literal>C-x C-q</literal>, and emacs will
578           prompt for a commit message and then check in the file for
579           you.</para>
580
581           <para>For a multiple-file change, things are a bit
582           trickier. There are several ways to do this, but this is the
583           way I find easiest. First type the commit message into a
584           temporary file. Then either</para>
585
586 <screen>
587 $ cvs commit -F <replaceable>commit-message</replaceable> <replaceable>file_1</replaceable> .... <replaceable>file_n</replaceable>
588 </screen>
589
590           <para>or, if nothing else has changed in this part of the
591           source tree, </para>
592
593 <screen>
594 $ cvs commit -F <replaceable>commit-message</replaceable> <replaceable>directory</replaceable>
595 </screen>
596
597           <para>where <replaceable>directory</replaceable> is a common
598           parent directory for all your changes, and
599           <replaceable>commit-message</replaceable> is the name of the
600           file containing the commit message.</para>
601
602           <para>Shortly afterwards, you'll get some mail from the
603           relevant mailing list saying which files changed, and giving
604           the commit message. For a multiple-file change, you should
605           still get only <emphasis>one</emphasis> message.</para>
606         </listitem>
607       </itemizedlist>
608     </sect2>
609
610     <sect2 id="cvs-update">
611       <title>Updating Your Source Tree</title>
612
613       <para>It can be tempting to cvs update just part of a source
614       tree to bring in some changes that someone else has made, or
615       before committing your own changes. This is NOT RECOMMENDED!
616       Quite often changes in one part of the tree are dependent on
617       changes in another part of the tree (the
618       <literal>mk/*.mk</literal> files are a good example where
619       problems crop up quite often). Having an inconsistent tree is a
620       major cause of headaches. </para>
621
622       <para>So, to avoid a lot of hassle, follow this recipe for
623       updating your tree: </para>
624
625 <screen>
626 $ cd fptools
627 $ cvs update -Pd 2&gt;&amp;1 | tee log</screen>
628
629       <para>Look at the log file, and fix any conflicts (denoted by a
630       <quote>C</quote> in the first column). If you're using multiple
631       build trees, then for every build tree you have pointing at this
632       source tree, you need to update the links in case any new files
633       have appeared: </para>
634
635 <screen>
636 $ cd <replaceable>build-tree</replaceable>
637 $ lndir <replaceable>source-tree</replaceable>
638 </screen>
639
640       <para>Some files might have been removed, so you need to remove
641       the links pointing to these non-existent files:</para>
642
643 <screen>
644 $ find . -xtype l -exec rm '{}' \;
645 </screen>
646
647       <para>To be <emphasis>really</emphasis> safe, you should do
648       </para>
649
650 <screen>$ gmake all</screen>
651
652       <para>from the top-level, to update the dependencies and build
653       any changed files. </para>
654     </sect2>
655
656     <sect2 id="cvs-tags">
657       <title>GHC Tag Policy</title>
658
659       <para>If you want to check out a particular version of GHC,
660       you'll need to know how we tag versions in the repository.  The
661       policy (as of 4.04) is:</para>
662
663       <itemizedlist>
664         <listitem>
665           <para>The tree is branched before every major release.  The
666           branch tag is <literal>ghc-x-xx-branch</literal>, where
667           <literal>x-xx</literal> is the version number of the release
668           with the <literal>'.'</literal> replaced by a
669           <literal>'-'</literal>.  For example, the 4.04 release lives
670           on <literal>ghc-4-04-branch</literal>.</para>
671         </listitem>
672
673         <listitem>
674           <para>The release itself is tagged with
675           <literal>ghc-x-xx</literal> (on the branch).  eg. 4.06 is
676           called <literal>ghc-4-06</literal>.</para>
677         </listitem>
678
679         <listitem>
680           <para>We didn't always follow these guidelines, so to see
681           what tags there are for previous versions, do <literal>cvs
682           log</literal> on a file that's been around for a while (like
683           <literal>fptools/ghc/README</literal>).</para>
684         </listitem>
685       </itemizedlist>
686
687       <para>So, to check out a fresh GHC 4.06 tree you would
688       do:</para>
689
690 <screen>
691      $ cvs co -r ghc-4-06 fpconfig
692      $ cd fptools
693      $ cvs co -r ghc-4-06 ghc hslibs
694 </screen>
695     </sect2>
696
697     <sect2 id="cvs-hints">
698       <title>General Hints</title>
699
700       <itemizedlist>
701         <listitem>
702           <para>As a general rule: commit changes in small units,
703           preferably addressing one issue or implementing a single
704           feature.  Provide a descriptive log message so that the
705           repository records exactly which changes were required to
706           implement a given feature/fix a bug. I've found this
707           <emphasis>very</emphasis> useful in the past for finding out
708           when a particular bug was introduced: you can just wind back
709           the CVS tree until the bug disappears.</para>
710         </listitem>
711
712         <listitem>
713           <para>Keep the sources at least *buildable* at any given
714           time. No doubt bugs will creep in, but it's quite easy to
715           ensure that any change made at least leaves the tree in a
716           buildable state. We do nightly builds of GHC to keep an eye
717           on what things work/don't work each day and how we're doing
718           in relation to previous verions. This idea is truely wrecked
719           if the compiler won't build in the first place!</para>
720         </listitem>
721
722         <listitem>
723           <para>To check out extra bits into an already-checked-out
724           tree, use the following procedure.  Suppose you have a
725           checked-out fptools tree containing just ghc, and you want
726           to add nofib to it:</para>
727
728 <screen>
729 $ cd fptools
730 $ cvs checkout nofib
731 </screen>
732
733           <para>or: </para>
734
735 <screen>
736 $ cd fptools
737 $ cvs update -d nofib
738 </screen>
739           
740           <para>(the -d flag tells update to create a new
741           directory). If you just want part of the nofib suite, you
742           can do </para>
743
744 <screen>
745 $ cd fptools
746 $ cvs checkout nofib/spectral
747 </screen>
748
749           <para>This works because <literal>nofib</literal> is a
750           module in its own right, and spectral is a subdirectory of
751           the nofib module. The path argument to checkout must always
752           start with a module name. There's no equivalent form of this
753           command using <literal>update</literal>.</para>
754         </listitem>
755       </itemizedlist>
756     </sect2>
757   </sect1>
758
759   <sect1 id="projects">
760     <title>What projects are there?</title>
761
762     <para>The <literal>fptools</literal> suite consists of several
763     <firstterm>projects</firstterm>, most of which can be downloaded,
764     built and installed individually.  Each project corresponds to a
765     subdirectory in the source tree, and if checking out from CVS then
766     each project can be checked out individually by sitting in the top
767     level of your source tree and typing <command>cvs checkout
768     <replaceable>project</replaceable></command>.</para>
769
770     <para>Here is a list of the projects currently available:</para>
771
772     <variablelist>
773       <varlistentry>
774         <term><literal>ghc</literal></term>
775         <indexterm><primary><literal>ghc</literal></primary>
776         <secondary>project</secondary></indexterm>
777         <listitem>
778           <para>The <ulink url="http://www.haskell.org/ghc/">Glasgow
779           Haskell Compiler</ulink> (minus libraries).  Absolutely
780           required for building GHC.</para>
781         </listitem>
782       </varlistentry>
783
784       <varlistentry>
785         <term><literal>glafp-utils</literal></term>
786         <indexterm><primary><literal>glafp-utils</literal></primary><secondary>project</secondary></indexterm>
787         <listitem>
788           <para>Utility programs, some of which are used by the
789           build/installation system.  Required for pretty much
790           everything.</para>
791         </listitem>
792       </varlistentry>
793
794       <varlistentry>
795         <term><literal>green-card</literal></term>
796         <indexterm><primary><literal>green-card</literal></primary><secondary>project</secondary></indexterm>
797         <listitem>
798           <para>The <ulink
799           url="http://www.haskell.org/greencard/">Green Card</ulink>
800           system for generating Haskell foreign function
801           interfaces.</para>
802         </listitem>
803       </varlistentry>
804
805       <varlistentry>
806         <term><literal>haggis</literal></term>
807         <indexterm><primary><literal>haggis</literal></primary><secondary>project</secondary></indexterm>
808         <listitem>
809           <para>The <ulink
810           url="http://www.dcs.gla.ac.uk/fp/software/haggis/">Haggis</ulink>
811           Haskell GUI framework.</para>
812         </listitem>
813       </varlistentry>
814
815       <varlistentry>
816         <term><literal>happy</literal></term>
817         <indexterm><primary><literal>happy</literal></primary><secondary>project</secondary></indexterm>
818         <listitem>
819           <para>The <ulink
820           url="http://www.haskell.org/happy/">Happy</ulink> Parser
821           generator.</para>
822         </listitem>
823       </varlistentry>
824
825       <varlistentry>
826         <term><literal>hdirect</literal></term>
827         <indexterm><primary><literal>hdirect</literal></primary><secondary>project</secondary></indexterm>
828         <listitem>
829           <para>The <ulink
830           url="http://www.haskell.org/hdirect/">H/Direct</ulink>
831           Haskell interoperability tool.</para>
832         </listitem>
833       </varlistentry>
834
835       <varlistentry>
836         <term><literal>hood</literal></term>
837         <indexterm><primary><literal>hood</literal></primary><secondary>project</secondary></indexterm>
838         <listitem>
839           <para>The <ulink url="http://www.haskell.org/hood/">Haskell
840           Object Observation Debugger</ulink>.</para>
841         </listitem>
842       </varlistentry>
843
844       <varlistentry>
845         <term><literal>hslibs</literal></term>
846         <indexterm><primary><literal>hslibs</literal></primary><secondary>project</secondary></indexterm>
847         <listitem>
848           <para>GHC's libraries.  Required for building GHC.</para>
849         </listitem>
850       </varlistentry>
851
852       <varlistentry>
853         <term><literal>libraries</literal></term>
854         <indexterm><primary><literal></literal></primary><secondary>project</secondary></indexterm>
855         <listitem>
856           <para>Hierarchical Haskell library suite
857           (experimental).</para>
858         </listitem>
859       </varlistentry>
860
861       <varlistentry>
862         <term><literal>mhms</literal></term>
863         <indexterm><primary><literal></literal></primary><secondary>project</secondary></indexterm>
864         <listitem>
865           <para>The Modular Haskell Metric System.</para>
866         </listitem>
867       </varlistentry>
868
869       <varlistentry>
870         <term><literal>nofib</literal></term>
871         <indexterm><primary><literal>nofib</literal></primary><secondary>project</secondary></indexterm>
872         <listitem>
873           <para>The NoFib suite: A collection of Haskell programs used
874           primarily for benchmarking.</para>
875         </listitem>
876       </varlistentry>
877
878       <varlistentry>
879         <term><literal>testsuite</literal></term>
880         <indexterm><primary><literal>testsuite</literal></primary><secondary>project</secondary></indexterm>
881         <listitem>
882           <para>A testing framework, including GHC's regression test
883           suite.</para>
884         </listitem>
885       </varlistentry>
886     </variablelist>
887
888     <para>So, to build GHC you need at least the
889     <literal>ghc</literal> and <literal>hslibs</literal> projects (a
890     GHC source distribution will already include the bits you
891     need).</para>
892   </sect1>
893
894   <sect1 id="sec-build-checks">
895     <title>Things to check before you start</title>
896
897     <para>Here's a list of things to check before you get
898     started.</para>
899
900     <orderedlist>
901
902       <listitem>
903         <indexterm><primary>Disk space needed</primary></indexterm>
904         <para>Disk space needed: from about 100Mb for a basic GHC
905         build, up to probably 500Mb for a GHC build with everything
906         included (libraries built several different ways,
907         etc.).</para>
908       </listitem>
909
910       <listitem>
911         <para>Use an appropriate machine, compilers, and things.
912         SPARC boxes, PCs running Linux or FreeBSD, and Alphas running
913         OSF/1 are all fully supported.  Win32 and HP boxes are in
914         pretty good shape.  PCs running Solaris, DEC Alphas running
915         Linux or some BSD variant, MIPS and AIX boxes will need some
916         minimal porting effort before they work (as of 4.06).  <xref
917         linkend="sec-port-info"> gives the full run-down on ports or
918         lack thereof.</para>
919       </listitem>
920
921       <listitem>
922         <para>Be sure that the ``pre-supposed'' utilities are
923         installed.  <Xref LinkEnd="sec-pre-supposed">
924         elaborates.</para>
925       </listitem>
926
927       <listitem>
928         <para>If you have any problem when building or installing the
929         Glasgow tools, please check the ``known pitfalls'' (<Xref
930         LinkEnd="sec-build-pitfalls">).  Also check the FAQ for the
931         version you're building, which should be available from the
932         relevant download page on the <ULink
933         URL="http://www.haskell.org/ghc/" >GHC web
934         site</ULink>.</para>
935
936         <indexterm><primary>known bugs</primary></indexterm>
937         <indexterm><primary>bugs, known</primary></indexterm>
938
939         <para>If you feel there is still some shortcoming in our
940         procedure or instructions, please report it.</para>
941
942         <para>For GHC, please see the bug-reporting section of the GHC
943         Users' Guide (separate document), to maximise the usefulness
944         of your report.</para>
945
946         <indexterm><primary>bugs</primary><secondary>seporting</secondary></indexterm>
947
948         <para>If in doubt, please send a message to
949 <email>glasgow-haskell-bugs@haskell.org</email>.
950 <indexterm><primary>bugs</primary><secondary>mailing
951 list</secondary></indexterm></para>
952
953       </listitem>
954     </orderedlist>
955   </sect1>
956
957 <Sect1 id="sec-port-info">
958 <Title>What machines the Glasgow tools run on
959 </Title>
960
961 <para>
962 <indexterm><primary>ports, GHC</primary></indexterm>
963 <indexterm><primary>GHC ports</primary></indexterm>
964 <indexterm><primary>supported platforms</primary></indexterm>
965 <indexterm><primary>platforms, supported</primary></indexterm>
966 The main question is whether or not the Haskell compiler (GHC) runs on
967 your platform.
968 </para>
969
970 <para>
971 A ``platform'' is a architecture/manufacturer/operating-system
972 combination, such as <Literal>sparc-sun-solaris2</Literal>.  Other common ones are
973 <Literal>alpha-dec-osf2</Literal>, <Literal>hppa1.1-hp-hpux9</Literal>, <Literal>i386-unknown-linux</Literal>,
974 <Literal>i386-unknown-solaris2</Literal>, <Literal>i386-unknown-freebsd</Literal>,
975 <Literal>i386-unknown-cygwin32</Literal>, <Literal>m68k-sun-sunos4</Literal>, <Literal>mips-sgi-irix5</Literal>,
976 <Literal>sparc-sun-sunos4</Literal>, <Literal>sparc-sun-solaris2</Literal>, <Literal>powerpc-ibm-aix</Literal>.
977 </para>
978
979 <para>
980 Bear in mind that certain ``bundles'', e.g. parallel Haskell, may not
981 work on all machines for which basic Haskell compiling is supported.
982 </para>
983
984 <para>
985 Some libraries may only work on a limited number of platforms; for
986 example, a sockets library is of no use unless the operating system
987 supports the underlying BSDisms.
988 </para>
989
990 <Sect2>
991 <Title>What platforms the Haskell compiler (GHC) runs on</Title>
992
993 <para>
994 <indexterm><primary>fully-supported platforms</primary></indexterm>
995 <indexterm><primary>native-code generator</primary></indexterm>
996 <indexterm><primary>registerised ports</primary></indexterm>
997 <indexterm><primary>unregisterised ports</primary></indexterm>
998 The GHC hierarchy of Porting Goodness: (a)&nbsp;Best is a native-code
999 generator; (b)&nbsp;next best is a ``registerised''
1000 port; (c)&nbsp;the bare minimum is an ``unregisterised'' port.
1001 (``Unregisterised'' is so terrible that we won't say more about it).
1002 </para>
1003
1004 <para>
1005 We use Sparcs running Solaris 2.7 and x86 boxes running FreeBSD and
1006 Linux, so those are the best supported platforms, unsurprisingly.
1007 </para>
1008
1009 <para>
1010 Here's everything that's known about GHC ports.  We identify platforms
1011 by their ``canonical'' CPU/Manufacturer/OS triple.
1012 </para>
1013
1014       <variablelist>
1015
1016         <varlistentry>
1017           <term>alpha-dec-{osf,linux,freebsd,openbsd,netbsd}:</term>
1018           <indexterm><primary>alpha-dec-osf</primary></indexterm>
1019           <indexterm><primary>alpha-dec-linux</primary></indexterm>
1020           <indexterm><primary>alpha-dec-freebsd</primary></indexterm>
1021           <indexterm><primary>alpha-dec-openbsd</primary></indexterm>
1022           <indexterm><primary>alpha-dec-netbsd</primary></indexterm>
1023           
1024           <listitem>
1025             <para>The OSF port is currently working (as of GHC version
1026             5.02.1) and well supported.  The native code generator is
1027             currently non-working.  Other operating systems will
1028             require some minor porting.</para>
1029           </listitem>
1030         </varlistentry>
1031
1032         <varlistentry>
1033           <term>sparc-sun-sunos4</term>
1034           <indexterm><primary>sparc-sun-sunos4</primary></indexterm>
1035           <listitem>
1036             <para>Probably works with minor tweaks, hasn't been tested
1037             for a while.</para>
1038           </listitem>
1039         </varlistentry>
1040
1041         <varlistentry>
1042           <term>sparc-sun-solaris2</term>
1043           <indexterm><primary>sparc-sun-solaris2</primary></indexterm>
1044           <listitem>
1045             <para>Fully supported, including native-code
1046             generator.</para>
1047           </listitem>
1048         </varlistentry>
1049
1050         <varlistentry>
1051           <term>hppa1.1-hp-hpux (HP-PA boxes running HPUX 9.x)</term>
1052           <indexterm><primary>hppa1.1-hp-hpux</primary></indexterm>
1053           <listitem>
1054             <para>Works registerised.  No native-code
1055             generator.</para>
1056           </listitem>
1057         </varlistentry>
1058
1059         <varlistentry>
1060           <term>i386-unknown-linux (PCs running Linux, ELF binary format)</term>
1061           <indexterm><primary>i386-*-linux</primary></indexterm>
1062           <listitem>
1063             <para>GHC works registerised and has a native code
1064             generator.  You <Emphasis>must</Emphasis> have GCC 2.7.x
1065             or later.  NOTE about <literal>glibc</literal> versions:
1066             GHC binaries built on a system running <literal>glibc
1067             2.0</literal> won't work on a system running
1068             <literal>glibc 2.1</literal>, and vice versa.  In general,
1069             don't expect compatibility between
1070             <literal>glibc</literal> versions, even if the shared
1071             library version hasn't changed.</para>
1072           </listitem>
1073         </varlistentry>
1074
1075         <varlistentry>
1076           <term>i386-unknown-freebsd (PCs running FreeBSD 2.2
1077 or higher)</term>
1078           <indexterm><primary>i386-unknown-freebsd</primary></indexterm>
1079           <listitem>
1080             <para>GHC works registerised.  Pre-built packages are
1081             available in the native package format, so if you just
1082             need binaries you're better off just installing the
1083             package.</para>
1084           </listitem>
1085         </varlistentry>
1086         
1087         <varlistentry>
1088           <term>i386-unknown-{netbsd,openbsd) (PCs running NetBSD
1089             and OpenBSD)</term>
1090           <indexterm><primary>i386-unknown-netbsd</primary></indexterm> 
1091           <indexterm><primary>i386-unknown-openbsd</primary></indexterm> 
1092           <listitem>
1093             <para>Will require some minor porting effort, but should
1094             work registerised.</para>
1095           </listitem>
1096         </varlistentry>
1097
1098         <varlistentry>
1099           <term>i386-unknown-cygwin32:</term>
1100           <indexterm><primary>i386-unknown-cygwin32</primary></indexterm>
1101           <listitem>
1102             <para>Fully supported under Win9x/NT, including a native
1103             code generator. Requires the <Literal>cygwin32</Literal>
1104             compatibility library and a healthy collection of GNU
1105             tools (i.e., gcc, GNU ld, bash etc.).</para>
1106           </listitem>
1107         </varlistentry>
1108
1109         <varlistentry>
1110           <term>mips-sgi-irix5</term>
1111           <indexterm><primary>mips-sgi-irix[5-6]</primary></indexterm>
1112           <listitem>
1113             <para>Port currently doesn't work, needs some minimal
1114             porting effort.  As usual, we don't have access to
1115             machines and there hasn't been an overwhelming demand for
1116             this port, but feel free to get in touch.</para>
1117           </listitem>
1118         </varlistentry>
1119
1120         <varlistentry>
1121           <term>powerpc-ibm-aix</term>
1122           <indexterm><primary>powerpc-ibm-aix</primary></indexterm>
1123           <listitem>
1124             <para>Port currently doesn't work, needs some minimal
1125             porting effort.  As usual, we don't have access to
1126             machines and there hasn't been an overwhelming demand for
1127             this port, but feel free to get in touch.</para>
1128           </listitem>
1129         </varlistentry>
1130
1131         <varlistentry>
1132           <term>powerpc-apple-darwin</term>
1133           <indexterm><primary>powerpc-apple-darwin</primary></indexterm> 
1134           <listitem>
1135             <para>Works, unregisterised only at the moment.</para>
1136           </listitem>
1137         </varlistentry>
1138       </variablelist>
1139
1140       <para>Various other systems have had GHC ported to them in the
1141       distant past, including various Motorola 68k boxes.  The 68k
1142       support still remains, but porting to one of these systems will
1143       certainly be a non-trivial task.</para>
1144     </sect2>
1145
1146     <sect2>
1147       <title>What machines the other tools run on</title>
1148
1149       <para>Unless you hear otherwise, the other tools work if GHC
1150       works.</para>
1151     </sect2>
1152   </sect1>
1153
1154
1155 <Sect1 id="sec-pre-supposed">
1156 <Title>Installing pre-supposed utilities
1157
1158 <indexterm><primary>pre-supposed utilities</primary></indexterm>
1159 <indexterm><primary>utilities, pre-supposed</primary></indexterm></Title>
1160
1161 <para>
1162 Here are the gory details about some utility programs you may need;
1163 <Command>perl</Command>, <Command>gcc</Command> and
1164 <command>happy</command> are the only important
1165 ones. (PVM<indexterm><primary>PVM</primary></indexterm> is important
1166 if you're going for Parallel Haskell.)  The
1167 <Command>configure</Command><indexterm><primary>configure</primary></indexterm>
1168 script will tell you if you are missing something.
1169 </para>
1170
1171 <para>
1172 <VariableList>
1173
1174 <VarListEntry>
1175 <term>Perl:</term>
1176 <indexterm><primary>pre-supposed: Perl</primary></indexterm>
1177 <indexterm><primary>Perl, pre-supposed</primary></indexterm>
1178 <listitem>
1179 <para>
1180 <Emphasis>You have to have Perl to proceed!</Emphasis>
1181 It is pretty easy to install.
1182 </para>
1183
1184 <para>
1185 Perl&nbsp;5 is required.  For Win32 platforms, you should use the binary
1186 supplied in the InstallShield (copy it to <filename>/bin</filename>).
1187 The Cygwin-supplied Perl seems not to work.
1188 </para>
1189
1190 <para>
1191 Perl should be put somewhere so that it can be invoked by the
1192 <Literal>&num;!</Literal> script-invoking mechanism. The full
1193 pathname may need to be less than 32 characters long on some
1194 systems.
1195 </para>
1196
1197 </listitem></VarListEntry>
1198 <VarListEntry>
1199 <term>GNU C (<Command>gcc</Command>):</term>
1200 <indexterm><primary>pre-supposed: GCC (GNU C compiler)</primary></indexterm>
1201 <indexterm><primary>GCC (GNU C compiler), pre-supposed</primary></indexterm>
1202 <listitem>
1203
1204 <para>
1205 We recommend using GCC version 2.95.2 on all platforms.  Failing that,
1206 version 2.7.2 is stable on most platforms.  Earlier versions of GCC
1207 can be assumed not to work, and versions in between 2.7.2 and 2.95.2
1208 (including <command>egcs</command>) have varying degrees of stability
1209 depending on the platform.
1210 </para>
1211
1212 <para>
1213 If your GCC dies with ``internal error'' on some GHC source file,
1214 please let us know, so we can report it and get things improved.
1215 (Exception: on iX86 boxes&mdash;you may need to fiddle with GHC's
1216 <Option>-monly-N-regs</Option> option; see the User's Guide)
1217 </para>
1218 </listitem></VarListEntry>
1219
1220 <varlistentry>
1221 <term>Happy:</term>
1222 <indexterm><primary>Happy</primary></indexterm>
1223 <listitem>
1224 <para>Happy is a parser generator tool for Haskell, and is used to
1225 generate GHC's parsers.  Happy is written in Haskell, and is a project
1226 in the CVS repository (<literal>fptools/happy</literal>).  It can be
1227 built from source, but bear in mind that you'll need GHC installed in
1228 order to build it.  To avoid the chicken/egg problem, install a binary
1229 distribtion of either Happy or GHC to get started.  Happy
1230 distributions are available from <ulink
1231 url="http://www.haskell.org/happy/">Happy's Web Page</ulink>.
1232 </para>
1233 </listitem>
1234 </varlistentry>
1235
1236 <VarListEntry>
1237 <term>Autoconf:</term>
1238 <indexterm><primary>pre-supposed: Autoconf</primary></indexterm>
1239 <indexterm><primary>Autoconf, pre-supposed</primary></indexterm>
1240 <listitem>
1241 <para>
1242 GNU Autoconf is needed if you intend to build from the CVS sources, it
1243 is <Emphasis>not</Emphasis> needed if you just intend to build a
1244 standard source distribution.
1245 </para>
1246
1247 <para>
1248 Autoconf builds the <Command>configure</Command> script from
1249 <Filename>configure.in</Filename> and <Filename>aclocal.m4</Filename>.
1250 If you modify either of these files, you'll need
1251 <command>autoconf</command> to rebuild <Filename>configure</Filename>.
1252 </para>
1253
1254 </listitem></VarListEntry>
1255 <VarListEntry>
1256 <term><Command>sed</Command></term>
1257 <indexterm><primary>pre-supposed: sed</primary></indexterm>
1258 <indexterm><primary>sed, pre-supposed</primary></indexterm>
1259 <listitem>
1260 <para>
1261 You need a working <Command>sed</Command> if you are going to build
1262 from sources.  The build-configuration stuff needs it.  GNU sed
1263 version 2.0.4 is no good!  It has a bug in it that is tickled by the
1264 build-configuration.  2.0.5 is OK. Others are probably OK too
1265 (assuming we don't create too elaborate configure scripts.)
1266 </para>
1267 </listitem></VarListEntry>
1268 </VariableList>
1269 </para>
1270
1271 <para>
1272 One <Literal>fptools</Literal> project is worth a quick note at this
1273 point, because it is useful for all the others:
1274 <Literal>glafp-utils</Literal> contains several utilities which aren't
1275 particularly Glasgow-ish, but Occasionally Indispensable.  Like
1276 <Command>lndir</Command> for creating symbolic link trees.
1277 </para>
1278
1279 <Sect2 id="pre-supposed-gph-tools">
1280 <Title>Tools for building parallel GHC (GPH)
1281 </Title>
1282
1283 <para>
1284 <VariableList>
1285
1286 <VarListEntry>
1287 <term>PVM version 3:</term>
1288 <indexterm><primary>pre-supposed: PVM3 (Parallel Virtual Machine)</primary></indexterm>
1289 <indexterm><primary>PVM3 (Parallel Virtual Machine), pre-supposed</primary></indexterm>
1290 <listitem>
1291
1292 <para>
1293 PVM is the Parallel Virtual Machine on which Parallel Haskell programs
1294 run.  (You only need this if you plan to run Parallel Haskell.
1295 Concurent Haskell, which runs concurrent threads on a uniprocessor
1296 doesn't need it.)  Underneath PVM, you can have (for example) a
1297 network of workstations (slow) or a multiprocessor box (faster).
1298 </para>
1299
1300 <para>
1301 The current version of PVM is 3.3.11; we use 3.3.7.  It is readily
1302 available on the net; I think I got it from
1303 <Literal>research.att.com</Literal>, in <Filename>netlib</Filename>.
1304 </para>
1305
1306 <para>
1307 A PVM installation is slightly quirky, but easy to do.  Just follow
1308 the <Filename>Readme</Filename> instructions.
1309 </para>
1310 </listitem></VarListEntry>
1311 <VarListEntry>
1312 <term><Command>bash</Command>:</term>
1313 <indexterm><primary>bash, presupposed (Parallel Haskell only)</primary></indexterm>
1314 <listitem>
1315 <para>
1316 Sadly, the <Command>gr2ps</Command> script, used to convert ``parallelism profiles''
1317 to PostScript, is written in Bash (GNU's Bourne Again shell).
1318 This bug will be fixed (someday).
1319 </para>
1320 </listitem></VarListEntry>
1321 </VariableList>
1322 </para>
1323
1324 </Sect2>
1325
1326 <Sect2 id="pre-supposed-doc-tools">
1327 <Title>Tools for building the Documentation
1328 </Title>
1329
1330 <para>
1331 The following additional tools are required if you want to format the
1332 documentation that comes with the <Literal>fptools</Literal> projects:
1333 </para>
1334
1335 <para>
1336 <VariableList>
1337
1338 <VarListEntry>
1339 <term>DocBook:</term>
1340 <indexterm><primary>pre-supposed: DocBook</primary></indexterm>
1341 <indexterm><primary>DocBook, pre-supposed</primary></indexterm>
1342 <listitem>
1343 <para>
1344 All our documentation is written in SGML, using the DocBook DTD.
1345 Instructions on installing and configuring the DocBook tools are in the
1346 installation guide (in the GHC user guide).
1347 </para>
1348
1349 </listitem></VarListEntry>
1350 <VarListEntry>
1351 <term>TeX:</term>
1352 <indexterm><primary>pre-supposed: TeX</primary></indexterm>
1353 <indexterm><primary>TeX, pre-supposed</primary></indexterm>
1354 <listitem>
1355 <para>
1356 A decent TeX distribution is required if you want to produce printable
1357 documentation.  We recomment teTeX, which includes just about
1358 everything you need.
1359 </para>
1360 </listitem></VarListEntry>
1361 </VariableList>
1362 </para>
1363
1364       <para>
1365         In order to actually build any documentation, you need to set
1366         <constant>SGMLDocWays</constant> in your
1367         <filename>build.mk</filename>. Valid values to add to this
1368         list are: <literal>dvi</literal>, <literal>ps</literal>,
1369         <literal>pdf</literal>, <literal>html</literal>, and
1370         <literal>rtf</literal>.
1371       </para>
1372       
1373 </Sect2>
1374
1375 <Sect2 id="pre-supposed-other-tools">
1376 <Title>Other useful tools
1377 </Title>
1378
1379 <VariableList>
1380 <VarListEntry>
1381 <term>Flex:</term>
1382 <indexterm><primary>pre-supposed: flex</primary></indexterm> 
1383 <indexterm><primary>flex, pre-supposed</primary></indexterm>
1384 <listitem>
1385
1386 <para>
1387 This is a quite-a-bit-better-than-Lex lexer.  Used to build a couple
1388 of utilities in <Literal>glafp-utils</Literal>.  Depending on your
1389 operating system, the supplied <Command>lex</Command> may or may not
1390 work; you should get the GNU version.
1391 </para>
1392 </listitem></VarListEntry>
1393 </VariableList>
1394
1395 </Sect2>
1396
1397 </Sect1>
1398
1399 <Sect1 id="sec-building-from-source">
1400 <Title>Building from source
1401
1402 <indexterm><primary>Building from source</primary></indexterm>
1403 <indexterm><primary>Source, building from</primary></indexterm></Title>
1404
1405 <para>
1406 You've been rash enough to want to build some of
1407 the Glasgow Functional Programming tools (GHC, Happy,
1408 nofib, etc.) from source.  You've slurped the source,
1409 from the CVS repository or from a source distribution, and
1410 now you're sitting looking at a huge mound of bits, wondering
1411 what to do next.
1412 </para>
1413
1414 <para>
1415 Gingerly, you type <Command>make</Command>.  Wrong already!
1416 </para>
1417
1418 <para>
1419 This rest of this guide is intended for duffers like me, who aren't
1420 really interested in Makefiles and systems configurations, but who
1421 need a mental model of the interlocking pieces so that they can make
1422 them work, extend them consistently when adding new software, and lay
1423 hands on them gently when they don't work.
1424 </para>
1425
1426 <Sect2 id="sec-source-tree">
1427 <Title>Your source tree
1428 </Title>
1429
1430 <para>
1431 The source code is held in your <Emphasis>source tree</Emphasis>.
1432 The root directory of your source tree <Emphasis>must</Emphasis>
1433 contain the following directories and files:
1434 </para>
1435
1436 <para>
1437
1438 <ItemizedList>
1439 <listitem>
1440
1441 <para>
1442 <Filename>Makefile</Filename>: the root Makefile.
1443 </para>
1444 </listitem>
1445 <listitem>
1446
1447 <para>
1448 <Filename>mk/</Filename>: the directory that contains the
1449 main Makefile code, shared by all the
1450 <Literal>fptools</Literal> software.
1451 </para>
1452 </listitem>
1453 <listitem>
1454
1455 <para>
1456  <Filename>configure.in</Filename>, <Filename>config.sub</Filename>, <Filename>config.guess</Filename>:
1457 these files support the configuration process.
1458 </para>
1459 </listitem>
1460 <listitem>
1461
1462 <para>
1463  <Filename>install-sh</Filename>.
1464 </para>
1465 </listitem>
1466
1467 </ItemizedList>
1468
1469 </para>
1470
1471 <para>
1472 All the other directories are individual <Emphasis>projects</Emphasis> of the
1473 <Literal>fptools</Literal> system&mdash;for example, the Glasgow Haskell Compiler
1474 (<Literal>ghc</Literal>), the Happy parser generator (<Literal>happy</Literal>), the <Literal>nofib</Literal> benchmark
1475 suite, and so on.  You can have zero or more of these.  Needless to
1476 say, some of them are needed to build others.
1477 </para>
1478
1479 <para>
1480 The important thing to remember is that even if you want only one
1481 project (<Literal>happy</Literal>, say), you must have a source tree whose root
1482 directory contains <Filename>Makefile</Filename>, <Filename>mk/</Filename>, <Filename>configure.in</Filename>, and the
1483 project(s) you want (<Filename>happy/</Filename> in this case).  You cannot get by with
1484 just the <Filename>happy/</Filename> directory.
1485 </para>
1486
1487 </Sect2>
1488
1489 <Sect2>
1490 <Title>Build trees
1491 <indexterm><primary>build trees</primary></indexterm>
1492 <indexterm><primary>link trees, for building</primary></indexterm></Title>
1493
1494 <para>
1495 While you can build a system in the source tree, we don't recommend it.
1496 We often want to build multiple versions of our software
1497 for different architectures, or with different options (e.g. profiling).
1498 It's very desirable to share a single copy of the source code among
1499 all these builds.
1500 </para>
1501
1502 <para>
1503 So for every source tree we have zero or more <Emphasis>build trees</Emphasis>.  Each
1504 build tree is initially an exact copy of the source tree, except that
1505 each file is a symbolic link to the source file, rather than being a
1506 copy of the source file.  There are ``standard'' Unix utilities that
1507 make such copies, so standard that they go by different names:
1508 <Command>lndir</Command><indexterm><primary>lndir</primary></indexterm>, <Command>mkshadowdir</Command><indexterm><primary>mkshadowdir</primary></indexterm> are two (If you
1509 don't have either, the source distribution includes sources for the
1510 X11 <Command>lndir</Command>&mdash;check out <Filename>fptools/glafp-utils/lndir</Filename>). See <Xref LinkEnd="sec-storysofar"> for a typical invocation.
1511 </para>
1512
1513 <para>
1514 The build tree does not need to be anywhere near the source tree in
1515 the file system.  Indeed, one advantage of separating the build tree
1516 from the source is that the build tree can be placed in a
1517 non-backed-up partition, saving your systems support people from
1518 backing up untold megabytes of easily-regenerated, and
1519 rapidly-changing, gubbins.  The golden rule is that (with a single
1520 exception&mdash;<XRef LinkEnd="sec-build-config">)
1521 <Emphasis>absolutely everything in the build tree is either a symbolic
1522 link to the source tree, or else is mechanically generated</Emphasis>.
1523 It should be perfectly OK for your build tree to vanish overnight; an
1524 hour or two compiling and you're on the road again.
1525 </para>
1526
1527 <para>
1528 You need to be a bit careful, though, that any new files you create
1529 (if you do any development work) are in the source tree, not a build tree!
1530 </para>
1531
1532 <para>
1533 Remember, that the source files in the build tree are <Emphasis>symbolic
1534 links</Emphasis> to the files in the source tree.  (The build tree soon
1535 accumulates lots of built files like <Filename>Foo.o</Filename>, as well.)  You
1536 can <Emphasis>delete</Emphasis> a source file from the build tree without affecting
1537 the source tree (though it's an odd thing to do).  On the other hand,
1538 if you <Emphasis>edit</Emphasis> a source file from the build tree, you'll edit the
1539 source-tree file directly.  (You can set up Emacs so that if you edit
1540 a source file from the build tree, Emacs will silently create an
1541 edited copy of the source file in the build tree, leaving the source
1542 file unchanged; but the danger is that you think you've edited the
1543 source file whereas actually all you've done is edit the build-tree
1544 copy.  More commonly you do want to edit the source file.)
1545 </para>
1546
1547 <para>
1548 Like the source tree, the top level of your build tree must be (a
1549 linked copy of) the root directory of the <Literal>fptools</Literal> suite.  Inside
1550 Makefiles, the root of your build tree is called
1551 <constant>&dollar;(FPTOOLS&lowbar;TOP)</constant><indexterm><primary>FPTOOLS&lowbar;TOP</primary></indexterm>.  In the rest of this document path
1552 names are relative to <constant>&dollar;(FPTOOLS&lowbar;TOP)</constant> unless otherwise stated.  For
1553 example, the file <Filename>ghc/mk/target.mk</Filename> is actually
1554 <Filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/ghc/mk/target.mk</Filename>.
1555 </para>
1556
1557 </Sect2>
1558
1559 <Sect2 id="sec-build-config">
1560 <Title>Getting the build you want
1561 </Title>
1562
1563 <para>
1564 When you build <Literal>fptools</Literal> you will be compiling code on a particular
1565 <Emphasis>host platform</Emphasis>, to run on a particular <Emphasis>target platform</Emphasis>
1566 (usually the same as the host platform)<indexterm><primary>platform</primary></indexterm>.  The
1567 difficulty is that there are minor differences between different
1568 platforms; minor, but enough that the code needs to be a bit different
1569 for each.  There are some big differences too: for a different
1570 architecture we need to build GHC with a different native-code
1571 generator.
1572 </para>
1573
1574 <para>
1575 There are also knobs you can turn to control how the <Literal>fptools</Literal>
1576 software is built.  For example, you might want to build GHC optimised
1577 (so that it runs fast) or unoptimised (so that you can compile it fast
1578 after you've modified it.  Or, you might want to compile it with
1579 debugging on (so that extra consistency-checking code gets included)
1580 or off.  And so on.
1581 </para>
1582
1583 <para>
1584 All of this stuff is called the <Emphasis>configuration</Emphasis> of your build.
1585 You set the configuration using a three-step process.
1586 <VariableList>
1587
1588 <VarListEntry>
1589 <term>Step 1: get ready for configuration.</term>
1590 <listitem>
1591               <para>Change directory to
1592               <constant>&dollar;(FPTOOLS&lowbar;TOP)</constant> and
1593               issue the command
1594               <Command>autoconf</Command><indexterm><primary>autoconf</primary></indexterm>
1595               (with no arguments). This GNU program converts
1596               <Filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/configure.in</Filename>
1597               to a shell script called
1598               <Filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/configure</Filename>.
1599               </para>
1600
1601               <para>Some projects, including GHC, have their own
1602               configure script.  If there's an
1603               <constant>&dollar;(FPTOOLS&lowbar;TOP)/&lt;project&gt;/configure.in</constant>,
1604               then you need to run <command>autoconf</command> in that
1605               directory too.</para>
1606
1607               <para>Both these steps are completely
1608               platform-independent; they just mean that the
1609               human-written file (<Filename>configure.in</Filename>)
1610               can be short, although the resulting shell script,
1611               <Command>configure</Command>, and
1612               <Filename>mk/config.h.in</Filename>, are long.</para>
1613
1614               <para>In case you don't have <Command>autoconf</Command>
1615               we distribute the results, <Command>configure</Command>,
1616               and <Filename>mk/config.h.in</Filename>, with the source
1617               distribution.  They aren't kept in the repository,
1618               though.</para>
1619             </listitem>
1620           </varlistentry>
1621
1622           <varlistentry>
1623             <term>Step 2: system configuration.</term>
1624             <listitem>
1625               <para>Runs the newly-created
1626               <Command>configure</Command> script, thus:</para>
1627
1628 <ProgramListing>
1629 ./configure <optional><parameter>args</parameter></optional>
1630 </ProgramListing>
1631
1632               <para><Command>configure</Command>'s mission is to
1633               scurry round your computer working out what architecture
1634               it has, what operating system, whether it has the
1635               <Function>vfork</Function> system call, where
1636               <Command>yacc</Command> is kept, whether
1637               <Command>gcc</Command> is available, where various
1638               obscure <Literal>&num;include</Literal> files are,
1639               whether it's a leap year, and what the systems manager
1640               had for lunch.  It communicates these snippets of
1641               information in two ways:</para>
1642
1643               <itemizedlist>
1644                 <listitem>
1645
1646                   <para>It translates
1647                   <Filename>mk/config.mk.in</Filename><indexterm><primary>config.mk.in</primary></indexterm>
1648                   to
1649                   <Filename>mk/config.mk</Filename><indexterm><primary>config.mk</primary></indexterm>,
1650                   substituting for things between
1651                   ``<Literal>@</Literal>'' brackets.  So,
1652                   ``<Literal>@HaveGcc@</Literal>'' will be replaced by
1653                   ``<Literal>YES</Literal>'' or
1654                   ``<Literal>NO</Literal>'' depending on what
1655                   <Command>configure</Command> finds.
1656                   <Filename>mk/config.mk</Filename> is included by
1657                   every Makefile (directly or indirectly), so the
1658                   configuration information is thereby communicated to
1659                   all Makefiles.</para>
1660                 </listitem>
1661
1662                 <listitem>
1663                   <para> It translates
1664                   <Filename>mk/config.h.in</Filename><indexterm><primary>config.h.in</primary></indexterm>
1665                   to
1666                   <Filename>mk/config.h</Filename><indexterm><primary>config.h</primary></indexterm>.
1667                   The latter is <Literal>&num;include</Literal>d by
1668                   various C programs, which can thereby make use of
1669                   configuration information.</para>
1670                 </listitem>
1671               </itemizedlist>
1672
1673               <para><command>configure</command> takes some optional
1674               arguments.  Use <literal>./configure --help</literal> to
1675               get a list of the available arguments.  Here are some of
1676               the ones you might need:</para>
1677
1678               <variablelist>
1679                 <varlistentry>
1680                   <term><literal>--with-ghc=<parameter>path</parameter></literal></term>
1681                   <indexterm><primary><literal>--with-ghc</literal></primary>
1682                   </indexterm>
1683                   <listitem>
1684                     <para>Specifies the path to an installed GHC which
1685                     you would like to use.  This compiler will be used
1686                     for compiling GHC-specific code (eg. GHC itself).
1687                     This option <emphasis>cannot</emphasis> be
1688                     specified using <filename>build.mk</filename> (see
1689                     later), because <command>configure</command> needs
1690                     to auto-detect the version of GHC you're using.
1691                     The default is to look for a compiler named
1692                     <literal>ghc</literal> in your path.</para>
1693                   </listitem>
1694                 </varlistentry>
1695                   
1696                 <varlistentry>
1697                   <term><literal>--with-hc=<parameter>path</parameter></literal></term>
1698                   <indexterm><primary><literal>--with-hc</literal></primary>
1699                   </indexterm>
1700                   <listitem>
1701                     <para>Specifies the path to any installed Haskell
1702                     compiler.  This compiler will be used for
1703                     compiling generic Haskell code.  The default is to
1704                     use <literal>ghc</literal>.</para>
1705                   </listitem>
1706                 </varlistentry>
1707
1708                 <varlistentry>
1709                   <term><literal>--with-gcc=<parameter>path</parameter></literal></term>
1710                   <indexterm><primary><literal>--with-gcc</literal></primary>
1711                   </indexterm>
1712                   <listitem>
1713                     <para>Specifies the path to the installed
1714                     GCC. This compiler will be used to compile all C
1715                     files, <emphasis>except</emphasis> any generated
1716                     by the installed Haskell compiler, which will have
1717                     its own idea of which C compiler (if any) to use.
1718                     The default is to use <literal>gcc</literal>.</para>
1719                   </listitem>
1720                 </varlistentry>
1721               </variablelist>
1722
1723               <para><command>configure</command> caches the results of
1724               its run in <Filename>config.cache</Filename>.  Quite
1725               often you don't want that; you're running
1726               <Command>configure</Command> a second time because
1727               something has changed.  In that case, simply delete
1728               <Filename>config.cache</Filename>.</para>
1729             </listitem>
1730           </varlistentry>
1731
1732 <VarListEntry>
1733 <term>Step 3: build configuration.</term>
1734 <listitem>
1735 <para>
1736 Next, you say how this build of <Literal>fptools</Literal> is to differ from the
1737 standard defaults by creating a new file <Filename>mk/build.mk</Filename><indexterm><primary>build.mk</primary></indexterm>
1738 <Emphasis>in the build tree</Emphasis>.  This file is the one and only file you edit
1739 in the build tree, precisely because it says how this build differs
1740 from the source.  (Just in case your build tree does die, you might
1741 want to keep a private directory of <Filename>build.mk</Filename> files, and use a
1742 symbolic link in each build tree to point to the appropriate one.)  So
1743 <Filename>mk/build.mk</Filename> never exists in the source tree&mdash;you create one in
1744 each build tree from the template.  We'll discuss what to put in it
1745 shortly.  
1746 </para>
1747 </listitem></VarListEntry>
1748 </VariableList>
1749 </para>
1750
1751 <para>
1752 And that's it for configuration. Simple, eh?
1753 </para>
1754
1755       <para>What do you put in your build-specific configuration file
1756       <filename>mk/build.mk</filename>?  <Emphasis>For almost all
1757       purposes all you will do is put make variable definitions that
1758       override those in</Emphasis>
1759       <filename>mk/config.mk.in</filename>.  The whole point of
1760       <filename>mk/config.mk.in</filename>&mdash;and its derived
1761       counterpart <filename>mk/config.mk</filename>&mdash;is to define
1762       the build configuration. It is heavily commented, as you will
1763       see if you look at it.  So generally, what you do is look at
1764       <filename>mk/config.mk.in</filename>, and add definitions in
1765       <filename>mk/build.mk</filename> that override any of the
1766       <filename>config.mk</filename> definitions that you want to
1767       change.  (The override occurs because the main boilerplate file,
1768       <filename>mk/boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>,
1769       includes <filename>build.mk</filename> after
1770       <filename>config.mk</filename>.)</para>
1771
1772       <para>For example, <filename>config.mk.in</filename> contains
1773       the definition:</para>
1774
1775 <ProgramListing>
1776 GhcHcOpts=-O -Rghc-timing
1777 </ProgramListing>
1778
1779       <para>The accompanying comment explains that this is the list of
1780       flags passed to GHC when building GHC itself.  For doing
1781       development, it is wise to add <literal>-DDEBUG</literal>, to
1782       enable debugging code.  So you would add the following to
1783       <filename>build.mk</filename>:</para>
1784
1785       <para>or, if you prefer,</para>
1786
1787 <ProgramListing>
1788 GhcHcOpts += -DDEBUG
1789 </ProgramListing>
1790
1791       <para>GNU <Command>make</Command> allows existing definitions to
1792       have new text appended using the ``<Literal>+=</Literal>''
1793       operator, which is quite a convenient feature.)</para>
1794
1795       <para>If you want to remove the <literal>-O</literal> as well (a
1796       good idea when developing, because the turn-around cycle gets a
1797       lot quicker), you can just override
1798       <literal>GhcLibHcOpts</literal> altogether:</para>
1799
1800 <ProgramListing>
1801 GhcHcOpts=-DDEBUG -Rghc-timing
1802 </ProgramListing>
1803
1804       <para>When reading <filename>config.mk.in</filename>, remember
1805       that anything between ``@...@'' signs is going to be substituted
1806       by <Command>configure</Command> later.  You
1807       <Emphasis>can</Emphasis> override the resulting definition if
1808       you want, but you need to be a bit surer what you are doing.
1809       For example, there's a line that says:</para>
1810
1811 <ProgramListing>
1812 YACC = @YaccCmd@
1813 </ProgramListing>
1814
1815       <para>This defines the Make variables <constant>YACC</constant>
1816       to the pathname for a <Command>yacc</Command> that
1817       <Command>configure</Command> finds somewhere.  If you have your
1818       own pet <Command>yacc</Command> you want to use instead, that's
1819       fine. Just add this line to <filename>mk/build.mk</filename>:</para>
1820
1821 <ProgramListing>
1822 YACC = myyacc
1823 </ProgramListing>
1824
1825       <para>You do not <Emphasis>have</Emphasis> to have a
1826       <filename>mk/build.mk</filename> file at all; if you don't,
1827       you'll get all the default settings from
1828       <filename>mk/config.mk.in</filename>.</para>
1829
1830       <para>You can also use <filename>build.mk</filename> to override
1831       anything that <Command>configure</Command> got wrong.  One place
1832       where this happens often is with the definition of
1833       <constant>FPTOOLS&lowbar;TOP&lowbar;ABS</constant>: this
1834       variable is supposed to be the canonical path to the top of your
1835       source tree, but if your system uses an automounter then the
1836       correct directory is hard to find automatically.  If you find
1837       that <Command>configure</Command> has got it wrong, just put the
1838       correct definition in <filename>build.mk</filename>.</para>
1839
1840 </Sect2>
1841
1842     <sect2 id="sec-storysofar">
1843       <title>The story so far</title>
1844
1845       <para>Let's summarise the steps you need to carry to get
1846       yourself a fully-configured build tree from scratch.</para>
1847
1848       <orderedlist>
1849         <listitem>
1850           <para> Get your source tree from somewhere (CVS repository
1851           or source distribution).  Say you call the root directory
1852           <filename>myfptools</filename> (it does not have to be
1853           called <filename>fptools</filename>).  Make sure that you
1854           have the essential files (see <XRef
1855           LinkEnd="sec-source-tree">).</para>
1856         </listitem>
1857
1858         <listitem>
1859
1860           <para>(Optional) Use <Command>lndir</Command> or
1861           <Command>mkshadowdir</Command> to create a build tree.</para>
1862
1863 <programlisting>
1864 $ cd myfptools
1865 $ mkshadowdir . /scratch/joe-bloggs/myfptools-sun4
1866 </programlisting>
1867
1868           <para>(N.B. <Command>mkshadowdir</Command>'s first argument
1869           is taken relative to its second.) You probably want to give
1870           the build tree a name that suggests its main defining
1871           characteristic (in your mind at least), in case you later
1872           add others.</para>
1873         </listitem>
1874
1875         <listitem>
1876           <para>Change directory to the build tree.  Everything is
1877           going to happen there now.</para>
1878
1879 <programlisting>
1880 $ cd /scratch/joe-bloggs/myfptools-sun4
1881 </programlisting>
1882
1883         </listitem>
1884
1885         <listitem>
1886           <para>Prepare for system configuration:</para>
1887
1888 <programlisting>
1889 $ autoconf
1890 </programlisting>
1891
1892           <para>(You can skip this step if you are starting from a
1893           source distribution, and you already have
1894           <filename>configure</filename> and
1895           <filename>mk/config.h.in</filename>.)</para>
1896
1897           <para>Some projects, including GHC itself, have their own
1898           configure scripts, so it is necessary to run autoconf again
1899           in the appropriate subdirectories. eg:</para>
1900
1901 <programlisting>
1902 $ (cd ghc; autoconf)
1903 </programlisting>
1904         </listitem>
1905
1906         <listitem>
1907           <para>Do system configuration:</para>
1908
1909 <programlisting>
1910 $ ./configure
1911 </programlisting>
1912
1913           <para>Don't forget to check whether you need to add any
1914           arguments to <literal>configure</literal>; for example, a
1915           common requirement is to specify which GHC to use with
1916           <option>--with-ghc=<replaceable>ghc</replaceable></option>.</para>
1917         </listitem>
1918
1919         <listitem>
1920           <para>Create the file <filename>mk/build.mk</filename>,
1921           adding definitions for your desired configuration
1922           options.</para>
1923
1924 <programlisting>
1925 $ emacs mk/build.mk
1926 </programlisting>
1927         </listitem>
1928       </orderedlist>
1929
1930       <para>You can make subsequent changes to
1931       <filename>mk/build.mk</filename> as often as you like.  You do
1932       not have to run any further configuration programs to make these
1933       changes take effect. In theory you should, however, say
1934       <Command>gmake clean</Command>, <Command>gmake all</Command>,
1935       because configuration option changes could affect
1936       anything&mdash;but in practice you are likely to know what's
1937       affected.</para>
1938     </sect2>
1939
1940     <sect2>
1941       <Title>Making things</Title>
1942
1943       <para>At this point you have made yourself a fully-configured
1944       build tree, so you are ready to start building real
1945       things.</para>
1946
1947       <para>The first thing you need to know is that <Emphasis>you
1948       must use GNU <Command>make</Command>, usually called
1949       <Command>gmake</Command>, not standard Unix
1950       <Command>make</Command></Emphasis>.  If you use standard Unix
1951       <Command>make</Command> you will get all sorts of error messages
1952       (but no damage) because the <Literal>fptools</Literal>
1953       <Command>Makefiles</Command> use GNU <Command>make</Command>'s
1954       facilities extensively.</para>
1955
1956       <para>To just build the whole thing, <command>cd</command> to
1957       the top of your <literal>fptools</literal> tree and type
1958       <command>gmake</command>.  This will prepare the tree and build
1959       the various projects in the correct order.</para>
1960
1961     </Sect2>
1962
1963     <Sect2 id="sec-standard-targets">
1964       <Title>Standard Targets</title>
1965       <indexterm><primary>targets, standard makefile</primary></indexterm>
1966       <indexterm><primary>makefile targets</primary></indexterm>
1967
1968       <para>In any directory you should be able to make the following:
1969
1970 <VariableList>
1971
1972 <VarListEntry>
1973 <term><Literal>boot</Literal>:</term>
1974 <listitem>
1975 <para>does the one-off preparation required to get ready for the real
1976 work.  Notably, it does <Command>gmake depend</Command> in all
1977 directories that contain programs.  It also builds the necessary tools
1978 for compilation to proceed.</para>
1979
1980 <para>Invoking the <literal>boot</literal> target explicitly is not
1981 normally necessary.  From the top-level <literal>fptools</literal>
1982 directory, invoking <literal>gmake</literal> causes <literal>gmake
1983 boot all</literal> to be invoked in each of the project
1984 subdirectories, in the order specified by
1985 <literal>&dollar;(AllTargets)</literal> in
1986 <literal>config.mk</literal>.</para>
1987
1988 <para>If you're working in a subdirectory somewhere and need to update
1989 the dependencies, <literal>gmake boot</literal> is a good way to do it.</para>
1990
1991 </listitem></VarListEntry>
1992 <VarListEntry>
1993 <term><Literal>all</Literal>:</term>
1994 <listitem>
1995 <para>
1996 makes all the final target(s) for this Makefile.
1997 Depending on which directory you are in a ``final target'' may be an
1998 executable program, a library archive, a shell script, or a Postscript
1999 file.  Typing <Command>gmake</Command> alone is generally the same as typing <Command>gmake all</Command>.
2000 </para>
2001 </listitem></VarListEntry>
2002 <VarListEntry>
2003 <term><Literal>install</Literal>:</term>
2004 <listitem>
2005 <para>
2006 installs the things built by <Literal>all</Literal> (except for the documentation).  Where does it
2007 install them?  That is specified by
2008 <filename>mk/config.mk.in</filename>; you can override it in
2009 <filename>mk/build.mk</filename>, or by running
2010 <command>configure</command> with command-line arguments like
2011 <literal>--bindir=/home/simonpj/bin</literal>;  see <literal>./configure
2012 --help</literal> for the full details.
2013 </para>
2014 </listitem></VarListEntry>
2015 <VarListEntry>
2016 <term><Literal>install-docs</Literal>:</term>
2017 <listitem>
2018 <para>
2019 installs the documentation. Otherwise behaves just like <literal>install</literal>.
2020 </para>
2021 </listitem></VarListEntry>
2022 <VarListEntry>
2023 <term><Literal>uninstall</Literal>:</term>
2024 <listitem>
2025 <para>
2026 reverses the effect of <Literal>install</Literal>.
2027 </para>
2028 </listitem></VarListEntry>
2029
2030 <VarListEntry>
2031 <term><Literal>clean</Literal>:</term>
2032 <listitem>
2033 <para>
2034 Delete all files from the current directory that are normally created
2035 by building the program.  Don't delete the files that record the
2036 configuration, or files generated by <Command>gmake boot</Command>.
2037 Also preserve files that could be made by building, but normally
2038 aren't because the distribution comes with them.</para>
2039 </listitem></VarListEntry>
2040
2041 <varlistentry>
2042 <term><literal>distclean</literal>:</term>
2043 <listitem>
2044 <para>Delete all files from the current directory that are created by
2045 configuring or building the program. If you have unpacked the source
2046 and built the program without creating any other files, <literal>make
2047 distclean</literal> should leave only the files that were in the
2048 distribution.</para>
2049 </listitem>
2050 </varlistentry>
2051
2052 <varlistentry>
2053 <term><literal>mostlyclean</literal>:</term>
2054 <listitem>
2055 <para>Like <literal>clean</literal>, but may refrain from deleting a
2056 few files that people normally don't want to recompile.</para>
2057 </listitem>
2058 </varlistentry>
2059
2060 <VarListEntry>
2061 <term><Literal>maintainer-clean</Literal>:</term>
2062 <listitem>
2063 <para>
2064 Delete everything from the current directory that can be reconstructed
2065 with this Makefile.  This typically includes everything deleted by
2066 <literal>distclean</literal>, plus more: C source files produced by
2067 Bison, tags tables, Info files, and so on.</para>
2068
2069 <para>One exception, however: <literal>make maintainer-clean</literal>
2070 should not delete <filename>configure</filename> even if
2071 <filename>configure</filename> can be remade using a rule in the
2072 <filename>Makefile</filename>. More generally, <literal>make
2073 maintainer-clean</literal> should not delete anything that needs to
2074 exist in order to run <filename>configure</filename> and then begin to
2075 build the program.</para>
2076 </listitem>
2077 </varlistentry>
2078
2079 <VarListEntry>
2080 <term><Literal>check</Literal>:</term>
2081 <listitem>
2082 <para>
2083 run the test suite.
2084 </para>
2085 </listitem></VarListEntry>
2086 </VariableList>
2087 </para>
2088
2089 <para>
2090 All of these standard targets automatically recurse into
2091 sub-directories.  Certain other standard targets do not:
2092 </para>
2093
2094 <para>
2095 <VariableList>
2096
2097 <VarListEntry>
2098 <term><Literal>configure</Literal>:</term>
2099 <listitem>
2100 <para>
2101 is only available in the root directory
2102 <constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>; it has been discussed in <XRef LinkEnd="sec-build-config">.
2103 </para>
2104 </listitem></VarListEntry>
2105 <VarListEntry>
2106 <term><Literal>depend</Literal>:</term>
2107 <listitem>
2108 <para>
2109 make a <filename>.depend</filename> file in each directory that needs
2110 it. This <filename>.depend</filename> file contains mechanically-generated dependency
2111 information; for example, suppose a directory contains a Haskell 
2112 source module <filename>Foo.lhs</filename> which imports another module <Literal>Baz</Literal>.
2113 Then the generated <filename>.depend</filename> file will contain the dependency:
2114 </para>
2115
2116 <para>
2117
2118 <ProgramListing>
2119 Foo.o : Baz.hi
2120 </ProgramListing>
2121
2122 </para>
2123
2124 <para>
2125 which says that the object file <filename>Foo.o</filename> depends on the interface file
2126 <filename>Baz.hi</filename> generated by compiling module <Literal>Baz</Literal>.  The <filename>.depend</filename> file is
2127 automatically included by every Makefile.
2128 </para>
2129 </listitem></VarListEntry>
2130 <VarListEntry>
2131 <term><Literal>binary-dist</Literal>:</term>
2132 <listitem>
2133 <para>
2134 make a binary distribution.  This is the
2135 target we use to build the binary distributions of GHC and Happy.
2136 </para>
2137 </listitem></VarListEntry>
2138 <VarListEntry>
2139 <term><Literal>dist</Literal>:</term>
2140 <listitem>
2141 <para>
2142 make a source distribution.  Note that this target does &ldquo;make
2143 distclean&rdquo; as part of its work; don't use it if you want to keep
2144 what you've built.
2145 </para>
2146 </listitem></VarListEntry>
2147 </VariableList>
2148 </para>
2149
2150 <para>
2151 Most <filename>Makefile</filename>s have targets other than these.  You can discover them by looking in the <filename>Makefile</filename> itself.
2152 </para>
2153
2154 </Sect2>
2155
2156 <sect2>
2157 <title>Using a project from the build tree</title>
2158 <para>
2159 If you want to build GHC (say) and just use it direct from the build
2160 tree without doing <literal>make install</literal> first, you can run
2161 the in-place driver script:
2162 <filename>ghc/compiler/ghc-inplace</filename>.
2163 </para>
2164
2165 <para> Do <emphasis>NOT</emphasis> use
2166 <filename>ghc/compiler/ghc</filename>, or
2167 <filename>ghc/compiler/ghc-5.xx</filename>, as these are the scripts
2168 intended for installation, and contain hard-wired paths to the
2169 installed libraries, rather than the libraries in the build tree.
2170 </para>
2171
2172 <para>
2173 Happy can similarly be run from the build tree, using
2174 <filename>happy/src/happy-inplace</filename>.
2175 </para>
2176 </sect2>
2177
2178 <Sect2>
2179 <Title>Fast Making <indexterm><primary>fastmake</primary></indexterm>
2180 <indexterm><primary>dependencies, omitting</primary></indexterm>
2181 <indexterm><primary>FAST, makefile
2182 variable</primary></indexterm></Title>
2183
2184 <para>
2185 Sometimes the dependencies get in the way: if you've made a small
2186 change to one file, and you're absolutely sure that it won't affect
2187 anything else, but you know that <Command>make</Command> is going to rebuild everything
2188 anyway, the following hack may be useful:
2189 </para>
2190
2191 <para>
2192
2193 <ProgramListing>
2194 gmake FAST=YES 
2195 </ProgramListing>
2196
2197 </para>
2198
2199 <para>
2200 This tells the make system to ignore dependencies and just build what
2201 you tell it to.  In other words, it's equivalent to temporarily
2202 removing the <filename>.depend</filename> file in the current directory (where
2203 <Command>mkdependHS</Command> and friends store their dependency information).
2204 </para>
2205
2206 <para>
2207 A bit of history: GHC used to come with a <Command>fastmake</Command> script that did
2208 the above job, but GNU make provides the features we need to do it
2209 without resorting to a script.  Also, we've found that fastmaking is
2210 less useful since the advent of GHC's recompilation checker (see the
2211 User's Guide section on "Separate Compilation").
2212 </para>
2213
2214 </Sect2>
2215
2216 </Sect1>
2217
2218 <Sect1 id="sec-makefile-arch">
2219 <Title>The <filename>Makefile</filename> architecture
2220 <indexterm><primary>makefile architecture</primary></indexterm></Title>
2221
2222 <para>
2223 <Command>make</Command> is great if everything works&mdash;you type <Command>gmake install</Command> and
2224 lo! the right things get compiled and installed in the right places.
2225 Our goal is to make this happen often, but somehow it often doesn't;
2226 instead some weird error message eventually emerges from the bowels of
2227 a directory you didn't know existed.
2228 </para>
2229
2230 <para>
2231 The purpose of this section is to give you a road-map to help you figure
2232 out what is going right and what is going wrong.
2233 </para>
2234
2235     <sect2>
2236       <title>Debugging</title>
2237       
2238       <para>Debugging <filename>Makefile</filename>s is something of a
2239       black art, but here's a couple of tricks that we find
2240       particularly useful.  The following command allows you to see
2241       the contents of any make variable in the context of the current
2242       <filename>Makefile</filename>:</para>
2243
2244 <screen>$  make show VALUE=HS_SRCS</screen>
2245
2246       <para>where you can replace <literal>HS_SRCS</literal> with the
2247       name of any variable you wish to see the value of.</para>
2248       
2249       <para>GNU make has a <option>-d</option> option which generates
2250       a dump of the decision procedure used to arrive at a conclusion
2251       about which files should be recompiled.  Sometimes useful for
2252       tracking down problems with superfluous or missing
2253       recompilations.</para>
2254     </sect2>
2255
2256 <Sect2>
2257 <Title>A small project</Title>
2258
2259 <para>
2260 To get started, let us look at the <filename>Makefile</filename> for an imaginary small
2261 <Literal>fptools</Literal> project, <Literal>small</Literal>.  Each project in <Literal>fptools</Literal> has its own
2262 directory in <constant>FPTOOLS&lowbar;TOP</constant>, so the <Literal>small</Literal> project will have its own
2263 directory <constant>FPOOLS&lowbar;TOP/small/</constant>.  Inside the <filename>small/</filename> directory there
2264 will be a <filename>Makefile</filename>, looking something like this:
2265 </para>
2266
2267 <para>
2268 <indexterm><primary>Makefile, minimal</primary></indexterm>
2269
2270 <ProgramListing>
2271 #     Makefile for fptools project "small"
2272
2273 TOP = ..
2274 include $(TOP)/mk/boilerplate.mk
2275
2276 SRCS = $(wildcard *.lhs) $(wildcard *.c)
2277 HS_PROG = small
2278
2279 include $(TOP)/target.mk
2280 </ProgramListing>
2281
2282 </para>
2283
2284 <para>
2285 This <filename>Makefile</filename> has three sections:
2286 </para>
2287
2288 <para>
2289
2290 <OrderedList>
2291 <listitem>
2292
2293 <para>
2294  The first section includes
2295 <FOOTNOTE>
2296
2297 <para>
2298 One of the most important
2299 features of GNU <Command>make</Command> that we use is the ability for a <filename>Makefile</filename> to
2300 include another named file, very like <Command>cpp</Command>'s <Literal>&num;include</Literal>
2301 directive.
2302 </para>
2303
2304 </FOOTNOTE>
2305  a file of ``boilerplate'' code from the level
2306 above (which in this case will be
2307 <filename><constant>FPTOOLS&lowbar;TOP</constant>/mk/boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>).  As its name
2308 suggests, <filename>boilerplate.mk</filename> consists of a large quantity of standard
2309 <filename>Makefile</filename> code.  We discuss this boilerplate in more detail in
2310 <XRef LinkEnd="sec-boiler">.
2311 <indexterm><primary>include, directive in Makefiles</primary></indexterm>
2312 <indexterm><primary>Makefile inclusion</primary></indexterm>
2313
2314 Before the <Literal>include</Literal> statement, you must define the <Command>make</Command> variable
2315 <constant>TOP</constant><indexterm><primary>TOP</primary></indexterm> to be the directory containing the <filename>mk</filename> directory in
2316 which the <filename>boilerplate.mk</filename> file is.  It is <Emphasis>not</Emphasis> OK to simply say
2317
2318
2319 <ProgramListing>
2320 include ../mk/boilerplate.mk  # NO NO NO
2321 </ProgramListing>
2322
2323
2324 Why?  Because the <filename>boilerplate.mk</filename> file needs to know where it is, so
2325 that it can, in turn, <Literal>include</Literal> other files.  (Unfortunately, when an
2326 <Literal>include</Literal>d file does an <Literal>include</Literal>, the filename is treated relative to
2327 the directory in which <Command>gmake</Command> is being run, not the directory in
2328 which the <Literal>include</Literal>d sits.)  In general, <Emphasis>every file <filename>foo.mk</filename>
2329 assumes that <filename><constant>&dollar;(TOP)</constant>/mk/foo.mk</filename> refers to itself.</Emphasis> It is up to the
2330 <filename>Makefile</filename> doing the <Literal>include</Literal> to ensure this is the case.
2331
2332 Files intended for inclusion in other <filename>Makefile</filename>s are written to have
2333 the following property: <Emphasis>after <filename>foo.mk</filename> is <Literal>include</Literal>d, it leaves
2334 <constant>TOP</constant> containing the same value as it had just before the <Literal>include</Literal>
2335 statement</Emphasis>.  In our example, this invariant guarantees that the
2336 <Literal>include</Literal> for <filename>target.mk</filename> will look in the same directory as that for
2337 <filename>boilerplate.mk</filename>.
2338
2339 </para>
2340 </listitem>
2341 <listitem>
2342
2343 <para>
2344  The second section defines the following standard <Command>make</Command>
2345 variables: <constant>SRCS</constant><indexterm><primary>SRCS</primary></indexterm> (the source files from which is to be
2346 built), and <constant>HS&lowbar;PROG</constant><indexterm><primary>HS&lowbar;PROG</primary></indexterm> (the executable binary to be
2347 built).  We will discuss in more detail what the ``standard
2348 variables'' are, and how they affect what happens, in <XRef LinkEnd="sec-targets">.
2349
2350 The definition for <constant>SRCS</constant> uses the useful GNU <Command>make</Command> construct
2351 <Literal>&dollar;(wildcard&nbsp;$pat$)</Literal><indexterm><primary>wildcard</primary></indexterm>, which expands to a list of all
2352 the files matching the pattern <Literal>pat</Literal> in the current directory.  In
2353 this example, <constant>SRCS</constant> is set to the list of all the <filename>.lhs</filename> and <filename>.c</filename>
2354 files in the directory.  (Let's suppose there is one of each,
2355 <filename>Foo.lhs</filename> and <filename>Baz.c</filename>.)
2356
2357 </para>
2358 </listitem>
2359 <listitem>
2360
2361 <para>
2362  The last section includes a second file of standard code,
2363 called <filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>.  It contains the rules that tell
2364 <Command>gmake</Command> how to make the standard targets (<Xref LinkEnd="sec-standard-targets">).  Why, you ask,
2365 can't this standard code be part of <filename>boilerplate.mk</filename>?  Good question.
2366 We discuss the reason later, in <Xref LinkEnd="sec-boiler-arch">.
2367
2368 You do not <Emphasis>have</Emphasis> to <Literal>include</Literal> the <filename>target.mk</filename> file.  Instead, you
2369 can write rules of your own for all the standard targets.  Usually,
2370 though, you will find quite a big payoff from using the canned rules
2371 in <filename>target.mk</filename>; the price tag is that you have to understand what
2372 canned rules get enabled, and what they do (<Xref LinkEnd="sec-targets">).
2373
2374 </para>
2375 </listitem>
2376
2377 </OrderedList>
2378
2379 </para>
2380
2381 <para>
2382 In our example <filename>Makefile</filename>, most of the work is done by the two
2383 <Literal>include</Literal>d files.  When you say <Command>gmake all</Command>, the following things
2384 happen:
2385 </para>
2386
2387 <para>
2388
2389 <ItemizedList>
2390 <listitem>
2391
2392 <para>
2393  <Command>gmake</Command> figures out that the object files are <filename>Foo.o</filename> and
2394 <filename>Baz.o</filename>.
2395
2396 </para>
2397 </listitem>
2398 <listitem>
2399
2400 <para>
2401  It uses a boilerplate pattern rule to compile <filename>Foo.lhs</filename> to
2402 <filename>Foo.o</filename> using a Haskell compiler.  (Which one?  That is set in the
2403 build configuration.)
2404
2405 </para>
2406 </listitem>
2407 <listitem>
2408
2409 <para>
2410  It uses another standard pattern rule to compile <filename>Baz.c</filename> to
2411 <filename>Baz.o</filename>, using a C compiler.  (Ditto.)
2412
2413 </para>
2414 </listitem>
2415 <listitem>
2416
2417 <para>
2418  It links the resulting <filename>.o</filename> files together to make <Literal>small</Literal>,
2419 using the Haskell compiler to do the link step.  (Why not use <Command>ld</Command>?
2420 Because the Haskell compiler knows what standard libraries to link in.
2421 How did <Command>gmake</Command> know to use the Haskell compiler to do the link,
2422 rather than the C compiler?  Because we set the variable <constant>HS&lowbar;PROG</constant>
2423 rather than <constant>C&lowbar;PROG</constant>.)
2424
2425 </para>
2426 </listitem>
2427
2428 </ItemizedList>
2429
2430 </para>
2431
2432 <para>
2433 All <filename>Makefile</filename>s should follow the above three-section format.
2434 </para>
2435
2436 </Sect2>
2437
2438 <Sect2>
2439 <Title>A larger project</Title>
2440
2441 <para>
2442 Larger projects are usually structured into a number of sub-directories,
2443 each of which has its own <filename>Makefile</filename>.  (In very large projects, this
2444 sub-structure might be iterated recursively, though that is rare.)
2445 To give you the idea, here's part of the directory structure for
2446 the (rather large) GHC project:
2447 </para>
2448
2449 <para>
2450
2451 <Screen>
2452 $(FPTOOLS_TOP)/ghc/
2453   Makefile
2454   mk/
2455     boilerplate.mk
2456     rules.mk
2457    docs/
2458     Makefile
2459     ...source files for documentation...
2460    driver/
2461     Makefile
2462     ...source files for driver...
2463    compiler/
2464     Makefile
2465     parser/...source files for parser...
2466     renamer/...source files for renamer...
2467     ...etc...
2468 </Screen>
2469
2470 </para>
2471
2472 <para>
2473 The sub-directories <filename>docs</filename>, <filename>driver</filename>, <filename>compiler</filename>, and so on, each
2474 contains a sub-component of GHC, and each has its own <filename>Makefile</filename>.
2475 There must also be a <filename>Makefile</filename> in <filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/ghc</filename>.  It does most
2476 of its work by recursively invoking <Command>gmake</Command> on the <filename>Makefile</filename>s in the
2477 sub-directories.  We say that <filename>ghc/Makefile</filename> is a <Emphasis>non-leaf
2478 <filename>Makefile</filename></Emphasis>, because it does little except organise its children,
2479 while the <filename>Makefile</filename>s in the sub-directories are all <Emphasis>leaf
2480 <filename>Makefile</filename>s</Emphasis>.  (In principle the sub-directories might themselves
2481 contain a non-leaf <filename>Makefile</filename> and several sub-sub-directories, but
2482 that does not happen in GHC.)
2483 </para>
2484
2485 <para>
2486 The <filename>Makefile</filename> in <filename>ghc/compiler</filename> is considered a leaf <filename>Makefile</filename> even
2487 though the <filename>ghc/compiler</filename> has sub-directories, because these sub-directories
2488 do not themselves have <filename>Makefile</filename>s in them.  They are just used to structure
2489 the collection of modules that make up GHC, but all are managed by the
2490 single <filename>Makefile</filename> in <filename>ghc/compiler</filename>.
2491 </para>
2492
2493 <para>
2494 You will notice that <filename>ghc/</filename> also contains a directory <filename>ghc/mk/</filename>.  It
2495 contains GHC-specific <filename>Makefile</filename> boilerplate code.  More precisely:
2496 </para>
2497
2498 <para>
2499
2500 <ItemizedList>
2501 <listitem>
2502
2503 <para>
2504  <filename>ghc/mk/boilerplate.mk</filename> is included at the top of
2505 <filename>ghc/Makefile</filename>, and of all the leaf <filename>Makefile</filename>s in the
2506 sub-directories.  It in turn <Literal>include</Literal>s the main boilerplate file
2507 <filename>mk/boilerplate.mk</filename>.
2508
2509
2510 </para>
2511 </listitem>
2512 <listitem>
2513
2514 <para>
2515  <filename>ghc/mk/target.mk</filename> is <Literal>include</Literal>d at the bottom of
2516 <filename>ghc/Makefile</filename>, and of all the leaf <filename>Makefile</filename>s in the
2517 sub-directories.  It in turn <Literal>include</Literal>s the file <filename>mk/target.mk</filename>.
2518
2519 </para>
2520 </listitem>
2521
2522 </ItemizedList>
2523
2524 </para>
2525
2526 <para>
2527 So these two files are the place to look for GHC-wide customisation
2528 of the standard boilerplate.
2529 </para>
2530
2531 </Sect2>
2532
2533 <Sect2 id="sec-boiler-arch">
2534 <Title>Boilerplate architecture
2535 <indexterm><primary>boilerplate architecture</primary></indexterm>
2536 </Title>
2537
2538 <para>
2539 Every <filename>Makefile</filename> includes a <filename>boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm> file
2540 at the top, and <filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm> file at the bottom.  In
2541 this section we discuss what is in these files, and why there have to
2542 be two of them.  In general:
2543 </para>
2544
2545 <para>
2546
2547 <ItemizedList>
2548 <listitem>
2549
2550 <para>
2551  <filename>boilerplate.mk</filename> consists of:
2552
2553 <ItemizedList>
2554 <listitem>
2555
2556 <para>
2557  <Emphasis>Definitions of millions of <Command>make</Command> variables</Emphasis> that
2558 collectively specify the build configuration.  Examples:
2559 <constant>HC&lowbar;OPTS</constant><indexterm><primary>HC&lowbar;OPTS</primary></indexterm>, the options to feed to the Haskell compiler;
2560 <constant>NoFibSubDirs</constant><indexterm><primary>NoFibSubDirs</primary></indexterm>, the sub-directories to enable within the
2561 <Literal>nofib</Literal> project; <constant>GhcWithHc</constant><indexterm><primary>GhcWithHc</primary></indexterm>, the name of the Haskell
2562 compiler to use when compiling GHC in the <Literal>ghc</Literal> project.  
2563 </para>
2564 </listitem>
2565 <listitem>
2566
2567 <para>
2568 <Emphasis>Standard pattern rules</Emphasis> that tell <Command>gmake</Command> how to construct one
2569 file from another.
2570 </para>
2571 </listitem>
2572
2573 </ItemizedList>
2574
2575
2576 <filename>boilerplate.mk</filename> needs to be <Literal>include</Literal>d at the <Emphasis>top</Emphasis>
2577 of each <filename>Makefile</filename>, so that the user can replace the
2578 boilerplate definitions or pattern rules by simply giving a new
2579 definition or pattern rule in the <filename>Makefile</filename>.  <Command>gmake</Command>
2580 simply takes the last definition as the definitive one.
2581
2582 Instead of <Emphasis>replacing</Emphasis> boilerplate definitions, it is also quite
2583 common to <Emphasis>augment</Emphasis> them. For example, a <filename>Makefile</filename> might say:
2584
2585
2586 <ProgramListing>
2587 SRC_HC_OPTS += -O
2588 </ProgramListing>
2589
2590
2591 thereby adding ``<Option>-O</Option>'' to the end of <constant>SRC&lowbar;HC&lowbar;OPTS</constant><indexterm><primary>SRC&lowbar;HC&lowbar;OPTS</primary></indexterm>.
2592
2593 </para>
2594 </listitem>
2595 <listitem>
2596
2597 <para>
2598  <filename>target.mk</filename> contains <Command>make</Command> rules for the standard
2599 targets described in <Xref LinkEnd="sec-standard-targets">.  These rules are selectively included,
2600 depending on the setting of certain <Command>make</Command> variables.  These
2601 variables are usually set in the middle section of the
2602 <filename>Makefile</filename> between the two <Literal>include</Literal>s.
2603
2604 <filename>target.mk</filename> must be included at the end (rather than being part of
2605 <filename>boilerplate.mk</filename>) for several tiresome reasons:
2606
2607
2608 <ItemizedList>
2609 <listitem>
2610
2611 <para>
2612  <Command>gmake</Command> commits target and dependency lists earlier than
2613 it should.  For example, <FIlename>target.mk</FIlename> has a rule that looks like
2614 this: 
2615
2616
2617 <ProgramListing>
2618 $(HS_PROG) : $(OBJS)
2619       $(HC) $(LD_OPTS) $&#60; -o $@
2620 </ProgramListing>
2621
2622
2623 If this rule was in <filename>boilerplate.mk</filename> then <constant>&dollar;(HS&lowbar;PROG)</constant><indexterm><primary>HS&lowbar;PROG</primary></indexterm>
2624 and <constant>&dollar;(OBJS)</constant><indexterm><primary>OBJS</primary></indexterm> would not have their final values at the
2625 moment <Command>gmake</Command> encountered the rule.  Alas, <Command>gmake</Command> takes a snapshot
2626 of their current values, and wires that snapshot into the rule.  (In
2627 contrast, the commands executed when the rule ``fires'' are only
2628 substituted at the moment of firing.)  So, the rule must follow the
2629 definitions given in the <filename>Makefile</filename> itself.
2630
2631 </para>
2632 </listitem>
2633 <listitem>
2634
2635 <para>
2636  Unlike pattern rules, ordinary rules cannot be overriden or
2637 replaced by subsequent rules for the same target (at least, not without an
2638 error message).  Including ordinary rules in <filename>boilerplate.mk</filename> would
2639 prevent the user from writing rules for specific targets in specific cases.
2640
2641 </para>
2642 </listitem>
2643 <listitem>
2644
2645 <para>
2646  There are a couple of other reasons I've forgotten, but it doesn't
2647 matter too much.
2648 </para>
2649 </listitem>
2650
2651 </ItemizedList>
2652
2653 </para>
2654 </listitem>
2655
2656 </ItemizedList>
2657
2658 </para>
2659
2660 </Sect2>
2661
2662 <Sect2 id="sec-boiler">
2663 <Title>The main <filename>mk/boilerplate.mk</filename> file
2664
2665 <indexterm><primary>boilerplate.mk</primary></indexterm></Title>
2666
2667 <para>
2668 If you look at <filename><constant>&dollar;(FPTOOLS&lowbar;TOP)</constant>/mk/boilerplate.mk</filename> you will find
2669 that it consists of the following sections, each held in a separate
2670 file: 
2671 </para>
2672
2673       <variablelist>
2674
2675         <varlistentry>
2676           <term><filename>config.mk</filename></term>
2677           <indexterm><primary>config.mk</primary></indexterm>
2678           <listitem>
2679             <para>is the build configuration file we discussed at
2680             length in <Xref LinkEnd="sec-build-config">.</para>
2681           </listitem>
2682         </varlistentry>
2683
2684         <varlistentry>
2685           <term><filename>paths.mk</filename></term>
2686           <indexterm><primary>paths.mk</primary></indexterm>
2687           <listitem>
2688             <para>defines <Command>make</Command> variables for
2689             pathnames and file lists.  This file contains code for
2690             automatically compiling lists of source files and deriving
2691             lists of object files from those.  The results can be
2692             overriden in the <filename>Makefile</filename>, but in
2693             most cases the automatic setup should do the right
2694             thing.</para>
2695             
2696             <para>The following variables may be set in the
2697             <filename>Makefile</filename> to affect how the automatic
2698             source file search is done:</para>
2699
2700             <variablelist>
2701               <varlistentry>
2702                 <term><literal>ALL_DIRS</literal></term>
2703                 <indexterm><primary><literal>ALL_DIRS</literal></primary>
2704                 </indexterm>
2705                 <listitem>
2706                   <para>Set to a list of directories to search in
2707                   addition to the current directory for source
2708                   files.</para>
2709                 </listitem>
2710               </varlistentry>
2711
2712               <varlistentry>
2713                 <term><literal>EXCLUDE_SRCS</literal></term>
2714                 <indexterm><primary><literal>EXCLUDE_SRCS</literal></primary>
2715                 </indexterm>
2716                 <listitem>
2717                   <para>Set to a list of source files (relative to the
2718                   current directory) to omit from the automatic
2719                   search.  The source searching machinery is clever
2720                   enough to know that if you exclude a source file
2721                   from which other sources are derived, then the
2722                   derived sources should also be excluded.  For
2723                   example, if you set <literal>EXCLUDED_SRCS</literal>
2724                   to include <filename>Foo.y</filename>, then
2725                   <filename>Foo.hs</filename> will also be
2726                   excluded.</para>
2727                 </listitem>
2728               </varlistentry>
2729
2730               <varlistentry>
2731                 <term><literal>EXTRA_SRCS</literal></term>
2732                 <indexterm><primary><literal>EXCLUDE_SRCS</literal></primary>
2733                 </indexterm>
2734                   <listitem>
2735                   <para>Set to a list of extra source files (perhaps
2736                   in directories not listed in
2737                   <literal>ALL_DIRS</literal>) that should be
2738                   considered.</para>
2739                 </listitem>
2740               </varlistentry>
2741             </variablelist>
2742
2743             <para>The results of the automatic source file search are
2744             placed in the following make variables:</para>
2745
2746             <variablelist>
2747               <varlistentry>
2748                 <term><literal>SRCS</literal></term>
2749                 <indexterm><primary><literal>SRCS</literal></primary></indexterm>
2750                 <listitem>
2751                   <para>All source files found, sorted and without
2752                   duplicates, including those which might not exist
2753                   yet but will be derived from other existing sources.
2754                   <literal>SRCS</literal> <emphasis>can</emphasis> be
2755                   overriden if necessary, in which case the variables
2756                   below will follow suit.</para>
2757                 </listitem>
2758               </varlistentry>
2759
2760               <varlistentry>
2761                 <term><literal>HS_SRCS</literal></term>
2762                 <indexterm><primary><literal>HS_SRCS</literal></primary></indexterm>
2763                 <listitem>
2764                   <para>all Haskell source files in the current
2765                   directory, including those derived from other source
2766                   files (eg. Happy sources also give rise to Haskell
2767                   sources).</para>
2768                 </listitem>
2769               </varlistentry>
2770
2771               <varlistentry>
2772                 <term><literal>HS_OBJS</literal></term>
2773                 <indexterm><primary><literal>HS_OBJS</literal></primary></indexterm>
2774                 <listitem>
2775                   <para>Object files derived from
2776                   <literal>HS_SRCS</literal>.</para>
2777                 </listitem>
2778               </varlistentry>
2779
2780               <varlistentry>
2781                 <term><literal>HS_IFACES</literal></term>
2782                 <indexterm><primary><literal>HS_IFACES</literal></primary></indexterm>
2783                 <listitem>
2784                   <para>Interface files (<literal>.hi</literal> files)
2785                   derived from <literal>HS_SRCS</literal>.</para>
2786                 </listitem>
2787               </varlistentry>
2788
2789               <varlistentry>
2790                 <term><literal>C_SRCS</literal></term>
2791                 <indexterm><primary><literal>C_SRCS</literal></primary></indexterm>
2792                 <listitem>
2793                   <para>All C source files found.</para>
2794                 </listitem>
2795               </varlistentry>
2796
2797               <varlistentry>
2798                 <term><literal>C_OBJS</literal></term>
2799                 <indexterm><primary><literal>C_OBJS</literal></primary></indexterm>
2800                 <listitem>
2801                   <para>Object files derived from
2802                   <literal>C_SRCS</literal>.</para>
2803                 </listitem>
2804               </varlistentry>
2805
2806               <varlistentry>
2807                 <term><literal>SCRIPT_SRCS</literal></term>
2808                 <indexterm><primary><literal>SCRIPT_SRCS</literal></primary></indexterm>
2809                 <listitem>
2810                   <para>All script source files found
2811                   (<literal>.lprl</literal> files).</para>
2812                 </listitem>
2813               </varlistentry>
2814
2815               <varlistentry>
2816                 <term><literal>SCRIPT_OBJS</literal></term>
2817                 <indexterm><primary><literal>SCRIPT_OBJS</literal></primary></indexterm>
2818                 <listitem>
2819                   <para><quote>object</quote> files derived from
2820                   <literal>SCRIPT_SRCS</literal>
2821                   (<literal>.prl</literal> files).</para>
2822                 </listitem>
2823               </varlistentry>
2824
2825               <varlistentry>
2826                 <term><literal>HSC_SRCS</literal></term>
2827                 <indexterm><primary><literal>HSC_SRCS</literal></primary></indexterm>
2828                 <listitem>
2829                   <para>All <literal>hsc2hs</literal> source files
2830                   (<literal>.hsc</literal> files).</para>
2831                 </listitem>
2832               </varlistentry>
2833
2834               <varlistentry>
2835                 <term><literal>HAPPY_SRCS</literal></term>
2836                 <indexterm><primary><literal>HAPPY_SRCS</literal></primary></indexterm>
2837                 <listitem>
2838                   <para>All <literal>happy</literal> source files
2839                   (<literal>.y</literal> or <literal>.hy</literal> files).</para>
2840                 </listitem>
2841               </varlistentry>
2842
2843               <varlistentry>
2844                 <term><literal>OBJS</literal></term>
2845                 <indexterm><primary>OBJS</primary></indexterm>
2846                 <listitem>
2847                   <para>the concatenation of
2848                   <literal>&dollar;(HS_OBJS)</literal>,
2849                   <literal>&dollar;(C_OBJS)</literal>, and
2850                   <literal>&dollar;(SCRIPT_OBJS)</literal>.</para>
2851                 </listitem>
2852               </varlistentry>
2853             </variablelist>
2854
2855             <para>Any or all of these definitions can easily be
2856             overriden by giving new definitions in your
2857             <filename>Makefile</filename>.</para>
2858
2859             <para>What, exactly, does <filename>paths.mk</filename>
2860             consider a <quote>source file</quote> to be?  It's based
2861             on the file's suffix (e.g. <filename>.hs</filename>,
2862             <filename>.lhs</filename>, <filename>.c</filename>,
2863             <filename>.hy</filename>, etc), but this is the kind of
2864             detail that changes, so rather than enumerate the source
2865             suffices here the best thing to do is to look in
2866             <filename>paths.mk</filename>.</para>
2867           </listitem>
2868         </varlistentry>
2869
2870         <varlistentry>
2871           <term><filename>opts.mk</filename></term>
2872           <indexterm><primary>opts.mk</primary></indexterm>
2873           <listitem>
2874             <para>defines <Command>make</Command> variables for option
2875             strings to pass to each program. For example, it defines
2876             <constant>HC&lowbar;OPTS</constant><indexterm><primary>HC&lowbar;OPTS</primary></indexterm>,
2877             the option strings to pass to the Haskell compiler.  See
2878             <Xref LinkEnd="sec-suffix">.</para>
2879           </listitem>
2880         </varlistentry>
2881
2882         <varlistentry>
2883           <term><filename>suffix.mk</filename></term>
2884           <indexterm><primary>suffix.mk</primary></indexterm>
2885           <listitem>
2886             <para>defines standard pattern rules&mdash;see <Xref
2887             LinkEnd="sec-suffix">.</para>
2888           </listitem>
2889         </varlistentry>
2890       </variablelist>
2891
2892 <para>
2893 Any of the variables and pattern rules defined by the boilerplate file
2894 can easily be overridden in any particular <filename>Makefile</filename>, because the
2895 boilerplate <Literal>include</Literal> comes first.  Definitions after this <Literal>include</Literal>
2896 directive simply override the default ones in <filename>boilerplate.mk</filename>.
2897 </para>
2898
2899 </Sect2>
2900
2901 <Sect2 id="sec-suffix">
2902 <Title>Pattern rules and options
2903
2904 <indexterm><primary>Pattern rules</primary></indexterm></Title>
2905
2906 <para>
2907 The file <filename>suffix.mk</filename><indexterm><primary>suffix.mk</primary></indexterm> defines standard <Emphasis>pattern
2908 rules</Emphasis> that say how to build one kind of file from another, for
2909 example, how to build a <filename>.o</filename> file from a <filename>.c</filename> file.  (GNU <Command>make</Command>'s
2910 <Emphasis>pattern rules</Emphasis> are more powerful and easier to use than Unix
2911 <Command>make</Command>'s <Emphasis>suffix rules</Emphasis>.)
2912 </para>
2913
2914 <para>
2915 Almost all the rules look something like this:
2916 </para>
2917
2918 <para>
2919
2920 <ProgramListing>
2921 %.o : %.c
2922       $(RM) $@
2923       $(CC) $(CC_OPTS) -c $&#60; -o $@
2924 </ProgramListing>
2925
2926 </para>
2927
2928 <para>
2929 Here's how to understand the rule.  It says that
2930 <Emphasis>something</Emphasis><filename>.o</filename> (say <filename>Foo.o</filename>) can be built from
2931 <Emphasis>something</Emphasis><filename>.c</filename> (<filename>Foo.c</filename>), by invoking the C compiler
2932 (path name held in <constant>&dollar;(CC)</constant>), passing to it the options
2933 <constant>&dollar;(CC&lowbar;OPTS)</constant> and the rule's dependent file of the rule
2934 <Literal>&dollar;&lt;</Literal> (<filename>Foo.c</filename> in this case), and putting the result in
2935 the rule's target <Literal>&dollar;@</Literal> (<filename>Foo.o</filename> in this case).
2936 </para>
2937
2938 <para>
2939 Every program is held in a <Command>make</Command> variable defined in
2940 <filename>mk/config.mk</filename>&mdash;look in <filename>mk/config.mk</filename> for the
2941 complete list.  One important one is the Haskell compiler, which is
2942 called <constant>&dollar;(HC)</constant>.
2943 </para>
2944
2945 <para>
2946 Every program's options are are held in a <Command>make</Command> variables called
2947 <constant>&lt;prog&gt;&lowbar;OPTS</constant>.  the <constant>&lt;prog&gt;&lowbar;OPTS</constant> variables are defined in
2948 <filename>mk/opts.mk</filename>.  Almost all of them are defined like this:
2949 </para>
2950
2951 <para>
2952
2953 <ProgramListing>
2954 CC_OPTS = $(SRC_CC_OPTS) $(WAY$(_way)_CC_OPTS) $($*_CC_OPTS) $(EXTRA_CC_OPTS)
2955 </ProgramListing>
2956
2957 </para>
2958
2959 <para>
2960 The four variables from which <constant>CC&lowbar;OPTS</constant> is built have the following meaning:
2961 </para>
2962
2963 <para>
2964 <VariableList>
2965
2966 <varlistentry>
2967 <term><constant>SRC&lowbar;CC&lowbar;OPTS</constant><indexterm><primary>SRC&lowbar;CC&lowbar;OPTS</primary></indexterm>:</term>
2968 <listitem>
2969 <para>
2970 options passed to all C
2971 compilations.
2972 </para>
2973 </listitem></varlistentry>
2974 <varlistentry>
2975 <term><constant>WAY&lowbar;&lt;way&gt;&lowbar;CC&lowbar;OPTS</constant>:</term>
2976 <listitem>
2977 <para>
2978 options passed to C
2979 compilations for way <Literal>&lt;way&gt;</Literal>. For example,
2980 <constant>WAY&lowbar;mp&lowbar;CC&lowbar;OPTS</constant> gives options to pass to the C compiler when
2981 compiling way <Literal>mp</Literal>.  The variable <constant>WAY&lowbar;CC&lowbar;OPTS</constant> holds
2982 options to pass to the C compiler when compiling the standard way.
2983 (<Xref LinkEnd="sec-ways"> dicusses multi-way
2984 compilation.)  
2985 </para>
2986 </listitem></varlistentry>
2987 <varlistentry>
2988 <term><constant>&lt;module&gt;&lowbar;CC&lowbar;OPTS</constant>:</term>
2989 <listitem>
2990 <para>
2991 options to
2992 pass to the C compiler that are specific to module <Literal>&lt;module&gt;</Literal>.  For example, <constant>SMap&lowbar;CC&lowbar;OPTS</constant> gives the specific options
2993 to pass to the C compiler when compiling <filename>SMap.c</filename>.
2994 </para>
2995 </listitem></varlistentry>
2996 <varlistentry>
2997 <term><constant>EXTRA&lowbar;CC&lowbar;OPTS</constant><indexterm><primary>EXTRA&lowbar;CC&lowbar;OPTS</primary></indexterm>:</term>
2998 <listitem>
2999 <para>
3000 extra options to pass to all
3001 C compilations.  This is intended for command line use, thus:
3002 </para>
3003
3004 <para>
3005
3006 <ProgramListing>
3007 gmake libHS.a EXTRA_CC_OPTS="-v"
3008 </ProgramListing>
3009
3010 </para>
3011 </listitem></varlistentry>
3012 </VariableList>
3013 </para>
3014
3015 </Sect2>
3016
3017 <Sect2 id="sec-targets">
3018 <Title>The main <filename>mk/target.mk</filename> file
3019
3020 <indexterm><primary>target.mk</primary></indexterm></Title>
3021
3022 <para>
3023 <filename>target.mk</filename> contains canned rules for all the standard targets
3024 described in <Xref LinkEnd="sec-standard-targets">.  It is complicated by the fact that you don't want all of
3025 these rules to be active in every <filename>Makefile</filename>.  Rather than have a
3026 plethora of tiny files which you can include selectively, there is a
3027 single file, <filename>target.mk</filename>, which selectively includes rules based on
3028 whether you have defined certain variables in your <filename>Makefile</filename>.  This
3029 section explains what rules you get, what variables control them, and
3030 what the rules do.  Hopefully, you will also get enough of an idea of
3031 what is supposed to happen that you can read and understand any weird
3032 special cases yourself.
3033 </para>
3034
3035 <para>
3036 <VariableList>
3037
3038 <varlistentry>
3039 <term><constant>HS&lowbar;PROG</constant><indexterm><primary>HS&lowbar;PROG</primary></indexterm>.</term>
3040 <listitem>
3041 <para>
3042 If <constant>HS&lowbar;PROG</constant> is defined, you get
3043 rules with the following targets:
3044 <VariableList>
3045
3046 <varlistentry>
3047 <term><filename>HS&lowbar;PROG</filename><indexterm><primary>HS&lowbar;PROG</primary></indexterm></term>
3048 <listitem>
3049 <para>
3050 itself.  This rule links <constant>&dollar;(OBJS)</constant>
3051 with the Haskell runtime system to get an executable called
3052 <constant>&dollar;(HS&lowbar;PROG)</constant>.
3053 </para>
3054 </listitem></varlistentry>
3055 <varlistentry>
3056 <term><Literal>install</Literal><indexterm><primary>install</primary></indexterm></term>
3057 <listitem>
3058 <para>
3059 installs <constant>&dollar;(HS&lowbar;PROG)</constant>
3060 in <constant>&dollar;(bindir)</constant>.
3061 </para>
3062 </listitem></varlistentry>
3063 </VariableList>
3064 </para>
3065 </listitem></varlistentry>
3066 <varlistentry>
3067 <term><constant>C&lowbar;PROG</constant><indexterm><primary>C&lowbar;PROG</primary></indexterm></term>
3068 <listitem>
3069 <para>
3070 is similar to <constant>HS&lowbar;PROG</constant>, except that
3071 the link step links <constant>&dollar;(C&lowbar;OBJS)</constant> with the C runtime system.
3072 </para>
3073 </listitem></varlistentry>
3074 <varlistentry>
3075 <term><constant>LIBRARY</constant><indexterm><primary>LIBRARY</primary></indexterm></term>
3076 <listitem>
3077 <para>
3078 is similar to <constant>HS&lowbar;PROG</constant>, except that
3079 it links <constant>&dollar;(LIB&lowbar;OBJS)</constant> to make the library archive <constant>&dollar;(LIBRARY)</constant>, and
3080 <Literal>install</Literal> installs it in <constant>&dollar;(libdir)</constant>.
3081 </para>
3082 </listitem></varlistentry>
3083 <varlistentry>
3084 <term><constant>LIB&lowbar;DATA</constant><indexterm><primary>LIB&lowbar;DATA</primary></indexterm></term>
3085 <listitem>
3086 <para>
3087 &hellip;
3088 </para>
3089 </listitem></varlistentry>
3090 <varlistentry>
3091 <term><constant>LIB&lowbar;EXEC</constant><indexterm><primary>LIB&lowbar;EXEC</primary></indexterm></term>
3092 <listitem>
3093 <para>
3094 &hellip;
3095 </para>
3096 </listitem></varlistentry>
3097 <varlistentry>
3098 <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>
3099 <listitem>
3100 <para>
3101 If <constant>HS&lowbar;SRCS</constant>
3102 is defined and non-empty, a rule for the target <Literal>depend</Literal> is included,
3103 which generates dependency information for Haskell programs.
3104 Similarly for <constant>C&lowbar;SRCS</constant>.
3105 </para>
3106 </listitem></varlistentry>
3107 </VariableList>
3108 </para>
3109
3110 <para>
3111 All of these rules are ``double-colon'' rules, thus
3112 </para>
3113
3114 <para>
3115
3116 <ProgramListing>
3117 install :: $(HS_PROG)
3118       ...how to install it...
3119 </ProgramListing>
3120
3121 </para>
3122
3123 <para>
3124 GNU <Command>make</Command> treats double-colon rules as separate entities.  If there
3125 are several double-colon rules for the same target it takes each in
3126 turn and fires it if its dependencies say to do so.  This means that
3127 you can, for example, define both <constant>HS&lowbar;PROG</constant> and <constant>LIBRARY</constant>, which will
3128 generate two rules for <Literal>install</Literal>.  When you type <Command>gmake install</Command> both
3129 rules will be fired, and both the program and the library will be
3130 installed, just as you wanted.
3131 </para>
3132
3133 </Sect2>
3134
3135 <Sect2 id="sec-subdirs">
3136 <Title>Recursion
3137
3138 <indexterm><primary>recursion, in makefiles</primary></indexterm>
3139 <indexterm><primary>Makefile, recursing into subdirectories</primary></indexterm></Title>
3140
3141 <para>
3142 In leaf <filename>Makefile</filename>s the variable <constant>SUBDIRS</constant><indexterm><primary>SUBDIRS</primary></indexterm> is undefined.
3143 In non-leaf <filename>Makefile</filename>s, <constant>SUBDIRS</constant> is set to the list of
3144 sub-directories that contain subordinate <filename>Makefile</filename>s.  <Emphasis>It is up to
3145 you to set <constant>SUBDIRS</constant> in the <filename>Makefile</filename>.</Emphasis> There is no automation here&mdash;<constant>SUBDIRS</constant> is too important to automate.
3146 </para>
3147
3148 <para>
3149 When <constant>SUBDIRS</constant> is defined, <filename>target.mk</filename> includes a rather
3150 neat rule for the standard targets (<Xref LinkEnd="sec-standard-targets"> that simply invokes
3151 <Command>make</Command> recursively in each of the sub-directories.
3152 </para>
3153
3154 <para>
3155 <Emphasis>These recursive invocations are guaranteed to occur in the order
3156 in which the list of directories is specified in <constant>SUBDIRS</constant>. </Emphasis>This
3157 guarantee can be important.  For example, when you say <Command>gmake boot</Command> it
3158 can be important that the recursive invocation of <Command>make boot</Command> is done
3159 in one sub-directory (the include files, say) before another (the
3160 source files).  Generally, put the most independent sub-directory
3161 first, and the most dependent last.
3162 </para>
3163
3164 </Sect2>
3165
3166     <sect2 id="sec-ways">
3167       <title>Way management</title>
3168       <indexterm><primary>way management</primary></indexterm>
3169
3170       <para>We sometimes want to build essentially the same system in
3171       several different ``ways''.  For example, we want to build GHC's
3172       <literal>Prelude</literal> libraries with and without profiling,
3173       so that there is an appropriately-built library archive to link
3174       with when the user compiles his program.  It would be possible
3175       to have a completely separate build tree for each such ``way'',
3176       but it would be horribly bureaucratic, especially since often
3177       only parts of the build tree need to be constructed in multiple
3178       ways.</para>
3179
3180       <para>Instead, the
3181       <filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>
3182       contains some clever magic to allow you to build several
3183       versions of a system; and to control locally how many versions
3184       are built and how they differ.  This section explains the
3185       magic.</para>
3186
3187       <para>The files for a particular way are distinguished by
3188       munging the suffix.  The <quote>normal way</quote> is always
3189       built, and its files have the standard suffices
3190       <filename>.o</filename>, <filename>.hi</filename>, and so on.
3191       In addition, you can build one or more extra ways, each
3192       distinguished by a <emphasis>way tag</emphasis>.  The object
3193       files and interface files for one of these extra ways are
3194       distinguished by their suffix.  For example, way
3195       <literal>mp</literal> has files
3196       <filename>.mp&lowbar;o</filename> and
3197       <filename>.mp&lowbar;hi</filename>.  Library archives have their
3198       way tag the other side of the dot, for boring reasons; thus,
3199       <filename>libHS&lowbar;mp.a</filename>.</para>
3200
3201       <para>A <Command>make</Command> variable called
3202       <constant>way</constant> holds the current way tag.
3203       <emphasis><constant>way</constant> is only ever set on the
3204       command line of <Command>gmake</Command></emphasis> (usually in
3205       a recursive invocation of <command>gmake</command> by the
3206       system).  It is never set inside a
3207       <filename>Makefile</filename>.  So it is a global constant for
3208       any one invocation of <Command>gmake</Command>.  Two other
3209       <Command>make</Command> variables,
3210       <constant>way&lowbar;</constant> and
3211       <constant>&lowbar;way</constant> are immediately derived from
3212       <constant>&dollar;(way)</constant> and never altered.  If
3213       <constant>way</constant> is not set, then neither are
3214       <constant>way&lowbar;</constant> and
3215       <constant>&lowbar;way</constant>, and the invocation of
3216       <Command>make</Command> will build the <quote>normal
3217       way</quote>.  If <constant>way</constant> is set, then the other
3218       two variables are set in sympathy.  For example, if
3219       <constant>&dollar;(way)</constant> is ``<Literal>mp</Literal>'',
3220       then <constant>way&lowbar;</constant> is set to
3221       ``<Literal>mp&lowbar;</Literal>'' and
3222       <constant>&lowbar;way</constant> is set to
3223       ``<Literal>&lowbar;mp</Literal>''.  These three variables are
3224       then used when constructing file names.</para>
3225
3226       <para>So how does <Command>make</Command> ever get recursively
3227       invoked with <constant>way</constant> set?  There are two ways
3228       in which this happens:</para>
3229
3230       <itemizedlist>
3231         <listitem>
3232           <para>For some (but not all) of the standard targets, when
3233           in a leaf sub-directory, <Command>make</Command> is
3234           recursively invoked for each way tag in
3235           <constant>&dollar;(WAYS)</constant>.  You set
3236           <constant>WAYS</constant> in the
3237           <filename>Makefile</filename> to the list of way tags you
3238           want these targets built for.  The mechanism here is very
3239           much like the recursive invocation of
3240           <Command>make</Command> in sub-directories (<Xref
3241           LinkEnd="sec-subdirs">).  It is up to you to set
3242           <constant>WAYS</constant> in your
3243           <filename>Makefile</filename>; this is how you control what
3244           ways will get built.</para>
3245         </listitem>
3246
3247         <listitem>
3248           <para>For a useful collection of targets (such as
3249           <filename>libHS&lowbar;mp.a</filename>,
3250           <filename>Foo.mp&lowbar;o</filename>) there is a rule which
3251           recursively invokes <Command>make</Command> to make the
3252           specified target, setting the <constant>way</constant>
3253           variable.  So if you say <Command>gmake
3254           Foo.mp&lowbar;o</Command> you should see a recursive
3255           invocation <Command>gmake Foo.mp&lowbar;o way=mp</Command>,
3256           and <Emphasis>in this recursive invocation the pattern rule
3257           for compiling a Haskell file into a <filename>.o</filename>
3258           file will match</Emphasis>.  The key pattern rules (in
3259           <filename>suffix.mk</filename>) look like this:
3260
3261 <ProgramListing>
3262 %.$(way_)o : %.lhs
3263       $(HC) $(HC_OPTS) $&#60; -o $@
3264 </ProgramListing>
3265
3266           Neat, eh?</para>
3267         </listitem>
3268
3269         <listitem>
3270           <para>You can invoke <command>make</command> with a
3271           particular <literal>way</literal> setting yourself, in order
3272           to build files related to a particular
3273           <literal>way</literal> in the current directory.  eg.
3274
3275 <screen>
3276 $ make way=p
3277 </screen>
3278
3279           will build files for the profiling way only in the current
3280           directory. </para>
3281         </listitem>
3282       </itemizedlist>
3283
3284 </Sect2>
3285
3286 <Sect2>
3287 <Title>When the canned rule isn't right</Title>
3288
3289 <para>
3290 Sometimes the canned rule just doesn't do the right thing.  For
3291 example, in the <Literal>nofib</Literal> suite we want the link step to print out
3292 timing information.  The thing to do here is <Emphasis>not</Emphasis> to define
3293 <constant>HS&lowbar;PROG</constant> or <constant>C&lowbar;PROG</constant>, and instead define a special purpose rule in
3294 your own <filename>Makefile</filename>.  By using different variable names you will avoid
3295 the canned rules being included, and conflicting with yours.
3296 </para>
3297
3298 </Sect2>
3299
3300 </Sect1>
3301
3302 <Sect1 id="sec-booting-from-C">
3303 <Title>Booting/porting from C (<filename>.hc</filename>) files
3304
3305 <indexterm><primary>building GHC from .hc files</primary></indexterm>
3306 <indexterm><primary>booting GHC from .hc files</primary></indexterm>
3307 <indexterm><primary>porting GHC</primary></indexterm></Title>
3308
3309 <para>
3310 This section is for people trying to get GHC going by using the supplied
3311 intermediate C (<filename>.hc</filename>) files.  This would probably be
3312 because no binaries have been provided, or because the machine is not ``fully
3313 supported''.
3314 </para>
3315
3316 <para>
3317 The intermediate C files are normally made available together with a source
3318 release, please check the announce message for exact directions of where to
3319 find them. If we haven't made them available or you can't find them, please
3320 ask.
3321 </para>
3322
3323 <para>
3324 Assuming you've got them, unpack them on top of a fresh source tree.  This
3325 will place matching <filename>.hc</filename> files next to the corresponding
3326 Haskell source in the compiler subdirectory <filename>ghc</filename> and in
3327 the language package of hslibs (i.e., in <filename>hslibs/lang</filename>).
3328 Then follow the `normal' instructions in <Xref
3329 LinkEnd="sec-building-from-source"> for setting up a build tree.
3330 </para>
3331
3332 <para>
3333 The actual build process is fully automated by the
3334 <filename>hc-build</filename> script located in the
3335 <filename>distrib</filename> directory.  If you eventually want to install GHC
3336 into the directory <filename>INSTALL_DIRECTORY</filename>, the following
3337 command will execute the whole build process (it won't install yet):
3338 </para>
3339 <Screen>
3340 foo% distrib/hc-build --prefix=INSTALL_DIRECTORY
3341 </Screen>
3342 <indexterm><primary>--hc-build</primary></indexterm>
3343 <para>
3344 By default, the installation directory is <filename>/usr/local</filename>.  If
3345 that is what you want, you may omit the argument to
3346 <filename>hc-build</filename>.  Generally, any option given to
3347 <filename>hc-build</filename> is passed through to the configuration script
3348 <filename>configure</filename>.  If <filename>hc-build</filename>
3349 successfully completes the build process, you can install the resulting
3350 system, as normal, with
3351 </para>
3352 <Screen>
3353 foo% make install
3354 </Screen>
3355
3356 <para>
3357 That's the mechanics of the boot process, but, of course, if you're
3358 trying to boot on a platform that is not supported and significantly
3359 `different' from any of the supported ones, this is only the start of
3360 the adventure&hellip;(ToDo: porting tips&mdash;stuff to look out for, etc.)
3361 </para>
3362
3363 </Sect1>
3364
3365 <Sect1 id="sec-build-pitfalls">
3366 <Title>Known pitfalls in building Glasgow Haskell
3367
3368 <indexterm><primary>problems, building</primary></indexterm>
3369 <indexterm><primary>pitfalls, in building</primary></indexterm>
3370 <indexterm><primary>building pitfalls</primary></indexterm></Title>
3371
3372 <para>
3373 WARNINGS about pitfalls and known ``problems'':
3374 </para>
3375
3376 <para>
3377
3378 <OrderedList>
3379 <listitem>
3380
3381 <para>
3382 One difficulty that comes up from time to time is running out of space
3383 in <literal>TMPDIR</literal>.  (It is impossible for the configuration stuff to
3384 compensate for the vagaries of different sysadmin approaches to temp
3385 space.)
3386 <indexterm><primary>tmp, running out of space in</primary></indexterm>
3387
3388 The quickest way around it is <Command>setenv TMPDIR /usr/tmp</Command><indexterm><primary>TMPDIR</primary></indexterm> or
3389 even <Command>setenv TMPDIR .</Command> (or the equivalent incantation with your shell
3390 of choice).
3391
3392 The best way around it is to say
3393
3394 <ProgramListing>
3395 export TMPDIR=&#60;dir&#62;
3396 </ProgramListing>
3397
3398 in your <filename>build.mk</filename> file.
3399 Then GHC and the other <Literal>fptools</Literal> programs will use the appropriate directory
3400 in all cases.
3401
3402
3403 </para>
3404 </listitem>
3405 <listitem>
3406
3407 <para>
3408 In compiling some support-code bits, e.g., in <filename>ghc/rts/gmp</filename> and even
3409 in <filename>ghc/lib</filename>, you may get a few C-compiler warnings.  We think these
3410 are OK.
3411
3412 </para>
3413 </listitem>
3414 <listitem>
3415
3416 <para>
3417 When compiling via C, you'll sometimes get ``warning: assignment from
3418 incompatible pointer type'' out of GCC.  Harmless.
3419
3420 </para>
3421 </listitem>
3422 <listitem>
3423
3424 <para>
3425 Similarly, <Command>ar</Command>chiving warning messages like the following are not
3426 a problem:
3427
3428 <Screen>
3429 ar: filename GlaIOMonad__1_2s.o truncated to GlaIOMonad_
3430 ar: filename GlaIOMonad__2_2s.o truncated to GlaIOMonad_
3431 ...
3432 </Screen>
3433
3434
3435 </para>
3436 </listitem>
3437 <listitem>
3438
3439 <para>
3440  In compiling the compiler proper (in <filename>compiler/</filename>), you <Emphasis>may</Emphasis>
3441 get an ``Out of heap space'' error message.  These can vary with the
3442 vagaries of different systems, it seems.  The solution is simple:
3443
3444
3445 <ItemizedList>
3446 <listitem>
3447
3448 <para>
3449  If you're compiling with GHC 4.00 or later, then the
3450 <Emphasis>maximum</Emphasis> heap size must have been reached.  This
3451 is somewhat unlikely, since the maximum is set to 64M by default.
3452 Anyway, you can raise it with the
3453 <Option>-optCrts-M&lt;size&gt;</Option> flag (add this flag to
3454 <constant>&lt;module&gt;&lowbar;HC&lowbar;OPTS</constant>
3455 <Command>make</Command> variable in the appropriate
3456 <filename>Makefile</filename>).
3457
3458 </para>
3459 </listitem>
3460 <listitem>
3461
3462 <para>
3463  For GHC &#60; 4.00, add a suitable <Option>-H</Option> flag to the <filename>Makefile</filename>, as
3464 above.
3465
3466 </para>
3467 </listitem>
3468
3469 </ItemizedList>
3470
3471
3472 and try again: <Command>gmake</Command>.  (see <Xref LinkEnd="sec-suffix"> for information about
3473 <constant>&lt;module&gt;&lowbar;HC&lowbar;OPTS</constant>.)
3474
3475 Alternatively, just cut to the chase:
3476
3477 <Screen>
3478 % cd ghc/compiler
3479 % make EXTRA_HC_OPTS=-optCrts-M128M
3480 </Screen>
3481
3482
3483 </para>
3484 </listitem>
3485 <listitem>
3486
3487 <para>
3488 If you try to compile some Haskell, and you get errors from GCC about
3489 lots of things from <filename>/usr/include/math.h</filename>, then your GCC was
3490 mis-installed.  <Command>fixincludes</Command> wasn't run when it should've been.
3491
3492 As <Command>fixincludes</Command> is now automagically run as part of GCC installation,
3493 this bug also suggests that you have an old GCC.
3494
3495
3496 </para>
3497 </listitem>
3498 <listitem>
3499
3500 <para>
3501 You <Emphasis>may</Emphasis> need to re-<Command>ranlib</Command><indexterm><primary>ranlib</primary></indexterm> your libraries (on Sun4s).
3502
3503
3504 <Screen>
3505 % cd $(libdir)/ghc-x.xx/sparc-sun-sunos4
3506 % foreach i ( `find . -name '*.a' -print` ) # or other-shell equiv...
3507 ?    ranlib $i
3508 ?    # or, on some machines: ar s $i
3509 ? end
3510 </Screen>
3511
3512
3513 We'd be interested to know if this is still necessary.
3514
3515
3516 </para>
3517 </listitem>
3518 <listitem>
3519
3520 <para>
3521 GHC's sources go through <Command>cpp</Command> before being compiled, and <Command>cpp</Command> varies
3522 a bit from one Unix to another.  One particular gotcha is macro calls
3523 like this:
3524
3525
3526 <ProgramListing>
3527 SLIT("Hello, world")
3528 </ProgramListing>
3529
3530
3531 Some <Command>cpp</Command>s treat the comma inside the string as separating two macro
3532 arguments, so you get
3533
3534
3535 <Screen>
3536 :731: macro `SLIT' used with too many (2) args
3537 </Screen>
3538
3539
3540 Alas, <Command>cpp</Command> doesn't tell you the offending file!
3541
3542 Workaround: don't put weird things in string args to <Command>cpp</Command> macros.
3543 </para>
3544 </listitem>
3545
3546 </OrderedList>
3547
3548 </para>
3549
3550 </Sect1>
3551
3552
3553 <Sect1 id="winbuild"><Title>Notes for building under Windows</Title>
3554
3555 <para>
3556 This section summarises how to get the utilities you need on your
3557 Win95/98/NT/2000 machine to use CVS and build GHC. Similar notes for
3558 installing and running GHC may be found in the user guide. In general,
3559 Win95/Win98 behave the same, and WinNT/Win2k behave the same.
3560 You should read the GHC installation guide sections on Windows (in the user
3561 guide) before continuing to read these notes.
3562 </para>
3563
3564
3565 <Sect2><Title>Before you start</Title>
3566
3567 <itemizedlist>
3568 <listitem>
3569 <para>
3570 Make sure that the user environment variable
3571 <constant>MAKE_MODE</constant> is set to <Literal>UNIX</Literal>. If you
3572 don't do this you get very weird messages when you type
3573 <Command>make</Command>, such as:
3574 <Screen>
3575 /c: /c: No such file or directory
3576 </Screen>
3577 </para>
3578 </listitem>
3579
3580 <listitem>
3581 <para>GHC uses the <emphasis>mingw</emphasis> C compiler to
3582 generate code, so you have to install that. Just pick up a mingw bundle at
3583 <ulink url="http://www.mingw.org/">http://www.mingw.org/</ulink>.
3584 We install it in <filename>c:/mingw</filename>.
3585 </para>
3586 </listitem>
3587
3588 <listitem>
3589 <para>
3590 Install a version of GHC, and put it in your
3591 <constant>PATH</constant> (the installer tells you the path element
3592 you need to add upon completion.)
3593 </para>
3594 </listitem>
3595
3596 <!--
3597 <listitem>
3598 <para>
3599 Because of various hard-wired infelicities, you need to copy
3600 <Filename>bash.exe</Filename>, <Filename>perl.exe</Filename> and
3601 <Filename>cat.exe</Filename> (from Cygwin's <Filename>bin</Filename>
3602 directory), to <Filename>/bin</Filename> (discover where your Cygwin
3603 root directory is by typing <Command>mount</Command>). If
3604 <Command>/bin</Command> points to the Cygwin <Filename>bin</Filename>
3605 directory, there's no need to copy anything.
3606 </para>
3607 </listitem>
3608
3609 <listitem>
3610 <para>
3611 By default, cygwin provides the command shell <filename>ash</filename>
3612 as <filename>sh.exe</filename>. It has a couple of 'issues', so
3613 in your <filename>/bin</filename> directory, make sure that <filename>
3614 bash.exe</filename> is also provided as <filename>sh.exe</filename>.
3615 </para>
3616 </listitem>
3617
3618
3619 <listitem>
3620 <para> Both <command>cvs</command> and <command>ssh</command>
3621 come with Cygwin, but make sure you select them when running
3622 the Cygwin installer.
3623 </listitem>
3624
3625 <listitem>
3626 <para> Check out a copy of GHC sources from
3627 the CVS repository, following the instructions above (<xref linkend="cvs-access">).
3628 </para>
3629 </listitem>
3630 </itemizedlist>
3631 </sect2>
3632
3633 <Sect2><Title>Building GHC</Title>
3634       
3635 <ItemizedList>
3636
3637 <listitem>
3638 <para>
3639 Run <Command>autoconf</Command> both in <filename>fptools</filename>
3640 and in <filename>fptools/ghc</filename>.  If you omit the latter step you'll
3641 get an error when you run <filename>./configure</filename>:
3642 <Screen>
3643 ...lots of stuff...
3644 creating mk/config.h
3645 mk/config.h is unchanged
3646 configuring in ghc
3647 running /bin/sh ./configure  --cache-file=.././config.cache --srcdir=.
3648 ./configure: ./configure: No such file or directory
3649 configure: error: ./configure failed for ghc
3650 </Screen>
3651 </para>
3652 </listitem>
3653
3654 <listitem>
3655 <para>
3656 You either need to add <filename>ghc</filename> to your
3657 <constant>PATH</constant> before you invoke
3658 <Command>configure</Command>, or use the <Command>configure</Command>
3659 option <option>--with-ghc=c:/ghc/ghc-some-version/bin/ghc</option>.
3660
3661 </para>
3662 <para>
3663 The Windows installer for GHC tells you at the end what
3664 additions you need to make to your <constant>PATH</constant>.
3665 </para>
3666 </listitem>
3667
3668 <listitem>
3669   <para> 
3670     After <command>autoconf</command> run <command>./configure</command> in
3671     <filename>fptools/</filename> thus:
3672
3673 <Screen>
3674   ./configure --host=i386-unknown-mingw32 --with-gcc=/mingw/bin/gcc
3675 </Screen>
3676
3677 Both these options are important! It's possible to get into
3678 trouble using the wrong C compiler!
3679 </para>
3680 </listitem>
3681
3682 </ItemizedList>
3683 </Sect2>
3684
3685
3686 <!--
3687     <sect2>
3688       <title>Building the Windows InstallShield&reg; Installer</title>
3689
3690       <para>
3691         This section is intended for GHC developers only; no-one else
3692         should need to build an InstallShield.
3693       </para>
3694
3695       <para>
3696         Having built a second-stage tree and done <command>make
3697         install</command> on it, open the InstallShield
3698         (<filename>.ism</filename>) file. Open the Project screen, and
3699         then the Project subfolder of the Path variables folder, and
3700         set <literal>SourceFiles</literal> to the top of your
3701         tree. You might also need to set <literal>GHCBITS</literal> to
3702         point to the tree of various external bits that are added into
3703         the IS mix. You should then be able to build an InstallShield.
3704       </para>
3705
3706       <sect3>
3707         <title>Extra features of the InstallShield</title>
3708
3709         <para>
3710           The InstallShield has some IS-specific twiddles:
3711
3712           <itemizedlist>
3713             <listitem>
3714               <para>
3715                 Two registry entries are set under
3716                 <literal>HKEY_LOCAL_MACHINE\SOFTWARE\GHC</literal>:
3717                 <literal>Path</literal> and
3718                 <literal>Version</literal>, which record respectively
3719                 the directory in which GHC was installed, and the
3720                 version number.
3721               </para>
3722             </listitem>
3723             <listitem>
3724               <para>
3725                 The InstallShield adds some entries to the Program
3726                 menu, for GHCi and for the documentation. See under
3727                 Setup Design and the individual components (each
3728                 component can add entries to the menu).
3729               </para>
3730             </listitem>
3731           </itemizedlist>
3732         </para>
3733       </sect3>
3734       
3735       <sect3>
3736         <title>External add-ins</title>
3737
3738         <para>
3739           The external add-ins consist of Mingwin gcc and Mingwin
3740           Perl. The layout of the add-ins tree is as follows:
3741
3742           <screen>
3743 extra-bin/
3744   gcc.exe
3745   perl.exe    (Mingwin perl)
3746   perl56.dll
3747 gcc-lib/
3748   Mingwin gcc binaries, libraries and headers
3749 include/
3750   Mingwin includes
3751 </screen>
3752           </para>
3753         </sect3>
3754       
3755     </sect2>
3756 -->
3757
3758 </Sect1>
3759
3760 </Article>