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