4 Lets assume we have created a project directory and already have a
7 Every project needs a name, we'll call this example "proglet".
17 It is assumed that (apart from external dependencies) all the files that
18 make up a package live under a common project root directory. This
19 simple example has all the project files in one directory, but most
20 packages will use one or more subdirectories.
22 To turn this into a Cabal package we need two extra files in the
23 project's root directory:
25 - ``proglet.cabal``: containing package metadata and build information.
27 - ``Setup.hs``: usually containing a few standardized lines of code,
28 but can be customized if necessary.
30 We can create both files manually or we can use ``cabal init`` to create
36 The ``cabal init`` command is interactive. It asks us a number of
37 questions starting with the package name and version.
42 Package name [default "proglet"]?
43 Package version [default "0.1"]?
46 It also asks questions about various other bits of package metadata. For
47 a package that you never intend to distribute to others, these fields
50 One of the important questions is whether the package contains a library
51 or an executable. Libraries are collections of Haskell modules that can
52 be re-used by other Haskell libraries and programs, while executables
53 are standalone programs.
57 What does the package build:
62 For the moment these are the only choices. For more complex packages
63 (e.g. a library and multiple executables or test suites) the ``.cabal``
64 file can be edited afterwards.
66 Finally, ``cabal init`` creates the initial ``proglet.cabal`` and
67 ``Setup.hs`` files, and depending on your choice of license, a
68 ``LICENSE`` file as well.
73 Generating Setup.hs...
74 Generating proglet.cabal...
76 You may want to edit the .cabal file and add a Description field.
78 As this stage the ``proglet.cabal`` is not quite complete and before you
79 are able to build the package you will need to edit the file and add
80 some build information about the library or executable.
82 Editing the .cabal file
83 -----------------------
87 Load up the ``.cabal`` file in a text editor. The first part of the
88 ``.cabal`` file has the package metadata and towards the end of the file
89 you will find the :pkg-section:`executable` or :pkg-section:`library` section.
91 You will see that the fields that have yet to be filled in are commented
92 out. Cabal files use "``--``" Haskell-style comment syntax. (Note that
93 comments are only allowed on lines on their own. Trailing comments on
94 other lines are not allowed because they could be confused with program
97 If you selected earlier to create a library package then your ``.cabal``
98 file will have a section that looks like this:
103 exposed-modules: Proglet
107 Alternatively, if you selected an executable then there will be a
117 The build information fields listed (but commented out) are just the few
118 most important and common fields. There are many others that are covered
119 later in this chapter.
121 Most of the build information fields are the same between libraries and
122 executables. The difference is that libraries have a number of "exposed"
123 modules that make up the public interface of the library, while
124 executables have a file containing a ``Main`` module.
126 The name of a library always matches the name of the package, so it is
127 not specified in the library section. Executables often follow the name
128 of the package too, but this is not required and the name is given
131 Modules included in the package
132 -------------------------------
134 For a library, ``cabal init`` looks in the project directory for files
135 that look like Haskell modules and adds all the modules to the
136 :pkg-field:`library:exposed-modules` field. For modules that do not form part
137 of your package's public interface, you can move those modules to the
138 :pkg-field:`other-modules` field. Either way, all modules in the library need
141 For an executable, ``cabal init`` does not try to guess which file
142 contains your program's ``Main`` module. You will need to fill in the
143 :pkg-field:`executable:main-is` field with the file name of your program's
144 ``Main`` module (including ``.hs`` or ``.lhs`` extension). Other modules
145 included in the executable should be listed in the :pkg-field:`other-modules`
148 Modules imported from other packages
149 ------------------------------------
151 While your library or executable may include a number of modules, it
152 almost certainly also imports a number of external modules from the
153 standard libraries or other pre-packaged libraries. (These other
154 libraries are of course just Cabal packages that contain a library.)
156 You have to list all of the library packages that your library or
157 executable imports modules from. Or to put it another way: you have to
158 list all the other packages that your package depends on.
160 For example, suppose the example ``Proglet`` module imports the module
161 ``Data.Map``. The ``Data.Map`` module comes from the ``containers``
162 package, so we must list it:
167 exposed-modules: Proglet
169 build-depends: containers, base == 4.*
171 In addition, almost every package also depends on the ``base`` library
172 package because it exports the standard ``Prelude`` module plus other
173 basic modules like ``Data.List``.
175 You will notice that we have listed ``base == 4.*``. This gives a
176 constraint on the version of the base package that our package will work
177 with. The most common kinds of constraints are:
180 - ``pkgname >= n && < m``
183 The last is just shorthand, for example ``base == 4.*`` means exactly
184 the same thing as ``base >= 4 && < 5``.
189 For simple packages that's it! We can now try configuring and building
192 .. code-block:: console
197 Assuming those two steps worked then you can also install the package:
199 .. code-block:: console
203 For libraries this makes them available for use in GHCi or to be used by
204 other packages. For executables it installs the program so that you can
205 run it (though you may first need to adjust your system's ``$PATH``).
210 What we have covered so far should be enough for very simple packages
211 that you use on your own system.
213 The next few sections cover more details needed for more complex
214 packages and details needed for distributing packages to other people.
216 The previous chapter covers building and installing packages -- your own
217 packages or ones developed by other people.
222 Before diving into the details of writing packages it helps to
223 understand a bit about packages in the Haskell world and the particular
224 approach that Cabal takes.
226 The point of packages
227 ---------------------
229 Packages are a mechanism for organising and distributing code. Packages
230 are particularly suited for "programming in the large", that is building
231 big systems by using and re-using code written by different people at
234 People organise code into packages based on functionality and
235 dependencies. Social factors are also important: most packages have a
236 single author, or a relatively small team of authors.
238 Packages are also used for distribution: the idea is that a package can
239 be created in one place and be moved to a different computer and be
240 usable in that different environment. There are a surprising number of
241 details that have to be got right for this to work, and a good package
242 system helps to simply this process and make it reliable.
244 Packages come in two main flavours: libraries of reusable code, and
245 complete programs. Libraries present a code interface, an API, while
246 programs can be run directly. In the Haskell world, library packages
247 expose a set of Haskell modules as their public interface. Cabal
248 packages can contain a library or executables or both.
250 Some programming languages have packages as a builtin language concept.
251 For example in Java, a package provides a local namespace for types and
252 other definitions. In the Haskell world, packages are not a part of the
253 language itself. Haskell programs consist of a number of modules, and
254 packages just provide a way to partition the modules into sets of
255 related functionality. Thus the choice of module names in Haskell is
256 still important, even when using packages.
258 Package names and versions
259 --------------------------
261 All packages have a name, e.g. "HUnit". Package names are assumed to be
262 unique. Cabal package names may contain letters, numbers and hyphens,
263 but not spaces and may also not contain a hyphened section consisting of
264 only numbers. The namespace for Cabal packages is flat, not
267 Packages also have a version, e.g "1.1". This matches the typical way in
268 which packages are developed. Strictly speaking, each version of a
269 package is independent, but usually they are very similar. Cabal package
270 versions follow the conventional numeric style, consisting of a sequence
271 of digits such as "1.0.1" or "2.0". There are a range of common
272 conventions for "versioning" packages, that is giving some meaning to
273 the version number in terms of changes in the package. Section [TODO]
274 has some tips on package versioning.
276 The combination of package name and version is called the *package ID*
277 and is written with a hyphen to separate the name and version, e.g.
280 For Cabal packages, the combination of the package name and version
281 *uniquely* identifies each package. Or to put it another way: two
282 packages with the same name and version are considered to *be* the same.
284 Strictly speaking, the package ID only identifies each Cabal *source*
285 package; the same Cabal source package can be configured and built in
286 different ways. There is a separate installed package ID that uniquely
287 identifies each installed package instance. Most of the time however,
288 users need not be aware of this detail.
290 Kinds of package: Cabal vs GHC vs system
291 ----------------------------------------
293 It can be slightly confusing at first because there are various
294 different notions of package floating around. Fortunately the details
295 are not very complicated.
298 Cabal packages are really source packages. That is they contain
299 Haskell (and sometimes C) source code.
301 Cabal packages can be compiled to produce GHC packages. They can
302 also be translated into operating system packages.
305 This is GHC's view on packages. GHC only cares about library
306 packages, not executables. Library packages have to be registered
307 with GHC for them to be available in GHCi or to be used when
308 compiling other programs or packages.
310 The low-level tool ``ghc-pkg`` is used to register GHC packages and
311 to get information on what packages are currently registered.
313 You never need to make GHC packages manually. When you build and
314 install a Cabal package containing a library then it gets registered
315 with GHC automatically.
317 Haskell implementations other than GHC have essentially the same
318 concept of registered packages. For the most part, Cabal hides the
321 Operating system packages
322 On operating systems like Linux and Mac OS X, the system has a
323 specific notion of a package and there are tools for installing and
326 The Cabal package format is designed to allow Cabal packages to be
327 translated, mostly-automatically, into operating system packages.
328 They are usually translated 1:1, that is a single Cabal package
329 becomes a single system package.
331 It is also possible to make Windows installers from Cabal packages,
332 though this is typically done for a program together with all of its
333 library dependencies, rather than packaging each library separately.
338 The Cabal package is the unit of distribution. What this means is that
339 each Cabal package can be distributed on its own in source or binary
340 form. Of course there may dependencies between packages, but there is
341 usually a degree of flexibility in which versions of packages can work
342 together so distributing them independently makes sense.
344 It is perhaps easiest to see what being "the unit of distribution"
345 means by contrast to an alternative approach. Many projects are made up
346 of several interdependent packages and during development these might
347 all be kept under one common directory tree and be built and tested
348 together. When it comes to distribution however, rather than
349 distributing them all together in a single tarball, it is required that
350 they each be distributed independently in their own tarballs.
352 Cabal's approach is to say that if you can specify a dependency on a
353 package then that package should be able to be distributed
354 independently. Or to put it the other way round, if you want to
355 distribute it as a single unit, then it should be a single package.
357 Explicit dependencies and automatic package management
358 ------------------------------------------------------
360 Cabal takes the approach that all packages dependencies are specified
361 explicitly and specified in a declarative way. The point is to enable
362 automatic package management. This means tools like ``cabal`` can
363 resolve dependencies and install a package plus all of its dependencies
364 automatically. Alternatively, it is possible to mechanically (or mostly
365 mechanically) translate Cabal packages into system packages and let the
366 system package manager install dependencies automatically.
368 It is important to track dependencies accurately so that packages can
369 reliably be moved from one system to another system and still be able to
370 build it there. Cabal is therefore relatively strict about specifying
371 dependencies. For example Cabal's default build system will not even let
372 code build if it tries to import a module from a package that isn't
373 listed in the ``.cabal`` file, even if that package is actually
374 installed. This helps to ensure that there are no "untracked
375 dependencies" that could cause the code to fail to build on some other
378 The explicit dependency approach is in contrast to the traditional
379 "./configure" approach where instead of specifying dependencies
380 declaratively, the ``./configure`` script checks if the dependencies are
381 present on the system. Some manual work is required to transform a
382 ``./configure`` based package into a Linux distribution package (or
383 similar). This conversion work is usually done by people other than the
384 package author(s). The practical effect of this is that only the most
385 popular packages will benefit from automatic package management.
386 Instead, Cabal forces the original author to specify the dependencies
387 but the advantage is that every package can benefit from automatic
390 The "./configure" approach tends to encourage packages that adapt
391 themselves to the environment in which they are built, for example by
392 disabling optional features so that they can continue to work when a
393 particular dependency is not available. This approach makes sense in a
394 world where installing additional dependencies is a tiresome manual
395 process and so minimising dependencies is important. The automatic
396 package management view is that packages should just declare what they
397 need and the package manager will take responsibility for ensuring that
398 all the dependencies are installed.
400 Sometimes of course optional features and optional dependencies do make
401 sense. Cabal packages can have optional features and varying
402 dependencies. These conditional dependencies are still specified in a
403 declarative way however and remain compatible with automatic package
404 management. The need to remain compatible with automatic package
405 management means that Cabal's conditional dependencies system is a bit
406 less flexible than with the "./configure" approach.
411 One of the purposes of Cabal is to make it easier to build packages on
412 different platforms (operating systems and CPU architectures), with
413 different compiler versions and indeed even with different Haskell
414 implementations. (Yes, there are Haskell implementations other than
417 Cabal provides abstractions of features present in different Haskell
418 implementations and wherever possible it is best to take advantage of
419 these to increase portability. Where necessary however it is possible to
420 use specific features of specific implementations.
422 For example a package author can list in the package's ``.cabal`` what
423 language extensions the code uses. This allows Cabal to figure out if
424 the language extension is supported by the Haskell implementation that
425 the user picks. Additionally, certain language extensions such as
426 Template Haskell require special handling from the build system and by
427 listing the extension it provides the build system with enough
428 information to do the right thing.
430 Another similar example is linking with foreign libraries. Rather than
431 specifying GHC flags directly, the package author can list the libraries
432 that are needed and the build system will take care of using the right
433 flags for the compiler. Additionally this makes it easier for tools to
434 discover what system C libraries a package needs, which is useful for
435 tracking dependencies on system libraries (e.g. when translating into
436 Linux distribution packages).
438 In fact both of these examples fall into the category of explicitly
439 specifying dependencies. Not all dependencies are other Cabal packages.
440 Foreign libraries are clearly another kind of dependency. It's also
441 possible to think of language extensions as dependencies: the package
442 depends on a Haskell implementation that supports all those extensions.
444 Where compiler-specific options are needed however, there is an "escape
445 hatch" available. The developer can specify implementation-specific
446 options and more generally there is a configuration mechanism to
447 customise many aspects of how a package is built depending on the
448 Haskell implementation, the operating system, computer architecture and
449 user-specified configuration flags.
454 The Cabal package is the unit of distribution. When installed, its
455 purpose is to make available:
457 - One or more Haskell programs.
459 - At most one library, exposing a number of Haskell modules.
461 However having both a library and executables in a package does not work
462 very well; if the executables depend on the library, they must
463 explicitly list all the modules they directly or indirectly import from
464 that library. Fortunately, starting with Cabal 1.8.0.4, executables can
465 also declare the package that they are in as a dependency, and Cabal
466 will treat them as if they were in another package that depended on the
469 Internally, the package may consist of much more than a bunch of Haskell
470 modules: it may also have C source code and header files, source code
471 meant for preprocessing, documentation, test cases, auxiliary tools etc.
473 A package is identified by a globally-unique *package name*, which
474 consists of one or more alphanumeric words separated by hyphens. To
475 avoid ambiguity, each of these words should contain at least one letter.
476 Chaos will result if two distinct packages with the same name are
477 installed on the same system. A particular version of the package is
478 distinguished by a *version number*, consisting of a sequence of one or
479 more integers separated by dots. These can be combined to form a single
480 text string called the *package ID*, using a hyphen to separate the name
481 from the version, e.g. "``HUnit-1.1``".
485 Packages are not part of the Haskell language; they simply
486 populate the hierarchical space of module names. In GHC 6.6 and later a
487 program may contain multiple modules with the same name if they come
488 from separate packages; in all other current Haskell systems packages
489 may not overlap in the modules they provide, including hidden modules.
494 Suppose you have a directory hierarchy containing the source files that
495 make up your package. You will need to add two more files to the root
496 directory of the package:
498 :file:`{package}.cabal`
499 a Unicode UTF-8 text file containing a package description. For
500 details of the syntax of this file, see the section on
501 `package descriptions`_.
504 a single-module Haskell program to perform various setup tasks (with
505 the interface described in the section on :ref:`installing-packages`).
506 This module should import only modules that will be present in all Haskell
507 implementations, including modules of the Cabal library. The content of
508 this file is determined by the :pkg-field:`build-type` setting in the
509 ``.cabal`` file. In most cases it will be trivial, calling on the Cabal
510 library to do most of the work.
512 Once you have these, you can create a source bundle of this directory
513 for distribution. Building of the package is discussed in the section on
514 :ref:`installing-packages`.
516 One of the purposes of Cabal is to make it easier to build a package
517 with different Haskell implementations. So it provides abstractions of
518 features present in different Haskell implementations and wherever
519 possible it is best to take advantage of these to increase portability.
520 Where necessary however it is possible to use specific features of
521 specific implementations. For example one of the pieces of information a
522 package author can put in the package's ``.cabal`` file is what language
523 extensions the code uses. This is far preferable to specifying flags for
524 a specific compiler as it allows Cabal to pick the right flags for the
525 Haskell implementation that the user picks. It also allows Cabal to
526 figure out if the language extension is even supported by the Haskell
527 implementation that the user picks. Where compiler-specific options are
528 needed however, there is an "escape hatch" available. The developer can
529 specify implementation-specific options and more generally there is a
530 configuration mechanism to customise many aspects of how a package is
531 built depending on the Haskell implementation, the Operating system,
532 computer architecture and user-specified configuration flags.
542 extensions: ForeignFunctionInterface
547 Example: A package containing a simple library
548 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
550 The HUnit package contains a file ``HUnit.cabal`` containing:
556 synopsis: A unit testing framework for Haskell
557 homepage: http://hunit.sourceforge.net/
559 author: Dean Herington
561 license-file: LICENSE
562 cabal-version: >= 1.10
566 build-depends: base >= 2 && < 4
567 exposed-modules: Test.HUnit.Base, Test.HUnit.Lang,
568 Test.HUnit.Terminal, Test.HUnit.Text, Test.HUnit
569 default-extensions: CPP
571 and the following ``Setup.hs``:
573 .. code-block:: haskell
575 import Distribution.Simple
578 Example: A package containing executable programs
579 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
585 synopsis: Small package with two programs
586 author: Angela Author
589 cabal-version: >= 1.2
594 hs-source-dirs: prog1
599 hs-source-dirs: prog2
602 with ``Setup.hs`` the same as above.
604 Example: A package containing a library and executable programs
605 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
611 synopsis: Package with library and two programs
613 author: Angela Author
615 cabal-version: >= 1.2
619 exposed-modules: A, B, C
623 hs-source-dirs: prog1
628 hs-source-dirs: prog2
629 other-modules: A, C, Utils
631 with ``Setup.hs`` the same as above. Note that any library modules
632 required (directly or indirectly) by an executable must be listed again.
634 The trivial setup script used in these examples uses the *simple build
635 infrastructure* provided by the Cabal library (see
636 `Distribution.Simple <../release/cabal-latest/doc/API/Cabal/Distribution-Simple.html>`__).
637 The simplicity lies in its interface rather that its implementation. It
638 automatically handles preprocessing with standard preprocessors, and
639 builds packages for all the Haskell implementations.
641 The simple build infrastructure can also handle packages where building
642 is governed by system-dependent parameters, if you specify a little more
643 (see the section on `system-dependent parameters`_).
644 A few packages require `more elaborate solutions <more complex packages>`_.
649 The package description file must have a name ending in "``.cabal``". It
650 must be a Unicode text file encoded using valid UTF-8. There must be
651 exactly one such file in the directory. The first part of the name is
652 usually the package name, and some of the tools that operate on Cabal
653 packages require this.
655 In the package description file, lines whose first non-whitespace
656 characters are "``--``" are treated as comments and ignored.
658 This file should contain of a number global property descriptions and
661 - The `package properties`_ describe the package
662 as a whole, such as name, license, author, etc.
664 - Optionally, a number of *configuration flags* can be declared. These
665 can be used to enable or disable certain features of a package. (see
666 the section on `configurations`_).
668 - The (optional) library section specifies the `library`_ properties and
669 relevant `build information`_.
671 - Following is an arbitrary number of executable sections which describe
672 an executable program and relevant `build information`_.
674 Each section consists of a number of property descriptions in the form
675 of field/value pairs, with a syntax roughly like mail message headers.
677 - Case is not significant in field names, but is significant in field
680 - To continue a field value, indent the next line relative to the field
683 - Field names may be indented, but all field values in the same section
684 must use the same indentation.
686 - Tabs are *not* allowed as indentation characters due to a missing
687 standard interpretation of tab width.
689 - To get a blank line in a field value, use an indented "``.``"
691 The syntax of the value depends on the field. Field types include:
693 *token*, *filename*, *directory*
694 Either a sequence of one or more non-space non-comma characters, or
695 a quoted string in Haskell 98 lexical syntax. The latter can be used
696 for escaping whitespace, for example:
697 ``ghc-options: -Wall "-with-rtsopts=-T -I1"``. Unless otherwise
698 stated, relative filenames and directories are interpreted from the
699 package root directory.
700 *freeform*, *URL*, *address*
701 An arbitrary, uninterpreted string.
703 A letter followed by zero or more alphanumerics or underscores.
705 A compiler flavor (one of: ``GHC``, ``JHC``, ``UHC`` or ``LHC``)
706 followed by a version range. For example, ``GHC ==6.10.3``, or
707 ``LHC >=0.6 && <0.8``.
709 Modules and preprocessors
710 ^^^^^^^^^^^^^^^^^^^^^^^^^
712 Haskell module names listed in the :pkg-field:`library:exposed-modules` and
713 :pkg-field:`library:other-modules` fields may correspond to Haskell source
714 files, i.e. with names ending in "``.hs``" or "``.lhs``", or to inputs for
715 various Haskell preprocessors. The simple build infrastructure understands the
718 - ``.gc`` (:hackage-pkg:`greencard`)
719 - ``.chs`` (:hackage-pkg:`c2hs`)
720 - ``.hsc`` (:hackage-pkg:`hsc2hs`)
721 - ``.y`` and ``.ly`` (happy_)
723 - ``.cpphs`` (cpphs_)
725 When building, Cabal will automatically run the appropriate preprocessor
726 and compile the Haskell module it produces. For the ``c2hs`` and
727 ``hsc2hs`` preprocessors, Cabal will also automatically add, compile and
728 link any C sources generated by the preprocessor (produced by
729 ``hsc2hs``'s ``#def`` feature or ``c2hs``'s auto-generated wrapper
732 Some fields take lists of values, which are optionally separated by
733 commas, except for the :pkg-field:`build-depends` field, where the commas are
736 Some fields are marked as required. All others are optional, and unless
737 otherwise specified have empty default values.
742 These fields may occur in the first top-level properties section and
743 describe the package as a whole:
745 .. pkg-field:: name: package-name (required)
747 The unique name of the package, without the version number.
749 .. pkg-field:: version: numbers (required)
751 The package version number, usually consisting of a sequence of
752 natural numbers separated by dots.
754 .. pkg-field:: cabal-version: >= x.y
756 The version of the Cabal specification that this package description
757 uses. The Cabal specification does slowly evolve, introducing new
758 features and occasionally changing the meaning of existing features.
759 By specifying which version of the spec you are using it enables
760 programs which process the package description to know what syntax
761 to expect and what each part means.
763 For historical reasons this is always expressed using *>=* version
764 range syntax. No other kinds of version range make sense, in
765 particular upper bounds do not make sense. In future this field will
766 specify just a version number, rather than a version range.
768 The version number you specify will affect both compatibility and
769 behaviour. Most tools (including the Cabal library and cabal
770 program) understand a range of versions of the Cabal specification.
771 Older tools will of course only work with older versions of the
772 Cabal specification. Most of the time, tools that are too old will
773 recognise this fact and produce a suitable error message.
775 As for behaviour, new versions of the Cabal spec can change the
776 meaning of existing syntax. This means if you want to take advantage
777 of the new meaning or behaviour then you must specify the newer
778 Cabal version. Tools are expected to use the meaning and behaviour
779 appropriate to the version given in the package description.
781 In particular, the syntax of package descriptions changed
782 significantly with Cabal version 1.2 and the :pkg-field:`cabal-version`
783 field is now required. Files written in the old syntax are still
784 recognized, so if you require compatibility with very old Cabal
785 versions then you may write your package description file using the
786 old syntax. Please consult the user's guide of an older Cabal
787 version for a description of that syntax.
789 .. pkg-field:: build-type: identifier
793 The type of build used by this package. Build types are the
795 `BuildType <../release/cabal-latest/doc/API/Cabal/Distribution-PackageDescription.html#t:BuildType>`__
796 type, defaulting to ``Custom``.
798 If the build type is anything other than ``Custom``, then the
799 ``Setup.hs`` file *must* be exactly the standardized content
800 discussed below. This is because in these cases, ``cabal`` will
801 ignore the ``Setup.hs`` file completely, whereas other methods of
802 package management, such as ``runhaskell Setup.hs [CMD]``, still
803 rely on the ``Setup.hs`` file.
805 For build type ``Simple``, the contents of ``Setup.hs`` must be:
807 .. code-block:: haskell
809 import Distribution.Simple
812 For build type ``Configure`` (see the section on `system-dependent
813 parameters`_ below), the contents of
814 ``Setup.hs`` must be:
816 .. code-block:: haskell
818 import Distribution.Simple
819 main = defaultMainWithHooks autoconfUserHooks
821 For build type ``Make`` (see the section on `more complex packages`_ below),
822 the contents of ``Setup.hs`` must be:
824 .. code-block:: haskell
826 import Distribution.Make
829 For build type ``Custom``, the file ``Setup.hs`` can be customized,
830 and will be used both by ``cabal`` and other tools.
832 For most packages, the build type ``Simple`` is sufficient.
834 .. pkg-field:: license: identifier
836 :default: ``AllRightsReserved``
838 The type of license under which this package is distributed. License
839 names are the constants of the
840 `License <../release/cabal-latest/doc/API/Cabal/Distribution-License.html#t:License>`__
843 .. pkg-field:: license-file: filename
844 .. pkg-field:: license-files: filename list
846 The name of a file(s) containing the precise copyright license for
847 this package. The license file(s) will be installed with the
850 If you have multiple license files then use the :pkg-field:`license-files`
851 field instead of (or in addition to) the :pkg-field:`license-file` field.
853 .. pkg-field:: copyright: freeform
855 The content of a copyright notice, typically the name of the holder
856 of the copyright on the package and the year(s) from which copyright
857 is claimed. For example::
859 copyright: (c) 2006-2007 Joe Bloggs
861 .. pkg-field:: author: freeform
863 The original author of the package.
865 Remember that ``.cabal`` files are Unicode, using the UTF-8
868 .. pkg-field:: maintainer: address
870 The current maintainer or maintainers of the package. This is an
871 e-mail address to which users should send bug reports, feature
872 requests and patches.
874 .. pkg-field:: stability: freeform
876 The stability level of the package, e.g. ``alpha``,
877 ``experimental``, ``provisional``, ``stable``.
879 .. pkg-field:: homepage: URL
881 The package homepage.
883 .. pkg-field:: bug-reports: URL
885 The URL where users should direct bug reports. This would normally
888 - A ``mailto:`` URL, e.g. for a person or a mailing list.
890 - An ``http:`` (or ``https:``) URL for an online bug tracking
893 For example Cabal itself uses a web-based bug tracking system
897 bug-reports: http://hackage.haskell.org/trac/hackage/
899 .. pkg-field:: package-url: URL
901 The location of a source bundle for the package. The distribution
902 should be a Cabal package.
904 .. pkg-field:: synopsis: freeform
906 A very short description of the package, for use in a table of
907 packages. This is your headline, so keep it short (one line) but as
908 informative as possible. Save space by not including the package
909 name or saying it's written in Haskell.
911 .. pkg-field:: description: freeform
913 Description of the package. This may be several paragraphs, and
914 should be aimed at a Haskell programmer who has never heard of your
917 For library packages, this field is used as prologue text by
918 :ref:`setup-haddock` and thus may contain the same markup as Haddock_
919 documentation comments.
921 .. pkg-field:: category: freeform
923 A classification category for future use by the package catalogue
924 Hackage_. These categories have not
925 yet been specified, but the upper levels of the module hierarchy
928 .. pkg-field:: tested-with: compiler list
930 A list of compilers and versions against which the package has been
931 tested (or at least built).
933 .. pkg-field:: data-files: filename list
935 A list of files to be installed for run-time use by the package.
936 This is useful for packages that use a large amount of static data,
937 such as tables of values or code templates. Cabal provides a way to
938 `find these files at run-time <accessing data files from package code>`_.
940 A limited form of ``*`` wildcards in file names, for example
941 ``data-files: images/*.png`` matches all the ``.png`` files in the
942 ``images`` directory.
944 The limitation is that ``*`` wildcards are only allowed in place of
945 the file name, not in the directory name or file extension. In
946 particular, wildcards do not include directories contents
947 recursively. Furthermore, if a wildcard is used it must be used with
948 an extension, so ``data-files: data/*`` is not allowed. When
949 matching a wildcard plus extension, a file's full extension must
950 match exactly, so ``*.gz`` matches ``foo.gz`` but not
951 ``foo.tar.gz``. A wildcard that does not match any files is an
954 The reason for providing only a very limited form of wildcard is to
955 concisely express the common case of a large number of related files
956 of the same file type without making it too easy to accidentally
957 include unwanted files.
959 .. pkg-field:: data-dir: directory
961 The directory where Cabal looks for data files to install, relative
962 to the source directory. By default, Cabal will look in the source
965 .. pkg-field:: extra-source-files: filename list
967 A list of additional files to be included in source distributions
968 built with :ref:`setup-sdist`. As with :pkg-field:`data-files` it can use
969 a limited form of ``*`` wildcards in file names.
971 .. pkg-field:: extra-doc-files: filename list
973 A list of additional files to be included in source distributions,
974 and also copied to the html directory when Haddock documentation is
975 generated. As with :pkg-field:`data-files` it can use a limited form of
976 ``*`` wildcards in file names.
978 .. pkg-field:: extra-tmp-files: filename list
980 A list of additional files or directories to be removed by
981 :ref:`setup-clean`. These would typically be additional files created by
982 additional hooks, such as the scheme described in the section on
983 `system-dependent parameters`_
988 .. pkg-section:: library
989 :synopsis: Library build information.
991 Build information for libraries. There can be only one library in a
992 package, and it's name is the same as package name set by global
993 :pkg-field:`name` field.
995 The library section should contain the following fields:
997 .. pkg-field:: exposed-modules: identifier list
999 :required: if this package contains a library
1001 A list of modules added by this package.
1003 .. pkg-field:: exposed: boolean
1007 Some Haskell compilers (notably GHC) support the notion of packages
1008 being "exposed" or "hidden" which means the modules they provide can
1009 be easily imported without always having to specify which package
1010 they come from. However this only works effectively if the modules
1011 provided by all exposed packages do not overlap (otherwise a module
1012 import would be ambiguous).
1014 Almost all new libraries use hierarchical module names that do not
1015 clash, so it is very uncommon to have to use this field. However it
1016 may be necessary to set ``exposed: False`` for some old libraries
1017 that use a flat module namespace or where it is known that the
1018 exposed modules would clash with other common modules.
1020 .. pkg-field:: reexported-modules: exportlist
1022 Supported only in GHC 7.10 and later. A list of modules to
1023 *reexport* from this package. The syntax of this field is
1024 ``orig-pkg:Name as NewName`` to reexport module ``Name`` from
1025 ``orig-pkg`` with the new name ``NewName``. We also support
1026 abbreviated versions of the syntax: if you omit ``as NewName``,
1027 we'll reexport without renaming; if you omit ``orig-pkg``, then we
1028 will automatically figure out which package to reexport from, if
1031 Reexported modules are useful for compatibility shims when a package
1032 has been split into multiple packages, and they have the useful
1033 property that if a package provides a module, and another package
1034 reexports it under the same name, these are not considered a
1035 conflict (as would be the case with a stub module.) They can also be
1036 used to resolve name conflicts.
1038 The library section may also contain build information fields (see the
1039 section on `build information`_).
1041 Cabal 1.25 and later support "internal libraries", which are extra named
1042 libraries (as opposed to the usual unnamed library section). For
1043 example, suppose that your test suite needs access to some internal
1044 modules in your library, which you do not otherwise want to export. You
1045 could put these modules in an internal library, which the main library
1046 and the test suite :pkg-field:`build-depends` upon. Then your Cabal file might
1047 look something like this:
1054 cabal-version: >= 1.23
1057 library foo-internal
1058 exposed-modules: Foo.Internal
1062 exposed-modules: Foo.Public
1063 build-depends: foo-internal, base
1066 type: exitcode-stdio-1.0
1067 main-is: test-foo.hs
1068 build-depends: foo-internal, base
1070 Internal libraries are also useful for packages that define multiple
1071 executables, but do not define a publically accessible library. Internal
1072 libraries are only visible internally in the package (so they can only
1073 be added to the :pkg-field:`build-depends` of same-package libraries,
1074 executables, test suites, etc.) Internal libraries locally shadow any
1075 packages which have the same name (so don't name an internal library
1076 with the same name as an external dependency.)
1078 Opening an interpreter session
1079 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1081 While developing a package, it is often useful to make its code
1082 available inside an interpreter session. This can be done with the
1085 .. code-block:: console
1089 The name comes from the acronym
1090 `REPL <http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop>`__,
1091 which stands for "read-eval-print-loop". By default ``cabal repl`` loads
1092 the first component in a package. If the package contains several named
1093 components, the name can be given as an argument to ``repl``. The name
1094 can be also optionally prefixed with the component's type for
1095 disambiguation purposes. Example:
1097 .. code-block:: console
1100 $ cabal repl exe:foo
1101 $ cabal repl test:bar
1102 $ cabal repl bench:baz
1104 Freezing dependency versions
1105 """"""""""""""""""""""""""""
1107 If a package is built in several different environments, such as a
1108 development environment, a staging environment and a production
1109 environment, it may be necessary or desirable to ensure that the same
1110 dependency versions are selected in each environment. This can be done
1111 with the ``freeze`` command:
1113 .. code-block:: console
1117 The command writes the selected version for all dependencies to the
1118 ``cabal.config`` file. All environments which share this file will use
1119 the dependency versions specified in it.
1121 Generating dependency version bounds
1122 """"""""""""""""""""""""""""""""""""
1124 Cabal also has the ability to suggest dependency version bounds that
1125 conform to `Package Versioning Policy`_, which is
1126 a recommended versioning system for publicly released Cabal packages.
1127 This is done by running the ``gen-bounds`` command:
1129 .. code-block:: console
1133 For example, given the following dependencies specified in
1134 :pkg-field:`build-depends`:
1142 ``gen-bounds`` will suggest changing them to the following:
1147 foo >= 0.5.2 && < 0.6
1150 Listing outdated dependency version bounds
1151 """"""""""""""""""""""""""""""""""""""""""
1153 Manually updating dependency version bounds in a ``.cabal`` file or a
1154 freeze file can be tedious, especially when there's a lot of
1155 dependencies. The ``cabal outdated`` command is designed to help with
1156 that. It will print a list of packages for which there is a new
1157 version on Hackage that is outside the version bound specified in the
1158 ``build-depends`` field. The ``outdated`` command can also be
1159 configured to act on the freeze file (both old- and new-style) and
1160 ignore major (or all) version bumps on Hackage for a subset of
1163 The following flags are supported by the ``outdated`` command:
1166 Read dependency version bounds from the freeze file (``cabal.config``)
1167 instead of the package description file (``$PACKAGENAME.cabal``).
1168 ``--new-freeze-file``
1169 Read dependency version bounds from the new-style freeze file
1170 (``cabal.project.freeze``) instead of the package description file.
1172 Print only the names of outdated dependencies, one per line.
1174 Exit with a non-zero exit code when there are outdated dependencies.
1176 Don't print any output. Implies ``-v0`` and ``--exit-code``.
1177 ``--ignore`` *PACKAGENAMES*
1178 Don't warn about outdated dependency version bounds for the packages in this
1180 ``--minor`` *[PACKAGENAMES]*
1181 Ignore major version bumps for these packages. E.g. if there's a version 2.0
1182 of a package ``pkg`` on Hackage and the freeze file specifies the constraint
1183 ``pkg == 1.9``, ``cabal outdated --freeze --minor=pkg`` will only consider
1184 the ``pkg`` outdated when there's a version of ``pkg`` on Hackage satisfying
1185 ``pkg > 1.9 && < 2.0``. ``--minor`` can also be used without arguments, in
1186 that case major version bumps are ignored for all packages.
1190 .. code-block:: console
1194 Outdated dependencies:
1195 haskell-src-exts <1.17 (latest: 1.19.1)
1196 language-javascript <0.6 (latest: 0.6.0.9)
1197 unix ==2.7.2.0 (latest: 2.7.2.1)
1199 $ cabal outdated --simple-output
1204 $ cabal outdated --ignore=haskell-src-exts
1205 Outdated dependencies:
1206 language-javascript <0.6 (latest: 0.6.0.9)
1207 unix ==2.7.2.0 (latest: 2.7.2.1)
1209 $ cabal outdated --ignore=haskell-src-exts,language-javascript,unix
1210 All dependencies are up to date.
1212 $ cabal outdated --ignore=haskell-src-exts,language-javascript,unix -q
1216 $ cd /some/other/package
1217 $ cabal outdated --freeze-file
1218 Outdated dependencies:
1219 HTTP ==4000.3.3 (latest: 4000.3.4)
1220 HUnit ==1.3.1.1 (latest: 1.5.0.0)
1222 $ cabal outdated --freeze-file --ignore=HTTP --minor=HUnit
1223 Outdated dependencies:
1224 HUnit ==1.3.1.1 (latest: 1.3.1.2)
1230 .. pkg-section:: executable name
1231 :synopsis: Exectuable build info section.
1233 Executable sections (if present) describe executable programs contained
1234 in the package and must have an argument after the section label, which
1235 defines the name of the executable. This is a freeform argument but may
1238 The executable may be described using the following fields, as well as
1239 build information fields (see the section on `build information`_).
1241 .. pkg-field:: main-is: filename (required)
1243 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1244 module. Note that it is the ``.hs`` filename that must be listed,
1245 even if that file is generated using a preprocessor. The source file
1246 must be relative to one of the directories listed in
1247 :pkg-field:`hs-source-dirs`.
1252 You can have Cabal build and run your executables by using the ``run``
1255 .. code-block:: console
1257 $ cabal run EXECUTABLE [-- EXECUTABLE_FLAGS]
1259 This command will configure, build and run the executable
1260 ``EXECUTABLE``. The double dash separator is required to distinguish
1261 executable flags from ``run``'s own flags. If there is only one
1262 executable defined in the whole package, the executable's name can be
1263 omitted. See the output of ``cabal help run`` for a list of options you
1264 can pass to ``cabal run``.
1269 .. pkg-section:: test name
1270 :synopsis: Test suit build information.
1272 Test suite sections (if present) describe package test suites and must
1273 have an argument after the section label, which defines the name of the
1274 test suite. This is a freeform argument, but may not contain spaces. It
1275 should be unique among the names of the package's other test suites, the
1276 package's executables, and the package itself. Using test suite sections
1277 requires at least Cabal version 1.9.2.
1279 The test suite may be described using the following fields, as well as
1280 build information fields (see the section on `build information`_).
1282 .. pkg-field:: type: interface (required)
1284 The interface type and version of the test suite. Cabal supports two
1285 test suite interfaces, called ``exitcode-stdio-1.0`` and
1286 ``detailed-0.9``. Each of these types may require or disallow other
1287 fields as described below.
1289 Test suites using the ``exitcode-stdio-1.0`` interface are executables
1290 that indicate test failure with a non-zero exit code when run; they may
1291 provide human-readable log information through the standard output and
1292 error channels. The ``exitcode-stdio-1.0`` type requires the ``main-is``
1295 .. pkg-field:: main-is: filename
1296 :synopsis: Module containing tests main function.
1298 :required: ``exitcode-stdio-1.0``
1299 :disallowed: ``detailed-0.9``
1301 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1302 module. Note that it is the ``.hs`` filename that must be listed,
1303 even if that file is generated using a preprocessor. The source file
1304 must be relative to one of the directories listed in
1305 :pkg-field:`hs-source-dirs`. This field is analogous to the ``main-is`` field
1306 of an executable section.
1308 Test suites using the ``detailed-0.9`` interface are modules exporting
1309 the symbol ``tests :: IO [Test]``. The ``Test`` type is exported by the
1310 module ``Distribution.TestSuite`` provided by Cabal. For more details,
1311 see the example below.
1313 The ``detailed-0.9`` interface allows Cabal and other test agents to
1314 inspect a test suite's results case by case, producing detailed human-
1315 and machine-readable log files. The ``detailed-0.9`` interface requires
1316 the :pkg-field:`test-module` field.
1318 .. pkg-field:: test-module: identifier
1320 :required: ``detailed-0.9``
1321 :disallowed: ``exitcode-stdio-1.0``
1323 The module exporting the ``tests`` symbol.
1325 Example: Package using ``exitcode-stdio-1.0`` interface
1326 """""""""""""""""""""""""""""""""""""""""""""""""""""""
1328 The example package description and executable source file below
1329 demonstrate the use of the ``exitcode-stdio-1.0`` interface.
1331 .. code-block:: cabal
1337 Cabal-Version: >= 1.9.2
1341 type: exitcode-stdio-1.0
1342 main-is: test-foo.hs
1345 .. code-block:: haskell
1346 :caption: test-foo.hs
1350 import System.Exit (exitFailure)
1353 putStrLn "This test always fails!"
1356 Example: Package using ``detailed-0.9`` interface
1357 """""""""""""""""""""""""""""""""""""""""""""""""
1359 The example package description and test module source file below
1360 demonstrate the use of the ``detailed-0.9`` interface. The test module
1361 also develops a simple implementation of the interface set by
1362 ``Distribution.TestSuite``, but in actual usage the implementation would
1363 be provided by the library that provides the testing facility.
1365 .. code-block:: cabal
1371 Cabal-Version: >= 1.9.2
1377 build-depends: base, Cabal >= 1.9.2
1380 .. code-block:: haskell
1383 module Bar ( tests ) where
1385 import Distribution.TestSuite
1388 tests = return [ Test succeeds, Test fails ]
1390 succeeds = TestInstance
1391 { run = return $ Finished Pass
1395 , setOption = \_ _ -> Right succeeds
1397 fails = TestInstance
1398 { run = return $ Finished $ Fail "Always fails!"
1402 , setOption = \_ _ -> Right fails
1408 You can have Cabal run your test suites using its built-in test runner:
1412 $ cabal configure --enable-tests
1416 See the output of ``cabal help test`` for a list of options you can pass
1422 .. pkg-section:: benchmark name
1424 :synopsis: Benchmark build information.
1426 Benchmark sections (if present) describe benchmarks contained in the
1427 package and must have an argument after the section label, which defines
1428 the name of the benchmark. This is a freeform argument, but may not
1429 contain spaces. It should be unique among the names of the package's
1430 other benchmarks, the package's test suites, the package's executables,
1431 and the package itself. Using benchmark sections requires at least Cabal
1434 The benchmark may be described using the following fields, as well as
1435 build information fields (see the section on `build information`_).
1437 .. pkg-field:: type: interface (required)
1439 The interface type and version of the benchmark. At the moment Cabal
1440 only support one benchmark interface, called ``exitcode-stdio-1.0``.
1442 Benchmarks using the ``exitcode-stdio-1.0`` interface are executables
1443 that indicate failure to run the benchmark with a non-zero exit code
1444 when run; they may provide human-readable information through the
1445 standard output and error channels.
1447 .. pkg-field:: main-is: filename
1449 :required: ``exitcode-stdio-1.0``
1451 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1452 module. Note that it is the ``.hs`` filename that must be listed,
1453 even if that file is generated using a preprocessor. The source file
1454 must be relative to one of the directories listed in
1455 :pkg-field:`hs-source-dirs`. This field is analogous to the ``main-is``
1456 field of an executable section.
1458 Example: Package using ``exitcode-stdio-1.0`` interface
1459 """""""""""""""""""""""""""""""""""""""""""""""""""""""
1461 The example package description and executable source file below
1462 demonstrate the use of the ``exitcode-stdio-1.0`` interface.
1464 .. code-block:: cabal
1466 :name: foo-bench.cabal
1471 Cabal-Version: >= 1.9.2
1475 type: exitcode-stdio-1.0
1476 main-is: bench-foo.hs
1477 build-depends: base, time
1479 .. code-block:: haskell
1480 :caption: bench-foo.hs
1482 {-# LANGUAGE BangPatterns #-}
1485 import Data.Time.Clock
1489 fib n = fib (n-1) + fib (n-2)
1492 start <- getCurrentTime
1494 end <- getCurrentTime
1495 putStrLn $ "fib 20 took " ++ show (diffUTCTime end start)
1500 You can have Cabal run your benchmark using its built-in benchmark
1505 $ cabal configure --enable-benchmarks
1509 See the output of ``cabal help bench`` for a list of options you can
1510 pass to ``cabal bench``.
1515 Foreign libraries are system libraries intended to be linked against
1516 programs written in C or other "foreign" languages. They
1517 come in two primary flavours: dynamic libraries (``.so`` files on Linux,
1518 ``.dylib`` files on OSX, ``.dll`` files on Windows, etc.) are linked against
1519 executables when the executable is run (or even lazily during
1520 execution), while static libraries (``.a`` files on Linux/OSX, ``.lib``
1521 files on Windows) get linked against the executable at compile time.
1523 Foreign libraries only work with GHC 7.8 and later.
1525 A typical stanza for a foreign library looks like
1529 foreign-library myforeignlib
1531 lib-version-info: 6:3:2
1535 mod-def-file: MyForeignLib.def
1537 other-modules: MyForeignLib.SomeModule
1538 MyForeignLib.SomeOtherModule
1539 build-depends: base >=4.7 && <4.9
1541 c-sources: csrc/MyForeignLibWrapper.c
1542 default-language: Haskell2010
1544 .. pkg-field:: type: foreign library type
1546 Cabal recognizes ``native-static`` and ``native-shared`` here, although
1547 we currently only support building `native-shared` libraries.
1549 .. pkg-field:: options: foreign library option list
1551 Options for building the foreign library, typically specific to the
1552 specified type of foreign library. Currently we only support
1553 ``standalone`` here. A standalone dynamic library is one that does not
1554 have any dependencies on other (Haskell) shared libraries; without
1555 the ``standalone`` option the generated library would have dependencies
1556 on the Haskell runtime library (``libHSrts``), the base library
1557 (``libHSbase``), etc. Currently, ``standalone`` *must* be used on Windows
1558 and *must not* be used on any other platform.
1560 .. pkg-field:: mod-def-file: filename
1562 This option can only be used when creating dynamic Windows libraries
1563 (that is, when using ``native-shared`` and the ``os`` is ``Windows``). If
1564 used, it must be a path to a _module definition file_. The details of
1565 module definition files are beyond the scope of this document; see the
1566 `GHC <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/win32-dlls.html>`_
1567 manual for some details and some further pointers.
1569 .. pkg-field:: lib-version-info: current:revision:age
1571 This field is currently only used on Linux.
1573 This field specifies a Libtool-style version-info field that sets
1574 an appropriate ABI version for the foreign library. Note that the
1575 three numbers specified in this field do not directly specify the
1576 actual ABI version: ``6:3:2`` results in library version ``4.2.3``.
1578 With this field set, the SONAME of the library is set, and symlinks
1581 How you should bump this field on an ABI change depends on the
1582 breakage you introduce:
1584 - Programs using the previous version may use the new version as
1585 drop-in replacement, and programs using the new version can also
1586 work with the previous one. In other words, no recompiling nor
1587 relinking is needed. In this case, bump ``revision`` only, don't
1588 touch current nor age.
1589 - Programs using the previous version may use the new version as
1590 drop-in replacement, but programs using the new version may use
1591 APIs not present in the previous one. In other words, a program
1592 linking against the new version may fail with "unresolved
1593 symbols" if linking against the old version at runtime: set
1594 revision to 0, bump current and age.
1595 - Programs may need to be changed, recompiled, and relinked in
1596 order to use the new version. Bump current, set revision and age
1599 Also refer to the Libtool documentation on the version-info field.
1601 .. pkg-field:: lib-version-linux: version
1603 This field is only used on Linux.
1605 Specifies the library ABI version directly for foreign libraries
1606 built on Linux: so specifying ``4.2.3`` causes a library
1607 ``libfoo.so.4.2.3`` to be built with SONAME ``libfoo.so.4``, and
1608 appropriate symlinks ``libfoo.so.4`` and ``libfoo.so`` to be
1611 Note that typically foreign libraries should export a way to initialize
1612 and shutdown the Haskell runtime. In the example above, this is done by
1613 the ``csrc/MyForeignLibWrapper.c`` file, which might look something like
1620 HsBool myForeignLibInit(void){
1622 char *argv[] = { "+RTS", "-A32m", NULL };
1623 char **pargv = argv;
1625 // Initialize Haskell runtime
1626 hs_init(&argc, &pargv);
1628 // do any other initialization here and
1629 // return false if there was a problem
1630 return HS_BOOL_TRUE;
1633 void myForeignLibExit(void){
1637 With modern ghc regular libraries are installed in directories that contain
1638 package keys. This isn't usually a problem because the package gets registered
1639 in ghc's package DB and so we can figure out what the location of the library
1640 is. Foreign libraries however don't get registered, which means that we'd have
1641 to have a way of finding out where a platform library got installed (other than by
1642 searching the ``lib/`` directory). Instead, we install foreign libraries in
1643 ``~/.cabal/lib``, much like we install executables in ``~/.cabal/bin``.
1647 .. pkg-section:: None
1649 The following fields may be optionally present in a library, executable,
1650 test suite or benchmark section, and give information for the building
1651 of the corresponding library or executable. See also the sections on
1652 `system-dependent parameters`_ and `configurations`_ for a way to supply
1653 system-dependent values for these fields.
1655 .. pkg-field:: build-depends: package list
1657 A list of packages needed to build this one. Each package can be
1658 annotated with a version constraint.
1660 Version constraints use the operators ``==, >=, >, <, <=`` and a
1661 version number. Multiple constraints can be combined using ``&&`` or
1662 ``||``. If no version constraint is specified, any version is
1663 assumed to be acceptable. For example:
1670 foo >= 1.2.3 && < 1.3,
1673 Dependencies like ``foo >= 1.2.3 && < 1.3`` turn out to be very
1674 common because it is recommended practise for package versions to
1675 correspond to API versions (see PVP_).
1677 Since Cabal 1.6, there is a special wildcard syntax to help with
1682 build-depends: foo ==1.2.*
1684 It is only syntactic sugar. It is exactly equivalent to
1685 ``foo >= 1.2 && < 1.3``.
1687 Starting with Cabal 2.0, there's a new syntactic sugar to support
1689 major upper bounds conveniently, and is inspired by similiar
1690 syntactic sugar found in other language ecosystems where it's often
1691 called the "Caret" operator:
1695 build-depends: foo ^>= 1.2.3.4,
1698 The declaration above is exactly equivalent to
1702 build-depends: foo >= 1.2.3.4 && < 1.3,
1707 Prior to Cabal 1.8, ``build-depends`` specified in each
1708 section were global to all sections. This was unintentional, but
1709 some packages were written to depend on it, so if you need your
1710 :pkg-field:`build-depends` to be local to each section, you must specify
1711 at least ``Cabal-Version: >= 1.8`` in your ``.cabal`` file.
1715 Cabal 1.20 experimentally supported module thinning and
1716 renaming in ``build-depends``; however, this support has since been
1717 removed and should not be used.
1719 .. pkg-field:: other-modules: identifier list
1721 A list of modules used by the component but not exposed to users.
1722 For a library component, these would be hidden modules of the
1723 library. For an executable, these would be auxiliary modules to be
1724 linked with the file named in the ``main-is`` field.
1728 Every module in the package *must* be listed in one of
1729 :pkg-field:`other-modules`, :pkg-field:`library:exposed-modules` or
1730 :pkg-field:`executable:main-is` fields.
1732 .. pkg-field:: hs-source-dirs: directory list
1736 Root directories for the module hierarchy.
1738 For backwards compatibility, the old variant ``hs-source-dir`` is
1741 .. pkg-field:: default-extensions: identifier list
1743 A list of Haskell extensions used by every module. These determine
1744 corresponding compiler options enabled for all files. Extension
1745 names are the constructors of the
1746 `Extension <../release/cabal-latest/doc/API/Cabal/Language-Haskell-Extension.html#t:Extension>`__
1747 type. For example, ``CPP`` specifies that Haskell source files are
1748 to be preprocessed with a C preprocessor.
1750 .. pkg-field:: other-extensions: identifier list
1752 A list of Haskell extensions used by some (but not necessarily all)
1753 modules. From GHC version 6.6 onward, these may be specified by
1754 placing a ``LANGUAGE`` pragma in the source files affected e.g.
1756 .. code-block:: haskell
1758 {-# LANGUAGE CPP, MultiParamTypeClasses #-}
1760 In Cabal-1.24 the dependency solver will use this and
1761 :pkg-field:`default-extensions` information. Cabal prior to 1.24 will abort
1762 compilation if the current compiler doesn't provide the extensions.
1764 If you use some extensions conditionally, using CPP or conditional
1765 module lists, it is good to replicate the condition in
1766 :pkg-field:`other-extensions` declarations:
1770 other-extensions: CPP
1772 other-extensions: PolyKinds
1774 You could also omit the conditionally used extensions, as they are
1775 for information only, but it is recommended to replicate them in
1776 :pkg-field:`other-extensions` declarations.
1778 .. pkg-field:: extensions: identifier list
1781 Deprecated in favor of :pkg-field:`default-extensions`.
1783 .. pkg-field:: build-tool-depends: package:executable list
1785 A list of Haskell programs needed to build this component.
1786 Each is specified by the package containing the executable and the name of the executable itself, separated by a colon, and optionally followed by a version bound.
1787 It is fine for the package to be the current one, in which case this is termed an *internal*, rather than *external* executable dependency.
1789 External dependencies can (and should) contain a version bound like conventional :pkg-field:`build-depends` dependencies.
1790 Internal deps should not contain a version bound, as they will be always resolved within the same configuration of the package in the build plan.
1791 Specifically, version bounds that include the package's version will be warned for being extraneous, and version bounds that exclude the package's version will raise and error for being impossible to follow.
1793 Cabal can make sure that specified programs are built and on the ``PATH`` before building the component in question.
1794 It will always do so for internal dependencies, and also do so for external dependencies when using Nix-style local builds.
1796 :pkg-field:`build-tool-depends` was added in Cabal 2.0, and it will
1797 be ignored (with a warning) with old versions of Cabal. See
1798 :pkg-field:`build-tools` for more information about backwards
1801 .. pkg-field:: build-tools: program list
1804 Deprecated in favor of :pkg-field:`build-tool-depends`, but `see below for backwards compatibility information. <buildtoolsbc>`_
1806 A list of Haskell programs needed to build this component.
1807 Each may be followed by an optional version bound.
1808 Confusingly, each program in the list either refer to one of three things:
1810 1. Another executables in the same package
1812 2. One of a hard-coded set of packages containing common build tools
1813 (possibly extended by a ``Custom`` setup script)
1815 3. A pre-built executable that should already be on the ``PATH``
1816 (Supported only by Cabal 2.0 and later.)
1818 These cases are listed in order of priority:
1819 an executable in the package will override any of the hard-coded packages with the same name,
1820 and a hard-coded package will override any executable on the ``PATH``.
1822 In the first two cases, the list entry is desugared into a :pkg-field:`build-tool-depends` entry.
1823 In the first case, the entry is desugared into a :pkg-field:`build-tool-depends` entry by prefixing with ``$pkg:``.
1824 In the second case, it is desugared by looking up the package and executable name in a hard-coded table.
1825 In either case, the optional version bound is passed through unchanged.
1826 Refer to the documentation for :pkg-field:`build-tool-depends` to understand the desugared field's meaning, along with restrictions on version bounds.
1830 Although this field is deprecated in favor of :pkg-field:`build-tool-depends`, there are some situations where you may prefer to use :pkg-field:`build-tool` in cases (1) and (2), as it is supported by more versions of Cabal.
1831 In case (3), :pkg-field:`build-tool-depends` is better for backwards-compatibility, as it will be ignored by old versions of Cabal; if you add the executable to :pkg-field:`build-tools`, a setup script built against old Cabal will choke.
1832 If an old version of Cabal is used, an end-user will have to manually arrange for the requested executable to be in your ``PATH``.
1834 .. pkg-field:: buildable: boolean
1838 Is the component buildable? Like some of the other fields below,
1839 this field is more useful with the slightly more elaborate form of
1840 the simple build infrastructure described in the section on
1841 `system-dependent parameters`_.
1843 .. pkg-field:: ghc-options: token list
1845 Additional options for GHC. You can often achieve the same effect
1846 using the :pkg-field:`extensions` field, which is preferred.
1848 Options required only by one module may be specified by placing an
1849 ``OPTIONS_GHC`` pragma in the source file affected.
1851 As with many other fields, whitespace can be escaped by using
1852 Haskell string syntax. Example:
1853 ``ghc-options: -Wcompat "-with-rtsopts=-T -I1" -Wall``.
1855 .. pkg-field:: ghc-prof-options: token list
1857 Additional options for GHC when the package is built with profiling
1860 Note that as of Cabal-1.24, the default profiling detail level
1861 defaults to ``exported-functions`` for libraries and
1862 ``toplevel-functions`` for executables. For GHC these correspond to
1863 the flags ``-fprof-auto-exported`` and ``-fprof-auto-top``. Prior to
1864 Cabal-1.24 the level defaulted to ``none``. These levels can be
1865 adjusted by the person building the package with the
1866 ``--profiling-detail`` and ``--library-profiling-detail`` flags.
1868 It is typically better for the person building the package to pick
1869 the profiling detail level rather than for the package author. So
1870 unless you have special needs it is probably better not to specify
1871 any of the GHC ``-fprof-auto*`` flags here. However if you wish to
1872 override the profiling detail level, you can do so using the
1873 :pkg-field:`ghc-prof-options` field: use ``-fno-prof-auto`` or one of the
1874 other ``-fprof-auto*`` flags.
1876 .. pkg-field:: ghc-shared-options: token list
1878 Additional options for GHC when the package is built as shared
1879 library. The options specified via this field are combined with the
1880 ones specified via :pkg-field:`ghc-options`, and are passed to GHC during
1881 both the compile and link phases.
1883 .. pkg-field:: includes: filename list
1885 A list of header files to be included in any compilations via C.
1886 This field applies to both header files that are already installed
1887 on the system and to those coming with the package to be installed.
1888 The former files should be found in absolute paths, while the latter
1889 files should be found in paths relative to the top of the source
1890 tree or relative to one of the directories listed in
1891 :pkg-field:`include-dirs`.
1893 These files typically contain function prototypes for foreign
1894 imports used by the package. This is in contrast to
1895 :pkg-field:`install-includes`, which lists header files that are intended
1896 to be exposed to other packages that transitively depend on this
1899 .. pkg-field:: install-includes: filename list
1901 A list of header files from this package to be installed into
1902 ``$libdir/includes`` when the package is installed. Files listed in
1903 :pkg-field:`install-includes` should be found in relative to the top of the
1904 source tree or relative to one of the directories listed in
1905 :pkg-field:`include-dirs`.
1907 :pkg-field:`install-includes` is typically used to name header files that
1908 contain prototypes for foreign imports used in Haskell code in this
1909 package, for which the C implementations are also provided with the
1910 package. For example, here is a ``.cabal`` file for a hypothetical
1911 ``bindings-clib`` package that bundles the C source code for ``clib``::
1915 install-includes: clib.h
1917 Now any package that depends (directly or transitively) on the
1918 ``bindings-clib`` library can use ``clib.h``.
1920 Note that in order for files listed in :pkg-field:`install-includes` to be
1921 usable when compiling the package itself, they need to be listed in
1922 the :pkg-field:`includes` field as well.
1924 .. pkg-field:: include-dirs: directory list
1926 A list of directories to search for header files, when preprocessing
1927 with ``c2hs``, ``hsc2hs``, ``cpphs`` or the C preprocessor, and also
1928 when compiling via C. Directories can be absolute paths (e.g., for
1929 system directories) or paths that are relative to the top of the
1930 source tree. Cabal looks in these directories when attempting to
1931 locate files listed in :pkg-field:`includes` and
1932 :pkg-field:`install-includes`.
1934 .. pkg-field:: c-sources: filename list
1936 A list of C source files to be compiled and linked with the Haskell
1939 .. pkg-field:: js-sources: filename list
1941 A list of JavaScript source files to be linked with the Haskell
1942 files (only for JavaScript targets).
1944 .. pkg-field:: extra-libraries: token list
1946 A list of extra libraries to link with.
1948 .. pkg-field:: extra-ghci-libraries: token list
1950 A list of extra libraries to be used instead of 'extra-libraries'
1951 when the package is loaded with GHCi.
1953 .. pkg-field:: extra-lib-dirs: directory list
1955 A list of directories to search for libraries.
1957 .. pkg-field:: cc-options: token list
1959 Command-line arguments to be passed to the C compiler. Since the
1960 arguments are compiler-dependent, this field is more useful with the
1961 setup described in the section on `system-dependent parameters`_.
1963 .. pkg-field:: cpp-options: token list
1965 Command-line arguments for pre-processing Haskell code. Applies to
1966 haskell source and other pre-processed Haskell source like .hsc
1967 .chs. Does not apply to C code, that's what cc-options is for.
1969 .. pkg-field:: ld-options: token list
1971 Command-line arguments to be passed to the linker. Since the
1972 arguments are compiler-dependent, this field is more useful with the
1973 setup described in the section on `system-dependent parameters`_.
1975 .. pkg-field:: pkgconfig-depends: package list
1978 `pkg-config <http://www.freedesktop.org/wiki/Software/pkg-config/>`__
1979 packages, needed to build this package. They can be annotated with
1980 versions, e.g. ``gtk+-2.0 >= 2.10, cairo >= 1.0``. If no version
1981 constraint is specified, any version is assumed to be acceptable.
1982 Cabal uses ``pkg-config`` to find if the packages are available on
1983 the system and to find the extra compilation and linker options
1984 needed to use the packages.
1986 If you need to bind to a C library that supports ``pkg-config`` (use
1987 ``pkg-config --list-all`` to find out if it is supported) then it is
1988 much preferable to use this field rather than hard code options into
1991 .. pkg-field:: frameworks: token list
1993 On Darwin/MacOS X, a list of frameworks to link to. See Apple's
1994 developer documentation for more details on frameworks. This entry
1995 is ignored on all other platforms.
1997 .. pkg-field:: extra-frameworks-dirs: directory list
1999 On Darwin/MacOS X, a list of directories to search for frameworks.
2000 This entry is ignored on all other platforms.
2005 Library and executable sections may include conditional blocks, which
2006 test for various system parameters and configuration flags. The flags
2007 mechanism is rather generic, but most of the time a flag represents
2008 certain feature, that can be switched on or off by the package user.
2009 Here is an example package description file using configurations:
2011 Example: A package containing a library and executable programs
2012 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2018 Cabal-Version: >= 1.2
2021 Synopsis: Test package to test configurations
2025 Description: Enable debug support
2029 Description: Include API for web frontend.
2030 -- Cabal checks if the configuration is possible, first
2031 -- with this flag set to True and if not it tries with False
2035 Exposed-Modules: Testing.Test1
2039 GHC-Options: -DDEBUG
2041 CC-Options: "-DDEBUG"
2043 CC-Options: "-DNDEBUG"
2045 if flag(webfrontend)
2046 Build-Depends: cgi > 0.42
2047 Other-Modules: Testing.WebStuff
2051 Other-Modules: Testing.Test1
2055 CC-Options: "-DDEBUG"
2056 GHC-Options: -DDEBUG
2061 Flags, conditionals, library and executable sections use layout to
2062 indicate structure. This is very similar to the Haskell layout rule.
2063 Entries in a section have to all be indented to the same level which
2064 must be more than the section header. Tabs are not allowed to be used
2067 As an alternative to using layout you can also use explicit braces
2068 ``{}``. In this case the indentation of entries in a section does not
2069 matter, though different fields within a block must be on different
2070 lines. Here is a bit of the above example again, using braces:
2072 Example: Using explicit braces rather than indentation for layout
2073 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2079 Cabal-Version: >= 1.2
2082 Synopsis: Test package to test configurations
2086 Description: Enable debug support
2092 Exposed-Modules: Testing.Test1
2095 GHC-Options: -DDEBUG
2097 CC-Options: "-DDEBUG"
2099 CC-Options: "-DNDEBUG"
2107 .. pkg-section:: flag name
2108 :synopsis: Flag declaration.
2110 Flag section declares a flag which can be used in `conditional blocks`_.
2112 A flag section may contain the following fields:
2114 .. pkg-field:: description: freeform
2116 The description of this flag.
2118 .. pkg-field:: default: boolean
2122 The default value of this flag.
2126 This value may be `overridden in several
2127 ways <installing-packages.html#controlling-flag-assignments>`__. The
2128 rationale for having flags default to True is that users usually
2129 want new features as soon as they are available. Flags representing
2130 features that are not (yet) recommended for most users (such as
2131 experimental features or debugging support) should therefore
2132 explicitly override the default to False.
2134 .. pkg-field:: manual: boolean
2138 By default, Cabal will first try to satisfy dependencies with the
2139 default flag value and then, if that is not possible, with the
2140 negated value. However, if the flag is manual, then the default
2141 value (which can be overridden by commandline flags) will be used.
2146 Conditional blocks may appear anywhere inside a library or executable
2147 section. They have to follow rather strict formatting rules. Conditional
2148 blocks must always be of the shape
2153 property-descriptions-or-conditionals
2160 property-descriptions-or-conditionals
2162 property-descriptions-or-conditionals
2164 Note that the ``if`` and the condition have to be all on the same line.
2169 Conditions can be formed using boolean tests and the boolean operators
2170 ``||`` (disjunction / logical "or"), ``&&`` (conjunction / logical
2171 "and"), or ``!`` (negation / logical "not"). The unary ``!`` takes
2172 highest precedence, ``||`` takes lowest. Precedence levels may be
2173 overridden through the use of parentheses. For example,
2174 ``os(darwin) && !arch(i386) || os(freebsd)`` is equivalent to
2175 ``(os(darwin) && !(arch(i386))) || os(freebsd)``.
2177 The following tests are currently supported.
2180 Tests if the current operating system is *name*. The argument is
2181 tested against ``System.Info.os`` on the target system. There is
2182 unfortunately some disagreement between Haskell implementations
2183 about the standard values of ``System.Info.os``. Cabal canonicalises
2184 it so that in particular ``os(windows)`` works on all
2185 implementations. If the canonicalised os names match, this test
2186 evaluates to true, otherwise false. The match is case-insensitive.
2187 :samp:`arch({name})`
2188 Tests if the current architecture is *name*. The argument is matched
2189 against ``System.Info.arch`` on the target system. If the arch names
2190 match, this test evaluates to true, otherwise false. The match is
2192 :samp:`impl({compiler})`
2193 Tests for the configured Haskell implementation. An optional version
2194 constraint may be specified (for example ``impl(ghc >= 6.6.1)``). If
2195 the configured implementation is of the right type and matches the
2196 version constraint, then this evaluates to true, otherwise false.
2197 The match is case-insensitive.
2199 Note that including a version constraint in an ``impl`` test causes
2200 it to check for two properties:
2202 - The current compiler has the specified name, and
2204 - The compiler's version satisfied the specified version constraint
2206 As a result, ``!impl(ghc >= x.y.z)`` is not entirely equivalent to
2207 ``impl(ghc < x.y.z)``. The test ``!impl(ghc >= x.y.z)`` checks that:
2209 - The current compiler is not GHC, or
2211 - The version of GHC is earlier than version x.y.z.
2213 :samp:`flag({name})`
2214 Evaluates to the current assignment of the flag of the given name.
2215 Flag names are case insensitive. Testing for flags that have not
2216 been introduced with a flag section is an error.
2218 Constant value true.
2220 Constant value false.
2222 Resolution of Conditions and Flags
2223 """"""""""""""""""""""""""""""""""
2225 If a package descriptions specifies configuration flags the package user
2226 can `control these in several
2227 ways <installing-packages.html#controlling-flag-assignments>`__. If the
2228 user does not fix the value of a flag, Cabal will try to find a flag
2229 assignment in the following way.
2231 - For each flag specified, it will assign its default value, evaluate
2232 all conditions with this flag assignment, and check if all
2233 dependencies can be satisfied. If this check succeeded, the package
2234 will be configured with those flag assignments.
2236 - If dependencies were missing, the last flag (as by the order in which
2237 the flags were introduced in the package description) is tried with
2238 its alternative value and so on. This continues until either an
2239 assignment is found where all dependencies can be satisfied, or all
2240 possible flag assignments have been tried.
2242 To put it another way, Cabal does a complete backtracking search to find
2243 a satisfiable package configuration. It is only the dependencies
2244 specified in the :pkg-field:`build-depends` field in conditional blocks that
2245 determine if a particular flag assignment is satisfiable
2246 (:pkg-field:`build-tools` are not considered). The order of the declaration and
2247 the default value of the flags determines the search order. Flags
2248 overridden on the command line fix the assignment of that flag, so no
2249 backtracking will be tried for that flag.
2251 If no suitable flag assignment could be found, the configuration phase
2252 will fail and a list of missing dependencies will be printed. Note that
2253 this resolution process is exponential in the worst case (i.e., in the
2254 case where dependencies cannot be satisfied). There are some
2255 optimizations applied internally, but the overall complexity remains
2258 Meaning of field values when using conditionals
2259 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2261 During the configuration phase, a flag assignment is chosen, all
2262 conditionals are evaluated, and the package description is combined into
2263 a flat package descriptions. If the same field both inside a conditional
2264 and outside then they are combined using the following rules.
2266 - Boolean fields are combined using conjunction (logical "and").
2268 - List fields are combined by appending the inner items to the outer
2273 other-extensions: CPP
2275 other-extensions: MultiParamTypeClasses
2277 when compiled using GHC will be combined to
2281 other-extensions: CPP, MultiParamTypeClasses
2283 Similarly, if two conditional sections appear at the same nesting
2284 level, properties specified in the latter will come after properties
2285 specified in the former.
2287 - All other fields must not be specified in ambiguous ways. For example
2292 if flag(useothermain)
2293 Main-is: OtherMain.hs
2295 will lead to an error. Instead use
2299 if flag(useothermain)
2300 Main-is: OtherMain.hs
2307 .. pkg-section:: source-repository
2309 It is often useful to be able to specify a source revision control
2310 repository for a package. Cabal lets you specifying this information in
2311 a relatively structured form which enables other tools to interpret and
2312 make effective use of the information. For example the information
2313 should be sufficient for an automatic tool to checkout the sources.
2315 Cabal supports specifying different information for various common
2316 source control systems. Obviously not all automated tools will support
2317 all source control systems.
2319 Cabal supports specifying repositories for different use cases. By
2320 declaring which case we mean automated tools can be more useful. There
2321 are currently two kinds defined:
2323 - The ``head`` kind refers to the latest development branch of the
2324 package. This may be used for example to track activity of a project
2325 or as an indication to outside developers what sources to get for
2326 making new contributions.
2328 - The ``this`` kind refers to the branch and tag of a repository that
2329 contains the sources for this version or release of a package. For
2330 most source control systems this involves specifying a tag, id or
2331 hash of some form and perhaps a branch. The purpose is to be able to
2332 reconstruct the sources corresponding to a particular package
2333 version. This might be used to indicate what sources to get if
2334 someone needs to fix a bug in an older branch that is no longer an
2337 You can specify one kind or the other or both. As an example here are
2338 the repositories for the Cabal library. Note that the ``this`` kind of
2339 repository specifies a tag.
2343 source-repository head
2345 location: http://darcs.haskell.org/cabal/
2347 source-repository this
2349 location: http://darcs.haskell.org/cabal-branches/cabal-1.6/
2352 The exact fields are as follows:
2354 .. pkg-field:: type: token
2356 The name of the source control system used for this repository. The
2357 currently recognised types are:
2363 - ``mercurial`` (or alias ``hg``)
2364 - ``bazaar`` (or alias ``bzr``)
2368 This field is required.
2370 .. pkg-field:: location: URL
2372 The location of the repository. The exact form of this field depends
2373 on the repository type. For example:
2375 - for darcs: ``http://code.haskell.org/foo/``
2376 - for git: ``git://github.com/foo/bar.git``
2377 - for CVS: ``anoncvs@cvs.foo.org:/cvs``
2379 This field is required.
2381 .. pkg-field:: module: token
2383 CVS requires a named module, as each CVS server can host multiple
2386 This field is required for the CVS repository type and should not be
2389 .. pkg-field:: branch: token
2391 Many source control systems support the notion of a branch, as a
2392 distinct concept from having repositories in separate locations. For
2393 example CVS, SVN and git use branches while for darcs uses different
2394 locations for different branches. If you need to specify a branch to
2395 identify a your repository then specify it in this field.
2397 This field is optional.
2399 .. pkg-field:: tag: token
2401 A tag identifies a particular state of a source repository. The tag
2402 can be used with a ``this`` repository kind to identify the state of
2403 a repository corresponding to a particular package version or
2404 release. The exact form of the tag depends on the repository type.
2406 This field is required for the ``this`` repository kind.
2408 .. pkg-field:: subdir: directory
2410 Some projects put the sources for multiple packages under a single
2411 source repository. This field lets you specify the relative path
2412 from the root of the repository to the top directory for the
2413 package, i.e. the directory containing the package's ``.cabal``
2416 This field is optional. It default to empty which corresponds to the
2417 root directory of the repository.
2419 Downloading a package's source
2420 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2422 The ``cabal get`` command allows to access a package's source code -
2423 either by unpacking a tarball downloaded from Hackage (the default) or
2424 by checking out a working copy from the package's source repository.
2428 $ cabal get [FLAGS] PACKAGES
2430 The ``get`` command supports the following options:
2432 ``-d --destdir`` *PATH*
2433 Where to place the package source, defaults to (a subdirectory of)
2434 the current directory.
2435 ``-s --source-repository`` *[head\|this\|...]*
2436 Fork the package's source repository using the appropriate version
2437 control system. The optional argument allows to choose a specific
2439 ``--index-state`` *[HEAD\|@<unix-timestamp>\|<iso8601-utc-timestamp>]*
2440 Use source package index state as it existed at a previous time. Accepts
2441 unix-timestamps (e.g. ``@1474732068``), ISO8601 UTC timestamps (e.g.
2442 ``2016-09-24T17:47:48Z``), or ``HEAD`` (default).
2443 This determines which package versions are available as well as which
2444 ``.cabal`` file revision is selected (unless ``--pristine`` is used).
2446 Unpack the original pristine tarball, rather than updating the
2447 ``.cabal`` file with the latest revision from the package archive.
2449 Custom setup scripts
2450 --------------------
2452 .. pkg-section:: custom-setup
2453 :synopsis: Custom Setup.hs build information.
2456 The optional :pkg-section:`custom-setup` stanza contains information needed
2457 for the compilation of custom ``Setup.hs`` scripts,
2463 base >= 4.5 && < 4.11,
2466 .. pkg-field:: setup-depends: package list
2469 The dependencies needed to compile ``Setup.hs``. See the
2470 :pkg-field:`build-depends` field for a description of the syntax expected by
2473 Autogenerated modules
2474 ---------------------
2476 Modules that are built automatically at setup, created with a custom
2477 setup script, must appear on :pkg-field:`other-modules` for the library,
2478 executable, test-suite or benchmark stanzas or also on
2479 :pkg-field:`library:exposed-modules` for libraries to be used, but are not
2480 really on the package when distributed. This makes commands like sdist fail
2481 because the file is not found.
2483 This special modules must appear again on the :pkg-field:`autogen-modules`
2484 field of the stanza that is using it, besides :pkg-field:`other-modules` or
2485 :pkg-field:`library:exposed-modules`. With this there is no need to create
2486 complex build hooks for this poweruser case.
2488 .. pkg-field:: autogen-modules: module list
2490 .. TODO: document autogen-modules field
2492 Right now :pkg-field:`executable:main-is` modules are not supported on
2493 :pkg-field:`autogen-modules`.
2498 default-language: Haskell2010
2509 default-language: Haskell2010
2518 Accessing data files from package code
2519 --------------------------------------
2521 The placement on the target system of files listed in
2522 the :pkg-field:`data-files` field varies between systems, and in some cases
2523 one can even move packages around after installation (see `prefix
2524 independence <installing-packages.html#prefix-independence>`__). To
2525 enable packages to find these files in a portable way, Cabal generates a
2526 module called :file:`Paths_{pkgname}` (with any hyphens in *pkgname*
2527 replaced by underscores) during building, so that it may be imported by
2528 modules of the package. This module defines a function
2530 .. code-block:: haskell
2532 getDataFileName :: FilePath -> IO FilePath
2534 If the argument is a filename listed in the :pkg-field:`data-files` field, the
2535 result is the name of the corresponding file on the system on which the
2540 If you decide to import the :file:`Paths_{pkgname}` module then it
2541 *must* be listed in the :pkg-field:`other-modules` field just like any other
2542 module in your package and on :pkg-field:`autogen-modules` as the file is
2545 The :file:`Paths_{pkgname}` module is not platform independent, as any
2546 other autogenerated module, so it does not get included in the source
2547 tarballs generated by ``sdist``.
2549 The :file:`Paths_{pkgname}` module also includes some other useful
2550 functions and values, which record the version of the package and some
2551 other directories which the package has been configured to be installed
2552 into (e.g. data files live in ``getDataDir``):
2554 .. code-block:: haskell
2558 getBinDir :: IO FilePath
2559 getLibDir :: IO FilePath
2560 getDynLibDir :: IO FilePath
2561 getDataDir :: IO FilePath
2562 getLibexecDir :: IO FilePath
2563 getSysconfDir :: IO FilePath
2565 The actual location of all these directories can be individually
2566 overridden at runtime using environment variables of the form
2567 ``pkg_name_var``, where ``pkg_name`` is the name of the package with all
2568 hyphens converted into underscores, and ``var`` is either ``bindir``,
2569 ``libdir``, ``dynlibdir``, ``datadir``, ``libexedir`` or ``sysconfdir``. For example,
2570 the configured data directory for ``pretty-show`` is controlled with the
2571 ``pretty_show_datadir`` environment variable.
2573 Accessing the package version
2574 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2576 The aforementioned auto generated :file:`Paths_{pkgname}` module also
2577 exports the constant ``version ::``
2578 `Version <http://hackage.haskell.org/package/base/docs/Data-Version.html>`__
2579 which is defined as the version of your package as specified in the
2582 System-dependent parameters
2583 ---------------------------
2585 For some packages, especially those interfacing with C libraries,
2586 implementation details and the build procedure depend on the build
2587 environment. The ``build-type`` ``Configure`` can be used to handle many
2588 such situations. In this case, ``Setup.hs`` should be:
2590 .. code-block:: haskell
2592 import Distribution.Simple
2593 main = defaultMainWithHooks autoconfUserHooks
2595 Most packages, however, would probably do better using the ``Simple``
2596 build type and `configurations`_.
2598 The :pkg-field:`build-type` ``Configure`` differs from ``Simple`` in two ways:
2600 - The package root directory must contain a shell script called
2601 ``configure``. The configure step will run the script. This
2602 ``configure`` script may be produced by
2603 `autoconf <http://www.gnu.org/software/autoconf/>`__ or may be
2604 hand-written. The ``configure`` script typically discovers
2605 information about the system and records it for later steps, e.g. by
2606 generating system-dependent header files for inclusion in C source
2607 files and preprocessed Haskell source files. (Clearly this won't work
2608 for Windows without MSYS or Cygwin: other ideas are needed.)
2610 - If the package root directory contains a file called
2611 *package*\ ``.buildinfo`` after the configuration step, subsequent
2612 steps will read it to obtain additional settings for `build
2613 information`_ fields,to be merged with the ones
2614 given in the ``.cabal`` file. In particular, this file may be
2615 generated by the ``configure`` script mentioned above, allowing these
2616 settings to vary depending on the build environment.
2618 The build information file should have the following structure:
2622 ``executable:`` *name* *buildinfo*
2624 ``executable:`` *name* *buildinfo* ...
2626 where each *buildinfo* consists of settings of fields listed in the
2627 section on `build information`_. The first one (if
2628 present) relates to the library, while each of the others relate to the
2629 named executable. (The names must match the package description, but you
2630 don't have to have entries for all of them.)
2632 Neither of these files is required. If they are absent, this setup
2633 script is equivalent to ``defaultMain``.
2635 Example: Using autoconf
2636 ^^^^^^^^^^^^^^^^^^^^^^^
2638 This example is for people familiar with the
2639 `autoconf <http://www.gnu.org/software/autoconf/>`__ tools.
2641 In the X11 package, the file ``configure.ac`` contains:
2643 .. code-block:: shell
2645 AC_INIT([Haskell X11 package], [1.1], [libraries@haskell.org], [X11])
2647 # Safety check: Ensure that we are in the correct source directory.
2648 AC_CONFIG_SRCDIR([X11.cabal])
2650 # Header file to place defines in
2651 AC_CONFIG_HEADERS([include/HsX11Config.h])
2653 # Check for X11 include paths and libraries
2655 AC_TRY_CPP([#include <X11/Xlib.h>],,[no_x=yes])
2657 # Build the package if we found X11 stuff
2658 if test "$no_x" = yes
2659 then BUILD_PACKAGE_BOOL=False
2660 else BUILD_PACKAGE_BOOL=True
2662 AC_SUBST([BUILD_PACKAGE_BOOL])
2664 AC_CONFIG_FILES([X11.buildinfo])
2667 Then the setup script will run the ``configure`` script, which checks
2668 for the presence of the X11 libraries and substitutes for variables in
2669 the file ``X11.buildinfo.in``:
2673 buildable: @BUILD_PACKAGE_BOOL@
2674 cc-options: @X_CFLAGS@
2675 ld-options: @X_LIBS@
2677 This generates a file ``X11.buildinfo`` supplying the parameters needed
2683 cc-options: -I/usr/X11R6/include
2684 ld-options: -L/usr/X11R6/lib
2686 The ``configure`` script also generates a header file
2687 ``include/HsX11Config.h`` containing C preprocessor defines recording
2688 the results of various tests. This file may be included by C source
2689 files and preprocessed Haskell source files in the package.
2693 Packages using these features will also need to list additional
2694 files such as ``configure``, templates for ``.buildinfo`` files, files
2695 named only in ``.buildinfo`` files, header files and so on in the
2696 :pkg-field:`extra-source-files` field to ensure that they are included in
2697 source distributions. They should also list files and directories generated
2698 by ``configure`` in the :pkg-field:`extra-tmp-files` field to ensure that
2699 they are removed by ``setup clean``.
2701 Quite often the files generated by ``configure`` need to be listed
2702 somewhere in the package description (for example, in the
2703 :pkg-field:`install-includes` field). However, we usually don't want generated
2704 files to be included in the source tarball. The solution is again
2705 provided by the ``.buildinfo`` file. In the above example, the following
2706 line should be added to ``X11.buildinfo``:
2710 install-includes: HsX11Config.h
2712 In this way, the generated ``HsX11Config.h`` file won't be included in
2713 the source tarball in addition to ``HsX11Config.h.in``, but it will be
2714 copied to the right location during the install process. Packages that
2715 use custom ``Setup.hs`` scripts can update the necessary fields
2716 programmatically instead of using the ``.buildinfo`` file.
2718 Conditional compilation
2719 -----------------------
2721 Sometimes you want to write code that works with more than one version
2722 of a dependency. You can specify a range of versions for the dependency
2723 in the :pkg-field:`build-depends`, but how do you then write the code that can
2724 use different versions of the API?
2726 Haskell lets you preprocess your code using the C preprocessor (either
2727 the real C preprocessor, or ``cpphs``). To enable this, add
2728 ``extensions: CPP`` to your package description. When using CPP, Cabal
2729 provides some pre-defined macros to let you test the version of
2730 dependent packages; for example, suppose your package works with either
2731 version 3 or version 4 of the ``base`` package, you could select the
2732 available version in your Haskell modules like this:
2736 #if MIN_VERSION_base(4,0,0)
2737 ... code that works with base-4 ...
2739 ... code that works with base-3 ...
2742 In general, Cabal supplies a macro
2743 ``MIN_VERSION_``\ *``package``*\ ``_(A,B,C)`` for each package depended
2744 on via :pkg-field:`build-depends`. This macro is true if the actual version of
2745 the package in use is greater than or equal to ``A.B.C`` (using the
2746 conventional ordering on version numbers, which is lexicographic on the
2747 sequence, but numeric on each component, so for example 1.2.0 is greater
2750 Since version 1.20, there is also the ``MIN_TOOL_VERSION_``\ *``tool``*
2751 family of macros for conditioning on the version of build tools used to
2752 build the program (e.g. ``hsc2hs``).
2754 Cabal places the definitions of these macros into an
2755 automatically-generated header file, which is included when
2756 preprocessing Haskell source code by passing options to the C
2759 Cabal also allows to detect when the source code is being used for
2760 generating documentation. The ``__HADDOCK_VERSION__`` macro is defined
2761 only when compiling via Haddock_
2762 instead of a normal Haskell compiler. The value of the
2763 ``__HADDOCK_VERSION__`` macro is defined as ``A*1000 + B*10 + C``, where
2764 ``A.B.C`` is the Haddock version. This can be useful for working around
2765 bugs in Haddock or generating prettier documentation in some special
2768 More complex packages
2769 ---------------------
2771 For packages that don't fit the simple schemes described above, you have
2774 - By using the :pkg-field:`build-type` ``Custom``, you can supply your own
2775 ``Setup.hs`` file, and customize the simple build infrastructure
2776 using *hooks*. These allow you to perform additional actions before
2777 and after each command is run, and also to specify additional
2778 preprocessors. A typical ``Setup.hs`` may look like this:
2780 .. code-block:: haskell
2782 import Distribution.Simple
2783 main = defaultMainWithHooks simpleUserHooks { postHaddock = posthaddock }
2785 posthaddock args flags desc info = ....
2787 See ``UserHooks`` in
2788 `Distribution.Simple <../release/cabal-latest/doc/API/Cabal/Distribution-Simple.html>`__
2789 for the details, but note that this interface is experimental, and
2790 likely to change in future releases.
2792 If you use a custom ``Setup.hs`` file you should strongly consider
2793 adding a :pkg-section:`custom-setup` stanza with a
2794 :pkg-field:`custom-setup:setup-depends` field to ensure that your setup
2795 script does not break with future dependency versions.
2797 - You could delegate all the work to ``make``, though this is unlikely
2798 to be very portable. Cabal supports this with the :pkg-field:`build-type`
2799 ``Make`` and a trivial setup library
2800 `Distribution.Make <../release/cabal-latest/doc/API/Cabal/Distribution-Make.html>`__,
2801 which simply parses the command line arguments and invokes ``make``.
2802 Here ``Setup.hs`` should look like this:
2804 .. code-block:: haskell
2806 import Distribution.Make
2809 The root directory of the package should contain a ``configure``
2810 script, and, after that has run, a ``Makefile`` with a default target
2811 that builds the package, plus targets ``install``, ``register``,
2812 ``unregister``, ``clean``, ``dist`` and ``docs``. Some options to
2813 commands are passed through as follows:
2815 - The ``--with-hc-pkg``, ``--prefix``, ``--bindir``, ``--libdir``,
2816 ``--dynlibdir``, ``--datadir``, ``--libexecdir`` and ``--sysconfdir`` options to
2817 the ``configure`` command are passed on to the ``configure``
2818 script. In addition the value of the ``--with-compiler`` option is
2819 passed in a ``--with-hc`` option and all options specified with
2820 ``--configure-option=`` are passed on.
2822 - The ``--destdir`` option to the ``copy`` command becomes a setting
2823 of a ``destdir`` variable on the invocation of ``make copy``. The
2824 supplied ``Makefile`` should provide a ``copy`` target, which will
2825 probably look like this:
2827 .. code-block:: make
2830 $(MAKE) install prefix=$(destdir)/$(prefix) \
2831 bindir=$(destdir)/$(bindir) \
2832 libdir=$(destdir)/$(libdir) \
2833 dynlibdir=$(destdir)/$(dynlibdir) \
2834 datadir=$(destdir)/$(datadir) \
2835 libexecdir=$(destdir)/$(libexecdir) \
2836 sysconfdir=$(destdir)/$(sysconfdir) \
2838 - Finally, with the :pkg-field:`build-type` ``Custom``, you can also write your
2839 own setup script from scratch. It must conform to the interface
2840 described in the section on `building and installing
2841 packages <installing-packages.html>`__, and you may use the Cabal
2842 library for all or part of the work. One option is to copy the source
2843 of ``Distribution.Simple``, and alter it for your needs. Good luck.
2846 .. include:: references.inc