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 <xref
17 linkend="book-hslibs">), and packages can be added/removed from an
18 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="building-packages">
54 <title>Building a package from Haskell source</title>
55 <indexterm><primary>packages</primary>
56 <secondary>building</secondary></indexterm>
58 <para>It takes some special considerations to build a new
63 <para>A package may contain several Haskell modules. A
64 package may span many directories, or many packages may
65 exist in a single directory. Packages may not be mutually
70 <para>A package has a name
71 (e.g. <filename>std</filename>)</para>
75 <para>The Haskell code in a package may be built into one or
76 more Unix libraries (e.g. <filename>libHSfoo.a</filename>),
77 or a single DLL on Windows
78 (e.g. <filename>HSfoo.dll</filename>). The restriction to a
79 single DLL on Windows is that the package system is used to
80 tell the compiler when it should make an inter-DLL call
81 rather than an intra-DLL call (inter-DLL calls require an
82 extra indirection).</para>
84 <para>Versions of the Haskell libraries for use with GHCi
85 may also be included: GHCi cannot load <literal>.a</literal>
86 files directly, instead it will look for an object file
87 called <filename>HSfoo.o</filename> (the object suffix
88 varies between platforms, as usual) and load that. An
89 object file can be built from a <literal>.a</literal>
90 archive as follows (using GNU <command>ld</command> on
94 ld -r --whole-archive -o HSfoo.o libHSfoo.a
99 <para>GHC does not maintain detailed cross-package
100 dependency information. It does remember which modules in
101 other packages the current module depends on, but not which
102 things within those imported things.</para>
106 <para>To compile a module which is to be part of a new package,
107 use the <literal>-package-name</literal> option:</para>
111 <term><option>-package-name <replaceable>foo</replaceable></option></term>
112 <indexterm><primary><literal>-package-name</literal></primary>
113 <secondary>option</secondary></indexterm>
115 <para>This option is added to the command line when
116 compiling a module that is destined to be part of package
117 <literal>foo</literal>. If this flag is omitted then the
118 default package <literal>Main</literal> is assumed.</para>
123 <para>Failure to use the <literal>-package-name</literal> option
124 when compiling a package will result in disaster on Windows, but
125 is relatively harmless on Unix at the moment (it will just cause
126 a few extra dependencies in some interface files). However,
127 bear in mind that we might add support for Unix shared libraries
128 at some point in the future.</para>
130 <para>It is worth noting that on Windows, because each package
131 is built as a DLL, and a reference to a DLL costs an extra
132 indirection, intra-package references are cheaper than
133 inter-package references. Of course, this applies to the
134 <filename>Main</filename> package as well.</para>
137 <sect2 id="package-management">
138 <title>Package management</title>
139 <indexterm><primary>packages</primary>
140 <secondary>management</secondary></indexterm>
142 <para>GHC uses a package configuration file, called
143 <literal>packages.conf</literal>, which can be found in your GHC
144 install directory. This file isn't intended to be edited
145 directly, instead packages can be added or removed using GHC's
146 package management tool, <literal>ghc-pkg</literal>.</para>
150 <term><option>--list-packages</option></term>
151 <term><option>-l</option></term>
152 <indexterm><primary><option>--list-packages</option></primary></indexterm>
154 <para>This option displays the list of currently installed
158 $ ghc-pkg --list-packages
159 gmp, rts, std, lang, concurrent, data, net, posix, text, util
162 <para>Note that your GHC installation might have a
163 slightly different set of packages installed.</para>
165 <para>The <literal>gmp</literal> and
166 <literal>rts</literal> packages are always present, and
167 represent the multi-precision integer and runtime system
168 libraries respectively. The <literal>std</literal>
169 package contains the Haskell prelude and standard
170 libraries. The rest of the packages are optional
176 <term><option>--add-package</option></term>
177 <term><option>-a</option></term>
178 <indexterm><primary><option>--add-package</option></primary>
181 <para>Reads a package specification (see below) on stdin,
182 and adds it to the database of installed packages. The
183 package specification must be a package that isn't already
189 <term><option>--remove-package <replaceable>foo</replaceable></option></term>
190 <term><option>-r <replaceable>foo</replaceable></option></term>
191 <indexterm><primary><option>--delete-package</option></primary>
194 <para>Removes the specified package from the installed
195 configuration.</para>
200 <para>In both cases, the old package configuration file is saved
201 in <literal>packages.conf.old</literal> in your GHC install
202 directory, so in an emergency you can always copy this file into
203 <literal>package.conf</literal> to restore the old
206 <para>A package specification looks like this:</para>
211 import_dirs = ["/usr/local/lib/imports/mypkg"],
213 library_dirs = ["/usr/local/lib"],
214 hs_libraries = ["HSmypkg" ],
215 extra_libraries = ["HSmypkg_cbits"],
217 c_includes = ["HsMyPkg.h"],
218 package_deps = ["text", "data"],
221 extra_ld_opts = ["-lmy_clib"]
225 <para>Components of a package specification may be specified in
226 any order, and are:</para>
230 <term><literal>name</literal></term>
231 <indexterm><primary><literal>name</literal></primary>
232 <secondary>package specification</secondary></indexterm>
234 <para>The package's name, for use with
235 the <literal>-package</literal> flag and as listed in the
236 <literal>--list-packages</literal> list.
242 <term><literal>import_dirs</literal></term>
243 <indexterm><primary><literal>import_dirs</literal></primary>
244 <secondary>package specification</secondary></indexterm>
246 <para>A list of directories containing interface files
247 (<literal>.hi</literal> files) for this package.</para>
252 <term><literal>source_dirs</literal></term>
253 <indexterm><primary><literal>source_dirs</literal></primary>
254 <secondary>package specification</secondary></indexterm>
256 <para>A list of directories containing Haskell source
257 files for this package. This field isn't used by GHC, but
258 could potentially be used by an all-interpreted system
264 <term><literal>library_dirs</literal></term>
265 <indexterm><primary><literal>library_dirs</literal></primary>
266 <secondary>package specification</secondary></indexterm>
268 <para>A list of directories containing libraries for this
274 <term><literal>hs_libraries</literal></term>
275 <indexterm><primary><literal>hs_libraries</literal></primary>
276 <secondary>package specification</secondary></indexterm>
278 <para>A list of libraries containing Haskell code for this
279 package, with the <literal>.a</literal> or
280 <literal>.dll</literal> suffix omitted. On Unix, the
281 <literal>lib</literal> prefix is also omitted.</para>
283 <para>For use with GHCi, each library should have an
284 object file too. The name of the object file does
285 <emphasis>not</emphasis> have a <literal>lib</literal>
286 prefix, and has the normal object suffix for your
289 <para>For example, if we specify a Haskell library as
290 <filename>HSfoo</filename> in the package spec, then the
291 various flavours of library that GHC actually uses will be
295 <term><filename>libHSfoo.a</filename></term>
297 <para>The name of the library on Unix
302 <term><filename>HSfoo.dll</filename></term>
304 <para>The name of the dynamic library on Windows
309 <term><filename>HSfoo.o</filename></term>
310 <term><filename>HSfoo.obj</filename></term>
312 <para>The object version of the library used by
322 <term><literal>extra_libraries</literal></term>
323 <indexterm><primary><literal>extra_libraries</literal></primary>
324 <secondary>package specification</secondary></indexterm>
326 <para>A list of extra libraries for this package. The
327 difference between <literal>hs_libraries</literal> and
328 <literal>extra_libraries</literal> is that
329 <literal>hs_libraries</literal> normally have several
330 versions, to support profiling, parallel and other build
331 options. The various versions are given different
332 suffixes to distinguish them, for example the profiling
333 version of the standard prelude library is named
334 <filename>libHSstd_p.a</filename>, with the
335 <literal>_p</literal> indicating that this is a profiling
336 version. The suffix is added automatically by GHC for
337 <literal>hs_libraries</literal> only, no suffix is added
339 <literal>extra_libraries</literal>.</para>
341 <para>Also, <literal>extra_libraries</literal> are placed
342 on the linker command line after the
343 <literal>hs_libraries</literal> for the same package. If
344 your package has dependencies in the other direction (i.e.
345 <literal>extra_libraries</literal> depends on
346 <literal>hs_libraries</literal>), and the libraries are
347 static, you might need to make two separate
353 <term><literal>include_dirs</literal></term>
354 <indexterm><primary><literal>include_dirs</literal></primary>
355 <secondary>package specification</secondary></indexterm>
357 <para>A list of directories containing C includes for this
358 package (maybe the empty list).</para>
363 <term><literal>c_includes</literal></term>
364 <indexterm><primary><literal>c_includes</literal></primary>
365 <secondary>package specification</secondary></indexterm>
367 <para>A list of files to include for via-C compilations
368 using this package. Typically this include file will
369 contain function prototypes for any C functions used in
370 the package, in case they end up being called as a result
371 of Haskell functions from the package being
377 <term><literal>package_deps</literal></term>
378 <indexterm><primary><literal>package_deps</literal></primary>
379 <secondary>package specification</secondary></indexterm>
381 <para>A list of packages which this package depends
387 <term><literal>extra_ghc_opts</literal></term>
388 <indexterm><primary><literal>extra_ghc_opts</literal></primary>
389 <secondary>package specification</secondary></indexterm>
391 <para>Extra arguments to be added to the GHC command line
392 when this package is being used.</para>
397 <term><literal>extra_cc_opts</literal></term>
398 <indexterm><primary><literal>extra_cc_opts</literal></primary>
399 <secondary>package specification</secondary></indexterm>
401 <para>Extra arguments to be added to the gcc command line
402 when this package is being used (only for via-C
403 compilations).</para>
408 <term><literal>extra_ld_opts</literal></term>
409 <indexterm><primary><literal>extra_ld_opts</literal></primary>
410 <secondary>package specification</secondary></indexterm>
412 <para>Extra arguments to be added to the gcc command line
413 (for linking) when this package is being used.</para>
418 <para>For examples of more package specifications, take a look
419 at the <literal>package.conf</literal> in your GHC
425 ;;; Local Variables: ***
427 ;;; sgml-parent-document: ("using.sgml" "book" "chapter") ***