1 Package Description — <package>.cabal File
2 ==========================================
4 The package description file, commonly known as "the Cabal file", describes the
5 contents of a package. The Cabal package is the unit of distribution. When
6 installed, its purpose is to make available one or more:
8 - Haskell programs (executables); and/or
10 - libraries, exposing a number of Haskell modules.
12 Public library components can be depended upon by other Cabal packages and all
13 library components (both public and private) can be depended upon by other
14 components of the same package.
16 Internally, the package may consist of much more than a bunch of Haskell
17 modules: it may also have C source code and header files, source code
18 meant for preprocessing, documentation, test cases, auxiliary tools etc.
20 A package is identified by a globally-unique *package name*, which
21 consists of one or more alphanumeric words separated by hyphens. To
22 avoid ambiguity, each of these words should contain at least one letter.
23 Chaos will result if two distinct packages with the same name are
24 installed on the same system. A particular version of the package is
25 distinguished by a *version number*, consisting of a sequence of one or
26 more integers separated by dots. These can be combined to form a single
27 text string called the *package ID*, using a hyphen to separate the name
28 from the version, e.g. "``HUnit-1.1``".
32 Packages are not part of the Haskell language; they simply
33 populate the hierarchical space of module names. In GHC 6.6 and later a
34 program may contain multiple modules with the same name if they come
35 from separate packages; in all other current Haskell systems packages
36 may not overlap in the modules they provide, including hidden modules.
41 Suppose you have a directory hierarchy containing the source files that
42 make up your package. You will need to add two more files to the root
43 directory of the package:
45 :file:`{package-name}.cabal`
46 a Unicode UTF-8 text file containing a package description. For
47 details of the syntax of this file, see the section on
48 `package descriptions`_.
51 a single-module Haskell program to perform various setup tasks (with
52 the interface described in the section on :ref:`setup-commands`).
53 This module should import only modules that will be present in all Haskell
54 implementations, including modules of the Cabal library. The content of
55 this file is determined by the :pkg-field:`build-type` setting in the
56 ``.cabal`` file. In most cases it will be trivial, calling on the Cabal
57 library to do most of the work.
59 Once you have these, you can create a source bundle of this directory
60 for distribution. Building of the package is demonstrated in the section
61 :ref:`building-packages`.
63 One of the purposes of Cabal is to make it easier to build a package
64 with different Haskell implementations. So it provides abstractions of
65 features present in different Haskell implementations and wherever
66 possible it is best to take advantage of these to increase portability.
67 Where necessary however it is possible to use specific features of
68 specific implementations. For example one of the pieces of information a
69 package author can put in the package's ``.cabal`` file is what language
70 extensions the code uses. This is far preferable to specifying flags for
71 a specific compiler as it allows Cabal to pick the right flags for the
72 Haskell implementation that the user picks. It also allows Cabal to
73 figure out if the language extension is even supported by the Haskell
74 implementation that the user picks. Where compiler-specific options are
75 needed however, there is an "escape hatch" available. The developer can
76 specify implementation-specific options and more generally there is a
77 configuration mechanism to customise many aspects of how a package is
78 built depending on the Haskell implementation, the Operating system,
79 computer architecture and user-specified configuration flags.
87 default-language: Haskell2010
88 build-depends: base >= 4 && < 5
90 extensions: ForeignFunctionInterface
93 build-depends: Win32 >= 2.1 && < 2.6
95 Example: A package containing a simple library
96 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
98 The HUnit package contains a file ``HUnit.cabal`` containing:
105 synopsis: A unit testing framework for Haskell
106 homepage: http://hunit.sourceforge.net/
108 author: Dean Herington
109 license: BSD-3-Clause
110 license-file: LICENSE
114 build-depends: base >= 2 && < 4
115 exposed-modules: Test.HUnit.Base, Test.HUnit.Lang,
116 Test.HUnit.Terminal, Test.HUnit.Text, Test.HUnit
117 default-extensions: CPP
118 default-language: Haskell2010
120 and the following ``Setup.hs``:
122 .. code-block:: haskell
124 import Distribution.Simple
127 Example: A package containing executable programs
128 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135 synopsis: Small package with two programs
136 author: Angela Author
137 license: BSD-3-Clause
141 build-depends: HUnit >= 1.1.1 && < 1.2
143 hs-source-dirs: prog1
144 default-language: Haskell2010
147 -- A different main.hs because of hs-source-dirs.
149 build-depends: HUnit >= 1.1.1 && < 1.2
150 hs-source-dirs: prog2
152 default-language: Haskell2010
154 with ``Setup.hs`` the same as above.
156 Example: A package containing a library and executable programs
157 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
164 synopsis: Package with library and two programs
165 license: BSD-3-Clause
166 author: Angela Author
170 build-depends: HUnit >= 1.1.1 && < 1.2
172 exposed-modules: A, B, C
173 default-language: Haskell2010
177 hs-source-dirs: prog1
179 default-language: Haskell2010
182 -- A different main.hs because of hs-source-dirs.
184 -- No bound on a library provided by the same package.
185 build-depends: TestPackage
186 hs-source-dirs: prog2
188 default-language: Haskell2010
190 with ``Setup.hs`` the same as above. Note that any library modules
191 required (directly or indirectly) by an executable must be listed again.
193 The trivial setup script used in these examples uses the *simple build
194 infrastructure* provided by the Cabal library (see
195 `Distribution.Simple <https://hackage.haskell.org/package/Cabal/docs/Distribution-Simple.html>`__).
196 The simplicity lies in its interface rather that its implementation. It
197 automatically handles preprocessing with standard preprocessors, and
198 builds packages for all the Haskell implementations.
200 The simple build infrastructure can also handle packages where building
201 is governed by system-dependent parameters, if you specify a little more
202 (see the section on `system-dependent parameters`_).
203 A few packages require `more elaborate solutions <#more-complex-packages>`_.
210 The package description file must have a name ending in "``.cabal``". It
211 must be a Unicode text file encoded using valid UTF-8. There must be
212 exactly one such file in the directory. The first part of the name is
213 usually the package name, and some of the tools that operate on Cabal
214 packages require this; specifically, Hackage rejects packages which
215 don't follow this rule.
217 In the package description file, lines whose first non-whitespace
218 characters are "``--``" are treated as comments and ignored.
220 This file should contain a number global property descriptions and
223 - The `package properties`_ describe the package
224 as a whole, such as name, license, author, etc.
226 - Optionally, a number of *configuration flags* can be declared. These
227 can be used to enable or disable certain features of a package. (see
228 the section on `configurations`_).
230 - The (optional) library section specifies the `library`_ properties and
231 relevant `build information`_.
233 - Following is an arbitrary number of executable sections which describe
234 an executable program and relevant `build information`_.
236 Each section consists of a number of property descriptions in the form
237 of field/value pairs, with a syntax roughly like mail message headers.
239 - Case is not significant in field names, but is significant in field
242 - To continue a field value, indent the next line relative to the field
245 - Field names may be indented, but all field values in the same section
246 must use the same indentation.
248 - Tabs are *not* allowed as indentation characters due to a missing
249 standard interpretation of tab width.
251 - Before Cabal 3.0, to get a blank line in a field value, use an indented "``.``"
253 The syntax of the value depends on the field. Field types include:
255 *token*, *filename*, *directory*
256 Either a sequence of one or more non-space non-comma characters, or
257 a quoted string in Haskell 98 lexical syntax. The latter can be used
258 for escaping whitespace, for example:
259 ``ghc-options: -Wall "-with-rtsopts=-T -I1"``. Unless otherwise
260 stated, relative filenames and directories are interpreted from the
261 package root directory.
262 *freeform*, *URL*, *address*
263 An arbitrary, uninterpreted string.
265 A letter followed by zero or more alphanumerics or underscores.
267 A compiler flavor (one of: ``GHC``, ``UHC`` or ``LHC``)
268 followed by a version range. For example, ``GHC ==6.10.3``, or
269 ``LHC >=0.6 && <0.8``.
271 Modules and preprocessors
272 ^^^^^^^^^^^^^^^^^^^^^^^^^
274 Haskell module names listed in the :pkg-field:`library:exposed-modules` and
275 :pkg-field:`library:other-modules` fields may correspond to Haskell source
276 files, i.e. with names ending in "``.hs``" or "``.lhs``", or to inputs for
277 various Haskell preprocessors. The simple build infrastructure understands the
280 - ``.gc`` (:hackage-pkg:`greencard`)
281 - ``.chs`` (:hackage-pkg:`c2hs`)
282 - ``.hsc`` (:hackage-pkg:`hsc2hs`)
283 - ``.y`` and ``.ly`` (happy_)
285 - ``.cpphs`` (cpphs_)
287 When building, Cabal will automatically run the appropriate preprocessor
288 and compile the Haskell module it produces. For the ``c2hs`` and
289 ``hsc2hs`` preprocessors, Cabal will also automatically add, compile and
290 link any C sources generated by the preprocessor (produced by
291 ``hsc2hs``'s ``#def`` feature or ``c2hs``'s auto-generated wrapper
292 functions). Dependencies on pre-processors are specified via the
293 :pkg-field:`build-tools` or :pkg-field:`build-tool-depends` fields.
295 Some fields take lists of values, which are optionally separated by
296 commas, except for the :pkg-field:`build-depends` field, where the commas are
299 Some fields are marked as required. All others are optional, and unless
300 otherwise specified have empty default values.
305 These fields may occur in the first top-level properties section and
306 describe the package as a whole:
308 .. pkg-field:: name: package-name (required)
310 The unique name of the package, without the version number.
312 As pointed out in the section on `package descriptions`_, some
313 tools require the package-name specified for this field to match
314 the package description's file-name :file:`{package-name}.cabal`.
316 Package names are case-sensitive and must match the regular expression
317 (i.e. alphanumeric "words" separated by dashes; each alphanumeric
318 word must contain at least one letter):
319 ``[[:digit:]]*[[:alpha:]][[:alnum:]]*(-[[:digit:]]*[[:alpha:]][[:alnum:]]*)*``.
321 Or, expressed in ABNF_:
325 package-name = package-name-part *("-" package-name-part)
326 package-name-part = *DIGIT UALPHA *UALNUM
328 UALNUM = UALPHA / DIGIT
329 UALPHA = ... ; set of alphabetic Unicode code-points
333 Hackage restricts package names to the ASCII subset.
335 .. pkg-field:: version: numbers (required)
337 The package version number, usually consisting of a sequence of
338 natural numbers separated by dots, i.e. as the regular
339 expression ``[0-9]+([.][0-9]+)*`` or expressed in ABNF_:
343 package-version = 1*DIGIT *("." 1*DIGIT)
345 .. pkg-field:: cabal-version: x.y[.z]
347 The version of the Cabal specification that this package
348 description uses. The Cabal specification does slowly evolve (see
349 also :ref:`spec-history`), introducing new features and
350 occasionally changing the meaning of existing features.
351 Specifying which version of the specification you are using
352 enables programs which process the package description to know
353 what syntax to expect and what each part means.
355 The version number you specify will affect both compatibility and
356 behaviour. Most tools (including the Cabal library and the ``cabal``
357 program) understand a range of versions of the Cabal specification.
358 Older tools will of course only work with older versions of the
359 Cabal specification that was known at the time. Most of the time,
360 tools that are too old will recognise this fact and produce a
361 suitable error message. Likewise, ``cabal check`` will tell you
362 whether the version number is sufficiently high for the features
363 you use in the package description.
365 As for behaviour, new versions of the Cabal specification can change the
366 meaning of existing syntax. This means if you want to take advantage
367 of the new meaning or behaviour then you must specify the newer
368 Cabal version. Tools are expected to use the meaning and behaviour
369 appropriate to the version given in the package description.
371 In particular, the syntax of package descriptions changed
372 significantly with Cabal version 1.2 and the :pkg-field:`cabal-version`
373 field is now required. Files written in the old syntax are still
374 recognized, so if you require compatibility with very old Cabal
375 versions then you may write your package description file using the
376 old syntax. Please consult the user's guide of an older Cabal
377 version for a description of that syntax.
379 Starting with ``cabal-version: 2.2`` this field is only valid if
380 fully contained in the very first line of a package description
381 and ought to adhere to the ABNF_ grammar
385 newstyle-spec-version-decl = "cabal-version" *WS ":" *WS newstyle-spec-version *WS
387 newstyle-spec-version = NUM "." NUM [ "." NUM ]
389 NUM = DIGIT0 / DIGITP 1*DIGIT0
397 For package descriptions using a format prior to
398 ``cabal-version: 1.12`` the legacy syntax resembling a version
401 .. code-block:: cabal
403 cabal-version: >= 1.10
407 This legacy syntax is supported up until ``cabal-version: >=
408 2.0`` it is however strongly recommended to avoid using the
409 legacy syntax. See also :issue:`4899`.
413 .. pkg-field:: build-type: identifier
415 :default: ``Custom`` or ``Simple``
417 The type of build used by this package. Build types are the
419 `BuildType <https://hackage.haskell.org/package/Cabal-syntax/docs/Distribution-Types-BuildType.html#t:BuildType>`__
420 type. This field is optional and when missing, its default value
421 is inferred according to the following rules:
423 - When :pkg-field:`cabal-version` is set to ``2.2`` or higher,
424 the default is ``Simple`` unless a :pkg-section:`custom-setup`
425 exists, in which case the inferred default is ``Custom``.
427 - For lower :pkg-field:`cabal-version` values, the default is
428 ``Custom`` unconditionally.
430 If the build type is anything other than ``Custom``, then the
431 ``Setup.hs`` file *must* be exactly the standardized content
432 discussed below. This is because in these cases, ``cabal`` will
433 ignore the ``Setup.hs`` file completely, whereas other methods of
434 package management, such as ``runhaskell Setup.hs [CMD]``, still
435 rely on the ``Setup.hs`` file.
437 For build type ``Simple``, the contents of ``Setup.hs`` must be:
439 .. code-block:: haskell
441 import Distribution.Simple
444 For build type ``Hooks``, the contents of ``Setup.hs`` must be:
446 .. code-block:: haskell
448 import Distribution.Simple
449 import SetupHooks (setupHooks)
450 main = defaultMainWithSetupHooks setupHooks
452 For build type ``Configure`` (see the section on `system-dependent
453 parameters`_ below), the contents of
454 ``Setup.hs`` must be:
456 .. code-block:: haskell
458 import Distribution.Simple
459 main = defaultMainWithHooks autoconfUserHooks
461 For build type ``Make`` (see the section on `more complex packages`_ below),
462 the contents of ``Setup.hs`` must be:
464 .. code-block:: haskell
466 import Distribution.Make
469 For build type ``Custom``, the file ``Setup.hs`` can be customized,
470 and will be used both by ``cabal`` and other tools.
472 For most packages, the build type ``Simple`` is sufficient. For more exotic
473 needs, the ``Hooks`` build type is recommended; see :ref:`setup-hooks`.
475 .. pkg-field:: license: SPDX expression
479 The type of license under which this package is distributed.
481 Starting with ``cabal-version: 2.2`` the ``license`` field takes a
482 (case-sensitive) SPDX expression such as
484 .. code-block:: cabal
486 license: Apache-2.0 AND (MIT OR GPL-2.0-or-later)
488 See `SPDX IDs: How to use <https://spdx.org/ids-how>`__ for more
489 examples of SPDX expressions.
492 `list of SPDX license identifiers <https://spdx.org/licenses/>`__
493 is a function of the :pkg-field:`cabal-version` value as defined
494 in the following table:
496 +--------------------------+--------------------+
497 | Cabal specification | SPDX license list |
498 | version | version |
500 +==========================+====================+
501 | ``cabal-version: 2.2`` | ``3.0 2017-12-28`` |
502 +--------------------------+--------------------+
503 | ``cabal-version: 2.4`` | ``3.2 2018-07-10`` |
504 +--------------------------+--------------------+
506 **Pre-SPDX Legacy Identifiers**
508 The license identifier in the table below are defined for
509 ``cabal-version: 2.0`` and previous versions of the Cabal
512 +--------------------------+-----------------+
513 | :pkg-field:`license` | Note |
516 +==========================+=================+
520 +--------------------------+-----------------+
524 +--------------------------+-----------------+
525 | ``AGPL`` | since 1.18 |
527 +--------------------------+-----------------+
528 | ``BSD2`` | since 1.20 |
529 +--------------------------+-----------------+
531 +--------------------------+-----------------+
533 +--------------------------+-----------------+
534 | ``ISC`` | since 1.22 |
535 +--------------------------+-----------------+
536 | ``MPL-2.0`` | since 1.20 |
537 +--------------------------+-----------------+
540 +--------------------------+-----------------+
541 | ``PublicDomain`` | |
542 +--------------------------+-----------------+
543 | ``AllRightsReserved`` | |
544 +--------------------------+-----------------+
545 | ``OtherLicense`` | |
546 +--------------------------+-----------------+
549 .. pkg-field:: license-file: filename
551 See :pkg-field:`license-files`.
553 .. pkg-field:: license-files: filename list
556 The name of a file(s) containing the precise copyright license for
557 this package. The license file(s) will be installed with the
560 If you have multiple license files then use the :pkg-field:`license-files`
561 field instead of (or in addition to) the :pkg-field:`license-file` field.
563 .. pkg-field:: copyright: freeform
565 The content of a copyright notice, typically the name of the holder
566 of the copyright on the package and the year(s) from which copyright
567 is claimed. For example::
569 copyright: (c) 2006-2007 Joe Bloggs
571 .. pkg-field:: author: freeform
573 The original author of the package.
575 Remember that ``.cabal`` files are Unicode, using the UTF-8
578 .. pkg-field:: maintainer: address
580 The current maintainer or maintainers of the package. This is an
581 e-mail address to which users should send bug reports, feature
582 requests and patches.
584 .. pkg-field:: stability: freeform
586 The stability level of the package, e.g. ``alpha``,
587 ``experimental``, ``provisional``, ``stable``.
589 .. pkg-field:: homepage: URL
591 The package homepage.
593 .. pkg-field:: bug-reports: URL
595 The URL where users should direct bug reports. This would normally
598 - A ``mailto:`` URL, e.g. for a person or a mailing list.
600 - An ``http:`` (or ``https:``) URL for an online bug tracking
603 For example Cabal itself uses a web-based bug tracking system
607 bug-reports: https://github.com/haskell/cabal/issues
609 .. pkg-field:: package-url: URL
611 The location of a source bundle for the package. The distribution
612 should be a Cabal package.
614 .. pkg-field:: synopsis: freeform
616 A very short description of the package, for use in a table of
617 packages. This is your headline, so keep it short (one line) but as
618 informative as possible. Save space by not including the package
619 name or saying it's written in Haskell.
621 .. pkg-field:: description: freeform
623 Description of the package. This may be several paragraphs, and
624 should be aimed at a Haskell programmer who has never heard of your
627 For library packages, this field is used as prologue text by
628 :ref:`setup-haddock` and thus may contain the same markup as Haddock_
629 documentation comments.
631 .. pkg-field:: category: freeform
633 A classification category for future use by the package catalogue
634 Hackage_. These categories have not
635 yet been specified, but the upper levels of the module hierarchy
638 .. pkg-field:: tested-with: compiler list
640 A list of compilers and versions against which the package has been
641 tested (or at least built). The value of this field is not used by Cabal
642 and is rather intended as extra metadata for use by third party
643 tooling, such as e.g. CI tooling.
645 Here's a typical usage example:
649 tested-with: GHC == 9.10.1, GHC == 9.8.2, GHC == 9.6.5
651 The same can be spread over several lines, for instance:
655 tested-with: GHC == 9.10.1
659 The separating comma can also be dropped altogether:
668 However, this alternative might
669 `disappear <https://github.com/haskell/cabal/issues/4894#issuecomment-909008657>`__
672 Starting with :pkg-field:`cabal-version` 3.0,
673 there are further conveniences.
675 1. A preceding ``,`` is allowed, so a bullet-list style
676 is possible (recommended):
686 2. A concise set notation syntax is available:
690 tested-with: GHC == { 9.10.1, 9.8.2, 9.6.5 }
692 .. pkg-field:: data-files: filename list
694 A list of files to be installed for run-time use by the package.
695 This is useful for packages that use a large amount of static data,
696 such as tables of values or code templates. Cabal provides a way to
697 `find these files at run-time <#accessing-data-files-from-package-code>`_.
699 A limited form of ``*`` wildcards in file names, for example
700 ``data-files: images/*.png`` matches all the ``.png`` files in the
701 ``images`` directory. ``data-files: audio/**/*.mp3`` matches all
702 the ``.mp3`` files in the ``audio`` directory, including
705 The specific limitations of this wildcard syntax are
707 - ``*`` wildcards are only allowed in place of the file name, not
708 in the directory name or file extension. It must replace the
709 whole file name (e.g., ``*.html`` is allowed, but
710 ``chapter-*.html`` is not). If a wildcard is used, it must be
711 used with an extension, so ``data-files: data/*`` is not
714 - Prior to Cabal 2.4, when matching a wildcard plus extension, a
715 file's full extension must match exactly, so ``*.gz`` matches
716 ``foo.gz`` but not ``foo.tar.gz``. This restriction has been
717 lifted when ``cabal-version: 2.4`` or greater so that ``*.gz``
718 does match ``foo.tar.gz``
720 - ``*`` wildcards will not match if the file name is empty (e.g.,
721 ``*.html`` will not match ``foo/.html``).
723 - ``**`` wildcards can only appear as the final path component
724 before the file name (e.g., ``data/**/images/*.jpg`` is not
727 - Prior to Cabal 3.8, if a ``**`` wildcard is used, then
728 the file name must include a ``*`` wildcard (e.g.,
729 ``data/**/README.rst`` was not allowed). As of ``cabal-version:
730 3.8`` or greater, this restriction is lifted.
732 - A wildcard that does not match any files is an error.
734 The reason for providing only a very limited form of wildcard is to
735 concisely express the common case of a large number of related files
736 of the same file type without making it too easy to accidentally
737 include unwanted files.
739 On efficiency: if you use ``**`` patterns, the directory tree will
740 be walked starting with the parent directory of the ``**``. If
741 that's the root of the project, this might include ``.git/``,
742 ``dist-newstyle/``, or other large directories! To avoid this
743 behaviour, put the files that wildcards will match against in
746 ``**`` wildcards are available starting in Cabal 2.4
747 and `bug-free since Cabal 3.0 <https://github.com/haskell/cabal/issues/6125#issuecomment-1379878419>`_.
749 .. pkg-field:: data-dir: directory
751 The directory where Cabal looks for data files to install, relative
752 to the source directory. By default, Cabal will look in the source
755 .. pkg-field:: extra-source-files: filename list
757 A list of additional files to be included in source distributions built with :ref:`setup-sdist`.
758 As with :pkg-field:`data-files` it can use a limited form of ``*`` wildcards in file names.
759 Files listed here are tracked by ``cabal build``; changes in these files cause (partial) rebuilds.
761 .. pkg-field:: extra-doc-files: filename list
764 A list of additional files to be included in source distributions,
765 and also copied to the html directory when Haddock documentation is
766 generated. As with :pkg-field:`data-files` it can use a limited form of
767 ``*`` wildcards in file names.
769 .. pkg-field:: extra-tmp-files: filename list
771 A list of additional files or directories to be removed by
772 :ref:`setup-clean`. These would typically be additional files created by
773 additional hooks, such as the scheme described in the section on
774 `system-dependent parameters`_.
776 .. pkg-field:: extra-files: filename list
778 A list of additional files to be included in source distributions built with :ref:`setup-sdist`.
779 As with :pkg-field:`data-files` it can use a limited form of ``*`` wildcards in file names.
784 .. pkg-section:: library name
785 :synopsis: Library build information.
787 Build information for libraries.
789 A package can include zero or more library components. A library can be
790 unnamed or named (using the ``name`` argument). It can also be depended upon
791 only by components in the same package (private) or by those components and
792 components in other packages (public). A package can have no more than one
797 The 'cabal' executable provided by the 'cabal-install' package will not
798 accept dependencies on sublibraries of packages with no unnamed library.
800 This guide refers to an unnamed library as the main library and a named
801 library as a sublibrary (such components may be considered as subidiary, or
802 ancillary, to the main library). It refers to a private sublibrary as an
805 A sublibrary cannot have the same name as its package.
809 Before version 3.4 of the Cabal specification, a private sublibrary could
810 shadow a dependency on the main library of another package, if their
813 A main library is always public and a sublibrary is private by default.
814 See the :pkg-field:`library:visibility` field for setting a sublibrary as
817 Being able to include more than one public library in a package allows the
818 separation of the unit of distribution (the package) from the unit of
819 buildable code (the library). This is useful for Haskell projects with many
820 libraries that are distributed together as it avoids duplication and
821 potential inconsistencies.
825 Before version 3.0 of the Cabal specification, all sublibraries were
826 internal libraries. Before version 2.0, a package could not include
829 See :ref:`Sublibraries - Examples <sublibs>` for examples.
831 A library section should contain the following fields:
833 .. pkg-field:: visibility: visibility specifiers
838 ``private`` for sublibraries. Cannot be set for the main library, which
841 Can be set to ``private`` or ``public``. A ``private`` library component can
842 only be depended on by other components of the same package. A ``public``
843 component can be depended on by those components and by components of other
846 See the :pkg-field:`build-depends` field for the syntax to specify a
847 dependency on a library component.
849 .. pkg-field:: exposed-modules: identifier list
851 :required: if this package contains a library
853 A list of modules added by this package.
855 .. pkg-field:: virtual-modules: identifier list
858 A list of virtual modules provided by this package. Virtual modules
859 are modules without a source file. See for example the ``GHC.Prim``
860 module from the ``ghc-prim`` package. Modules listed here will not be
861 built, but still end up in the list of ``exposed-modules`` in the
862 installed package info when the package is registered in the package
865 .. pkg-field:: exposed: boolean
869 Some Haskell compilers (notably GHC) support the notion of packages
870 being "exposed" or "hidden" which means the modules they provide can
871 be easily imported without always having to specify which package
872 they come from. However this only works effectively if the modules
873 provided by all exposed packages do not overlap (otherwise a module
874 import would be ambiguous).
876 Almost all new libraries use hierarchical module names that do not
877 clash, so it is very uncommon to have to use this field. However it
878 may be necessary to set ``exposed: False`` for some old libraries
879 that use a flat module namespace or where it is known that the
880 exposed modules would clash with other common modules.
882 .. pkg-field:: reexported-modules: exportlist
885 Supported only in GHC 7.10 and later. A list of modules to
886 *reexport* from this package. The syntax of this field is
887 ``orig-pkg:Name as NewName`` to reexport module ``Name`` from
888 ``orig-pkg`` with the new name ``NewName``. We also support
889 abbreviated versions of the syntax: if you omit ``as NewName``,
890 we'll reexport without renaming; if you omit ``orig-pkg``, then we
891 will automatically figure out which package to reexport from, if
894 Reexported modules are useful for compatibility shims when a package
895 has been split into multiple packages, and they have the useful
896 property that if a package provides a module, and another package
897 reexports it under the same name, these are not considered a
898 conflict (as would be the case with a stub module.) They can also be
899 used to resolve name conflicts.
901 .. pkg-field:: signatures: signature list
904 Supported only in GHC 8.2 and later. A list of `module signatures <https://downloads.haskell.org/~ghc/master/users-guide/separate_compilation.html#module-signatures>`__ required by this package.
906 Module signatures are part of the :ref:`Backpack` extension to
907 the Haskell module system.
909 Packages that do not export any modules and only export required signatures
910 are called "signature-only packages", and their signatures are subjected to
912 <https://wiki.haskell.org/Module_signature#How_to_use_a_signature_package>`__.
916 The library section may also contain build information fields (see the
917 section on `build information`_).
921 **Sublibraries - Examples**
923 An example of the use of a private sublibrary (an internal library) is a test
924 suite that needs access to some internal modules in the package's main library,
925 which you do not otherwise want to expose. You could put those modules in an
926 internal library, which the main library and the test suite
927 :pkg-field:`build-depends` upon. Your Cabal file might then look something like
935 license: BSD-3-Clause
936 license-file: LICENSE
940 exposed-modules: Foo.Internal
941 -- NOTE: no explicit constraints on base needed
942 -- as they're inherited from the 'library' stanza
944 default-language: Haskell2010
947 exposed-modules: Foo.Public
948 build-depends: foo:foo-internal, base >= 4.3 && < 5
949 default-language: Haskell2010
952 type: exitcode-stdio-1.0
954 -- NOTE: no constraints on 'foo-internal' as same-package
955 -- dependencies implicitly refer to the same package instance
956 build-depends: foo:foo-internal, base
957 default-language: Haskell2010
959 Another example of the use of internal libraries is a package that includes one
960 or more executables but does not include a public library.
962 Internal libraries can be used to incorporate (vendor or bundle) an external
963 dependency into a package, effectively simulating *private dependencies*. Below
969 name: haddock-library
971 license: BSD-3-Clause
976 , bytestring ^>= 0.12.0.0
977 , containers ^>= 0.6.8 || ^>= 0.7.0
978 , transformers ^>= 0.6.1.0
983 build-depends: haddock-library:attoparsec
986 Documentation.Haddock
988 default-language: Haskell2010
993 , bytestring ^>= 0.12.0.0
994 , deepseq ^>= 1.5.0.0
996 hs-source-dirs: vendor/attoparsec-0.13.1.0
998 -- NB: haddock-library needs only small part of lib:attoparsec
999 -- internally, so we only bundle that subset here
1001 Data.Attoparsec.ByteString
1002 Data.Attoparsec.Combinator
1005 Data.Attoparsec.Internal
1007 ghc-options: -funbox-strict-fields -Wall -fwarn-tabs -O2
1009 default-language: Haskell2010
1014 A package description can contain multiple executable sections.
1015 The documentation of the `cabal run <cabal-commands.html#cabal-run>`__ command
1016 contains detailed information on how to run an executable.
1018 .. pkg-section:: executable name
1019 :synopsis: Executable build info section.
1021 Executable sections (if present) describe executable programs contained
1022 in the package and must have an argument after the section label, which
1023 defines the name of the executable. This is a freeform argument but may
1026 The executable may be described using the following fields, as well as
1027 build information fields (see the section on `build information`_).
1029 .. pkg-field:: main-is: filename (required)
1031 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1032 module. Note that it is the ``.hs`` filename that must be listed,
1033 even if that file is generated using a preprocessor. The source file
1034 must be relative to one of the directories listed in
1035 :pkg-field:`hs-source-dirs`. Further, while the name of the file may
1036 vary, the module itself must be named ``Main``.
1038 Starting with ``cabal-version: 1.18`` this field supports
1039 specifying a C, C++, or objC source file as the main entry point.
1041 .. pkg-field:: scope: token
1044 Whether the executable is ``public`` (default) or ``private``, i.e. meant to
1045 be run by other programs rather than the user. Private executables are
1046 installed into `$libexecdir/$libexecsubdir`.
1052 A package description can contain multiple test suite sections.
1053 The documentation of the `cabal test <cabal-commands.html#cabal-test>`__ command
1054 contains detailed information on how to run test suites.
1056 .. pkg-section:: test-suite name
1057 :synopsis: Test suite build information.
1059 Test suite sections (if present) describe package test suites and must
1060 have an argument after the section label, which defines the name of the
1061 test suite. This is a freeform argument, but may not contain spaces. It
1062 should be unique among the names of the package's other test suites, the
1063 package's executables, and the package itself. Using test suite sections
1064 requires at least Cabal version 1.9.2.
1066 The test suite may be described using the following fields, as well as
1067 build information fields (see the section on `build information`_).
1069 .. pkg-field:: type: interface (required until ``cabal-version`` 3.8)
1071 The interface type and version of the test suite. Cabal supports two
1072 test suite interfaces, called ``exitcode-stdio-1.0`` (default since ``cabal-version`` 3.8) and
1073 ``detailed-0.9``. Each of these types may require or disallow other
1074 fields as described below.
1076 Test suites using the ``exitcode-stdio-1.0`` (default since ``cabal-version`` 3.8) interface are executables
1077 that indicate test failure with a non-zero exit code when run; they may
1078 provide human-readable log information through the standard output and
1079 error channels. The ``exitcode-stdio-1.0`` type requires the ``main-is``
1082 .. pkg-field:: main-is: filename
1083 :synopsis: Module containing tests main function.
1085 :required: ``exitcode-stdio-1.0``
1086 :disallowed: ``detailed-0.9``
1088 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1089 module. Note that it is the ``.hs`` filename that must be listed,
1090 even if that file is generated using a preprocessor. The source file
1091 must be relative to one of the directories listed in
1092 :pkg-field:`hs-source-dirs`. This field is analogous to the ``main-is`` field
1093 of an executable section.
1095 Test suites using the ``detailed-0.9`` interface are modules exporting
1096 the symbol ``tests :: IO [Test]``. The ``Test`` type is exported by the
1097 module ``Distribution.TestSuite`` provided by Cabal. For more details,
1098 see the example below.
1100 The ``detailed-0.9`` interface allows Cabal and other test agents to
1101 inspect a test suite's results case by case, producing detailed human-
1102 and machine-readable log files. The ``detailed-0.9`` interface requires
1103 the :pkg-field:`test-module` field.
1105 .. pkg-field:: test-module: identifier
1107 :required: ``detailed-0.9``
1108 :disallowed: ``exitcode-stdio-1.0``
1110 The module exporting the ``tests`` symbol.
1112 .. pkg-field:: code-generators
1114 An optional list of preprocessors which can generate new modules
1115 for use in the test-suite.
1117 A list of executabes (possibly brought into scope by
1118 :pkg-field:`build-tool-depends`) that are run after all other
1119 preprocessors. These executables are invoked as so: ``exe-name
1120 TARGETDIR [SOURCEDIRS] -- [GHCOPTIONS]``. The arguments are, in order a target dir for
1121 output, a sequence of all source directories with source files of
1122 local lib components that the given test stanza depends on, and
1123 following a double dash, all options cabal would pass to ghc for a
1124 build. They are expected to output a newline-seperated list of
1125 generated modules which have been written to the targetdir
1126 (excepting, if written, the main module). This can
1127 be used for driving doctests and other discover-style tests generated
1131 Example: Package using ``exitcode-stdio-1.0`` interface
1132 """""""""""""""""""""""""""""""""""""""""""""""""""""""
1134 The example package description and executable source file below
1135 demonstrate the use of the ``exitcode-stdio-1.0`` interface.
1137 .. code-block:: cabal
1143 License: BSD-3-Clause
1147 type: exitcode-stdio-1.0
1148 main-is: test-foo.hs
1149 build-depends: base >= 4 && < 5
1150 default-language: Haskell2010
1152 .. code-block:: haskell
1153 :caption: test-foo.hs
1157 import System.Exit (exitFailure)
1160 putStrLn "This test always fails!"
1163 Example: Package using ``detailed-0.9`` interface
1164 """""""""""""""""""""""""""""""""""""""""""""""""
1166 The example package description and test module source file below
1167 demonstrate the use of the ``detailed-0.9`` interface. The test module
1168 also develops a simple implementation of the interface set by
1169 ``Distribution.TestSuite``, but in actual usage the implementation would
1170 be provided by the library that provides the testing facility.
1172 .. code-block:: cabal
1178 License: BSD-3-Clause
1184 build-depends: base >= 4 && < 5, Cabal >= 1.9.2 && < 2
1185 default-language: Haskell2010
1188 .. code-block:: haskell
1191 module Bar ( tests ) where
1193 import Distribution.TestSuite
1196 tests = return [ Test succeeds, Test fails ]
1198 succeeds = TestInstance
1199 { run = return $ Finished Pass
1203 , setOption = \_ _ -> Right succeeds
1205 fails = TestInstance
1206 { run = return $ Finished $ Fail "Always fails!"
1210 , setOption = \_ _ -> Right fails
1216 A package description can contain multiple benchmark sections.
1217 The documentation of the `cabal bench <cabal-commands.html#cabal-bench>`__ command
1218 contains detailed information on how to run benchmarks.
1220 .. pkg-section:: benchmark name
1222 :synopsis: Benchmark build information.
1224 Benchmark sections (if present) describe benchmarks contained in the
1225 package and must have an argument after the section label, which defines
1226 the name of the benchmark. This is a freeform argument, but may not
1227 contain spaces. It should be unique among the names of the package's
1228 other benchmarks, the package's test suites, the package's executables,
1229 and the package itself. Using benchmark sections requires at least Cabal
1232 The benchmark may be described using the following fields, as well as
1233 build information fields (see the section on `build information`_).
1235 .. pkg-field:: type: interface (required until ``cabal-version`` 3.8)
1237 The interface type and version of the benchmark. At the moment Cabal
1238 only support one benchmark interface, called ``exitcode-stdio-1.0``.
1240 Benchmarks using the ``exitcode-stdio-1.0`` (default since ``cabal-version`` 3.8) interface are executables
1241 that indicate failure to run the benchmark with a non-zero exit code
1242 when run; they may provide human-readable information through the
1243 standard output and error channels.
1245 .. pkg-field:: main-is: filename
1247 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1248 module. Note that it is the ``.hs`` filename that must be listed,
1249 even if that file is generated using a preprocessor. The source file
1250 must be relative to one of the directories listed in
1251 :pkg-field:`hs-source-dirs`. This field is analogous to the ``main-is``
1252 field of an executable section. Further, while the name of the file may
1253 vary, the module itself must be named ``Main``.
1256 """""""""""""""""""""""""""""""""""""""""""""""""""""""
1258 .. code-block:: cabal
1260 :name: foo-bench.cabal
1265 License: BSD-3-Clause
1269 type: exitcode-stdio-1.0
1270 main-is: bench-foo.hs
1271 build-depends: base >= 4 && < 5, time >= 1.1 && < 1.7
1272 default-language: Haskell2010
1274 .. code-block:: haskell
1275 :caption: bench-foo.hs
1277 {-# LANGUAGE BangPatterns #-}
1280 import Data.Time.Clock
1284 fib n = fib (n-1) + fib (n-2)
1287 start <- getCurrentTime
1289 end <- getCurrentTime
1290 putStrLn $ "fib 20 took " ++ show (diffUTCTime end start)
1296 .. pkg-section:: None
1298 The following fields may be optionally present in a library, executable,
1299 test suite or benchmark section, and give information for the building
1300 of the corresponding library or executable. See also the sections on
1301 `system-dependent parameters`_ and `configurations`_ for a way to supply
1302 system-dependent values for these fields.
1304 .. pkg-field:: build-depends: library list
1306 Declares the dependencies on *library* components required to build the
1307 current package component. See :pkg-field:`build-tool-depends` for declaring
1308 dependencies on build-time *tools*. Dependencies on libraries from another
1309 package should be annotated with a version constraint.
1313 A library is identified by the name of its package, optionally followed by a
1314 colon and the library's name (for example, ``my-package:my-library``). If a
1315 library name is omitted, the package's main library will be used. To refer
1316 expressly to a package's main library, use the name of the package as the
1317 library name (for example, ``my-package:my-package``). More than one library
1318 from the same package can be specified with the shorthand syntax
1319 ``my-package:{my-library1,my-library2}``.
1323 Before version 3.4 of the Cabal specification, from version 2.0, a
1324 private sublibrary (an internal library) was identified by only the name
1325 of the sublibrary. An internal library could shadow a dependency on the
1326 main library of another package, if the names clashed.
1328 See the section on :pkg-section:`library` for information about how a
1329 package can specify library components.
1331 **Version Constraints**
1333 Version constraints use the operators ``==, >=, >, <, <=`` and a
1334 version number. Multiple constraints can be combined using ``&&`` or
1339 Even though there is no ``/=`` operator, by combining operators we can
1340 skip over one or more versions, to skip a deprecated version or to skip
1341 versions that narrow the constraint solving more than we'd like.
1343 For example, the ``time =1.12.*`` series depends on ``base >=4.13 && <5``
1344 but ``time-1.12.3`` bumps the lower bound on base to ``>=4.14``. If we
1345 still want to compile with a ``ghc-8.8.*`` version of GHC that ships with
1346 ``base-4.13`` and with later GHC versions, then we can use ``time >=1.12
1347 && (time <1.12.3 || time >1.12.3)``.
1349 Hackage shows deprecated and preferred versions for packages, such as for
1350 `containers <https://hackage.haskell.org/package/containers/preferred>`_
1351 and `aeson <https://hackage.haskell.org/package/aeson/preferred>`_ for
1352 example. Deprecating package versions is not the same deprecating a
1353 package as a whole, for which Hackage keeps a `deprecated packages list
1354 <https://hackage.haskell.org/packages/deprecated>`_.
1356 If no version constraint is specified, any version is assumed to be
1357 acceptable. For example:
1364 foo >= 1.2.3 && < 1.3,
1367 Dependencies like ``foo >= 1.2.3 && < 1.3`` turn out to be very
1368 common because it is recommended practice for package versions to
1369 correspond to API versions (see PVP_).
1371 Since Cabal 1.6, there is a special wildcard syntax to help with
1376 build-depends: foo ==1.2.*
1378 It is only syntactic sugar. It is exactly equivalent to
1379 ``foo >= 1.2 && < 1.3``.
1383 A potential pitfall of the wildcard syntax is that the
1384 constraint ``nats == 1.0.*`` doesn't match the release
1385 ``nats-1`` because the version ``1`` is lexicographically less
1386 than ``1.0``. This is not an issue with the caret-operator
1387 ``^>=`` described below.
1389 Starting with Cabal 2.0, there's a new version operator to express
1390 PVP_-style major upper bounds conveniently, and is inspired by similar
1391 syntactic sugar found in other language ecosystems where it's often
1392 called the "Caret" operator:
1400 This allows to assert the positive knowledge that this package is
1401 *known* to be semantically compatible with the releases
1402 ``foo-1.2.3.4`` and ``bar-1`` respectively. The information
1403 encoded via such ``^>=``-assertions is used by the cabal solver to
1404 infer version constraints describing semantically compatible
1405 version ranges according to the PVP_ contract (see below).
1407 Another way to say this is that ``foo < 1.3`` expresses *negative*
1408 information, i.e. "``foo-1.3`` or ``foo-1.4.2`` will *not* be
1409 compatible"; whereas ``foo ^>= 1.2.3.4`` asserts the *positive*
1410 information that "``foo-1.2.3.4`` is *known* to be compatible" and (in
1411 the absence of additional information) according to the PVP_
1412 contract we can (positively) infer right away that all versions
1413 satisfying ``foo >= 1.2.3.4 && < 1.3`` will be compatible as well.
1417 More generally, the PVP_ contract implies that we can safely
1418 relax the lower bound to ``>= 1.2``, because if we know that
1419 ``foo-1.2.3.4`` is semantically compatible, then so is
1420 ``foo-1.2`` (if it typechecks). But we'd need to perform
1421 additional static analysis (i.e. perform typechecking) in order
1422 to know if our package in the role of an API consumer will
1423 successfully typecheck against the dependency ``foo-1.2``. But
1424 since we cannot do this analysis during constraint solving and
1425 to keep things simple, we pragmatically use ``foo >= 1.2.3.4``
1426 as the initially inferred approximation for the lower bound
1427 resulting from the assertion ``foo ^>= 1.2.3.4``. If further
1428 evidence becomes available that e.g. ``foo-1.2`` typechecks,
1429 one can simply revise the dependency specification to include
1430 the assertion ``foo ^>= 1.2``.
1432 The subtle but important difference in signaling allows tooling to
1433 treat explicitly expressed ``<``-style constraints and inferred
1434 (``^>=``-style) upper bounds differently. For instance,
1435 :cfg-field:`allow-newer`'s ``^``-modifier allows to relax only
1436 ``^>=``-style bounds while leaving explicitly stated
1437 ``<``-constraints unaffected.
1439 Ignoring the signaling intent, the default syntactic desugaring rules are
1441 - ``^>= x`` == ``>= x && < x.1``
1442 - ``^>= x.y`` == ``>= x.y && < x.(y+1)``
1443 - ``^>= x.y.z`` == ``>= x.y.z && < x.(y+1)``
1444 - ``^>= x.y.z.u`` == ``>= x.y.z.u && < x.(y+1)``
1449 One might expect the desugaring to truncate all version
1450 components below (and including) the patch-level, i.e.
1451 ``^>= x.y.z.u`` == ``>= x.y.z && < x.(y+1)``,
1452 as the major and minor version components alone are supposed to
1453 uniquely identify the API according to the PVP_. However, by
1454 designing ``^>=`` to be closer to the ``>=`` operator, we avoid
1455 the potentially confusing effect of ``^>=`` being more liberal
1456 than ``>=`` in the presence of patch-level versions.
1458 Consequently, the example declaration above is equivalent to
1463 foo >= 1.2.3.4 && < 1.3,
1468 Prior to Cabal 1.8, ``build-depends`` specified in each
1469 section were global to all sections. This was unintentional, but
1470 some packages were written to depend on it, so if you need your
1471 :pkg-field:`build-depends` to be local to each section, you must specify
1472 at least ``Cabal-Version: >= 1.8`` in your ``.cabal`` file.
1476 Cabal 1.20 experimentally supported module thinning and
1477 renaming in ``build-depends``; however, this support has since been
1478 removed and should not be used.
1480 Starting with Cabal 3.0, a set notation for the ``==`` and ``^>=`` operator
1481 is available. For instance,
1485 tested-with: GHC == 8.6.3, GHC == 8.4.4, GHC == 8.2.2, GHC == 8.0.2,
1486 GHC == 7.10.3, GHC == 7.8.4, GHC == 7.6.3, GHC == 7.4.2
1488 build-depends: network ^>= 2.6.3.6 || ^>= 2.7.0.2 || ^>= 2.8.0.0 || ^>= 3.0.1.0
1490 can be then written in a more convenient and concise form
1494 tested-with: GHC == { 8.6.3, 8.4.4, 8.2.2, 8.0.2, 7.10.3, 7.8.4, 7.6.3, 7.4.2 }
1496 build-depends: network ^>= { 2.6.3.6, 2.7.0.2, 2.8.0.0, 3.0.1.0 }
1499 .. pkg-field:: other-modules: identifier list
1501 A list of modules used by the component but not exposed to users.
1502 For a library component, these would be hidden modules of the
1503 library. For an executable, these would be auxiliary modules to be
1504 linked with the file named in the ``main-is`` field.
1508 Every module in the package *must* be listed in one of
1509 :pkg-field:`other-modules`, :pkg-field:`library:exposed-modules` or
1510 :pkg-field:`executable:main-is` fields.
1512 .. pkg-field:: hs-source-dir: directory list
1518 Root directories for the module hierarchy.
1520 Deprecated in favor of :pkg-field:`hs-source-dirs`.
1522 .. pkg-field:: hs-source-dirs: directory list
1526 Root directories for the module hierarchy.
1530 Components can share source directories but modules found there will be
1531 recompiled even if other components already built them, i.e., if a
1532 library and an executable share a source directory and the executable
1533 depends on the library and imports its ``Foo`` module, ``Foo`` will be
1534 compiled twice, once as part of the library and again for the executable.
1536 .. pkg-field:: default-extensions: identifier list
1539 A list of Haskell extensions used by every module. These determine
1540 corresponding compiler options enabled for all files. Extension
1541 names are the constructors of the
1542 `Extension <https://hackage.haskell.org/package/Cabal-syntax/docs/Language-Haskell-Extension.html#t:Extension>`__
1543 type. For example, ``CPP`` specifies that Haskell source files are
1544 to be preprocessed with a C preprocessor.
1546 .. pkg-field:: other-extensions: identifier list
1549 A list of Haskell extensions used by some (but not necessarily all)
1550 modules. From GHC version 6.6 onward, these may be specified by
1551 placing a ``LANGUAGE`` pragma in the source files affected e.g.
1553 .. code-block:: haskell
1555 {-# LANGUAGE CPP, MultiParamTypeClasses #-}
1557 In Cabal-1.24 the dependency solver will use this and
1558 :pkg-field:`default-extensions` information. Cabal prior to 1.24 will abort
1559 compilation if the current compiler doesn't provide the extensions.
1561 If you use some extensions conditionally, using CPP or conditional
1562 module lists, it is good to replicate the condition in
1563 :pkg-field:`other-extensions` declarations:
1567 other-extensions: CPP
1569 other-extensions: PolyKinds
1571 You could also omit the conditionally used extensions, as they are
1572 for information only, but it is recommended to replicate them in
1573 :pkg-field:`other-extensions` declarations.
1575 .. pkg-field:: default-language: identifier
1578 Specifies a language standard or a group of language extensions to be activated for the project. In the case of GHC, `see here for details <https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/control.html#controlling-extensions>`__.
1580 The possible values are:
1582 - ``GHC2024`` (only available for GHC version ``9.10`` or later)
1583 - ``GHC2021`` (only available for GHC version ``9.2`` or later)
1587 .. pkg-field:: other-languages: identifier
1592 .. pkg-field:: extensions: identifier list
1596 Deprecated in favor of :pkg-field:`default-extensions`.
1598 .. pkg-field:: build-tool-depends: package:executable list
1601 A list of Haskell executables needed to build this component. Executables are provided
1602 during the whole duration of the component, so this field can be used for executables
1603 needed during :pkg-section:`test-suite` as well.
1605 Each is specified by the package containing the executable and the name of the
1606 executable itself, separated by a colon, and optionally followed by a version bound.
1608 All executables defined in the given Cabal file are termed as *internal* dependencies
1609 as opposed to the rest which are *external* dependencies.
1611 Each of the two is handled differently:
1613 1. External dependencies can (and should) contain a version bound like conventional
1614 :pkg-field:`build-depends` dependencies.
1615 2. Internal dependencies should not contain a version bound, as they will be always
1616 resolved within the same configuration of the package in the build plan.
1617 Specifically, version bounds that include the package's version will be warned for
1618 being extraneous, and version bounds that exclude the package's version will raise
1619 an error for being impossible to follow.
1621 For example (1) using a test-suite to make sure README.md Haskell snippets are tested using
1622 `markdown-unlit <http://hackage.haskell.org/package/markdown-unlit>`__:
1626 build-tool-depends: markdown-unlit:markdown-unlit >= 0.5.0 && < 0.6
1628 For example (2) using a test-suite to test executable behaviour in the same package:
1632 build-tool-depends: mypackage:executable
1634 Cabal tries to make sure that all specified programs are atomically built and prepended
1635 on the ``PATH`` shell variable before building the component in question, but can only do
1636 so for Nix-style builds. Specifically:
1638 a) For Nix-style local builds, both internal and external dependencies.
1639 b) For old-style builds, only for internal dependencies [#old-style-build-tool-depends]_.
1640 It's up to the user to provide needed executables in this case under ``PATH``.
1645 :pkg-field:`build-tool-depends` was added in Cabal 2.0, and it will
1646 be ignored (with a warning) with old versions of Cabal. See
1647 :pkg-field:`build-tools` for more information about backwards
1650 .. pkg-field:: build-tools: program list
1654 Deprecated in favor of :pkg-field:`build-tool-depends`, but :ref:`see below for backwards compatibility information <buildtoolsbc>`.
1656 A list of Haskell programs needed to build this component.
1657 Each may be followed by an optional version bound.
1658 Confusingly, each program in the list either refer to one of three things:
1660 1. Another executables in the same package (supported since Cabal 1.12)
1662 2. Tool name contained in Cabal's :ref:`hard-coded set of common tools <buildtoolsmap>`
1664 3. A pre-built executable that should already be on the ``PATH``
1665 (supported since Cabal 2.0)
1667 These cases are listed in order of priority:
1668 an executable in the package will override any of the hard-coded packages with the same name,
1669 and a hard-coded package will override any executable on the ``PATH``.
1671 In the first two cases, the list entry is desugared into a :pkg-field:`build-tool-depends` entry.
1672 In the first case, the entry is desugared into a :pkg-field:`build-tool-depends` entry by prefixing with ``$pkg:``.
1673 In the second case, it is desugared by looking up the package and executable name in a hard-coded table.
1674 In either case, the optional version bound is passed through unchanged.
1675 Refer to the documentation for :pkg-field:`build-tool-depends` to understand the desugared field's meaning, along with restrictions on version bounds.
1679 **Backward Compatibility**
1681 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-tools` in cases (1) and (2), as it is supported by more versions of Cabal.
1682 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.
1683 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``.
1687 **Set of Known Tool Names**
1689 Identifiers specified in :pkg-field:`build-tools` are desugared into their respective equivalent :pkg-field:`build-tool-depends` form according to the table below. Consequently, a legacy specification such as::
1691 build-tools: alex >= 3.2.1 && < 3.3, happy >= 1.19.5 && < 1.20
1693 is simply desugared into the equivalent specification::
1695 build-tool-depends: alex:alex >= 3.2.1 && < 3.3, happy:happy >= 1.19.5 && < 1.20
1697 +--------------------------+-----------------------------------+-----------------+
1698 | :pkg-field:`build-tools` | desugared | Note |
1699 | identifier | :pkg-field:`build-tool-depends` | |
1701 +==========================+===================================+=================+
1702 | ``alex`` | ``alex:alex`` | |
1703 +--------------------------+-----------------------------------+-----------------+
1704 | ``c2hs`` | ``c2hs:c2hs`` | |
1705 +--------------------------+-----------------------------------+-----------------+
1706 | ``cpphs`` | ``cpphs:cpphs`` | |
1707 +--------------------------+-----------------------------------+-----------------+
1708 | ``greencard`` | ``greencard:greencard`` | |
1709 +--------------------------+-----------------------------------+-----------------+
1710 | ``haddock`` | ``haddock:haddock`` | |
1711 +--------------------------+-----------------------------------+-----------------+
1712 | ``happy`` | ``happy:happy`` | |
1713 +--------------------------+-----------------------------------+-----------------+
1714 | ``hsc2hs`` | ``hsc2hs:hsc2hs`` | |
1715 +--------------------------+-----------------------------------+-----------------+
1716 | ``hscolour`` | ``hscolour:hscolour`` | |
1717 +--------------------------+-----------------------------------+-----------------+
1718 | ``hspec-discover`` | ``hspec-discover:hspec-discover`` | since Cabal 2.0 |
1719 +--------------------------+-----------------------------------+-----------------+
1721 This built-in set can be programmatically extended via use of the
1722 :ref:`Hooks build type<setup-hooks>` .
1724 .. pkg-field:: buildable: boolean
1728 Is the component buildable? Like some of the other fields below,
1729 this field is more useful with the slightly more elaborate form of
1730 the simple build infrastructure described in the section on
1731 `system-dependent parameters`_.
1733 .. pkg-field:: ghc-options: token list
1735 Additional options for GHC.
1737 If specifying extensions (via ``-X<Extension>`` flags) one can often achieve
1738 the same effect using the :pkg-field:`default-extensions` field, which is
1741 Options required only by one module may be specified by placing an
1742 ``OPTIONS_GHC`` pragma in the source file affected.
1744 As with many other fields, whitespace can be escaped by using
1745 Haskell string syntax. Example:
1746 ``ghc-options: -Wcompat "-with-rtsopts=-T -I1" -Wall``.
1748 .. pkg-field:: ghc-prof-options: token list
1750 Additional options for GHC when the package is built with profiling
1753 Note that as of Cabal-1.24, the default profiling detail level
1754 defaults to ``exported-functions`` for libraries and
1755 ``toplevel-functions`` for executables. For GHC these correspond to
1756 the flags ``-fprof-auto-exported`` and ``-fprof-auto-top``. Prior to
1757 Cabal-1.24 the level defaulted to ``none``. These levels can be
1758 adjusted by the person building the package with the
1759 ``--profiling-detail`` and ``--library-profiling-detail`` flags.
1761 It is typically better for the person building the package to pick
1762 the profiling detail level rather than for the package author. So
1763 unless you have special needs it is probably better not to specify
1764 any of the GHC ``-fprof-auto*`` flags here. However if you wish to
1765 override the profiling detail level, you can do so using the
1766 :pkg-field:`ghc-prof-options` field: use ``-fno-prof-auto`` or one of the
1767 other ``-fprof-auto*`` flags.
1769 .. pkg-field:: ghc-shared-options: token list
1771 Additional options for GHC when the package is built as shared
1772 library. The options specified via this field are combined with the
1773 ones specified via :pkg-field:`ghc-options`, and are passed to GHC during
1774 both the compile and link phases.
1776 .. pkg-field:: ghc-prof-shared-options: token list
1778 Additional options for GHC when the package is built as shared profiling
1779 library. The options specified via this field are combined with the
1780 ones specified via :pkg-field:`ghc-options`, and are passed to GHC during
1781 both the compile and link phases.
1783 Note that if any :pkg-field:`ghc-shared-options` are set, the
1784 ``-dynamic-too` option will never be passed to GHC, leading to all modules
1785 being compiled twice (once to generate the ``.o`` files and another to
1786 generate the ``.dyn_o`` files).
1788 .. pkg-field:: ghcjs-options: token list
1790 Like :pkg-field:`ghc-options` but applies to GHCJS
1792 .. pkg-field:: ghcjs-prof-options: token list
1794 Like :pkg-field:`ghc-prof-options` but applies to GHCJS
1796 .. pkg-field:: ghcjs-shared-options: token list
1798 Like :pkg-field:`ghc-shared-options` but applies to GHCJS
1800 .. pkg-field:: ghcjs-prof-shared-options: token list
1802 Like :pkg-field:`ghc-prof-shared-options` but applies to GHCJS
1804 .. pkg-field:: includes: filename list
1808 From GHC 6.10.1, :pkg-field:`includes` has no effect when compiling with
1809 GHC. From Cabal 2.0, support for GHC versions before GHC 6.12 was removed.
1811 A list of header files to be included in any compilations via C.
1812 This field applies to both header files that are already installed
1813 on the system and to those coming with the package to be installed.
1814 The former files should be found in absolute paths, while the latter
1815 files should be found in paths relative to the top of the source
1816 tree or relative to one of the directories listed in
1817 :pkg-field:`include-dirs`.
1819 These files typically contain function prototypes for foreign
1820 imports used by the package. This is in contrast to
1821 :pkg-field:`install-includes`, which lists header files that are intended
1822 to be exposed to other packages that transitively depend on this
1825 .. pkg-field:: install-includes: filename list
1827 A list of header files from this package to be installed into
1828 ``$libdir/includes`` when the package is installed. Files listed in
1829 :pkg-field:`install-includes` should be found in relative to the top of the
1830 source tree or relative to one of the directories listed in
1831 :pkg-field:`include-dirs`.
1833 :pkg-field:`install-includes` is typically used to name header files that
1834 contain prototypes for foreign imports used in Haskell code in this
1835 package, for which the C implementations are also provided with the
1836 package. For example, here is a ``.cabal`` file for a hypothetical
1837 ``bindings-clib`` package that bundles the C source code for ``clib``::
1841 install-includes: clib.h
1843 Now any package that depends (directly or transitively) on the
1844 ``bindings-clib`` library can use ``clib.h``.
1846 Note that in order for files listed in :pkg-field:`install-includes` to be
1847 usable when compiling the package itself, they need to be listed in
1848 the :pkg-field:`includes` field as well.
1850 .. pkg-field:: include-dirs: directory list
1852 A list of directories to search for header files, when preprocessing
1853 with ``c2hs``, ``hsc2hs``, ``cpphs`` or the C preprocessor, and also
1854 when compiling via C. Directories can be absolute paths (e.g., for
1855 system directories) or paths that are relative to the top of the
1856 source tree. Cabal looks in these directories when attempting to
1857 locate files listed in :pkg-field:`includes` and
1858 :pkg-field:`install-includes`.
1860 Directories here will be passed as ``-I<dir>`` flags to GHC.
1862 .. pkg-field:: c-sources: filename list
1864 A list of C source files to be compiled and linked with the Haskell
1867 .. pkg-field:: cxx-sources: filename list
1870 A list of C++ source files to be compiled and linked with the Haskell
1871 files. Useful for segregating C and C++ sources when supplying different
1872 command-line arguments to the compiler via the :pkg-field:`cc-options`
1873 and the :pkg-field:`cxx-options` fields. The files listed in the
1874 :pkg-field:`cxx-sources` can reference files listed in the
1875 :pkg-field:`c-sources` field and vice-versa. The object files will be linked
1878 .. pkg-field:: asm-sources: filename list
1881 A list of assembly source files to be compiled and linked with the
1884 .. pkg-field:: cmm-sources: filename list
1887 A list of C-- source files to be compiled and linked with the Haskell
1890 .. pkg-field:: js-sources: filename list
1892 A list of JavaScript source files to be linked with the Haskell
1893 files (only for JavaScript targets).
1895 .. pkg-field:: extra-libraries: token list
1897 A list of extra libraries to link with (when not linking fully static
1898 executables). Libraries will be passed as ``-optl-l<lib>`` flags to GHC.
1900 .. pkg-field:: extra-libraries-static: token list
1902 A list of extra libraries to link with (when linking fully static
1905 .. pkg-field:: extra-ghci-libraries: token list
1907 A list of extra libraries to be used instead of 'extra-libraries'
1908 when the package is loaded with GHCi.
1910 .. pkg-field:: extra-bundled-libraries: token list
1913 A list of libraries that are supposed to be copied from the build
1914 directory alongside the produced Haskell libraries. Note that you
1915 are under the obligation to produce those libraries in the build
1916 directory (e.g. via a custom setup). Libraries listed here will
1917 be included when ``copy``-ing packages and be listed in the
1918 ``hs-libraries`` of the package configuration in the package database.
1919 Library names must either be prefixed with "HS" or "C" and corresponding
1920 library file names must match:
1922 - Libraries with name "HS<library-name>":
1923 - `libHS<library-name>.a`
1924 - `libHS<library-name>-ghc<ghc-flavour><ghc-version>.<dyn-library-extension>*`
1925 - Libraries with name "C<library-name>":
1926 - `libC<library-name>.a`
1927 - `lib<library-name>.<dyn-library-extension>*`
1929 .. pkg-field:: extra-lib-dirs: directory list
1931 A list of directories to search for libraries (when not linking fully static
1932 executables). Directories will be passed as ``-optl-L<dir>`` flags to GHC.
1934 .. pkg-field:: extra-lib-dirs-static: directory list
1936 A list of directories to search for libraries (when linking fully static
1939 .. pkg-field:: extra-library-flavours: notsure
1943 .. pkg-field:: extra-dynamic-library-flavours: notsure
1947 .. pkg-field:: cc-options: token list
1949 Command-line arguments to be passed to the Haskell compiler for the C
1950 compiling phase (as ``-optc`` flags for GHC). Since the
1951 arguments are compiler-dependent, this field is more useful with the
1952 setup described in the section on `system-dependent parameters`_.
1954 .. pkg-field:: cpp-options: token list
1956 Command-line arguments for pre-processing Haskell code. Applies to
1957 Haskell source and other pre-processed Haskell source like .hsc
1958 .chs. Does not apply to C code, that's what cc-options is for.
1959 Flags here will be passed as ``-optP`` flags to GHC.
1961 .. pkg-field:: cxx-options: token list
1964 Command-line arguments to be passed to the Haskell compiler for the C++
1965 compiling phase (as ``-optcxx`` flags for GHC).
1966 The C++ sources to which these command-line arguments
1967 should be applied can be specified with the :pkg-field:`cxx-sources`
1968 field. Command-line options for C and C++ can be passed separately to
1969 the compiler when compiling both C and C++ sources by segregating the C
1970 and C++ sources with the :pkg-field:`c-sources` and
1971 :pkg-field:`cxx-sources` fields respectively, and providing different
1972 command-line arguments with the :pkg-field:`cc-options` and the
1973 :pkg-field:`cxx-options` fields.
1975 .. pkg-field:: cmm-options: token list
1978 Command-line arguments to be passed to the Haskell compiler when compiling
1979 C-- code. See also :pkg-field:`cmm-sources`.
1981 .. pkg-field:: asm-options: token list
1984 Command-line arguments to be passed to the Haskell compiler (as ``-opta``
1985 flags for GHC) when compiling assembler code. See also :pkg-field:`asm-sources`.
1987 .. pkg-field:: ld-options: token list
1989 Command-line arguments to be passed to the Haskell compiler (as ``-optl``
1990 flags for GHC) for the linking phase. Note that only executables (including
1991 test-suites and benchmarks) are linked so this has no effect in libraries.
1992 Since the arguments are compiler-dependent, this field is more useful with
1993 the setup described in the section on `system-dependent parameters`_.
1995 .. pkg-field:: hsc2hs-options: token list
1998 Command-line arguments to be passed to ``hsc2hs``.
2000 .. pkg-field:: pkgconfig-depends: package list
2003 `pkg-config <http://www.freedesktop.org/wiki/Software/pkg-config/>`__
2004 packages, needed to build this package. They can be annotated with
2005 versions, e.g. ``gtk+-2.0 >= 2.10, cairo >= 1.0``. If no version
2006 constraint is specified, any version is assumed to be acceptable.
2007 Cabal uses ``pkg-config`` to find if the packages are available on
2008 the system and to find the extra compilation and linker options
2009 needed to use the packages.
2011 If you need to bind to a C library that supports ``pkg-config`` then
2012 it is much preferable to use this field rather than hard code options
2013 into the other fields. ``pkg-config --list-all`` will show you all
2014 supported libraries. Depending on your system you may need to adjust
2015 ``PKG_CONFIG_PATH``.
2017 .. pkg-field:: frameworks: token list
2019 On Darwin/MacOS X, a list of frameworks to link to. See Apple's
2020 developer documentation for more details on frameworks. This entry
2021 is ignored on all other platforms.
2023 .. pkg-field:: extra-framework-dirs: directory list
2026 On Darwin/MacOS X, a list of directories to search for frameworks.
2027 This entry is ignored on all other platforms.
2029 .. pkg-field:: mixins: mixin list
2032 Supported only in GHC 8.2 and later. A list of packages mentioned in the
2033 :pkg-field:`build-depends` field, each optionally accompanied by a list of
2034 module and module signature renamings. A valid mixin obeys the
2039 Mixin ::= PackageName IncludeRenaming
2040 IncludeRenaming ::= ModuleRenaming { "requires" ModuleRenaming }
2043 | "(" Renaming "," ... "," Renaming ")"
2044 | "hiding" "(" ModuleName "," ... "," ModuleName ")"
2047 | ModuleName "as" ModuleName
2049 The simplest mixin syntax is simply the name of a package mentioned in the
2050 :pkg-field:`build-depends` field. For example:
2060 But this doesn't have any effect. More interesting is to use the mixin
2061 entry to rename one or more modules from the package, like this:
2067 foo (Foo.Bar as AnotherFoo.Bar, Foo.Baz as AnotherFoo.Baz)
2069 Note that renaming a module like this will hide all the modules
2070 that are not explicitly named.
2072 Modules can also be hidden:
2078 foo hiding (Foo.Bar)
2080 Hiding modules exposes everything that is not explicitly hidden.
2084 Cabal files with :pkg-field:`cabal-version` < 3.0 suffer from an
2085 infelicity in how the entries of :pkg-field:`mixins` are parsed: an
2086 entry will fail to parse if the provided renaming clause has whitespace
2087 after the opening parenthesis.
2089 See issues :issue:`5150`, :issue:`4864`, and :issue:`5293`.
2091 There can be multiple mixin entries for a given package, in effect creating
2092 multiple copies of the dependency:
2098 foo (Foo.Bar as AnotherFoo.Bar, Foo.Baz as AnotherFoo.Baz),
2099 foo (Foo.Bar as YetAnotherFoo.Bar)
2101 The ``requires`` clause is used to rename the module signatures required by
2108 foo (Foo.Bar as AnotherFoo.Bar) requires (Foo.SomeSig as AnotherFoo.SomeSig)
2110 Signature-only packages don't have any modules, so only the signatures can
2111 be renamed, with the following syntax:
2117 sigonly requires (SigOnly.SomeSig as AnotherSigOnly.SomeSig)
2119 See the :pkg-field:`library:signatures` field for more details.
2121 Mixin packages are part of the :ref:`Backpack` extension to the
2122 Haskell module system.
2124 The matching of the module signatures required by a
2125 :pkg-field:`build-depends` dependency with the implementation modules
2126 present in another dependency is triggered by a coincidence of names. When
2127 the names of the signature and of the implementation are already the same,
2128 the matching is automatic. But when the names don't coincide, or we want to
2129 instantiate a signature in two different ways, adding mixin entries that
2130 perform renamings becomes necessary.
2134 :ref:`Backpack` has the limitation that implementation modules that instantiate
2135 signatures required by a :pkg-field:`build-depends` dependency can't
2136 reside in the same component that has the dependency. They must reside
2137 in a different package dependency, or at least in a separate internal
2143 Foreign libraries are system libraries intended to be linked against
2144 programs written in C or other "foreign" languages. They
2145 come in two primary flavours: dynamic libraries (``.so`` files on Linux,
2146 ``.dylib`` files on OSX, ``.dll`` files on Windows, etc.) are linked against
2147 executables when the executable is run (or even lazily during
2148 execution), while static libraries (``.a`` files on Linux/OSX, ``.lib``
2149 files on Windows) get linked against the executable at compile time.
2151 Foreign libraries only work with GHC 7.8 and later.
2153 A typical stanza for a foreign library looks like
2157 foreign-library myforeignlib
2159 lib-version-info: 6:3:2
2163 mod-def-file: MyForeignLib.def
2165 other-modules: MyForeignLib.SomeModule
2166 MyForeignLib.SomeOtherModule
2167 build-depends: base >=4.7 && <4.9
2169 c-sources: csrc/MyForeignLibWrapper.c
2170 default-language: Haskell2010
2173 .. pkg-section:: foreign-library name
2175 :synopsis: Foreign library build information.
2177 Build information for `foreign libraries`_.
2179 .. pkg-field:: type: foreign library type
2181 Cabal recognizes ``native-static`` and ``native-shared`` here, although
2182 we currently only support building `native-shared` libraries.
2184 .. pkg-field:: options: foreign library option list
2186 Options for building the foreign library, typically specific to the
2187 specified type of foreign library. Currently we only support
2188 ``standalone`` here. A standalone dynamic library is one that does not
2189 have any dependencies on other (Haskell) shared libraries; without
2190 the ``standalone`` option the generated library would have dependencies
2191 on the Haskell runtime library (``libHSrts``), the base library
2192 (``libHSbase``), etc. Currently, ``standalone`` *must* be used on Windows
2193 and *must not* be used on any other platform.
2195 .. pkg-field:: mod-def-file: filename
2197 This option can only be used when creating dynamic Windows libraries
2198 (that is, when using ``native-shared`` and the ``os`` is ``Windows``). If
2199 used, it must be a path to a *module definition file*. The details of
2200 module definition files are beyond the scope of this document; see the
2201 `GHC <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/win32-dlls.html>`_
2202 manual for some details and some further pointers.
2204 .. pkg-field:: lib-version-info: current:revision:age
2206 This field is currently only used on Linux.
2208 This field specifies a Libtool-style version-info field that sets
2209 an appropriate ABI version for the foreign library. Note that the
2210 three numbers specified in this field do not directly specify the
2211 actual ABI version: ``6:3:2`` results in library version ``4.2.3``.
2213 With this field set, the SONAME of the library is set, and symlinks
2216 How you should bump this field on an ABI change depends on the
2217 breakage you introduce:
2219 - Programs using the previous version may use the new version as
2220 drop-in replacement, and programs using the new version can also
2221 work with the previous one. In other words, no recompiling nor
2222 relinking is needed. In this case, bump ``revision`` only, don't
2223 touch current nor age.
2224 - Programs using the previous version may use the new version as
2225 drop-in replacement, but programs using the new version may use
2226 APIs not present in the previous one. In other words, a program
2227 linking against the new version may fail with "unresolved
2228 symbols" if linking against the old version at runtime: set
2229 revision to 0, bump current and age.
2230 - Programs may need to be changed, recompiled, and relinked in
2231 order to use the new version. Bump current, set revision and age
2234 Also refer to the Libtool documentation on the version-info field.
2236 .. pkg-field:: lib-version-linux: version
2238 This field is only used on Linux.
2240 Specifies the library ABI version directly for foreign libraries
2241 built on Linux: so specifying ``4.2.3`` causes a library
2242 ``libfoo.so.4.2.3`` to be built with SONAME ``libfoo.so.4``, and
2243 appropriate symlinks ``libfoo.so.4`` and ``libfoo.so`` to be
2246 Note that typically foreign libraries should export a way to initialize
2247 and shutdown the Haskell runtime. In the example above, this is done by
2248 the ``csrc/MyForeignLibWrapper.c`` file, which might look something like
2255 HsBool myForeignLibInit(void){
2257 char *argv[] = { "+RTS", "-A32m", NULL };
2258 char **pargv = argv;
2260 // Initialize Haskell runtime
2261 hs_init(&argc, &pargv);
2263 // do any other initialization here and
2264 // return false if there was a problem
2265 return HS_BOOL_TRUE;
2268 void myForeignLibExit(void){
2272 With modern ghc regular libraries are installed in directories that contain
2273 package keys. This isn't usually a problem because the package gets registered
2274 in ghc's package DB and so we can figure out what the location of the library
2275 is. Foreign libraries however don't get registered, which means that we'd have
2276 to have a way of finding out where a platform library got installed (other than by
2277 searching the ``lib/`` directory). Instead, we install foreign libraries in
2283 Library and executable sections may include conditional blocks, which
2284 test for various system parameters and configuration flags. The flags
2285 mechanism is rather generic, but most of the time a flag represents
2286 certain feature, that can be switched on or off by the package user.
2287 Here is an example package description file using configurations:
2289 Example: A package containing a library and executable programs
2290 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2297 License: BSD-3-Clause
2299 Synopsis: Test package to test configurations
2304 Description: Enable debug support
2309 Description: Include API for web frontend.
2314 description: Whether to build against @directory >= 1.2@
2315 -- This is an automatic flag which the solver will
2316 -- assign automatically while searching for a solution
2319 Build-Depends: base >= 4.2 && < 4.9
2320 Exposed-Modules: Testing.Test1
2321 Default-Extensions: CPP
2322 Default-Language: Haskell2010
2326 CPP-Options: -DDEBUG
2328 CC-Options: "-DDEBUG"
2330 CC-Options: "-DNDEBUG"
2332 if flag(WebFrontend)
2333 Build-Depends: cgi >= 0.42 && < 0.44
2334 Other-Modules: Testing.WebStuff
2335 CPP-Options: -DWEBFRONTEND
2337 if flag(NewDirectory)
2338 build-depends: directory >= 1.2 && < 1.4
2339 Build-Depends: time >= 1.0 && < 1.9
2341 build-depends: directory == 1.1.*
2342 Build-Depends: old-time >= 1.0 && < 1.2
2346 Other-Modules: Testing.Test1
2347 Build-Depends: base >= 4.2 && < 4.9
2348 Default-Language: Haskell2010
2351 CC-Options: "-DDEBUG"
2352 CPP-Options: -DDEBUG
2357 Flags, conditionals, library and executable sections use layout to
2358 indicate structure. This is very similar to the Haskell layout rule.
2359 Entries in a section have to all be indented to the same level which
2360 must be more than the section header. Tabs are not allowed to be used
2363 As an alternative to using layout you can also use explicit braces
2364 ``{}``. In this case the indentation of entries in a section does not
2365 matter, though different fields within a block must be on different
2366 lines. Here is a bit of the above example again, using braces:
2368 Example: Using explicit braces rather than indentation for layout
2369 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2376 License: BSD-3-Clause
2378 Synopsis: Test package to test configurations
2383 Description: Enable debug support
2389 Build-Depends: base >= 4.2 && < 4.9
2390 Exposed-Modules: Testing.Test1
2391 Default-Extensions: CPP
2392 Default-language: Haskell2010
2394 CPP-Options: -DDEBUG
2396 CC-Options: "-DDEBUG"
2398 CC-Options: "-DNDEBUG"
2406 .. pkg-section:: flag name
2407 :synopsis: Flag declaration.
2409 Flag section declares a flag which can be used in `conditional blocks`_.
2411 Flag names are case-insensitive and must match ``[[:alnum:]_][[:alnum:]_-]*``
2412 regular expression, or expressed as ABNF_:
2414 .. code-block:: abnf
2416 flag-name = (UALNUM / "_") *(UALNUM / "_" / "-")
2418 UALNUM = UALPHA / DIGIT
2419 UALPHA = ... ; set of alphabetic Unicode code-points
2423 Hackage accepts ASCII-only flags, ``[a-zA-Z0-9_][a-zA-Z0-9_-]*`` regexp.
2425 .. pkg-field:: description: freeform
2427 The description of this flag.
2429 .. pkg-field:: default: boolean
2433 The default value of this flag.
2437 This value may be :ref:`overridden in several
2438 ways <controlling flag assignments>`. The
2439 rationale for having flags default to True is that users usually
2440 want new features as soon as they are available. Flags representing
2441 features that are not (yet) recommended for most users (such as
2442 experimental features or debugging support) should therefore
2443 explicitly override the default to False.
2445 .. pkg-field:: manual: boolean
2450 By default, Cabal will first try to satisfy dependencies with the
2451 default flag value and then, if that is not possible, with the
2452 negated value. However, if the flag is manual, then the default
2453 value (which can be overridden by commandline flags) will be used.
2455 .. _conditional-blocks:
2460 Conditional blocks may appear anywhere inside a component or common
2461 section. They have to follow rather strict formatting rules. Conditional
2462 blocks must always be of the shape
2467 property-descriptions-or-conditionals
2474 property-descriptions-or-conditionals
2476 property-descriptions-or-conditionals
2478 Note that the ``if`` and the condition have to be all on the same line.
2480 Since Cabal 2.2 conditional blocks support ``elif`` construct.
2485 property-descriptions-or-conditionals
2487 property-descriptions-or-conditionals
2489 property-descriptions-or-conditionals
2496 Conditions can be formed using boolean tests and the boolean operators
2497 ``||`` (disjunction / logical "or"), ``&&`` (conjunction / logical
2498 "and"), or ``!`` (negation / logical "not"). The unary ``!`` takes
2499 highest precedence, ``||`` takes lowest. Precedence levels may be
2500 overridden through the use of parentheses. For example,
2501 ``os(darwin) && !arch(i386) || os(freebsd)`` is equivalent to
2502 ``(os(darwin) && !(arch(i386))) || os(freebsd)``.
2504 The following tests are currently supported.
2507 Tests if the current operating system is *name*. The argument is
2508 tested against ``System.Info.os`` on the target system. There is
2509 unfortunately some disagreement between Haskell implementations
2510 about the standard values of ``System.Info.os``. Cabal canonicalises
2511 it so that in particular ``os(windows)`` works on all
2512 implementations. If the canonicalised os names match, this test
2513 evaluates to true, otherwise false. The match is case-insensitive.
2514 :samp:`arch({name})`
2515 Tests if the current architecture is *name*. *name* should be the name of
2516 one of the nullary constructors of ``Distribution.System.Arch`` (e.g.
2517 ``x86_64``, ``aarch64`` or ``i386``), otherwise it will be treated as an
2518 'other architecture' of the given *name*. It will be compared with
2519 ``Distribution.System.buildArch``, which is derived from
2520 ``System.Info.arch`` (certain architectures are treated as synonymous; e.g.
2521 ``aarch64`` / ``arm64`` or ``powerpc64`` / ``powerpc64le`` are not
2522 distinguished). For a match, this test evaluates to true, otherwise false.
2523 The match is case-insensitive.
2524 :samp:`impl({compiler})`
2525 Tests for the configured Haskell implementation. An optional version
2526 constraint may be specified (for example ``impl(ghc >= 6.6.1)``). If
2527 the configured implementation is of the right type and matches the
2528 version constraint, then this evaluates to true, otherwise false.
2529 The match is case-insensitive.
2531 Note that including a version constraint in an ``impl`` test causes
2532 it to check for two properties:
2534 - The current compiler has the specified name, and
2536 - The compiler's version satisfied the specified version constraint
2538 As a result, ``!impl(ghc >= x.y.z)`` is not entirely equivalent to
2539 ``impl(ghc < x.y.z)``. The test ``!impl(ghc >= x.y.z)`` checks that:
2541 - The current compiler is not GHC, or
2543 - The version of GHC is earlier than version x.y.z.
2545 :samp:`flag({name})`
2546 Evaluates to the current assignment of the flag of the given name.
2547 Flag names are case insensitive. Testing for flags that have not
2548 been introduced with a flag section is an error.
2550 Constant value true.
2552 Constant value false.
2554 .. _resolution-of-conditions-and-flags:
2556 Resolution of Conditions and Flags
2557 """"""""""""""""""""""""""""""""""
2559 If a package descriptions specifies configuration flags the package user
2560 can :ref:`control these in several ways <controlling flag assignments>`. If the
2561 user does not fix the value of a flag, Cabal will try to find a flag
2562 assignment in the following way.
2564 - For each flag specified, it will assign its default value, evaluate
2565 all conditions with this flag assignment, and check if all
2566 dependencies can be satisfied. If this check succeeded, the package
2567 will be configured with those flag assignments.
2569 - If dependencies were missing, the last flag (as by the order in which
2570 the flags were introduced in the package description) is tried with
2571 its alternative value and so on. This continues until either an
2572 assignment is found where all dependencies can be satisfied, or all
2573 possible flag assignments have been tried.
2575 To put it another way, Cabal does a complete backtracking search to find
2576 a satisfiable package configuration. It is only the dependencies
2577 specified in the :pkg-field:`build-depends` field in conditional blocks that
2578 determine if a particular flag assignment is satisfiable
2579 (:pkg-field:`build-tools` are not considered). The order of the declaration and
2580 the default value of the flags determines the search order. Flags
2581 overridden on the command line fix the assignment of that flag, so no
2582 backtracking will be tried for that flag.
2584 If no suitable flag assignment could be found, the configuration phase
2585 will fail and a list of missing dependencies will be printed. Note that
2586 this resolution process is exponential in the worst case (i.e., in the
2587 case where dependencies cannot be satisfied). There are some
2588 optimizations applied internally, but the overall complexity remains
2591 Meaning of field values when using conditionals
2592 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2594 During the configuration phase, a flag assignment is chosen, all
2595 conditionals are evaluated, and the package description is combined into
2596 a flat package descriptions. If the same field is declared both inside
2597 a conditional and outside then they are combined using the following rules.
2599 - Boolean fields are combined using conjunction (logical "and").
2601 - List fields are combined by appending the inner items to the outer
2606 other-extensions: CPP
2608 other-extensions: MultiParamTypeClasses
2610 when compiled using GHC will be combined to
2614 other-extensions: CPP, MultiParamTypeClasses
2616 Similarly, if two conditional sections appear at the same nesting
2617 level, properties specified in the latter will come after properties
2618 specified in the former.
2620 - All other fields must not be specified in ambiguous ways. For example
2625 if flag(useothermain)
2626 Main-is: OtherMain.hs
2628 will lead to an error. Instead use
2632 if flag(useothermain)
2633 Main-is: OtherMain.hs
2642 .. pkg-section:: common name
2644 :synopsis: Common build info section
2646 Starting with Cabal-2.2 it's possible to use common build info stanzas.
2651 build-depends: base ^>= 4.18
2655 build-depends: tasty ^>= 1.4
2659 exposed-modules: Foo
2660 default-language: Haskell2010
2663 import: deps, test-deps
2664 type: exitcode-stdio-1.0
2667 default-language: Haskell2010
2669 - You can use `build information`_ fields in common stanzas.
2671 - Common stanzas must be defined before use.
2673 - Common stanzas can import other common stanzas.
2675 - You can import multiple stanzas at once. Stanza names must be separated by commas.
2677 - ``import`` must be the first field in a section. Since Cabal 3.0 imports
2678 are also allowed inside conditionals.
2682 The name `import` was chosen, because there is ``includes`` field.
2684 .. pkg-section:: None
2686 .. pkg-field:: import: token-list
2691 .. _pkg-author-source:
2693 *Source code* repository marker
2694 -------------------------------
2696 .. pkg-section:: source-repository
2699 A marker that points to the *source code* for this package within a
2700 **source code repository**.
2702 There are two kinds. You can specify one or the other or both at once:
2704 - The ``head`` kind refers to the latest development branch of the
2705 package. This may be used for example to track activity of a project
2706 or as an indication to outside developers what sources to get for
2707 making new contributions.
2709 - The ``this`` kind refers to the branch and tag of a repository that
2710 contains the sources for this version or release of a package. For most
2711 source control systems this involves specifying a tag, id or hash of some
2712 form and perhaps a branch.
2714 As an example, here are the repositories for the Cabal library. Note that the
2715 ``this`` kind of repository specifies a tag.
2719 source-repository head
2721 location: https://github.com/haskell/cabal
2723 source-repository this
2725 location: https://github.com/haskell/cabal
2728 The :ref:`cabal get<cabal-get>` command uses the kind of repository with
2729 its ``--source-repository`` option, if provided.
2731 .. _source-repository-fields:
2733 The :ref:`VCS fields<vcs-fields>` of ``source-repository`` are:
2736 data SourceRepo = SourceRepo
2737 { repoKind :: RepoKind
2738 , repoType :: Maybe RepoType
2739 , repoLocation :: Maybe String
2740 , repoModule :: Maybe String
2741 , repoBranch :: Maybe String
2742 , repoTag :: Maybe String
2743 , repoSubdir :: Maybe FilePath
2746 .. pkg-field:: type: VCS kind
2748 This field is required.
2750 .. pkg-field:: location: VCS location
2752 This field is required.
2754 .. pkg-field:: module: token
2756 CVS requires a named module, as each CVS server can host multiple
2759 This field is required for the CVS repository type and should not be
2762 .. pkg-field:: branch: VCS branch
2764 This field is optional.
2766 .. pkg-field:: tag: VCS tag
2768 This field is required for the ``this`` repository kind.
2770 This might be used to indicate what sources to get if someone needs to fix a
2771 bug in an older branch that is no longer an active head branch.
2773 .. pkg-field:: subdir: VCS subdirectory
2775 This field is optional but, if given, specifies a single subdirectory.
2782 The ``Hooks`` build type allows customising the configuration and the building
2783 of a package using a collection of **hooks** into the build system.
2785 Introduced in Cabal 3.14, this build type provides an alternative
2786 to :ref:`Custom setups <custom-setup>` which integrates better with the rest of the
2789 To use this build type in your package, you need to:
2791 * Declare a ``cabal-version`` of at least 3.14 in your ``.cabal`` file.
2792 * Declare ``build-type: Hooks`` in your ``.cabal`` file.
2793 * Include a ``custom-setup`` stanza in your ``.cabal`` file, which declares
2794 the version of the Hooks API your package is using.
2795 * Define a ``SetupHooks.hs`` module next to your ``.cabal`` file. It must
2796 export a value ``setupHooks :: SetupHooks``.
2798 More specifically, your ``.cabal`` file should resemble the following:
2800 .. code-block:: cabal
2807 base >= 4.18 && < 5,
2808 Cabal-hooks >= 0.1 && < 0.2
2810 while a basic ``SetupHooks.hs`` file might look like the following:
2812 .. code-block:: haskell
2814 module SetupHooks where
2815 import Distribution.Simple.SetupHooks ( SetupHooks, noSetupHooks )
2817 setupHooks :: SetupHooks
2820 { configureHooks = myConfigureHooks
2821 , buildHooks = myBuildHooks }
2825 Refer to the `Hackage documentation for the Distribution.Simple.SetupHooks module <https://hackage.haskell.org/package/Cabal-hooks/docs/Distribution-Simple-SetupHooks.html>`__
2826 for an overview of the ``Hooks`` API. Further motivation and a technical overview
2827 of the design is available in `Haskell Tech Proposal #60 <https://github.com/haskellfoundation/tech-proposals/blob/main/rfc/060-replacing-cabal-custom-build.md>`__ .
2831 Custom setup scripts
2832 --------------------
2834 Deprecated since Cabal 3.14: prefer using the :ref:`Hooks build type<setup-hooks>` instead.
2836 Since Cabal 1.24, custom ``Setup.hs`` are required to accurately track
2837 their dependencies by declaring them in the ``.cabal`` file rather than
2838 rely on dependencies being implicitly in scope. Please refer to
2839 `this article <https://www.well-typed.com/blog/2015/07/cabal-setup-deps/>`__
2842 As of Cabal library version 3.0, ``defaultMain*`` variants implement support
2843 for response files. Custom ``Setup.hs`` files that do not use one of these
2844 main functions are required to implement their own support, such as by using
2845 ``GHC.ResponseFile.getArgsWithResponseFiles``.
2847 Declaring a ``custom-setup`` stanza also enables the generation of
2848 ``MIN_VERSION_package_(A,B,C)`` CPP macros for the Setup component.
2850 .. pkg-section:: custom-setup
2851 :synopsis: Build information for ``Custom`` and ``Hooks`` build types
2854 A :pkg-section:`custom-setup` stanza is required for ``Custom`` and ``Hooks``
2855 :pkg-field:`build-type`, and will be ignored (with a warning)
2856 for other build types.
2858 The stanza contains information needed for the compilation
2859 of custom ``Setup.hs`` scripts, and of ``SetupHooks.hs`` hooks.
2866 base >= 4.18 && < 5,
2869 .. pkg-field:: setup-depends: package list
2872 The dependencies needed to compile ``Setup.hs`` or ``SetupHooks.hs``. See the
2873 :pkg-field:`build-depends` field for a description of the syntax expected by
2876 If the field is not specified the implicit package set will be used.
2877 The package set contains packages bundled with GHC (i.e. ``base``,
2878 ``bytestring``) and specifically ``Cabal``.
2879 The specific bounds are put on ``Cabal`` dependency:
2880 lower-bound is inferred from :pkg-field:`cabal-version`,
2881 and the upper-bound is ``< 1.25``.
2883 ``Cabal`` version is additionally restricted by GHC,
2884 with absolute minimum being ``1.20``, and for example ``Custom``
2885 builds with GHC-8.10 require at least ``Cabal-3.2``.
2888 Backward compatibility and ``custom-setup``
2889 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2891 Versions prior to Cabal 1.24 don't recognise ``custom-setup`` stanzas,
2892 and will behave agnostic to them (except for warning about an unknown
2893 'section'). Consequently, versions prior to Cabal 1.24 can't ensure the
2894 declared dependencies ``setup-depends`` are in scope, and instead
2895 whatever is registered in the current package database environment
2896 will become eligible (and resolved by the compiler) for the
2897 ``Setup.hs`` module.
2899 The availability of the
2900 ``MIN_VERSION_package_(A,B,C)`` CPP macros
2901 inside ``Setup.hs`` scripts depends on the condition that either
2903 - a ``custom-setup`` stanza has been declared (or ``cabal build`` is being used
2904 which injects an implicit hard-coded ``custom-setup`` stanza if it's missing),
2906 - GHC 8.0 or later is used (which natively injects package version CPP macros)
2908 Consequently, if you need to write backward compatible ``Setup.hs``
2909 scripts using CPP, you should declare a ``custom-setup`` stanza and
2910 use the pattern below:
2912 .. code-block:: haskell
2914 {-# LANGUAGE CPP #-}
2915 import Distribution.Simple
2917 #if defined(MIN_VERSION_Cabal)
2918 -- version macros are available and can be used as usual
2919 # if MIN_VERSION_Cabal(a,b,c)
2920 -- code specific to lib:Cabal >= a.b.c
2922 -- code specific to lib:Cabal < a.b.c
2925 # warning Enabling heuristic fall-back. Please upgrade cabal-install to 1.24 or later if Setup.hs fails to compile.
2927 -- package version macros not available; except for exotic environments,
2928 -- you can heuristically assume that lib:Cabal's version is correlated
2929 -- with __GLASGOW_HASKELL__, and specifically since we can assume that
2930 -- GHC < 8.0, we can assume that lib:Cabal is version 1.22 or older.
2935 The simplified (heuristic) CPP pattern shown below is useful if all you need
2936 is to distinguish ``Cabal < 2.0`` from ``Cabal >= 2.0``.
2938 .. code-block:: haskell
2940 {-# LANGUAGE CPP #-}
2941 import Distribution.Simple
2943 #if !defined(MIN_VERSION_Cabal)
2944 # define MIN_VERSION_Cabal(a,b,c) 0
2947 #if MIN_VERSION_Cabal(2,0,0)
2948 -- code for lib:Cabal >= 2.0
2950 -- code for lib:Cabal < 2.0
2957 Autogenerated modules and includes
2958 ----------------------------------
2960 .. pkg-section:: None
2962 Modules that are built automatically at setup, created with a custom
2963 setup script, must appear on :pkg-field:`other-modules` for the library,
2964 executable, test-suite or benchmark stanzas or also on
2965 :pkg-field:`library:exposed-modules` for libraries to be used, but are not
2966 really on the package when distributed. This makes commands like sdist fail
2967 because the file is not found.
2969 These special modules must appear again on the :pkg-field:`autogen-modules`
2970 field of the stanza that is using them, besides :pkg-field:`other-modules` or
2971 :pkg-field:`library:exposed-modules`. With this there is no need to create
2972 complex build hooks for this poweruser case.
2974 .. pkg-field:: autogen-modules: module list
2977 .. todo:: document autogen-modules field
2979 Right now :pkg-field:`executable:main-is` modules are not supported on
2980 :pkg-field:`autogen-modules`.
2985 default-language: Haskell2010
2996 default-language: Haskell2010
3005 .. pkg-field:: autogen-includes: filename list
3008 A list of header files from this package which are autogenerated
3009 (e.g. by a ``configure`` script). Autogenerated header files are not
3010 packaged by ``sdist`` command.
3013 .. _accessing-data-files:
3015 Accessing data files from package code
3016 --------------------------------------
3021 The placement on the target system of files listed in
3022 the :pkg-field:`data-files` field varies between systems, and in some cases
3023 one can even move packages around after installation
3024 (see :ref:`prefix independence`). To
3025 enable packages to find these files in a portable way, Cabal generates a
3026 module called :file:`Paths_{pkgname}` (with any hyphens in *pkgname*
3027 replaced by underscores) during building, so that it may be imported by
3028 modules of the package. This module defines a function
3030 .. code-block:: haskell
3032 getDataFileName :: FilePath -> IO FilePath
3034 If the argument is a filename listed in the :pkg-field:`data-files` field, the
3035 result is the name of the corresponding file on the system on which the
3040 If you decide to import the :file:`Paths_{pkgname}` module then it
3041 *must* be listed in the :pkg-field:`other-modules` field just like any other
3042 module in your package and on :pkg-field:`autogen-modules` as the file is
3045 The :file:`Paths_{pkgname}` module is not platform independent, as any
3046 other autogenerated module, so it does not get included in the source
3047 tarballs generated by ``sdist``.
3049 The :file:`Paths_{pkgname}` module also includes some other useful
3050 functions and values, which record the version of the package and some
3051 other directories which the package has been configured to be installed
3052 into (e.g. data files live in ``getDataDir``):
3054 .. code-block:: haskell
3058 getBinDir :: IO FilePath
3059 getLibDir :: IO FilePath
3060 getDynLibDir :: IO FilePath
3061 getDataDir :: IO FilePath
3062 getLibexecDir :: IO FilePath
3063 getSysconfDir :: IO FilePath
3065 The actual location of all these directories can be individually
3066 overridden at runtime using environment variables of the form
3067 ``pkg_name_var``, where ``pkg_name`` is the name of the package with all
3068 hyphens converted into underscores, and ``var`` is either ``bindir``,
3069 ``libdir``, ``dynlibdir``, ``datadir``, ``libexedir`` or ``sysconfdir``. For example,
3070 the configured data directory for ``pretty-show`` is controlled with the
3071 ``pretty_show_datadir`` environment variable.
3073 Accessing the package version
3074 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3076 .. index:: PackageInfo
3077 .. index:: PackageInfo_
3079 The auto generated :file:`PackageInfo_{pkgname}` module exports the constant
3080 ``version ::`` `Version <http://hackage.haskell.org/package/base/docs/Data-Version.html>`__
3081 which is defined as the version of your package as specified in the
3084 Accessing package-related informations
3085 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3087 The auto generated :file:`PackageInfo_{pkgname}` module exports the following
3088 package-related constants:
3090 .. code-block:: haskell
3098 Unlike :file:`Paths_{pkgname}` (see <#accessing-data-files-from-package-code>),
3099 :file:`PackageInfo_{pkgname}` is system- and path-independent. It aims to be
3100 easier to work with for hash-based tools such as Nix.
3102 .. _system-dependent parameters:
3104 System-dependent parameters
3105 ---------------------------
3107 For some packages, especially those interfacing with C libraries,
3108 implementation details and the build procedure depend on the build
3109 environment. The ``build-type`` ``Configure`` can be used to handle many
3110 such situations. In this case, ``Setup.hs`` should be:
3112 .. code-block:: haskell
3114 import Distribution.Simple
3115 main = defaultMainWithHooks autoconfUserHooks
3117 Most packages, however, would probably do better using the ``Simple``
3118 build type and `configurations`_.
3120 The :pkg-field:`build-type` ``Configure`` differs from ``Simple`` in two ways:
3122 - The package root directory must contain a shell script called
3123 ``configure``. The configure step will run the script. This
3124 ``configure`` script may be produced by
3125 `autoconf <http://www.gnu.org/software/autoconf/>`__ or may be
3126 hand-written. The ``configure`` script typically discovers
3127 information about the system and records it for later steps, e.g. by
3128 generating system-dependent header files for inclusion in C source
3129 files and preprocessed Haskell source files. (Clearly this won't work
3130 for Windows without MSYS or Cygwin: other ideas are needed.)
3132 - If the package root directory contains a file called
3133 *package*\ ``.buildinfo`` after the configuration step, subsequent
3134 steps will read it to obtain additional settings for `build
3135 information`_ fields,to be merged with the ones
3136 given in the ``.cabal`` file. In particular, this file may be
3137 generated by the ``configure`` script mentioned above, allowing these
3138 settings to vary depending on the build environment.
3140 Note that the package's ``extra-source-files`` are available to the
3141 ``configure`` script when it is executed. In typical ``autoconf`` fashion,
3142 ``--host`` flag will be passed to the ``configure`` script to indicate the host
3143 platform when cross-compiling. Moreover, various bits of build configuration
3144 will be passed via environment variables:
3146 - ``CC`` will reflect the path to the C compiler
3147 - ``CFLAGS`` will reflect the path to the C compiler
3148 - ``CABAL_FLAGS`` will contain the Cabal flag assignment of the current
3149 package using traditional Cabal flag syntax (e.g. ``+flagA -flagB``)
3150 - ``CABAL_FLAG_<flag>`` will be set to either ``0`` or ``1`` depending upon
3151 whether flag ``<flag>`` is enabled. Note that any any non-alpha-numeric
3152 characters in the flag name are replaced with ``_``.
3154 The build information file should have the following structure:
3158 ``executable:`` *name* *buildinfo*
3160 ``executable:`` *name* *buildinfo* ...
3162 where each *buildinfo* consists of settings of fields listed in the
3163 section on `build information`_. The first one (if
3164 present) relates to the library, while each of the others relate to the
3165 named executable. (The names must match the package description, but you
3166 don't have to have entries for all of them.)
3168 Neither of these files is required. If they are absent, this setup
3169 script is equivalent to ``defaultMain``.
3171 Example: Using autoconf
3172 ^^^^^^^^^^^^^^^^^^^^^^^
3174 This example is for people familiar with the
3175 `autoconf <http://www.gnu.org/software/autoconf/>`__ tools.
3177 In the X11 package, the file ``configure.ac`` contains:
3179 .. code-block:: shell
3181 AC_INIT([Haskell X11 package], [1.1], [libraries@haskell.org], [X11])
3183 # Safety check: Ensure that we are in the correct source directory.
3184 AC_CONFIG_SRCDIR([X11.cabal])
3186 # Header file to place defines in
3187 AC_CONFIG_HEADERS([include/HsX11Config.h])
3189 # Check for X11 include paths and libraries
3191 AC_TRY_CPP([#include <X11/Xlib.h>],,[no_x=yes])
3193 # Build the package if we found X11 stuff
3194 if test "$no_x" = yes
3195 then BUILD_PACKAGE_BOOL=False
3196 else BUILD_PACKAGE_BOOL=True
3198 AC_SUBST([BUILD_PACKAGE_BOOL])
3200 AC_CONFIG_FILES([X11.buildinfo])
3203 Then the setup script will run the ``configure`` script, which checks
3204 for the presence of the X11 libraries and substitutes for variables in
3205 the file ``X11.buildinfo.in``:
3209 buildable: @BUILD_PACKAGE_BOOL@
3210 cc-options: @X_CFLAGS@
3211 ld-options: @X_LIBS@
3213 This generates a file ``X11.buildinfo`` supplying the parameters needed
3219 cc-options: -I/usr/X11R6/include
3220 ld-options: -L/usr/X11R6/lib
3222 The ``configure`` script also generates a header file
3223 ``include/HsX11Config.h`` containing C preprocessor defines recording
3224 the results of various tests. This file may be included by C source
3225 files and preprocessed Haskell source files in the package.
3229 Packages using these features will also need to list additional
3230 files such as ``configure``, templates for ``.buildinfo`` files, files
3231 named only in ``.buildinfo`` files, header files and so on in the
3232 :pkg-field:`extra-source-files` field to ensure that they are included in
3233 source distributions. They should also list files and directories generated
3234 by ``configure`` in the :pkg-field:`extra-tmp-files` field to ensure that
3235 they are removed by ``setup clean``.
3237 Quite often the files generated by ``configure`` need to be listed
3238 somewhere in the package description (for example, in the
3239 :pkg-field:`install-includes` field). However, we usually don't want generated
3240 files to be included in the source tarball. The solution is again
3241 provided by the ``.buildinfo`` file. In the above example, the following
3242 line should be added to ``X11.buildinfo``:
3246 install-includes: HsX11Config.h
3248 In this way, the generated ``HsX11Config.h`` file won't be included in
3249 the source tarball in addition to ``HsX11Config.h.in``, but it will be
3250 copied to the right location during the install process. Packages that
3251 use custom ``Setup.hs`` scripts can update the necessary fields
3252 programmatically instead of using the ``.buildinfo`` file.
3254 Conditional compilation
3255 -----------------------
3257 Sometimes you want to write code that works with more than one version
3258 of a dependency. You can specify a range of versions for the dependency
3259 in the :pkg-field:`build-depends`, but how do you then write the code that can
3260 use different versions of the API?
3262 Haskell lets you preprocess your code using the C preprocessor (either
3263 the real C preprocessor, or ``cpphs``). To enable this, add
3264 ``extensions: CPP`` to your package description. When using CPP, Cabal
3265 provides some pre-defined macros to let you test the version of
3266 dependent packages; for example, suppose your package works with either
3267 version 3 or version 4 of the ``base`` package, you could select the
3268 available version in your Haskell modules like this:
3272 #if MIN_VERSION_base(4,0,0)
3273 ... code that works with base-4 ...
3275 ... code that works with base-3 ...
3278 In general, Cabal supplies a macro
3279 ``MIN_VERSION_``\ *``package``*\ ``_(A,B,C)`` for each package depended
3280 on via :pkg-field:`build-depends`. This macro is true if the actual version of
3281 the package in use is greater than or equal to ``A.B.C`` (using the
3282 conventional ordering on version numbers, which is lexicographic on the
3283 sequence, but numeric on each component, so for example 1.2.0 is greater
3286 Since version 1.20, the ``MIN_TOOL_VERSION_``\ *``tool``*
3287 family of macros lets you condition on the version of build tools used to
3288 build the program (e.g. ``hsc2hs``).
3290 Since version 1.24, the macro ``CURRENT_COMPONENT_ID``, which
3291 expands to the string of the component identifier that uniquely
3292 identifies this component. Furthermore, if the package is a library,
3293 the macro ``CURRENT_PACKAGE_KEY`` records the identifier that was passed
3294 to GHC for use in symbols and for type equality.
3296 Since version 2.0, the macro ``CURRENT_PACKAGE_VERSION`` expands
3297 to the string version number of the current package.
3299 Cabal places the definitions of these macros into an
3300 automatically-generated header file, which is included when
3301 preprocessing Haskell source code by passing options to the C
3304 Cabal also allows to detect when the source code is being used for
3305 generating documentation. The ``__HADDOCK_VERSION__`` macro is defined
3306 only when compiling via Haddock_
3307 instead of a normal Haskell compiler. The value of the
3308 ``__HADDOCK_VERSION__`` macro is defined as ``A*1000 + B*10 + C``, where
3309 ``A.B.C`` is the Haddock version. This can be useful for working around
3310 bugs in Haddock or generating prettier documentation in some special
3313 .. _more-complex-packages:
3315 More complex packages
3316 ---------------------
3318 For packages that don't fit the simple schemes described above, you have
3321 - By using the :pkg-field:`build-type` ``Custom``, you can supply your own
3322 ``Setup.hs`` file, and customize the simple build infrastructure
3323 using *hooks*. These allow you to perform additional actions before
3324 and after each command is run, and also to specify additional
3325 preprocessors. A typical ``Setup.hs`` may look like this:
3327 .. code-block:: haskell
3329 import Distribution.Simple
3330 main = defaultMainWithHooks simpleUserHooks { postHaddock = posthaddock }
3332 posthaddock args flags desc info = ....
3334 See ``UserHooks`` in
3335 `Distribution.Simple <https://hackage.haskell.org/package/Cabal/docs/Distribution-Simple.html>`__
3336 for the details, but note that this interface is experimental, and
3337 likely to change in future releases.
3339 If you use a custom ``Setup.hs`` file you should strongly consider
3340 adding a :pkg-section:`custom-setup` stanza with a
3341 :pkg-field:`custom-setup:setup-depends` field to ensure that your setup
3342 script does not break with future dependency versions.
3344 - You could delegate all the work to ``make``, though this is unlikely
3345 to be very portable. Cabal supports this with the :pkg-field:`build-type`
3346 ``Make`` and a trivial setup library
3347 `Distribution.Make <https://hackage.haskell.org/package/Cabal/docs/Distribution-Make.html>`__,
3348 which simply parses the command line arguments and invokes ``make``.
3349 Here ``Setup.hs`` should look like this:
3351 .. code-block:: haskell
3353 import Distribution.Make
3356 The root directory of the package should contain a ``configure``
3357 script, and, after that has run, a ``Makefile`` with a default target
3358 that builds the package, plus targets ``install``, ``register``,
3359 ``unregister``, ``clean``, ``dist`` and ``docs``. Some options to
3360 commands are passed through as follows:
3362 - The ``--with-hc-pkg``, ``--prefix``, ``--bindir``, ``--libdir``,
3363 ``--dynlibdir``, ``--datadir``, ``--libexecdir`` and ``--sysconfdir`` options to
3364 the ``configure`` command are passed on to the ``configure``
3365 script. In addition the value of the ``--with-compiler`` option is
3366 passed in a ``--with-hc`` option and all options specified with
3367 ``--configure-option=`` are passed on.
3369 - The ``--destdir`` option to the ``copy`` command becomes a setting
3370 of a ``destdir`` variable on the invocation of ``make copy``. The
3371 supplied ``Makefile`` should provide a ``copy`` target, which will
3372 probably look like this:
3374 .. code-block:: make
3377 $(MAKE) install prefix=$(destdir)/$(prefix) \
3378 bindir=$(destdir)/$(bindir) \
3379 libdir=$(destdir)/$(libdir) \
3380 dynlibdir=$(destdir)/$(dynlibdir) \
3381 datadir=$(destdir)/$(datadir) \
3382 libexecdir=$(destdir)/$(libexecdir) \
3383 sysconfdir=$(destdir)/$(sysconfdir) \
3385 - Finally, with the :pkg-field:`build-type` ``Custom``, you can also write your
3386 own setup script from scratch, and you may use the Cabal
3387 library for all or part of the work. One option is to copy the source
3388 of ``Distribution.Simple``, and alter it for your needs. Good luck.
3390 .. include:: references.inc
3392 .. rubric:: Footnotes
3394 .. [#old-style-build-tool-depends]
3396 Some packages (ab)use :pkg-field:`build-depends` on old-style builds, but this has a few major drawbacks:
3398 - using Nix-style builds it's considered an error if you depend on a exe-only package via build-depends: the solver will refuse it.
3399 - it may or may not place the executable on ``PATH``.
3400 - it does not ensure the correct version of the package is installed, so you might end up overwriting versions with each other.