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