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