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