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>To use a package, add the <literal>-package</literal> flag
29 to the GHC command line:</para>
33 <term><option>-package <replaceable>lib</replaceable></option></term>
34 <indexterm><primary>-package <replaceable>lib</replaceable> option</primary></indexterm>
36 <para>This option brings into scope all the modules from
37 package <literal><replaceable>lib</replaceable></literal> (they still have to
38 be imported in your Haskell source, however). It also
39 causes the relevant libraries to be linked when linking is
45 <para>Some packages depend on other packages, for example the
46 <literal>text</literal> package makes use of some of the modules
47 in the <literal>lang</literal> package. The package system
48 takes care of all these dependencies, so that when you say
49 <literal>-package text</literal> on the command line, you
50 automatically get <literal>-package lang</literal> too.</para>
53 <sect2 id="using-local-packages">
54 <title>Maintaining a local set of packages</title>
56 <para>When GHC starts up, it automatically reads the default set
57 of packages from a configuration file, normally named
58 <filename>package.conf</filename> in your GHC installation
61 <para>You can load in additional package configuration files
62 using the <option>-package-conf</option> option:</para>
66 <term><option>-package-conf <replaceable>file</replaceable></option></term>
67 <indexterm><primary><option>-package-conf <replaceable>file</replaceable></option></primary>
70 <para>Read in the package configuration file
71 <replaceable>file</replaceable> in addition to the system
72 default file. This allows the user to have a local set of
73 packages in addition to the system-wide ones.</para>
78 <para>To create your own package configuration file, just create
79 a new file and put the string
80 <quote><literal>[]</literal></quote> in it. Packages can be
81 added to the new configuration file using the
82 <literal>ghc-pkg</literal> tool, described in <xref
83 linkend="package-management">.</para>
86 <sect2 id="building-packages">
87 <title>Building a package from Haskell source</title>
88 <indexterm><primary>packages</primary>
89 <secondary>building</secondary></indexterm>
91 <para>It takes some special considerations to build a new
96 <para>A package may contain several Haskell modules. A
97 package may span many directories, or many packages may
98 exist in a single directory. Packages may not be mutually
103 <para>A package has a name
104 (e.g. <filename>base</filename>)</para>
108 <para>The Haskell code in a package may be built into one or
109 more archive libraries
110 (e.g. <filename>libHSfoo.a</filename>), or a single DLL on
111 Windows (e.g. <filename>HSfoo.dll</filename>). The
112 restriction to a single DLL on Windows is because the
113 package system is used to tell the compiler when it should
114 make an inter-DLL call rather than an intra-DLL call
115 (inter-DLL calls require an extra
116 indirection). <emphasis>Building packages as DLLs doesn't
117 work at the moment; see <XRef LinkEnd="win32-dlls-create">
118 for the gory details.</emphasis>
121 <para>Building a static library is done by using the
122 <literal>ar</literal> tool, like so:</para>
124 <screen>ar cqs libHSfoo.a A.o B.o C.o ...</screen>
126 <para>where <filename>A.o</filename>,
127 <filename>B.o</filename> and so on are the compiled Haskell
128 modules, and <filename>libHSfoo.a</filename> is the library
129 you wish to create. The syntax may differ slightly on your
130 system, so check the documentation if you run into
133 <para>Versions of the Haskell libraries for use with GHCi
134 may also be included: GHCi cannot load <literal>.a</literal>
135 files directly, instead it will look for an object file
136 called <filename>HSfoo.o</filename> and load that. On some
137 systems, the <literal>ghc-pkg</literal> tool can
138 automatically build the GHCi version of each library, see
139 <xref linkend="package-management">. To build these
140 libraries by hand from the <literal>.a</literal> archive, it
141 is possible to use GNU <command>ld</command> as
144 <screen>ld -r ––whole-archive -o HSfoo.o libHSfoo.a</screen>
148 <para>GHC does not maintain detailed cross-package
149 dependency information. It does remember which modules in
150 other packages the current module depends on, but not which
151 things within those imported things.</para>
155 <para>To compile a module which is to be part of a new package,
156 use the <literal>-package-name</literal> option:</para>
160 <term><option>-package-name <replaceable>foo</replaceable></option></term>
161 <indexterm><primary><literal>-package-name</literal></primary>
162 <secondary>option</secondary></indexterm>
164 <para>This option is added to the command line when
165 compiling a module that is destined to be part of package
166 <literal>foo</literal>. If this flag is omitted then the
167 default package <literal>Main</literal> is assumed.</para>
172 <para>Failure to use the <literal>-package-name</literal> option
173 when compiling a package will result in disaster on Windows, but
174 is relatively harmless on Unix at the moment (it will just cause
175 a few extra dependencies in some interface files). However,
176 bear in mind that we might add support for Unix shared libraries
177 at some point in the future.</para>
179 <para>It is worth noting that on Windows, when each package
180 is built as a DLL, since a reference to a DLL costs an extra
181 indirection, intra-package references are cheaper than
182 inter-package references. Of course, this applies to the
183 <filename>Main</filename> package as well.</para>
186 <sect2 id="package-management">
187 <title>Package management</title>
188 <indexterm><primary>packages</primary>
189 <secondary>management</secondary></indexterm>
191 <para>The <literal>ghc-pkg</literal> tool allows packages to be
192 added or removed from a package configuration file. By default,
193 the system-wide configuration file is used, but alternatively
194 packages can be added, updated or removed from a user-specified
195 configuration file using the <option>––config-file</option>
196 option. An empty package configuration file consists of the
197 string <quote><literal>[]</literal></quote>.</para>
199 <para>The <literal>ghc-pkg</literal> program accepts the
200 following options:</para>
204 <term><option>––add-package</option></term>
205 <term><option>-a</option></term>
206 <indexterm><primary><option>––add-package</option></primary></indexterm>
208 <para>Reads package specification from the input (see below),
209 and adds it to the database of installed packages. The
210 package specification must be a package that isn't already
216 <term><option>––input-file=<replaceable>file</replaceable></option></term>
217 <term><option>-i <replaceable>file</replaceable></option></term>
218 <indexterm><primary><option>––input-file</option></primary></indexterm>
220 <para>Read new package specifications from file
221 <replaceable>file</replaceable>. If a value of
222 <filename>"-"</filename> is given, standard input is used.
223 If no <option>-i</option> is present on the command-line,
224 an input file of <filename>"-"</filename> is assumed.
230 <term><option>––auto-ghci-libs</option></term>
231 <term><option>-g</option></term>
232 <indexterm><primary><option>––auto-ghci-libs</option></primary>
235 <para>Automatically generate the GHCi
236 <filename>.o</filename> version of each
237 <filename>.a</filename> Haskell library, using GNU ld (if
238 that is available). Without this option,
239 <literal>ghc-pkg</literal> will warn if GHCi versions of
240 any Haskell libraries in the package don't exist.</para>
242 <para>GHCi <literal>.o</literal> libraries don't
243 necessarily have to live in the same directory as the
244 corresponding <literal>.a</literal> library. However,
245 this option will cause the GHCi library to be created in
246 the same directory as the <literal>.a</literal>
252 <term><option>––config-file <replaceable>file</replaceable></option></term>
253 <term><option>-f <replaceable>file</replaceable></option></term>
254 <indexterm><primary><option>––config-file</option></primary>
257 <para>Use <replaceable>file</replaceable> instead of the
258 default package configuration file. This, in conjunction
259 with GHC's <option>-package-conf</option> option, allows
260 a user to have a local set of packages in addition to the
261 system-wide installed set.</para>
266 <term><option>––list-packages</option></term>
267 <term><option>-l</option></term>
268 <indexterm><primary><option>––list-packages</option></primary></indexterm>
270 <para>This option displays the list of currently installed
274 $ ghc-pkg ––list-packages
275 gmp, rts, std, lang, concurrent, data, net, posix, text, util
278 <para>Note that your GHC installation might have a
279 slightly different set of packages installed.</para>
281 <para>The <literal>gmp</literal> and
282 <literal>rts</literal> packages are always present, and
283 represent the multi-precision integer and runtime system
284 libraries respectively. The <literal>std</literal>
285 package contains the Haskell prelude and standard
286 libraries. The rest of the packages are optional
292 <term><option>––remove-package <replaceable>foo</replaceable></option></term>
293 <term><option>-r <replaceable>foo</replaceable></option></term>
294 <indexterm><primary><option>––delete-package</option></primary>
297 <para>Removes the specified package from the installed
298 configuration.</para>
302 <term><option>––update-package</option></term>
303 <term><option>-u</option></term>
304 <indexterm><primary><option>––update-package</option></primary></indexterm>
306 <para>Reads package specification from the input, and
307 adds it to the database of installed packages. If a package
308 with the same name is already installed, its configuration
309 data is replaced with the new information. If the package
310 doesn't already exist, it's added.
315 <term><option>––force</option></term>
316 <indexterm><primary><option>––force</option></primary></indexterm>
318 <para>Causes <literal>ghc-pkg</literal> to ignore missing
319 directories and libraries when adding a package, and just
320 go ahead and add it anyway. This might be useful if your
321 package installation system needs to add the package to
322 GHC before building and installing the files.</para>
327 <para>When modifying the configuration file
328 <replaceable>file</replaceable>, a copy of the original file is
329 saved in <replaceable>file</replaceable><literal>.old</literal>,
330 so in an emergency you can always restore the old settings by
331 copying the old file back again.</para>
333 <para>A package specification looks like this:</para>
338 import_dirs = ["${installdir}/imports/mypkg"],
340 library_dirs = ["${installdir}"],
341 hs_libraries = ["HSmypkg" ],
342 extra_libraries = ["HSmypkg_cbits"],
344 c_includes = ["HsMyPkg.h"],
345 package_deps = ["text", "data"],
348 extra_ld_opts = ["-lmy_clib"]
352 <para>Components of a package specification may be specified in
353 any order, and are:</para>
357 <term><literal>name</literal></term>
358 <indexterm><primary><literal>name</literal></primary>
359 <secondary>package specification</secondary></indexterm>
361 <para>The package's name, for use with
362 the <literal>-package</literal> flag and as listed in the
363 <literal>––list-packages</literal> list.
369 <term><literal>import_dirs</literal></term>
370 <indexterm><primary><literal>import_dirs</literal></primary>
371 <secondary>package specification</secondary></indexterm>
373 <para>A list of directories containing interface files
374 (<literal>.hi</literal> files) for this package.</para>
379 <term><literal>source_dirs</literal></term>
380 <indexterm><primary><literal>source_dirs</literal></primary>
381 <secondary>package specification</secondary></indexterm>
383 <para>A list of directories containing Haskell source
384 files for this package. This field isn't used by GHC, but
385 could potentially be used by an all-interpreted system
391 <term><literal>library_dirs</literal></term>
392 <indexterm><primary><literal>library_dirs</literal></primary>
393 <secondary>package specification</secondary></indexterm>
395 <para>A list of directories containing libraries for this
401 <term><literal>hs_libraries</literal></term>
402 <indexterm><primary><literal>hs_libraries</literal></primary>
403 <secondary>package specification</secondary></indexterm>
405 <para>A list of libraries containing Haskell code for this
406 package, with the <literal>.a</literal> or
407 <literal>.dll</literal> suffix omitted. When packages are
408 built as libraries, the
409 <literal>lib</literal> prefix is also omitted.</para>
411 <para>For use with GHCi, each library should have an
412 object file too. The name of the object file does
413 <emphasis>not</emphasis> have a <literal>lib</literal>
414 prefix, and has the normal object suffix for your
417 <para>For example, if we specify a Haskell library as
418 <filename>HSfoo</filename> in the package spec, then the
419 various flavours of library that GHC actually uses will be
423 <term><filename>libHSfoo.a</filename></term>
425 <para>The name of the library on Unix and Windows
426 (mingw) systems. Note that we don't support
427 building dynamic libraries of Haskell code on Unix
432 <term><filename>HSfoo.dll</filename></term>
434 <para>The name of the dynamic library on Windows
435 systems (optional).</para>
439 <term><filename>HSfoo.o</filename></term>
440 <term><filename>HSfoo.obj</filename></term>
442 <para>The object version of the library used by
452 <term><literal>extra_libraries</literal></term>
453 <indexterm><primary><literal>extra_libraries</literal></primary>
454 <secondary>package specification</secondary></indexterm>
456 <para>A list of extra libraries for this package. The
457 difference between <literal>hs_libraries</literal> and
458 <literal>extra_libraries</literal> is that
459 <literal>hs_libraries</literal> normally have several
460 versions, to support profiling, parallel and other build
461 options. The various versions are given different
462 suffixes to distinguish them, for example the profiling
463 version of the standard prelude library is named
464 <filename>libHSstd_p.a</filename>, with the
465 <literal>_p</literal> indicating that this is a profiling
466 version. The suffix is added automatically by GHC for
467 <literal>hs_libraries</literal> only, no suffix is added
469 <literal>extra_libraries</literal>.</para>
471 <para>The libraries listed in
472 <literal>extra_libraries</literal> may be any libraries
473 supported by your system's linker, including dynamic
474 libraries (<literal>.so</literal> on Unix,
475 <literal>.DLL</literal> on Windows).</para>
477 <para>Also, <literal>extra_libraries</literal> are placed
478 on the linker command line after the
479 <literal>hs_libraries</literal> for the same package. If
480 your package has dependencies in the other direction (i.e.
481 <literal>extra_libraries</literal> depends on
482 <literal>hs_libraries</literal>), and the libraries are
483 static, you might need to make two separate
489 <term><literal>include_dirs</literal></term>
490 <indexterm><primary><literal>include_dirs</literal></primary>
491 <secondary>package specification</secondary></indexterm>
493 <para>A list of directories containing C includes for this
494 package (maybe the empty list).</para>
499 <term><literal>c_includes</literal></term>
500 <indexterm><primary><literal>c_includes</literal></primary>
501 <secondary>package specification</secondary></indexterm>
503 <para>A list of files to include for via-C compilations
504 using this package. Typically this include file will
505 contain function prototypes for any C functions used in
506 the package, in case they end up being called as a result
507 of Haskell functions from the package being
513 <term><literal>package_deps</literal></term>
514 <indexterm><primary><literal>package_deps</literal></primary>
515 <secondary>package specification</secondary></indexterm>
517 <para>A list of packages which this package depends
523 <term><literal>extra_ghc_opts</literal></term>
524 <indexterm><primary><literal>extra_ghc_opts</literal></primary>
525 <secondary>package specification</secondary></indexterm>
527 <para>Extra arguments to be added to the GHC command line
528 when this package is being used.</para>
533 <term><literal>extra_cc_opts</literal></term>
534 <indexterm><primary><literal>extra_cc_opts</literal></primary>
535 <secondary>package specification</secondary></indexterm>
537 <para>Extra arguments to be added to the gcc command line
538 when this package is being used (only for via-C
539 compilations).</para>
544 <term><literal>extra_ld_opts</literal></term>
545 <indexterm><primary><literal>extra_ld_opts</literal></primary>
546 <secondary>package specification</secondary></indexterm>
548 <para>Extra arguments to be added to the gcc command line
549 (for linking) when this package is being used.</para>
554 <term><literal>framework_dirs</literal></term>
555 <indexterm><primary><literal>framework_dirs</literal></primary>
556 <secondary>package specification</secondary></indexterm>
558 <para>On Darwin/MacOS X, a list of directories containing frameworks for this
559 package. This corresponds to the <option>-framework-path</option> option.
560 It is ignored on all other platforms.</para>
565 <term><literal>extra_frameworks</literal></term>
566 <indexterm><primary><literal>extra_frameworks</literal></primary>
567 <secondary>package specification</secondary></indexterm>
569 <para>On Darwin/MacOS X, a list of frameworks to link to. This corresponds to the
570 <option>-framework</option> option. Take a look at Apple's developer documentation
571 to find out what frameworks actually are. This entry is ignored on all other platforms.</para>
577 The <literal>ghc-pkg</literal> tool performs expansion of
578 environment variables occurring in input package specifications.
579 So, if the <literal>mypkg</literal> was added to the package
583 $ installdir=/usr/local/lib ghc-pkg -a < mypkg.pkg
587 The occurrence of <literal>${installdir}</literal> is replaced
588 with <literal>/usr/local/lib</literal> in the package data that
589 is added for <literal>mypkg</literal>.
593 This feature enables the distribution of package specification
594 files that can be easily configured when installing.
597 <para>For examples of more package specifications, take a look
598 at the <literal>package.conf</literal> in your GHC
604 ;;; Local Variables: ***
606 ;;; sgml-parent-document: ("using.sgml" "book" "chapter") ***