[project @ 2003-10-21 10:06:33 by simonmar]
[ghc-hetmet.git] / ghc / docs / users_guide / separate_compilation.sgml
1   <sect1 id="separate-compilation">
2     <title>Filenames and separate compilation</title>
3     
4     <indexterm><primary>separate compilation</primary></indexterm>
5     <indexterm><primary>recompilation checker</primary></indexterm>
6     <indexterm><primary>make and recompilation</primary></indexterm>
7     
8     <para>This section describes what files GHC expects to find, what
9     files it creates, where these files are stored, and what options
10     affect this behaviour.</para>
11
12     <para>Note that this section is written with
13     <firstterm>hierarchical modules</firstterm> in mind (see <xref
14     linkend="hierarchical-modules">); hierarchical modules are an
15     extension to Haskell 98 which extends the lexical syntax of
16     module names to include a dot &lsquo;.&rsquo;.  Non-hierarchical
17     modules are thus a special case in which none of the module names
18     contain dots.</para>
19
20     <para>Pathname conventions vary from system to system.  In
21     particular, the directory separator is
22     &lsquo;<literal>/</literal>&rsquo; on Unix systems and
23     &lsquo;<literal>\</literal>&rsquo; on Windows systems.  In the
24     sections that follow, we shall consistently use
25     &lsquo;<literal>/</literal>&rsquo; as the directory separator;
26     substitute this for the appropriate character for your
27     system.</para>
28
29     <sect2 id="source-files">
30       <title>Haskell source files</title>
31     
32       <para>Each Haskell source module should be placed in a file on
33       its own.</para>
34
35       <para>The file should usually be named after the module name, by
36       replacing dots in the module name by directory separators.  For
37       example, on a Unix system, the module <literal>A.B.C</literal>
38       should be placed in the file <literal>A/B/C.hs</literal>,
39       relative to some base directory.  GHC's behaviour if this rule
40       is not followed is fully defined by the following section (<xref
41       linkend="output-files">).</para>
42     </sect2>
43
44     <sect2 id="output-files">
45       <title>Output files</title>
46
47       <indexterm><primary>interface files</primary></indexterm>
48       <indexterm><primary><literal>.hi</literal> files</primary></indexterm>
49       <indexterm><primary>object files</primary></indexterm>
50       <indexterm><primary><literal>.o</literal> files</primary></indexterm>
51       
52       <para>When asked to compile a source file, GHC normally
53       generates two files: an <firstterm>object file</firstterm>, and
54       an <firstterm>interface file</firstterm>. </para>
55
56       <para>The object file, which normally ends in a
57       <literal>.o</literal> suffix (or <literal>.obj</literal> if
58       you're on Windows), contains the compiled code for the module.</para>
59
60       <para>The interface file,
61       which normally ends in a <literal>.hi</literal> suffix, contains
62       the information that GHC needs in order to compile further
63       modules that depend on this module.  It contains things like the
64       types of exported functions, definitions of data types, and so
65       on.  It is stored in a binary format, so don't try to read one;
66       use the <option>--show-iface</option> option instead (see <xref
67       linkend="hi-options">).</para>
68
69       <para>You should think of the object file and the interface file as a
70       pair, since the interface file is in a sense a compiler-readable
71       description of the contents of the object file.  If the
72       interface file and object file get out of sync for any reason,
73       then the compiler may end up making assumptions about the object
74       file that aren't true; trouble will almost certainly follow.
75       For this reason, we recommend keeping object files and interface
76       files in the same place (GHC does this by default, but it is
77       possible to override the defaults as we'll explain
78       shortly).</para>
79
80       <para>Every module has a <emphasis>module name</emphasis>
81       defined in its source code (<literal>module A.B.C where
82       ...</literal>).  Unless overridden with the
83       <literal>-o</literal> and <literal>-ohi</literal> flags
84       respectively, GHC always puts the object file for module
85       <literal>A.B.C</literal> in
86       <replaceable>odir</replaceable><literal>/A/B/C.</literal><replaceable>osuf</replaceable>,
87       and the interface file in the file
88       <replaceable>hidir</replaceable><literal>/A/B/C.</literal><replaceable>hisuf</replaceable>,
89       where <replaceable>hidir</replaceable>,
90       <replaceable>hisuf</replaceable>,
91       <replaceable>odir</replaceable>, and
92       <replaceable>osuf</replaceable>, defined as follows:
93
94       <variablelist>
95         <varlistentry>
96           <term><replaceable>hidir</replaceable></term>
97           <listitem>
98             <para>is the value of the <option>-hidir</option> option if
99             one was given (<xref linkend="options-output">), or
100             <replaceable>root-path</replaceable> otherwise.</para>
101           </listitem>
102         </varlistentry>
103         <varlistentry>
104           <term><replaceable>hisuf</replaceable></term>
105           <listitem>
106             <para>is the value of the <option>-hisuf</option> option if
107             one was given (<xref linkend="options-output">), or <literal>hi</literal>
108             otherwise.</para>
109           </listitem>
110         </varlistentry>
111
112         <varlistentry>
113           <term><replaceable>odir</replaceable></term>
114           <listitem>
115             <para>is the value of the <option>-odir</option> option if
116             one was given (<xref linkend="options-output">), or
117             <replaceable>root-path</replaceable> otherwise.</para>
118           </listitem>
119         </varlistentry>
120         <varlistentry>
121           <term><replaceable>osuf</replaceable></term>
122           <listitem>
123             <para>is the value of the <option>-osuf</option> option if
124             one was given (<xref linkend="options-output">), or <literal>o</literal>
125             otherwise (<literal>obj</literal> on Windows).</para>
126           </listitem>
127         </varlistentry>
128       </variablelist>
129
130       The <replaceable>root-path</replaceable>, used in the above definitions, is derived from the
131       location of the source file, <replaceable>source-filename</replaceable>, as follows:
132
133   <variablelist>
134   <varlistentry>
135   <term>Rule 1</term>
136   <listitem>
137   <para>GHC matches <replaceable>source-filename</replaceable> against the pattern:
138   
139   <screen><replaceable>root-path</replaceable>/<literal>A/B/C.</literal><replaceable>extension</replaceable></screen>
140
141       where:
142
143       <variablelist>
144         <varlistentry>
145           <term><replaceable>extension</replaceable></term>
146           <listitem>
147             <para>is the source file extension (usually
148             <literal>.hs</literal> or <literal>.lhs</literal>).</para>
149           </listitem>
150         </varlistentry>
151         <varlistentry>
152           <term><replaceable>root-path</replaceable></term>
153           <listitem>
154             <para>is what is left after <literal>A/B/C.</literal><replaceable>extension</replaceable>
155             has been stripped off the end of <replaceable>source-file</replaceable>.</para>
156           </listitem>
157         </varlistentry>
158       </variablelist>
159   </para>
160   </listitem>
161   </varlistentry>
162   
163   <varlistentry>
164   <term>Rule 2</term>
165   <listitem>
166         <para>If <replaceable>source-filename</replaceable> does not match the pattern
167         above (presumably because it doesn't finish with <literal>A/B/C.hs</literal>
168         or <literal>A/B/C.lhs</literal>)
169         then <replaceable>root-path</replaceable> becomes the
170         whole of the directory portion of the filename.  </para>
171   </listitem>
172   </varlistentry>
173   </variablelist>
174
175       For example, if GHC compiles the module
176       <literal>A.B.C</literal> in the file
177       <filename>src/A/B/C.hs</filename>, with no <literal>-odir</literal> or <literal>-hidir</literal> flags,
178       the interface file will be put in <literal>src/A/B/C.hi</literal> and the object file in 
179       <literal>src/A/B/C.o</literal> (using Rule 1).
180       If the same module <literal>A.B.C</literal> was in file 
181       <filename>src/ABC.hs</filename>, 
182       the interface file will still be put in <literal>src/A/B/C.hi</literal> and the object file in 
183       <literal>src/A/B/C.o</literal> (using Rule 2).
184       </para>
185       <para>A common use for Rule 2 is to have many modules all called <literal>Main</literal> held in 
186       files <literal>Test1.hs</literal> <literal>Test2.hs</literal>, etc.    Beware, though: when compiling
187       (say) <literal>Test2.hs</literal>, GHC will consult <literal>Main.hi</literal> for version information
188       from the last recompilation.  Currently (a bug, really) GHC is not clever enough to spot that the source file has changed,
189       and so there is a danger that the recompilation checker will declare that no recompilation is needed when in fact it is.
190       Solution: delete the interface file first.
191       </para>
192   <para>Notice that (unless overriden with <option>-o</option> or <option>-ohi</option>) the filenames
193   of the object and interface files are always based on the module name. The reason for this is so that
194   GHC can find the interface file for module <literal>A.B.C</literal> when compiling the declaration
195   "<literal>import A.B.C</literal>".
196   </para>
197     </sect2>
198
199     <sect2 id="search-path">
200       <title>The search path</title>
201
202       <indexterm><primary>search path</primary>
203       </indexterm>
204       <indexterm><primary>interface files, finding them</primary></indexterm>
205       <indexterm><primary>finding interface files</primary></indexterm>
206
207       <para>In your program, you import a module
208       <literal>Foo</literal> by saying <literal>import Foo</literal>.
209       In <option>--make</option> mode or GHCi, GHC will look for a
210       source file for <literal>Foo</literal> and arrange to compile it
211       first.  Without <option>--make</option>, GHC will look for the
212       interface file for <literal>Foo</literal>, which should have
213       been created by an earlier compilation of
214       <literal>Foo</literal>.  GHC uses the same strategy in each of
215       these cases for finding the appropriate file.</para>
216
217       <para>This strategy is as follows: GHC keeps a list of
218       directories called the <firstterm>search path</firstterm>.  For
219       each of these directories, it tries appending
220       <replaceable>basename</replaceable><literal>.</literal><replaceable>extension</replaceable>
221       to the directory, and checks whether the file exists.  The value
222       of <replaceable>basename</replaceable> is the module name with
223       dots replaced by the directory separator ('/' or '\', depending
224       on the system), and <replaceable>extension</replaceable> is a
225       source extension (<literal>hs</literal>, <literal>lhs</literal>)
226       if we are in <option>--make</option> mode and GHCi, or
227       <replaceable>hisuf</replaceable> otherwise.</para>
228
229       <para>For example, suppose the search path contains directories
230       <literal>d1</literal>, <literal>d2</literal>, and
231       <literal>d3</literal>, and we are in <literal>--make</literal>
232       mode looking for the source file for a module
233       <literal>A.B.C</literal>.  GHC will look in
234       <literal>d1/A/B/C.hs</literal>, <literal>d1/A/B/C.lhs</literal>,
235       <literal>d2/A/B/C.hs</literal>, and so on.</para>
236
237       <para>The search path by default contains a single directory:
238       <quote>.</quote> (i.e. the current directory).  The following
239       options can be used to add to or change the contents of the
240       search path:</para>
241
242       <variablelist>
243         <varlistentry>
244           <term><option>-i<replaceable>dirs</replaceable></option></term>
245           <listitem>
246             <para><indexterm><primary><option>-i<replaceable>dirs</replaceable></option>
247             </primary></indexterm>This flag appends a colon-separated
248             list of <filename>dirs</filename> to the search path.</para>
249           </listitem>
250         </varlistentry>
251
252         <varlistentry>
253           <term><option>-i</option></term>
254           <listitem>
255             <para>resets the search path back to nothing.</para>
256           </listitem>
257         </varlistentry>
258       </variablelist>
259
260       <para>This isn't the whole story: GHC also looks for modules in
261       pre-compiled libraries, known as packages.  See the section on
262       packages (<xref linkend="packages">), for details.</para>
263     </sect2>
264
265     <sect2 id="options-output">
266       <title>Redirecting the compilation output(s)</title>
267
268       <indexterm><primary>output-directing options</primary></indexterm>
269       <indexterm><primary>redirecting compilation output</primary></indexterm>
270
271       <variablelist>
272         <varlistentry>
273           <term><option>-o</option> <replaceable>file</replaceable></term>
274           <indexterm><primary><option>-o</option></primary></indexterm>
275           <listitem>
276             <para>GHC's compiled output normally goes into a
277             <filename>.hc</filename>, <filename>.o</filename>, etc.,
278             file, depending on the last-run compilation phase.  The
279             option <option>-o <replaceable>file</replaceable></option>
280             re-directs the output of that last-run phase to
281             <replaceable>file</replaceable>.</para>
282
283             <para>Note: this &ldquo;feature&rdquo; can be
284             counterintuitive: <command>ghc -C -o foo.o
285             foo.hs</command> will put the intermediate C code in the
286             file <filename>foo.o</filename>, name
287             notwithstanding!</para>
288
289             <para>This option is most often used when creating an
290             executable file, to set the filename of the executable.
291             For example:
292 <screen>   ghc -o prog --make Main</screen>
293
294             will compile the program starting with module
295             <literal>Main</literal>  and put the executable in the
296             file <literal>prog</literal>.</para>
297
298             <para>Note: on Windows, if the result is an executable
299             file, the extension "<filename>.exe</filename>" is added
300             if the specified filename does not already have an
301             extension.  Thus
302 <programlisting>
303    ghc -o foo Main.hs
304 </programlisting>
305           will compile and link the module
306           <filename>Main.hs</filename>, and put the resulting
307           executable in <filename>foo.exe</filename> (not
308           <filename>foo</filename>).</para>
309           </listitem>
310         </varlistentry>
311
312         <varlistentry>
313           <term><option>-odir</option> <replaceable>dir</replaceable></term>
314           <indexterm><primary><option>-odir</option></primary></indexterm>
315           <listitem>
316             <para>Redirects object files to directory
317             <replaceable>dir</replaceable>.  For example:</para>
318
319 <Screen>
320 $ ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `arch`
321 </Screen>
322
323             <para>The object files, <filename>Foo.o</filename>,
324             <filename>Bar.o</filename>, and
325             <filename>Bumble.o</filename> would be put into a
326             subdirectory named after the architecture of the executing
327             machine (<filename>x86</filename>,
328             <filename>mips</filename>, etc).</para>
329
330             <para>Note that the <option>-odir</option> option does
331             <emphasis>not</emphasis> affect where the interface files
332             are put; use the <option>-hidir</option> option for that.
333             In the above example, they would still be put in
334             <filename>parse/Foo.hi</filename>,
335             <filename>parse/Bar.hi</filename>, and
336             <filename>gurgle/Bumble.hi</filename>.</para>
337           </listitem>
338         </varlistentry>
339
340         <varlistentry>
341           <term><option>-ohi</option>  <replaceable>file</replaceable></term>
342           <indexterm><primary><option>-ohi</option></primary>
343           </indexterm>
344           <listitem>
345             <para>The interface output may be directed to another file
346             <filename>bar2/Wurble.iface</filename> with the option
347             <option>-ohi bar2/Wurble.iface</option> (not
348             recommended).</para>
349
350             <para>WARNING: if you redirect the interface file
351             somewhere that GHC can't find it, then the recompilation
352             checker may get confused (at the least, you won't get any
353             recompilation avoidance).  We recommend using a
354             combination of <option>-hidir</option> and
355             <option>-hisuf</option> options instead, if
356             possible.</para>
357
358             <para>To avoid generating an interface at all, you could
359             use this option to redirect the interface into the bit
360             bucket: <literal>-ohi /dev/null</literal>, for
361             example.</para>
362           </listitem>
363         </varlistentry>
364       
365         <varlistentry>
366           <term><option>-hidir</option>  <replaceable>dir</replaceable></term>
367           <indexterm><primary><option>-hidir</option></primary>
368           </indexterm>
369           <listitem>
370             <para>Redirects all generated interface files into
371             <replaceable>dir</replaceable>, instead of the
372             default.</para>
373           </listitem>
374         </varlistentry>
375
376         <varlistentry>
377           <term><option>-osuf</option> <replaceable>suffix</replaceable></term>
378           <term><option>-hisuf</option> <replaceable>suffix</replaceable></term>
379           <term><option>-hcsuf</option> <replaceable>suffix</replaceable></term>
380           <indexterm><primary><option>-osuf</option></primary></indexterm>
381           <indexterm><primary><option>-hisuf</option></primary></indexterm>
382           <indexterm><primary><option>-hcsuf</option></primary></indexterm>
383           <listitem>
384             <para>EXOTICA: The <option>-osuf</option>
385             <replaceable>suffix</replaceable> will change the
386             <literal>.o</literal> file suffix for object files to
387             whatever you specify.  We use this when compiling
388             libraries, so that objects for the profiling versions of
389             the libraries don't clobber the normal ones.</para>
390
391             <para>Similarly, the <option>-hisuf</option>
392             <replaceable>suffix</replaceable> will change the
393             <literal>.hi</literal> file suffix for non-system
394             interface files (see <XRef LinkEnd="hi-options">).</para>
395
396             <para>Finally, the option <option>-hcsuf</option>
397             <replaceable>suffix</replaceable> will change the
398             <literal>.hc</literal> file suffix for compiler-generated
399             intermediate C files.</para>
400
401             <para>The <option>-hisuf</option>/<option>-osuf</option>
402             game is particularly useful if you want to compile a
403             program both with and without profiling, in the same
404             directory.  You can say:
405             <Screen>
406               ghc ...</Screen>
407             to get the ordinary version, and
408             <Screen>
409               ghc ... -osuf prof.o -hisuf prof.hi -prof -auto-all</Screen>
410             to get the profiled version.</para>
411           </listitem>
412         </varlistentry>
413       </variablelist>
414     </sect2>
415
416     <sect2 id="keeping-intermediates">
417       <title>Keeping Intermediate Files</title>
418       <indexterm><primary>intermediate files, saving</primary>
419       </indexterm>
420       <indexterm><primary><literal>.hc</literal> files, saving</primary>
421       </indexterm>
422       <indexterm><primary><literal>.s</literal> files, saving</primary>
423       </indexterm>
424
425       <para>The following options are useful for keeping certain
426       intermediate files around, when normally GHC would throw these
427       away after compilation:</para>
428
429       <variablelist>
430         <varlistentry>
431           <term><option>-keep-hc-files</option></term>
432           <indexterm>
433             <primary><option>-keep-hc-files</option></primary>
434           </indexterm>
435           <listitem>
436             <para>Keep intermediate <literal>.hc</literal> files when
437             doing <literal>.hs</literal>-to-<literal>.o</literal>
438             compilations via C (NOTE: <literal>.hc</literal> files
439             aren't generated when using the native code generator, you
440             may need to use <option>-fvia-C</option> to force them
441             to be produced).</para>
442           </listitem>
443         </varlistentry>
444
445         <varlistentry>
446           <term><option>-keep-s-files</option></term>
447           <indexterm>
448             <primary><option>-keep-s-files</option></primary>
449           </indexterm>
450           <listitem>
451             <para>Keep intermediate <literal>.s</literal> files.</para>
452           </listitem>
453         </varlistentry>
454
455         <varlistentry>
456           <term><option>-keep-raw-s-files</option></term>
457           <indexterm>
458             <primary><option>-keep-raw-s-files</option></primary>
459           </indexterm>
460           <listitem>
461             <para>Keep intermediate <literal>.raw-s</literal> files.
462             These are the direct output from the C compiler, before
463             GHC does &ldquo;assembly mangling&rdquo; to produce the
464             <literal>.s</literal> file.  Again, these are not produced
465             when using the native code generator.</para>
466           </listitem>
467         </varlistentry>
468
469         <varlistentry>
470           <term><option>-keep-tmp-files</option></term>
471           <indexterm>
472             <primary><option>-keep-tmp-files</option></primary>
473           </indexterm>
474           <indexterm>
475             <primary>temporary files</primary>
476             <secondary>keeping</secondary>
477           </indexterm>
478           <listitem>
479             <para>Instructs the GHC driver not to delete any of its
480             temporary files, which it normally keeps in
481             <literal>/tmp</literal> (or possibly elsewhere; see <xref
482             linkend="temp-files">).  Running GHC with
483             <option>-v</option> will show you what temporary files
484             were generated along the way.</para>
485           </listitem>
486         </varlistentry>
487       </variablelist>
488     </sect2>
489
490     <sect2 id="temp-files">
491       <title>Redirecting temporary files</title>
492
493       <indexterm>
494         <primary>temporary files</primary>
495         <secondary>redirecting</secondary>
496       </indexterm>
497
498       <variablelist>
499         <varlistentry>
500           <term><option>-tmpdir</option></term>
501           <indexterm><primary><option>-tmpdir</option></primary></indexterm>
502           <listitem>
503             <para>If you have trouble because of running out of space
504             in <filename>/tmp</filename> (or wherever your
505             installation thinks temporary files should go), you may
506             use the <option>-tmpdir
507             &lt;dir&gt;</option><IndexTerm><Primary>-tmpdir
508             &lt;dir&gt; option</Primary></IndexTerm> option to specify
509             an alternate directory.  For example, <option>-tmpdir
510             .</option> says to put temporary files in the current
511             working directory.</para>
512
513             <para>Alternatively, use your <Constant>TMPDIR</Constant>
514             environment variable.<IndexTerm><Primary>TMPDIR
515             environment variable</Primary></IndexTerm> Set it to the
516             name of the directory where temporary files should be put.
517             GCC and other programs will honour the
518             <Constant>TMPDIR</Constant> variable as well.</para>
519
520             <para>Even better idea: Set the
521             <Constant>DEFAULT_TMPDIR</Constant> make variable when
522             building GHC, and never worry about
523             <Constant>TMPDIR</Constant> again. (see the build
524             documentation).</para>
525           </listitem>
526         </varlistentry>
527       </variablelist>
528     </sect2>
529
530     <Sect2 id="hi-options">
531       <title>Other options related to interface files</title>
532       <indexterm><primary>interface files, options</primary></indexterm>
533
534       <variablelist>
535         <varlistentry>
536           <term><option>-ddump-hi</option></term>
537           <indexterm><primary><option>-ddump-hi</option></primary>
538           </indexterm>
539           <listitem>
540             <para>Dumps the new interface to standard output.</para>
541           </listitem>
542         </varlistentry>
543
544         <varlistentry>
545           <term><option>-ddump-hi-diffs</option></term>
546           <indexterm><primary><option>-ddump-hi-diffs</option></primary>
547           </indexterm>
548           <listitem>
549             <para>The compiler does not overwrite an existing
550             <filename>.hi</filename> interface file if the new one is
551             the same as the old one; this is friendly to
552             <command>make</command>.  When an interface does change,
553             it is often enlightening to be informed.  The
554             <option>-ddump-hi-diffs</option> option will make GHC run
555             <command>diff</command> on the old and new
556             <filename>.hi</filename> files.</para>
557           </listitem>
558         </varlistentry>
559
560         <varlistentry>
561           <term><option>-ddump-minimal-imports</option></term>
562           <indexterm><primary><option>-ddump-minimal-imports</option></primary>
563           </indexterm>
564           <listitem>
565             <para>Dump to the file "M.imports" (where M is the module
566             being compiled) a "minimal" set of import declarations.
567             You can safely replace all the import declarations in
568             "M.hs" with those found in "M.imports".  Why would you
569             want to do that?  Because the "minimal" imports (a) import
570             everything explicitly, by name, and (b) import nothing
571             that is not required.  It can be quite painful to maintain
572             this property by hand, so this flag is intended to reduce
573             the labour.</para>
574           </listitem>
575         </varlistentry>
576
577         <varlistentry>
578           <term><option>--show-iface</option>
579           <replaceable>file</replaceable></term>
580           <indexterm><primary><option>--show-iface</option></primary>
581           </indexterm>
582           <listitem>
583             <para>Where <replaceable>file</replaceable> is the name of
584             an interface file, dumps the contents of that interface in
585             a human-readable (ish) format.</para>
586           </listitem>
587         </varlistentry>
588       </variablelist>
589     </sect2>
590
591     <sect2 id="recomp">
592       <title>The recompilation checker</title>
593
594       <indexterm><primary>recompilation checker</primary></indexterm>
595
596       <variablelist>
597         <varlistentry>
598           <term><option>-no-recomp</option></term>
599           <indexterm><primary><option>-recomp</option></primary></indexterm>
600           <indexterm><primary><option>-no-recomp</option></primary></indexterm>
601           <listitem>
602             <para>Turn off recompilation checking (which is on by
603             default).  Recompilation checking normally stops
604             compilation early, leaving an existing
605             <filename>.o</filename> file in place, if it can be
606             determined that the module does not need to be
607             recompiled.</para>
608           </listitem>
609         </varlistentry>
610       </variablelist>
611       
612       <para>In the olden days, GHC compared the newly-generated
613       <filename>.hi</filename> file with the previous version; if they
614       were identical, it left the old one alone and didn't change its
615       modification date.  In consequence, importers of a module with
616       an unchanged output <filename>.hi</filename> file were not
617       recompiled.</para>
618
619       <para>This doesn't work any more.  Suppose module
620       <literal>C</literal> imports module <literal>B</literal>, and
621       <literal>B</literal> imports module <literal>A</literal>.  So
622       changes to module <literal>A</literal> might require module
623       <literal>C</literal> to be recompiled, and hence when
624       <filename>A.hi</filename> changes we should check whether
625       <literal>C</literal> should be recompiled.  However, the
626       dependencies of <literal>C</literal> will only list
627       <literal>B.hi</literal>, not <literal>A.hi</literal>, and some
628       changes to <literal>A</literal> (changing the definition of a
629       function that appears in an inlining of a function exported by
630       <literal>B</literal>, say) may conceivably not change
631       <filename>B.hi</filename> one jot.  So now&hellip;</para>
632
633       <para>GHC keeps a version number on each interface file, and on
634       each type signature within the interface file.  It also keeps in
635       every interface file a list of the version numbers of everything
636       it used when it last compiled the file.  If the source file's
637       modification date is earlier than the <filename>.o</filename>
638       file's date (i.e. the source hasn't changed since the file was
639       last compiled), and the reompilation checking is on, GHC will be
640       clever.  It compares the version numbers on the things it needs
641       this time with the version numbers on the things it needed last
642       time (gleaned from the interface file of the module being
643       compiled); if they are all the same it stops compiling rather
644       early in the process saying &ldquo;Compilation IS NOT
645       required&rdquo;.  What a beautiful sight!</para>
646
647       <para>Patrick Sansom had a workshop paper about how all this is
648       done (though the details have changed quite a bit). <ULink
649       URL="mailto:sansom@dcs.gla.ac.uk">Ask him</ULink> if you want a
650       copy.</para>
651
652     </sect2>
653
654     <sect2 id="using-make">
655       <title>Using <command>make</command></title>
656
657       <indexterm><primary><literal>make</literal></primary></indexterm>
658
659       <para>It is reasonably straightforward to set up a
660       <filename>Makefile</filename> to use with GHC, assuming you name
661       your source files the same as your modules.  Thus:</para>
662
663 <ProgramListing>
664 HC      = ghc
665 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
666
667 SRCS = Main.lhs Foo.lhs Bar.lhs
668 OBJS = Main.o   Foo.o   Bar.o
669
670 .SUFFIXES : .o .hs .hi .lhs .hc .s
671
672 cool_pgm : $(OBJS)
673         rm -f $@
674         $(HC) -o $@ $(HC_OPTS) $(OBJS)
675
676 # Standard suffix rules
677 .o.hi:
678         @:
679
680 .lhs.o:
681         $(HC) -c $&#60; $(HC_OPTS)
682
683 .hs.o:
684         $(HC) -c $&#60; $(HC_OPTS)
685
686 # Inter-module dependencies
687 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
688 Main.o Main.hc Main.s : Foo.hi Baz.hi   # Main imports Foo and Baz
689 </ProgramListing>
690
691       <para>(Sophisticated <command>make</command> variants may
692       achieve some of the above more elegantly.  Notably,
693       <command>gmake</command>'s pattern rules let you write the more
694       comprehensible:</para>
695
696 <ProgramListing>
697 %.o : %.lhs
698         $(HC) -c $&#60; $(HC_OPTS)
699 </ProgramListing>
700
701       <para>What we've shown should work with any
702       <command>make</command>.)</para>
703
704       <para>Note the cheesy <literal>.o.hi</literal> rule: It records
705       the dependency of the interface (<filename>.hi</filename>) file
706       on the source.  The rule says a <filename>.hi</filename> file
707       can be made from a <filename>.o</filename> file by
708       doing&hellip;nothing.  Which is true.</para>
709
710       <para>Note the inter-module dependencies at the end of the
711       Makefile, which take the form</para>
712
713 <ProgramListing>
714 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
715 </ProgramListing>
716
717       <para>They tell <command>make</command> that if any of
718       <literal>Foo.o</literal>, <literal>Foo.hc</literal> or
719       <literal>Foo.s</literal> have an earlier modification date than
720       <literal>Baz.hi</literal>, then the out-of-date file must be
721       brought up to date.  To bring it up to date,
722       <literal>make</literal> looks for a rule to do so; one of the
723       preceding suffix rules does the job nicely.</para>
724
725       <sect3 id="sec-makefile-dependencies">
726         <title>Dependency generation</title>
727         <indexterm><primary>dependencies in Makefiles</primary></indexterm>
728         <indexterm><primary>Makefile dependencies</primary></indexterm>
729
730         <para>Putting inter-dependencies of the form <literal>Foo.o :
731         Bar.hi</literal> into your <filename>Makefile</filename> by
732         hand is rather error-prone.  Don't worry, GHC has support for
733         automatically generating the required dependencies.  Add the
734         following to your <filename>Makefile</filename>:</para>
735
736 <ProgramListing>
737 depend :
738         ghc -M $(HC_OPTS) $(SRCS)
739 </ProgramListing>
740
741         <para>Now, before you start compiling, and any time you change
742         the <literal>imports</literal> in your program, do
743         <command>make depend</command> before you do <command>make
744         cool&lowbar;pgm</command>.  <command>ghc -M</command> will
745         append the needed dependencies to your
746         <filename>Makefile</filename>.</para>
747
748         <para>In general, if module <literal>A</literal> contains the
749         line
750
751 <programlisting>
752 import B ...blah...
753 </programlisting>
754
755         then <command>ghc -M</command> will generate a dependency line
756         of the form:
757
758 <programlisting>
759 A.o : B.hi
760 </programlisting>
761
762         If module <literal>A</literal> contains the line
763
764 <programlisting>
765 import {-# SOURCE #-} B ...blah...
766 </programlisting>
767
768         then <command>ghc -M</command> will generate a dependency
769         line of the form:
770
771 <programlisting>
772 A.o : B.hi-boot
773 </programlisting>
774
775        (See <xref linkend="mutual-recursion"> for details of
776        <literal>hi-boot</literal> style interface files.)  If
777        <literal>A</literal> imports multiple modules, then there will
778        be multiple lines with <filename>A.o</filename> as the
779        target.</para>
780
781         <para>By default, <command>ghc -M</command> generates all the
782         dependencies, and then concatenates them onto the end of
783         <filename>makefile</filename> (or
784         <filename>Makefile</filename> if <filename>makefile</filename>
785         doesn't exist) bracketed by the lines "<literal>&num; DO NOT
786         DELETE: Beginning of Haskell dependencies</literal>" and
787         "<literal>&num; DO NOT DELETE: End of Haskell
788         dependencies</literal>".  If these lines already exist in the
789         <filename>makefile</filename>, then the old dependencies are
790         deleted first.</para>
791
792         <para>Don't forget to use the same <option>-package</option>
793         options on the <literal>ghc -M</literal> command line as you
794         would when compiling; this enables the dependency generator to
795         locate any imported modules that come from packages.  The
796         package modules won't be included in the dependencies
797         generated, though (but see the
798         <option>&ndash;&ndash;include-prelude</option> option below).</para>
799
800         <para>The dependency generation phase of GHC can take some
801         additional options, which you may find useful.  For historical
802         reasons, each option passed to the dependency generator from
803         the GHC command line must be preceded by
804         <literal>-optdep</literal>.  For example, to pass <literal>-f
805         .depend</literal> to the dependency generator, you say
806
807 <screen>
808 ghc -M -optdep-f -optdep.depend ...
809 </screen>
810       
811         The options which affect dependency generation are:</para>
812         
813         <variablelist>
814           <varlistentry>
815             <term><option>-w</option></term>
816             <listitem>
817               <para>Turn off warnings about interface file shadowing.</para>
818             </listitem>
819           </varlistentry>
820           
821           <varlistentry>
822             <term><option>-f</option> <replaceable>file</replaceable></term>
823             <listitem>
824               <para>Use <replaceable>file</replaceable> as the makefile,
825               rather than <filename>makefile</filename> or
826               <filename>Makefile</filename>.  If
827               <replaceable>file</replaceable> doesn't exist,
828               <command>mkdependHS</command> creates it.  We often use
829               <option>-f .depend</option> to put the dependencies in
830               <filename>.depend</filename> and then
831               <command>include</command> the file
832               <filename>.depend</filename> into
833               <filename>Makefile</filename>.</para>
834             </listitem>
835           </varlistentry>
836
837           <varlistentry>
838             <term><option>-o &lt;osuf&gt;</option></term>
839             <listitem>
840               <para>Use <filename>.&lt;osuf&gt;</filename> as the
841               "target file" suffix ( default: <literal>o</literal>).
842               Multiple <option>-o</option> flags are permitted
843               (GHC2.05 onwards).  Thus "<option>-o hc -o o</option>"
844               will generate dependencies for <filename>.hc</filename>
845               and <filename>.o</filename> files.</para>
846             </listitem>
847           </varlistentry>
848
849           <varlistentry>
850             <term><option>-s &lt;suf&gt;</option></term>
851             <listitem>
852               <para>Make extra dependencies that declare that files
853               with suffix
854               <filename>.&lt;suf&gt;&lowbar;&lt;osuf&gt;</filename>
855               depend on interface files with suffix
856               <filename>.&lt;suf&gt;&lowbar;hi</filename>, or (for
857               <literal>&lcub;-&num; SOURCE &num;-&rcub;</literal>
858               imports) on <filename>.hi-boot</filename>.  Multiple
859               <option>-s</option> flags are permitted.  For example,
860               <option>-o hc -s a -s b</option> will make dependencies
861               for <filename>.hc</filename> on
862               <filename>.hi</filename>,
863               <filename>.a&lowbar;hc</filename> on
864               <filename>.a&lowbar;hi</filename>, and
865               <filename>.b&lowbar;hc</filename> on
866               <filename>.b&lowbar;hi</filename>.  (Useful in
867               conjunction with NoFib "ways".)</para>
868             </listitem>
869           </varlistentry>
870
871           <varlistentry>
872             <term><option>&ndash;&ndash;exclude-module=&lt;file&gt;</option></term>
873             <listitem>
874               <para>Regard <filename>&lt;file&gt;</filename> as
875               "stable"; i.e., exclude it from having dependencies on
876               it.</para>
877             </listitem>
878           </varlistentry>
879
880           <varlistentry>
881             <term><option>-x</option></term>
882             <listitem>
883               <para>same as <option>&ndash;&ndash;exclude-module</option></para>
884             </listitem>
885           </varlistentry>
886
887           <varlistentry>
888             <term><option>&ndash;&ndash;exclude-directory=&lt;dirs&gt;</option></term>
889             <listitem>
890               <para>Regard the colon-separated list of directories
891               <filename>&lt;dirs&gt;</filename> as containing stable,
892               don't generate any dependencies on modules
893               therein.</para>
894             </listitem>
895           </varlistentry>
896
897           <varlistentry>
898             <term><option>&ndash;&ndash;include-module=&lt;file&gt;</option></term>
899             <listitem>
900               <para>Regard <filename>&lt;file&gt;</filename> as not
901               "stable"; i.e., generate dependencies on it (if
902               any). This option is normally used in conjunction with
903               the <option>&ndash;&ndash;exclude-directory</option> option.</para>
904             </listitem>
905           </varlistentry>
906
907           <varlistentry>
908             <term><option>&ndash;&ndash;include-prelude</option></term>
909             <listitem>
910               <para>Regard modules imported from packages as unstable,
911               i.e., generate dependencies on the package modules used
912               (including <literal>Prelude</literal>, and all other
913               standard Haskell libraries).  This option is normally
914               only used by the various system libraries.</para>
915             </listitem>
916           </varlistentry>
917         </variablelist>
918
919       </sect3>
920     </sect2>
921
922     <sect2 id="mutual-recursion">
923       <title>How to compile mutually recursive modules</title>
924
925       <indexterm><primary>module system, recursion</primary></indexterm>
926       <indexterm><primary>recursion, between modules</primary></indexterm>
927
928       <para>Currently, the compiler does not have proper support for
929       dealing with mutually recursive modules:</para>
930
931 <ProgramListing>
932 module A where
933
934 import B
935
936 newtype TA = MkTA Int
937
938 f :: TB -&#62; TA
939 f (MkTB x) = MkTA x
940 --------
941 module B where
942
943 import A
944
945 data TB = MkTB !Int
946
947 g :: TA -&#62; TB
948 g (MkTA x) = MkTB x
949 </ProgramListing>
950
951       <para>When compiling either module A and B, the compiler will
952       try (in vain) to look for the interface file of the other. So,
953       to get mutually recursive modules off the ground, you need to
954       hand write an interface file for A or B, so as to break the
955       loop.  These hand-written interface files are called
956       <literal>hi-boot</literal> files, and are placed in a file
957       called <filename>&lt;module&gt;.hi-boot</filename>.  To import
958       from an <literal>hi-boot</literal> file instead of the standard
959       <filename>.hi</filename> file, use the following syntax in the
960       importing module: <indexterm><primary><literal>hi-boot</literal>
961       files</primary></indexterm> <indexterm><primary>importing,
962       <literal>hi-boot</literal> files</primary></indexterm></para>
963
964 <ProgramListing>
965 import {-# SOURCE #-} A
966 </ProgramListing>
967
968       <para>The hand-written interface need only contain the bare
969       minimum of information needed to get the bootstrapping process
970       started.  For example, it doesn't need to contain declarations
971       for <emphasis>everything</emphasis> that module
972       <literal>A</literal> exports, only the things required by the
973       module that imports <literal>A</literal> recursively.</para>
974
975       <para>For the example at hand, the boot interface file for A
976       would look like the following:</para>
977
978 <ProgramListing>
979 module A where
980 newtype TA = MkTA GHC.Base.Int
981 </ProgramListing>
982
983       <para>The syntax is similar to a normal Haskell source file, but
984       with some important differences:</para>
985
986       <itemizedlist>
987         <listitem>
988           <para>Non-local entities must be qualified with their
989           <emphasis>original</emphasis> defining module.  Qualifying
990           by a module which just re-exports the entity won't do.  In
991           particular, most <literal>Prelude</literal> entities aren't
992           actually defined in the <literal>Prelude</literal> (see for
993           example <literal>GHC.Base.Int</literal> in the above
994           example).  HINT: to find out the fully-qualified name for
995           entities in the <literal>Prelude</literal> (or anywhere for
996           that matter), try using GHCi's
997           <literal>:info</literal> command, eg.</para>
998 <programlisting>Prelude> :m -Prelude
999 > :i IO.IO
1000 -- GHC.IOBase.IO is a type constructor
1001 newtype GHC.IOBase.IO a
1002 ...</programlisting>
1003         </listitem>
1004         <listitem>
1005           <para>Only <literal>data</literal>, <literal>type</literal>,
1006           <literal>newtype</literal>, <literal>class</literal>, and
1007           type signature declarations may be included. You cannot declare
1008           <literal>instances</literal> or derive them automatically.
1009 </para>
1010         </listitem>
1011       </itemizedlist>
1012
1013       <para>Notice that we only put the declaration for the newtype
1014       <literal>TA</literal> in the <literal>hi-boot</literal> file,
1015       not the signature for <Function>f</Function>, since
1016       <Function>f</Function> isn't used by <literal>B</literal>.</para>
1017
1018       <para>If you want an <literal>hi-boot</literal> file to export a
1019       data type, but you don't want to give its constructors (because
1020       the constructors aren't used by the SOURCE-importing module),
1021       you can write simply:</para>
1022
1023 <ProgramListing>
1024 module A where
1025 data TA
1026 </ProgramListing>
1027
1028       <para>(You must write all the type parameters, but leave out the
1029       '=' and everything that follows it.)</para>
1030     </sect2>
1031
1032
1033     <sect2 id="orphan-modules">
1034       <title>Orphan modules and instance declarations</title>
1035
1036 <para> Haskell specifies that when compiling module M, any instance
1037 declaration in any module "below" M is visible.  (Module A is "below"
1038 M if A is imported directly by M, or if A is below a module that M imports directly.)
1039 In principle, GHC must therefore read the interface files of every module below M,
1040 just in case they contain an instance declaration that matters to M.  This would
1041 be a disaster in practice, so GHC tries to be clever. </para>
1042
1043 <para>In particular, if an instance declaration is in the same module as the definition
1044 of any type or class mentioned in the head of the instance declaration, then
1045 GHC has to visit that interface file anyway.  Example:</para>
1046 <ProgramListing>
1047   module A where
1048     instance C a =&gt; D (T a) where ...
1049     data T a = ...
1050 </ProgramListing>
1051 <para> The instance declaration is only relevant if the type T is in use, and if
1052 so, GHC will have visited A's interface file to find T's definition. </para>
1053
1054 <para> The only problem comes when a module contains an instance declaration
1055 and GHC has no other reason for visiting the module.  Example:
1056 <ProgramListing>
1057   module Orphan where
1058     instance C a =&gt; D (T a) where ...
1059     class C a where ...
1060 </ProgramListing>
1061 Here, neither D nor T is declared in module Orphan.
1062 We call such modules ``orphan modules'',
1063 defined thus:</para>
1064 <itemizedlist>
1065   <listitem> <para> An <emphasis>orphan module</emphasis> 
1066   <indexterm><primary>orphan module</primary></indexterm>
1067   contains at least one <emphasis>orphan instance</emphasis> or at 
1068   least one <emphasis>orphan rule</emphasis>.</para> </listitem>
1069
1070   <listitem><para> An instance declaration in a module M is an <emphasis>orphan instance</emphasis> if
1071   <indexterm><primary>orphan instance</primary></indexterm>
1072   none of the type constructors
1073   or classes mentioned in the instance head (the part after the ``<literal>=&gt;</literal>'') are declared
1074   in M.</para> 
1075
1076   <para> Only the instance head counts.  In the example above, it is not good enough for C's declaration 
1077   to be in module A; it must be the declaration of D or T.</para>
1078   </listitem>
1079
1080   <listitem><para> A rewrite rule in a module M is an <emphasis>orphan rule</emphasis>
1081   <indexterm><primary>orphan rule</primary></indexterm>
1082   if none of the variables, type constructors,
1083   or classes that are free in the left hand side of the rule are declared in M.
1084   </para> </listitem>
1085  </itemizedlist>
1086
1087
1088 <para> GHC identifies orphan modules, and visits the interface file of
1089 every orphan module below the module being compiled.  This is usually
1090 wasted work, but there is no avoiding it.  You should therefore do
1091 your best to have as few orphan modules as possible.
1092
1093 </para>
1094
1095 <para> You can identify an orphan module by looking in its interface
1096 file, <filename>M.hi</filename>, using the
1097 <option>--show-iface</option>.  If there is a ``!'' on the first line,
1098 GHC considers it an orphan module.
1099 </para>
1100 </sect2>
1101
1102   </sect1>
1103
1104 <!-- Emacs stuff:
1105      ;;; Local Variables: ***
1106      ;;; mode: sgml ***
1107      ;;; sgml-parent-document: ("using.sgml" "book" "chapter") ***
1108      ;;; End: ***
1109  -->