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