2 <title>Packages</title>
3 <indexterm><primary>packages</primary></indexterm>
5 <para>Packages are collections of libraries, conveniently grouped
6 together as a single entity. The package system is flexible: a
7 package may consist of Haskell code, foreign language code (eg. C
8 libraries), or a mixture of the two. A package is a good way to
9 group together related Haskell modules, and is essential if you
10 intend to make the modules into a Windows DLL (see below).</para>
12 <para>Because packages can contain both Haskell and C libraries, they
13 are also a good way to provide convenient access to a Haskell
14 layer over a C library.</para>
16 <para>GHC comes with several packages (see the accompanying
17 library documentation), and packages can be added to or removed
18 from an existing GHC installation, using the supplied
19 <literal>ghc-pkg</literal><indexterm><primary><literal>ghc-pkg</literal></primary>
20 </indexterm> tool, described in <xref
21 linkend="package-management">.</para>
23 <sect2 id="using-packages">
24 <title>Using a package</title>
25 <indexterm><primary>packages</primary>
26 <secondary>using</secondary></indexterm>
28 <para>Some packages are automatically available: you don't need
29 to specify any extra flags to use them (except in certain
30 circumstances; see below). All the packages which contain
31 hierarchical libraries fall into this category.</para>
33 <para>Some other packages are <emphasis>not</emphasis>
34 automatically available: those are normally the packages
35 containing old non-hierarchical libraries. To gain access to a
36 non-auto package, use the <option>-package</option> command-line
41 <term><option>-package <replaceable>lib</replaceable></option></term>
42 <indexterm><primary>-package <replaceable>lib</replaceable> option</primary></indexterm>
44 <para>This option brings into scope all the modules from
45 package <literal><replaceable>lib</replaceable></literal> (they still have to
46 be imported in your Haskell source, however). It also
47 causes the relevant libraries to be linked when linking is
53 <para>There's one case where you need to use the
54 <option>-package</option> option even for auto packages: when
55 linking a program in batch mode<footnote><para>This is because
56 GHC can't figure out from the object files which packages are
57 required; in <option>––make</option> mode and in
58 GHCi the compiler has more information available to figure out
59 the package dependencies. We might try to lift this restriction
60 in the future.</para></footnote>. For example, to link a
61 program consisting of objects <filename>Foo.o</filename> and
62 <filename>Main.o</filename>, where we made use of the
63 <literal>network</literal> package:</para>
65 <screen>$ ghc -o myprog Foo.o Main.o -package network</screen>
67 <para>Some packages depend on other packages, for example the
68 <literal>text</literal> package makes use of some of the modules
69 in the <literal>lang</literal> package. The package system
70 takes care of all these dependencies, so that when you say
71 <literal>-package text</literal> on the command line, you
72 automatically get <literal>-package lang</literal> too.</para>
75 <sect2 id="using-local-packages">
76 <title>Maintaining a local set of packages</title>
78 <para>When GHC starts up, it automatically reads the default set
79 of packages from a configuration file, normally named
80 <filename>package.conf</filename> in your GHC installation
83 <para>You can load in additional package configuration files
84 using the <option>-package-conf</option> option:</para>
88 <term><option>-package-conf <replaceable>file</replaceable></option></term>
89 <indexterm><primary><option>-package-conf <replaceable>file</replaceable></option></primary>
92 <para>Read in the package configuration file
93 <replaceable>file</replaceable> in addition to the system
94 default file. This allows the user to have a local set of
95 packages in addition to the system-wide ones.</para>
100 <para>To create your own package configuration file, just create
101 a new file and put the string
102 <quote><literal>[]</literal></quote> in it. Packages can be
103 added to the new configuration file using the
104 <literal>ghc-pkg</literal> tool, described in <xref
105 linkend="package-management">.</para>
108 <sect2 id="building-packages">
109 <title>Building a package from Haskell source</title>
110 <indexterm><primary>packages</primary>
111 <secondary>building</secondary></indexterm>
113 <para>It takes some special considerations to build a new
118 <para>A package may contain several Haskell modules. A
119 package may span many directories, or many packages may
120 exist in a single directory. Packages may not be mutually
125 <para>A package has a name
126 (e.g. <filename>base</filename>)</para>
130 <para>The Haskell code in a package may be built into one or
131 more archive libraries
132 (e.g. <filename>libHSfoo.a</filename>), or a single DLL on
133 Windows (e.g. <filename>HSfoo.dll</filename>). The
134 restriction to a single DLL on Windows is because the
135 package system is used to tell the compiler when it should
136 make an inter-DLL call rather than an intra-DLL call
137 (inter-DLL calls require an extra
138 indirection). <emphasis>Building packages as DLLs doesn't
139 work at the moment; see <XRef LinkEnd="win32-dlls-create">
140 for the gory details.</emphasis>
143 <para>Building a static library is done by using the
144 <literal>ar</literal> tool, like so:</para>
146 <screen>ar cqs libHSfoo.a A.o B.o C.o ...</screen>
148 <para>where <filename>A.o</filename>,
149 <filename>B.o</filename> and so on are the compiled Haskell
150 modules, and <filename>libHSfoo.a</filename> is the library
151 you wish to create. The syntax may differ slightly on your
152 system, so check the documentation if you run into
155 <para>Versions of the Haskell libraries for use with GHCi
156 may also be included: GHCi cannot load <literal>.a</literal>
157 files directly, instead it will look for an object file
158 called <filename>HSfoo.o</filename> and load that. On some
159 systems, the <literal>ghc-pkg</literal> tool can
160 automatically build the GHCi version of each library, see
161 <xref linkend="package-management">. To build these
162 libraries by hand from the <literal>.a</literal> archive, it
163 is possible to use GNU <command>ld</command> as
166 <screen>ld -r ––whole-archive -o HSfoo.o libHSfoo.a</screen>
170 <para>GHC does not maintain detailed cross-package
171 dependency information. It does remember which modules in
172 other packages the current module depends on, but not which
173 things within those imported things.</para>
177 <para>To compile a module which is to be part of a new package,
178 use the <literal>-package-name</literal> option:</para>
182 <term><option>-package-name <replaceable>foo</replaceable></option></term>
183 <indexterm><primary><literal>-package-name</literal></primary>
184 <secondary>option</secondary></indexterm>
186 <para>This option is added to the command line when
187 compiling a module that is destined to be part of package
188 <literal>foo</literal>. If this flag is omitted then the
189 default package <literal>Main</literal> is assumed.</para>
194 <para>Failure to use the <literal>-package-name</literal> option
195 when compiling a package will result in disaster on Windows, but
196 is relatively harmless on Unix at the moment (it will just cause
197 a few extra dependencies in some interface files). However,
198 bear in mind that we might add support for Unix shared libraries
199 at some point in the future.</para>
201 <para>It is worth noting that on Windows, when each package
202 is built as a DLL, since a reference to a DLL costs an extra
203 indirection, intra-package references are cheaper than
204 inter-package references. Of course, this applies to the
205 <filename>Main</filename> package as well.</para>
208 <sect2 id="package-management">
209 <title>Package management</title>
210 <indexterm><primary>packages</primary>
211 <secondary>management</secondary></indexterm>
213 <para>The <literal>ghc-pkg</literal> tool allows packages to be
214 added or removed from a package configuration file. By default,
215 the system-wide configuration file is used, but alternatively
216 packages can be added, updated or removed from a user-specified
217 configuration file using the <option>––config-file</option>
218 option. An empty package configuration file consists of the
219 string <quote><literal>[]</literal></quote>.</para>
221 <para>The <literal>ghc-pkg</literal> program accepts the
222 following options:</para>
226 <term><option>––add-package</option></term>
227 <term><option>-a</option></term>
228 <indexterm><primary><option>––add-package</option></primary></indexterm>
230 <para>Reads package specification from the input (see below),
231 and adds it to the database of installed packages. The
232 package specification must be a package that isn't already
238 <term><option>––input-file=<replaceable>file</replaceable></option></term>
239 <term><option>-i <replaceable>file</replaceable></option></term>
240 <indexterm><primary><option>––input-file</option></primary></indexterm>
242 <para>Read new package specifications from file
243 <replaceable>file</replaceable>. If a value of
244 <filename>"-"</filename> is given, standard input is used.
245 If no <option>-i</option> is present on the command-line,
246 an input file of <filename>"-"</filename> is assumed.
252 <term><option>––auto-ghci-libs</option></term>
253 <term><option>-g</option></term>
254 <indexterm><primary><option>––auto-ghci-libs</option></primary>
257 <para>Automatically generate the GHCi
258 <filename>.o</filename> version of each
259 <filename>.a</filename> Haskell library, using GNU ld (if
260 that is available). Without this option,
261 <literal>ghc-pkg</literal> will warn if GHCi versions of
262 any Haskell libraries in the package don't exist.</para>
264 <para>GHCi <literal>.o</literal> libraries don't
265 necessarily have to live in the same directory as the
266 corresponding <literal>.a</literal> library. However,
267 this option will cause the GHCi library to be created in
268 the same directory as the <literal>.a</literal>
274 <term><option>––config-file <replaceable>file</replaceable></option></term>
275 <term><option>-f <replaceable>file</replaceable></option></term>
276 <indexterm><primary><option>––config-file</option></primary>
279 <para>Use <replaceable>file</replaceable> as an additional
280 package configuration file. This is used to modify
281 configuration files for use with GHC's
282 <option>-package-conf</option> option.</para>
284 <para>There may be any number of configuration files named
285 on the command line; files mentioned later on the
286 command-line override those mentioned earlier. The
287 <emphasis>last</emphasis> configuration file mentioned on
288 the command-line is the only one that is actually modified
289 by <literal>ghc-pkg</literal>.</para>
295 <term><option>––list-packages</option></term>
296 <term><option>-l</option></term>
297 <indexterm><primary><option>––list-packages</option></primary></indexterm>
299 <para>This option displays the list of currently installed
300 packages, including those in extra configuration files
301 specified with the <option>––config-file</option>
305 $ ghc-pkg ––list-packages
306 /usr/local/lib/ghc-5.05/package.conf:
307 hdirect, readline, lang, concurrent, posix, util, data, text, net,
308 hssource, rts, haskell98, network, haskell-src, unix, base
311 <para>Note that your GHC installation might have a
312 slightly different set of packages installed.</para>
314 <para>The <literal>rts</literal> package is always
315 present, and represents the runtime system library. The
316 <literal>base</literal> package contains the Haskell
317 prelude and basic hierarchical libraries, and the
318 <literal>haskell98</literal> package contains the Haskell
319 98 standard libraries. The rest of the packages are
320 optional libraries.</para>
325 <term><option>––list-local-packages</option></term>
326 <term><option>-L</option></term>
327 <indexterm><primary><option>––list-local-packages</option></primary></indexterm>
329 <para>Displays the list of packages installed in the
330 topmost configuration file only: that will be the
331 configuration file specified using <option>-f</option> on
332 the command line, or the system configuration file
335 <para>This option may be more convenient than
336 <option>-l</option> when the output needs to be parsed by
342 <term><option>––remove-package <replaceable>foo</replaceable></option></term>
343 <term><option>-r <replaceable>foo</replaceable></option></term>
344 <indexterm><primary><option>––delete-package</option></primary>
347 <para>Removes the specified package from the installed
348 configuration.</para>
352 <term><option>––update-package</option></term>
353 <term><option>-u</option></term>
354 <indexterm><primary><option>––update-package</option></primary></indexterm>
356 <para>Reads package specification from the input, and
357 adds it to the database of installed packages. If a package
358 with the same name is already installed, its configuration
359 data is replaced with the new information. If the package
360 doesn't already exist, it's added.
365 <term><option>––force</option></term>
366 <indexterm><primary><option>––force</option></primary></indexterm>
368 <para>Causes <literal>ghc-pkg</literal> to ignore missing
369 directories and libraries when adding a package, and just
370 go ahead and add it anyway. This might be useful if your
371 package installation system needs to add the package to
372 GHC before building and installing the files.</para>
377 <para>When modifying the configuration file
378 <replaceable>file</replaceable>, a copy of the original file is
379 saved in <replaceable>file</replaceable><literal>.old</literal>,
380 so in an emergency you can always restore the old settings by
381 copying the old file back again.</para>
383 <para>A package specification looks like this:</para>
389 import_dirs = ["${installdir}/imports/mypkg"],
391 library_dirs = ["${installdir}"],
392 hs_libraries = ["HSmypkg" ],
393 extra_libraries = ["HSmypkg_cbits"],
395 c_includes = ["HsMyPkg.h"],
396 package_deps = ["text", "data"],
399 extra_ld_opts = ["-lmy_clib"]
403 <para>Components of a package specification may be specified in
404 any order, and are:</para>
408 <term><literal>name</literal></term>
409 <indexterm><primary><literal>name</literal></primary>
410 <secondary>package specification</secondary></indexterm>
412 <para>The package's name, for use with
413 the <literal>-package</literal> flag and as listed in the
414 <literal>––list-packages</literal> list.
420 <term><literal>auto</literal></term>
421 <indexterm><primary><literal>auto</literal></primary>
422 <secondary>package specification</secondary>
425 <para>Set to <literal>True</literal> if the package should
426 be automatically available (see <xref
427 linkend="using-packages">). This is normally set to
428 <literal>True</literal> for packages which contain
429 hierarchical libraries, because in that case there is no
430 danger of polluting the module namespace.</para>
435 <term><literal>import_dirs</literal></term>
436 <indexterm><primary><literal>import_dirs</literal></primary>
437 <secondary>package specification</secondary></indexterm>
439 <para>A list of directories containing interface files
440 (<literal>.hi</literal> files) for this package.</para>
442 <para>If the package contains profiling libraries, then
443 the interface files for those library modules should have
444 the suffix <literal>.p_hi</literal>. So the package can
445 contain both normal and profiling versions of the same
446 library without conflict (see also
447 <literal>library_dirs</literal> below).</para>
452 <term><literal>source_dirs</literal></term>
453 <indexterm><primary><literal>source_dirs</literal></primary>
454 <secondary>package specification</secondary></indexterm>
456 <para>A list of directories containing Haskell source
457 files for this package. This field isn't used by GHC, but
458 could potentially be used by an all-interpreted system
464 <term><literal>library_dirs</literal></term>
465 <indexterm><primary><literal>library_dirs</literal></primary>
466 <secondary>package specification</secondary></indexterm>
468 <para>A list of directories containing libraries for this
474 <term><literal>hs_libraries</literal></term>
475 <indexterm><primary><literal>hs_libraries</literal></primary>
476 <secondary>package specification</secondary></indexterm>
478 <para>A list of libraries containing Haskell code for this
479 package, with the <literal>.a</literal> or
480 <literal>.dll</literal> suffix omitted. When packages are
481 built as libraries, the
482 <literal>lib</literal> prefix is also omitted.</para>
484 <para>For use with GHCi, each library should have an
485 object file too. The name of the object file does
486 <emphasis>not</emphasis> have a <literal>lib</literal>
487 prefix, and has the normal object suffix for your
490 <para>For example, if we specify a Haskell library as
491 <filename>HSfoo</filename> in the package spec, then the
492 various flavours of library that GHC actually uses will be
496 <term><filename>libHSfoo.a</filename></term>
498 <para>The name of the library on Unix and Windows
499 (mingw) systems. Note that we don't support
500 building dynamic libraries of Haskell code on Unix
505 <term><filename>HSfoo.dll</filename></term>
507 <para>The name of the dynamic library on Windows
508 systems (optional).</para>
512 <term><filename>HSfoo.o</filename></term>
513 <term><filename>HSfoo.obj</filename></term>
515 <para>The object version of the library used by
525 <term><literal>extra_libraries</literal></term>
526 <indexterm><primary><literal>extra_libraries</literal></primary>
527 <secondary>package specification</secondary></indexterm>
529 <para>A list of extra libraries for this package. The
530 difference between <literal>hs_libraries</literal> and
531 <literal>extra_libraries</literal> is that
532 <literal>hs_libraries</literal> normally have several
533 versions, to support profiling, parallel and other build
534 options. The various versions are given different
535 suffixes to distinguish them, for example the profiling
536 version of the standard prelude library is named
537 <filename>libHSstd_p.a</filename>, with the
538 <literal>_p</literal> indicating that this is a profiling
539 version. The suffix is added automatically by GHC for
540 <literal>hs_libraries</literal> only, no suffix is added
542 <literal>extra_libraries</literal>.</para>
544 <para>The libraries listed in
545 <literal>extra_libraries</literal> may be any libraries
546 supported by your system's linker, including dynamic
547 libraries (<literal>.so</literal> on Unix,
548 <literal>.DLL</literal> on Windows).</para>
550 <para>Also, <literal>extra_libraries</literal> are placed
551 on the linker command line after the
552 <literal>hs_libraries</literal> for the same package. If
553 your package has dependencies in the other direction (i.e.
554 <literal>extra_libraries</literal> depends on
555 <literal>hs_libraries</literal>), and the libraries are
556 static, you might need to make two separate
562 <term><literal>include_dirs</literal></term>
563 <indexterm><primary><literal>include_dirs</literal></primary>
564 <secondary>package specification</secondary></indexterm>
566 <para>A list of directories containing C includes for this
567 package (maybe the empty list).</para>
572 <term><literal>c_includes</literal></term>
573 <indexterm><primary><literal>c_includes</literal></primary>
574 <secondary>package specification</secondary></indexterm>
576 <para>A list of files to include for via-C compilations
577 using this package. Typically this include file will
578 contain function prototypes for any C functions used in
579 the package, in case they end up being called as a result
580 of Haskell functions from the package being
586 <term><literal>package_deps</literal></term>
587 <indexterm><primary><literal>package_deps</literal></primary>
588 <secondary>package specification</secondary></indexterm>
590 <para>A list of packages which this package depends
596 <term><literal>extra_ghc_opts</literal></term>
597 <indexterm><primary><literal>extra_ghc_opts</literal></primary>
598 <secondary>package specification</secondary></indexterm>
600 <para>Extra arguments to be added to the GHC command line
601 when this package is being used.</para>
606 <term><literal>extra_cc_opts</literal></term>
607 <indexterm><primary><literal>extra_cc_opts</literal></primary>
608 <secondary>package specification</secondary></indexterm>
610 <para>Extra arguments to be added to the gcc command line
611 when this package is being used (only for via-C
612 compilations).</para>
617 <term><literal>extra_ld_opts</literal></term>
618 <indexterm><primary><literal>extra_ld_opts</literal></primary>
619 <secondary>package specification</secondary></indexterm>
621 <para>Extra arguments to be added to the
622 <command>gcc</command> command line (for linking) when
623 this package is being used.</para>
628 <term><literal>framework_dirs</literal></term>
629 <indexterm><primary><literal>framework_dirs</literal></primary>
630 <secondary>package specification</secondary></indexterm>
632 <para>On Darwin/MacOS X, a list of directories containing frameworks for this
633 package. This corresponds to the <option>-framework-path</option> option.
634 It is ignored on all other platforms.</para>
639 <term><literal>extra_frameworks</literal></term>
640 <indexterm><primary><literal>extra_frameworks</literal></primary>
641 <secondary>package specification</secondary></indexterm>
643 <para>On Darwin/MacOS X, a list of frameworks to link to. This corresponds to the
644 <option>-framework</option> option. Take a look at Apple's developer documentation
645 to find out what frameworks actually are. This entry is ignored on all other platforms.</para>
651 The <literal>ghc-pkg</literal> tool performs expansion of
652 environment variables occurring in input package specifications.
653 So, if the <literal>mypkg</literal> was added to the package
657 $ installdir=/usr/local/lib ghc-pkg -a < mypkg.pkg
661 The occurrence of <literal>${installdir}</literal> is replaced
662 with <literal>/usr/local/lib</literal> in the package data that
663 is added for <literal>mypkg</literal>.
667 This feature enables the distribution of package specification
668 files that can be easily configured when installing.
671 <para>For examples of more package specifications, take a look
672 at the <literal>package.conf</literal> in your GHC
678 ;;; Local Variables: ***
680 ;;; sgml-parent-document: ("using.sgml" "book" "chapter") ***