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