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