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