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>
86 <para>GHC does not maintain detailed cross-package
87 dependency information. It does remember which modules in
88 other packages the current module depends on, but not which
89 things within those imported things.</para>
93 <para>To compile a module which is to be part of a new package,
94 use the <literal>-package-name</literal> option:</para>
98 <term><option>-package-name <replaceable>foo</replaceable></option></term>
99 <indexterm><primary><literal>-package-name</literal></primary>
100 <secondary>option</secondary></indexterm>
102 <para>This option is added to the command line when
103 compiling a module that is destined to be part of package
104 <literal>foo</literal>. If this flag is omitted then the
105 default package <literal>Main</literal> is assumed.</para>
110 <para>Failure to use the <literal>-package-name</literal> option
111 when compiling a package will result in disaster on Windows, but
112 is relatively harmless on Unix at the moment (it will just cause
113 a few extra dependencies in some interface files). However,
114 bear in mind that we might add support for Unix shared libraries
115 at some point in the future.</para>
117 <para>It is worth noting that on Windows, because each package
118 is built as a DLL, and a reference to a DLL costs an extra
119 indirection, intra-package references are cheaper than
120 inter-package references. Of course, this applies to the
121 <filename>Main</filename> package as well.</para>
124 <sect2 id="package-management">
125 <title>Package management</title>
126 <indexterm><primary>packages</primary>
127 <secondary>management</secondary></indexterm>
129 <para>GHC uses a package configuration file, called
130 <literal>packages.conf</literal>, which can be found in your GHC
131 install directory. This file isn't intended to be edited
132 directly, instead packages can be added or removed using GHC's
133 package management tool, <literal>ghc-pkg</literal>.</para>
137 <term><option>--list-packages</option></term>
138 <term><option>-l</option></term>
139 <indexterm><primary><option>--list-packages</option></primary></indexterm>
141 <para>This option displays the list of currently installed
145 $ ghc-pkg --list-packages
146 gmp, rts, std, lang, concurrent, data, net, posix, text, util
149 <para>Note that your GHC installation might have a
150 slightly different set of packages installed.</para>
152 <para>The <literal>gmp</literal> and
153 <literal>rts</literal> packages are always present, and
154 represent the multi-precision integer and runtime system
155 libraries respectively. The <literal>std</literal>
156 package contains the Haskell prelude and standard
157 libraries. The rest of the packages are optional
163 <term><option>--add-package</option></term>
164 <term><option>-a</option></term>
165 <indexterm><primary><option>--add-package</option></primary>
168 <para>Reads a package specification (see below) on stdin,
169 and adds it to the database of installed packages. The
170 package specification must be a package that isn't already
176 <term><option>--remove-package <replaceable>foo</replaceable></option></term>
177 <term><option>-r <replaceable>foo</replaceable></option></term>
178 <indexterm><primary><option>--delete-package</option></primary>
181 <para>Removes the specified package from the installed
182 configuration.</para>
187 <para>In both cases, the old package configuration file is saved
188 in <literal>packages.conf.old</literal> in your GHC install
189 directory, so in an emergency you can always copy this file into
190 <literal>package.conf</literal> to restore the old
193 <para>A package specification looks like this:</para>
198 import_dirs = ["/usr/local/lib/imports/mypkg"],
200 library_dirs = ["/usr/local/lib"],
201 hs_libraries = ["HSmypkg" ],
202 extra_libraries = ["HSmypkg_cbits"],
204 c_includes = ["HsMyPkg.h"],
205 package_deps = ["text", "data"],
208 extra_ld_opts = ["-lmy_clib"]
212 <para>Components of a package specification may be specified in
213 any order, and are:</para>
217 <term><literal>name</literal></term>
218 <indexterm><primary><literal>name</literal></primary>
219 <secondary>package specification</secondary></indexterm>
221 <para>The package's name, for use with
222 the <literal>-package</literal> flag and as listed in the
223 <literal>--list-packages</literal> list.
229 <term><literal>import_dirs</literal></term>
230 <indexterm><primary><literal>import_dirs</literal></primary>
231 <secondary>package specification</secondary></indexterm>
233 <para>A list of directories containing interface files
234 (<literal>.hi</literal> files) for this package.</para>
239 <term><literal>source_dirs</literal></term>
240 <indexterm><primary><literal>source_dirs</literal></primary>
241 <secondary>package specification</secondary></indexterm>
243 <para>A list of directories containing Haskell source
244 files for this package. This field isn't used by GHC, but
245 could potentially be used by an all-interpreted system
251 <term><literal>library_dirs</literal></term>
252 <indexterm><primary><literal>library_dirs</literal></primary>
253 <secondary>package specification</secondary></indexterm>
255 <para>A list of directories containing libraries for this
261 <term><literal>hs_libraries</literal></term>
262 <indexterm><primary><literal>hs_libraries</literal></primary>
263 <secondary>package specification</secondary></indexterm>
265 <para>A list of libraries containing Haskell code for this
266 package, with the <literal>.a</literal> or
267 <literal>.dll</literal> suffix omitted. On Unix, the
268 <literal>lib</literal> prefix is also omitted.</para>
273 <term><literal>extra_libraries</literal></term>
274 <indexterm><primary><literal>extra_libraries</literal></primary>
275 <secondary>package specification</secondary></indexterm>
277 <para>A list of extra libraries for this package. The
278 difference between <literal>hs_libraries</literal> and
279 <literal>extra_libraries</literal> is that
280 <literal>hs_libraries</literal> normally have several
281 versions, to support profiling, parallel and other build
282 options. The various versions are given different
283 suffixes to distinguish them, for example the profiling
284 version of the standard prelude library is named
285 <filename>libHSstd_p.a</filename>, with the
286 <literal>_p</literal> indicating that this is a profiling
287 version. The suffix is added automatically by GHC for
288 <literal>hs_libraries</literal> only, no suffix is added
290 <literal>extra_libraries</literal>.</para>
292 <para>Also, <literal>extra_libraries</literal> are placed
293 on the linker command line before the
294 <literal>hs_libraries</literal> for the same package. If
295 your package has dependencies in the other direction, you
296 might need to make two separate packages.</para>
301 <term><literal>include_dirs</literal></term>
302 <indexterm><primary><literal>include_dirs</literal></primary>
303 <secondary>package specification</secondary></indexterm>
305 <para>A list of directories containing C includes for this
306 package (maybe the empty list).</para>
311 <term><literal>c_includes</literal></term>
312 <indexterm><primary><literal>c_includes</literal></primary>
313 <secondary>package specification</secondary></indexterm>
315 <para>A list of files to include for via-C compilations
316 using this package. Typically this include file will
317 contain function prototypes for any C functions used in
318 the package, in case they end up being called as a result
319 of Haskell functions from the package being
325 <term><literal>package_deps</literal></term>
326 <indexterm><primary><literal>package_deps</literal></primary>
327 <secondary>package specification</secondary></indexterm>
329 <para>A list of packages which this package depends
335 <term><literal>extra_ghc_opts</literal></term>
336 <indexterm><primary><literal>extra_ghc_opts</literal></primary>
337 <secondary>package specification</secondary></indexterm>
339 <para>Extra arguments to be added to the GHC command line
340 when this package is being used.</para>
345 <term><literal>extra_cc_opts</literal></term>
346 <indexterm><primary><literal>extra_cc_opts</literal></primary>
347 <secondary>package specification</secondary></indexterm>
349 <para>Extra arguments to be added to the gcc command line
350 when this package is being used (only for via-C
351 compilations).</para>
356 <term><literal>extra_ld_opts</literal></term>
357 <indexterm><primary><literal>extra_ld_opts</literal></primary>
358 <secondary>package specification</secondary></indexterm>
360 <para>Extra arguments to be added to the gcc command line
361 (for linking) when this package is being used.</para>
366 <para>For examples of more package specifications, take a look
367 at the <literal>package.conf</literal> in your GHC
373 ;;; Local Variables: ***
375 ;;; sgml-parent-document: ("using.sgml" "book" "chapter") ***