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