4 The Cabal package is the unit of distribution. When installed, its
5 purpose is to make available:
7 - One or more Haskell programs.
9 - At most one library, exposing a number of Haskell modules.
11 However having both a library and executables in a package does not work
12 very well; if the executables depend on the library, they must
13 explicitly list all the modules they directly or indirectly import from
14 that library. Fortunately, starting with Cabal 1.8.0.4, executables can
15 also declare the package that they are in as a dependency, and Cabal
16 will treat them as if they were in another package that depended on the
19 Internally, the package may consist of much more than a bunch of Haskell
20 modules: it may also have C source code and header files, source code
21 meant for preprocessing, documentation, test cases, auxiliary tools etc.
23 A package is identified by a globally-unique *package name*, which
24 consists of one or more alphanumeric words separated by hyphens. To
25 avoid ambiguity, each of these words should contain at least one letter.
26 Chaos will result if two distinct packages with the same name are
27 installed on the same system. A particular version of the package is
28 distinguished by a *version number*, consisting of a sequence of one or
29 more integers separated by dots. These can be combined to form a single
30 text string called the *package ID*, using a hyphen to separate the name
31 from the version, e.g. "``HUnit-1.1``".
35 Packages are not part of the Haskell language; they simply
36 populate the hierarchical space of module names. In GHC 6.6 and later a
37 program may contain multiple modules with the same name if they come
38 from separate packages; in all other current Haskell systems packages
39 may not overlap in the modules they provide, including hidden modules.
44 Suppose you have a directory hierarchy containing the source files that
45 make up your package. You will need to add two more files to the root
46 directory of the package:
48 :file:`{package-name}.cabal`
49 a Unicode UTF-8 text file containing a package description. For
50 details of the syntax of this file, see the section on
51 `package descriptions`_.
54 a single-module Haskell program to perform various setup tasks (with
55 the interface described in the section on :ref:`setup-commands`).
56 This module should import only modules that will be present in all Haskell
57 implementations, including modules of the Cabal library. The content of
58 this file is determined by the :pkg-field:`build-type` setting in the
59 ``.cabal`` file. In most cases it will be trivial, calling on the Cabal
60 library to do most of the work.
62 Once you have these, you can create a source bundle of this directory
63 for distribution. Building of the package is demonstrated in the section
64 :ref:`building-packages`.
66 One of the purposes of Cabal is to make it easier to build a package
67 with different Haskell implementations. So it provides abstractions of
68 features present in different Haskell implementations and wherever
69 possible it is best to take advantage of these to increase portability.
70 Where necessary however it is possible to use specific features of
71 specific implementations. For example one of the pieces of information a
72 package author can put in the package's ``.cabal`` file is what language
73 extensions the code uses. This is far preferable to specifying flags for
74 a specific compiler as it allows Cabal to pick the right flags for the
75 Haskell implementation that the user picks. It also allows Cabal to
76 figure out if the language extension is even supported by the Haskell
77 implementation that the user picks. Where compiler-specific options are
78 needed however, there is an "escape hatch" available. The developer can
79 specify implementation-specific options and more generally there is a
80 configuration mechanism to customise many aspects of how a package is
81 built depending on the Haskell implementation, the Operating system,
82 computer architecture and user-specified configuration flags.
90 default-language: Haskell2010
91 build-depends: base >= 4 && < 5
93 extensions: ForeignFunctionInterface
96 build-depends: Win32 >= 2.1 && < 2.6
98 Example: A package containing a simple library
99 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101 The HUnit package contains a file ``HUnit.cabal`` containing:
108 synopsis: A unit testing framework for Haskell
109 homepage: http://hunit.sourceforge.net/
111 author: Dean Herington
112 license: BSD-3-Clause
113 license-file: LICENSE
117 build-depends: base >= 2 && < 4
118 exposed-modules: Test.HUnit.Base, Test.HUnit.Lang,
119 Test.HUnit.Terminal, Test.HUnit.Text, Test.HUnit
120 default-extensions: CPP
121 default-language: Haskell2010
123 and the following ``Setup.hs``:
125 .. code-block:: haskell
127 import Distribution.Simple
130 Example: A package containing executable programs
131 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
138 synopsis: Small package with two programs
139 author: Angela Author
140 license: BSD-3-Clause
144 build-depends: HUnit >= 1.1.1 && < 1.2
146 hs-source-dirs: prog1
147 default-language: Haskell2010
150 -- A different main.hs because of hs-source-dirs.
152 build-depends: HUnit >= 1.1.1 && < 1.2
153 hs-source-dirs: prog2
155 default-language: Haskell2010
157 with ``Setup.hs`` the same as above.
159 Example: A package containing a library and executable programs
160 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
167 synopsis: Package with library and two programs
168 license: BSD-3-Clause
169 author: Angela Author
173 build-depends: HUnit >= 1.1.1 && < 1.2
175 exposed-modules: A, B, C
176 default-language: Haskell2010
180 hs-source-dirs: prog1
182 default-language: Haskell2010
185 -- A different main.hs because of hs-source-dirs.
187 -- No bound on internal libraries.
188 build-depends: TestPackage
189 hs-source-dirs: prog2
191 default-language: Haskell2010
193 with ``Setup.hs`` the same as above. Note that any library modules
194 required (directly or indirectly) by an executable must be listed again.
196 The trivial setup script used in these examples uses the *simple build
197 infrastructure* provided by the Cabal library (see
198 `Distribution.Simple <https://hackage.haskell.org/package/Cabal/docs/Distribution-Simple.html>`__).
199 The simplicity lies in its interface rather that its implementation. It
200 automatically handles preprocessing with standard preprocessors, and
201 builds packages for all the Haskell implementations.
203 The simple build infrastructure can also handle packages where building
204 is governed by system-dependent parameters, if you specify a little more
205 (see the section on `system-dependent parameters`_).
206 A few packages require `more elaborate solutions <more complex packages>`_.
213 The package description file must have a name ending in "``.cabal``". It
214 must be a Unicode text file encoded using valid UTF-8. There must be
215 exactly one such file in the directory. The first part of the name is
216 usually the package name, and some of the tools that operate on Cabal
217 packages require this; specifically, Hackage rejects packages which
218 don't follow this rule.
220 In the package description file, lines whose first non-whitespace
221 characters are "``--``" are treated as comments and ignored.
223 This file should contain a number global property descriptions and
226 - The `package properties`_ describe the package
227 as a whole, such as name, license, author, etc.
229 - Optionally, a number of *configuration flags* can be declared. These
230 can be used to enable or disable certain features of a package. (see
231 the section on `configurations`_).
233 - The (optional) library section specifies the `library`_ properties and
234 relevant `build information`_.
236 - Following is an arbitrary number of executable sections which describe
237 an executable program and relevant `build information`_.
239 Each section consists of a number of property descriptions in the form
240 of field/value pairs, with a syntax roughly like mail message headers.
242 - Case is not significant in field names, but is significant in field
245 - To continue a field value, indent the next line relative to the field
248 - Field names may be indented, but all field values in the same section
249 must use the same indentation.
251 - Tabs are *not* allowed as indentation characters due to a missing
252 standard interpretation of tab width.
254 - Before Cabal 3.0, to get a blank line in a field value, use an indented "``.``"
256 The syntax of the value depends on the field. Field types include:
258 *token*, *filename*, *directory*
259 Either a sequence of one or more non-space non-comma characters, or
260 a quoted string in Haskell 98 lexical syntax. The latter can be used
261 for escaping whitespace, for example:
262 ``ghc-options: -Wall "-with-rtsopts=-T -I1"``. Unless otherwise
263 stated, relative filenames and directories are interpreted from the
264 package root directory.
265 *freeform*, *URL*, *address*
266 An arbitrary, uninterpreted string.
268 A letter followed by zero or more alphanumerics or underscores.
270 A compiler flavor (one of: ``GHC``, ``UHC`` or ``LHC``)
271 followed by a version range. For example, ``GHC ==6.10.3``, or
272 ``LHC >=0.6 && <0.8``.
274 Modules and preprocessors
275 ^^^^^^^^^^^^^^^^^^^^^^^^^
277 Haskell module names listed in the :pkg-field:`library:exposed-modules` and
278 :pkg-field:`library:other-modules` fields may correspond to Haskell source
279 files, i.e. with names ending in "``.hs``" or "``.lhs``", or to inputs for
280 various Haskell preprocessors. The simple build infrastructure understands the
283 - ``.gc`` (:hackage-pkg:`greencard`)
284 - ``.chs`` (:hackage-pkg:`c2hs`)
285 - ``.hsc`` (:hackage-pkg:`hsc2hs`)
286 - ``.y`` and ``.ly`` (happy_)
288 - ``.cpphs`` (cpphs_)
290 When building, Cabal will automatically run the appropriate preprocessor
291 and compile the Haskell module it produces. For the ``c2hs`` and
292 ``hsc2hs`` preprocessors, Cabal will also automatically add, compile and
293 link any C sources generated by the preprocessor (produced by
294 ``hsc2hs``'s ``#def`` feature or ``c2hs``'s auto-generated wrapper
295 functions). Dependencies on pre-processors are specified via the
296 :pkg-field:`build-tools` or :pkg-field:`build-tool-depends` fields.
298 Some fields take lists of values, which are optionally separated by
299 commas, except for the :pkg-field:`build-depends` field, where the commas are
302 Some fields are marked as required. All others are optional, and unless
303 otherwise specified have empty default values.
308 These fields may occur in the first top-level properties section and
309 describe the package as a whole:
311 .. pkg-field:: name: package-name (required)
313 The unique name of the package, without the version number.
315 As pointed out in the section on `package descriptions`_, some
316 tools require the package-name specified for this field to match
317 the package description's file-name :file:`{package-name}.cabal`.
319 Package names are case-sensitive and must match the regular expression
320 (i.e. alphanumeric "words" separated by dashes; each alphanumeric
321 word must contain at least one letter):
322 ``[[:digit:]]*[[:alpha:]][[:alnum:]]*(-[[:digit:]]*[[:alpha:]][[:alnum:]]*)*``.
324 Or, expressed in ABNF_:
328 package-name = package-name-part *("-" package-name-part)
329 package-name-part = *DIGIT UALPHA *UALNUM
331 UALNUM = UALPHA / DIGIT
332 UALPHA = ... ; set of alphabetic Unicode code-points
336 Hackage restricts package names to the ASCII subset.
338 .. pkg-field:: version: numbers (required)
340 The package version number, usually consisting of a sequence of
341 natural numbers separated by dots, i.e. as the regular
342 expression ``[0-9]+([.][0-9]+)*`` or expressed in ABNF_:
346 package-version = 1*DIGIT *("." 1*DIGIT)
348 .. pkg-field:: cabal-version: x.y[.z]
350 The version of the Cabal specification that this package
351 description uses. The Cabal specification does slowly evolve (see
352 also :ref:`spec-history`), introducing new features and
353 occasionally changing the meaning of existing features.
354 Specifying which version of the specification you are using
355 enables programs which process the package description to know
356 what syntax to expect and what each part means.
358 The version number you specify will affect both compatibility and
359 behaviour. Most tools (including the Cabal library and the ``cabal``
360 program) understand a range of versions of the Cabal specification.
361 Older tools will of course only work with older versions of the
362 Cabal specification that was known at the time. Most of the time,
363 tools that are too old will recognise this fact and produce a
364 suitable error message. Likewise, ``cabal check`` will tell you
365 whether the version number is sufficiently high for the features
366 you use in the package description.
368 As for behaviour, new versions of the Cabal specification can change the
369 meaning of existing syntax. This means if you want to take advantage
370 of the new meaning or behaviour then you must specify the newer
371 Cabal version. Tools are expected to use the meaning and behaviour
372 appropriate to the version given in the package description.
374 In particular, the syntax of package descriptions changed
375 significantly with Cabal version 1.2 and the :pkg-field:`cabal-version`
376 field is now required. Files written in the old syntax are still
377 recognized, so if you require compatibility with very old Cabal
378 versions then you may write your package description file using the
379 old syntax. Please consult the user's guide of an older Cabal
380 version for a description of that syntax.
382 Starting with ``cabal-version: 2.2`` this field is only valid if
383 fully contained in the very first line of a package description
384 and ought to adhere to the ABNF_ grammar
388 newstyle-spec-version-decl = "cabal-version" *WS ":" *WS newstyle-spec-version *WS
390 newstyle-spec-version = NUM "." NUM [ "." NUM ]
392 NUM = DIGIT0 / DIGITP 1*DIGIT0
400 For package descriptions using a format prior to
401 ``cabal-version: 1.12`` the legacy syntax resembling a version
404 .. code-block:: cabal
406 cabal-version: >= 1.10
410 This legacy syntax is supported up until ``cabal-version: >=
411 2.0`` it is however strongly recommended to avoid using the
412 legacy syntax. See also :issue:`4899`.
416 .. pkg-field:: build-type: identifier
418 :default: ``Custom`` or ``Simple``
420 The type of build used by this package. Build types are the
422 `BuildType <https://hackage.haskell.org/package/Cabal/docs/Distribution-PackageDescription.html#t:BuildType>`__
423 type. This field is optional and when missing, its default value
424 is inferred according to the following rules:
426 - When :pkg-field:`cabal-version` is set to ``2.2`` or higher,
427 the default is ``Simple`` unless a :pkg-section:`custom-setup`
428 exists, in which case the inferred default is ``Custom``.
430 - For lower :pkg-field:`cabal-version` values, the default is
431 ``Custom`` unconditionally.
433 If the build type is anything other than ``Custom``, then the
434 ``Setup.hs`` file *must* be exactly the standardized content
435 discussed below. This is because in these cases, ``cabal`` will
436 ignore the ``Setup.hs`` file completely, whereas other methods of
437 package management, such as ``runhaskell Setup.hs [CMD]``, still
438 rely on the ``Setup.hs`` file.
440 For build type ``Simple``, the contents of ``Setup.hs`` must be:
442 .. code-block:: haskell
444 import Distribution.Simple
447 For build type ``Configure`` (see the section on `system-dependent
448 parameters`_ below), the contents of
449 ``Setup.hs`` must be:
451 .. code-block:: haskell
453 import Distribution.Simple
454 main = defaultMainWithHooks autoconfUserHooks
456 For build type ``Make`` (see the section on `more complex packages`_ below),
457 the contents of ``Setup.hs`` must be:
459 .. code-block:: haskell
461 import Distribution.Make
464 For build type ``Custom``, the file ``Setup.hs`` can be customized,
465 and will be used both by ``cabal`` and other tools.
467 For most packages, the build type ``Simple`` is sufficient.
469 .. pkg-field:: license: SPDX expression
473 The type of license under which this package is distributed.
475 Starting with ``cabal-version: 2.2`` the ``license`` field takes a
476 (case-sensitive) SPDX expression such as
478 .. code-block:: cabal
480 license: Apache-2.0 AND (MIT OR GPL-2.0-or-later)
482 See `SPDX IDs: How to use <https://spdx.org/ids-how>`__ for more
483 examples of SPDX expressions.
486 `list of SPDX license identifiers <https://spdx.org/licenses/>`__
487 is a function of the :pkg-field:`cabal-version` value as defined
488 in the following table:
490 +--------------------------+--------------------+
491 | Cabal specification | SPDX license list |
492 | version | version |
494 +==========================+====================+
495 | ``cabal-version: 2.2`` | ``3.0 2017-12-28`` |
496 +--------------------------+--------------------+
497 | ``cabal-version: 2.4`` | ``3.2 2018-07-10`` |
498 +--------------------------+--------------------+
500 **Pre-SPDX Legacy Identifiers**
502 The license identifier in the table below are defined for
503 ``cabal-version: 2.0`` and previous versions of the Cabal
506 +--------------------------+-----------------+
507 | :pkg-field:`license` | Note |
510 +==========================+=================+
514 +--------------------------+-----------------+
518 +--------------------------+-----------------+
519 | ``AGPL`` | since 1.18 |
521 +--------------------------+-----------------+
522 | ``BSD2`` | since 1.20 |
523 +--------------------------+-----------------+
525 +--------------------------+-----------------+
527 +--------------------------+-----------------+
528 | ``ISC`` | since 1.22 |
529 +--------------------------+-----------------+
530 | ``MPL-2.0`` | since 1.20 |
531 +--------------------------+-----------------+
534 +--------------------------+-----------------+
535 | ``PublicDomain`` | |
536 +--------------------------+-----------------+
537 | ``AllRightsReserved`` | |
538 +--------------------------+-----------------+
539 | ``OtherLicense`` | |
540 +--------------------------+-----------------+
543 .. pkg-field:: license-file: filename
545 See :pkg-field:`license-files`.
547 .. pkg-field:: license-files: filename list
550 The name of a file(s) containing the precise copyright license for
551 this package. The license file(s) will be installed with the
554 If you have multiple license files then use the :pkg-field:`license-files`
555 field instead of (or in addition to) the :pkg-field:`license-file` field.
557 .. pkg-field:: copyright: freeform
559 The content of a copyright notice, typically the name of the holder
560 of the copyright on the package and the year(s) from which copyright
561 is claimed. For example::
563 copyright: (c) 2006-2007 Joe Bloggs
565 .. pkg-field:: author: freeform
567 The original author of the package.
569 Remember that ``.cabal`` files are Unicode, using the UTF-8
572 .. pkg-field:: maintainer: address
574 The current maintainer or maintainers of the package. This is an
575 e-mail address to which users should send bug reports, feature
576 requests and patches.
578 .. pkg-field:: stability: freeform
580 The stability level of the package, e.g. ``alpha``,
581 ``experimental``, ``provisional``, ``stable``.
583 .. pkg-field:: homepage: URL
585 The package homepage.
587 .. pkg-field:: bug-reports: URL
589 The URL where users should direct bug reports. This would normally
592 - A ``mailto:`` URL, e.g. for a person or a mailing list.
594 - An ``http:`` (or ``https:``) URL for an online bug tracking
597 For example Cabal itself uses a web-based bug tracking system
601 bug-reports: https://github.com/haskell/cabal/issues
603 .. pkg-field:: package-url: URL
605 The location of a source bundle for the package. The distribution
606 should be a Cabal package.
608 .. pkg-field:: synopsis: freeform
610 A very short description of the package, for use in a table of
611 packages. This is your headline, so keep it short (one line) but as
612 informative as possible. Save space by not including the package
613 name or saying it's written in Haskell.
615 .. pkg-field:: description: freeform
617 Description of the package. This may be several paragraphs, and
618 should be aimed at a Haskell programmer who has never heard of your
621 For library packages, this field is used as prologue text by
622 :ref:`setup-haddock` and thus may contain the same markup as Haddock_
623 documentation comments.
625 .. pkg-field:: category: freeform
627 A classification category for future use by the package catalogue
628 Hackage_. These categories have not
629 yet been specified, but the upper levels of the module hierarchy
632 .. pkg-field:: tested-with: compiler list
634 A list of compilers and versions against which the package has been
635 tested (or at least built). The value of this field is not used by Cabal
636 and is rather intended as extra metadata for use by third party
637 tooling, such as e.g. CI tooling.
639 Here's a typical usage example:
643 tested-with: GHC == 9.0.1, GHC == 8.10.4, GHC == 8.8.4,
644 GHC == 8.6.5, GHC == 8.4.4, GHC == 8.2.2, GHC == 8.0.2,
645 GHC == 7.10.3, GHC == 7.8.4, GHC == 7.6.3, GHC == 7.4.2
647 The same can be spread over several lines, for instance:
651 tested-with: GHC == 9.0.1
663 The separating comma can also be dropped altogether:
680 However, this alternative might
681 `disappear <https://github.com/haskell/cabal/issues/4894#issuecomment-909008657>`__
684 Starting with :pkg-field:`cabal-version` 3.0,
685 there are further conveniences.
687 1. A preceding ``,`` is allowed, so a bullet-list style
688 is possible (recommended):
706 2. A concise set notation syntax is available:
710 tested-with: GHC == { 9.0.1, 8.10.4, 8.8.4, 8.6.5, 8.4.4, 8.2.2, 8.0.2, 7.10.3, 7.8.4, 7.6.3, 7.4.2 }
712 .. pkg-field:: data-files: filename list
714 A list of files to be installed for run-time use by the package.
715 This is useful for packages that use a large amount of static data,
716 such as tables of values or code templates. Cabal provides a way to
717 `find these files at run-time <#accessing-data-files-from-package-code>`_.
719 A limited form of ``*`` wildcards in file names, for example
720 ``data-files: images/*.png`` matches all the ``.png`` files in the
721 ``images`` directory. ``data-files: audio/**/*.mp3`` matches all
722 the ``.mp3`` files in the ``audio`` directory, including
725 The specific limitations of this wildcard syntax are
727 - ``*`` wildcards are only allowed in place of the file name, not
728 in the directory name or file extension. It must replace the
729 whole file name (e.g., ``*.html`` is allowed, but
730 ``chapter-*.html`` is not). If a wildcard is used, it must be
731 used with an extension, so ``data-files: data/*`` is not
734 - Prior to Cabal 2.4, when matching a wildcard plus extension, a
735 file's full extension must match exactly, so ``*.gz`` matches
736 ``foo.gz`` but not ``foo.tar.gz``. This restriction has been
737 lifted when ``cabal-version: 2.4`` or greater so that ``*.gz``
738 does match ``foo.tar.gz``
740 - ``*`` wildcards will not match if the file name is empty (e.g.,
741 ``*.html`` will not match ``foo/.html``).
743 - ``**`` wildcards can only appear as the final path component
744 before the file name (e.g., ``data/**/images/*.jpg`` is not
747 - Prior to Cabal 3.8, if a ``**`` wildcard is used, then
748 the file name must include a ``*`` wildcard (e.g.,
749 ``data/**/README.rst`` was not allowed). As of ``cabal-version:
750 3.8`` or greater, this restriction is lifted.
752 - A wildcard that does not match any files is an error.
754 The reason for providing only a very limited form of wildcard is to
755 concisely express the common case of a large number of related files
756 of the same file type without making it too easy to accidentally
757 include unwanted files.
759 On efficiency: if you use ``**`` patterns, the directory tree will
760 be walked starting with the parent directory of the ``**``. If
761 that's the root of the project, this might include ``.git/``,
762 ``dist-newstyle/``, or other large directories! To avoid this
763 behaviour, put the files that wildcards will match against in
766 ``**`` wildcards are available starting in Cabal 2.4
767 and `bug-free since Cabal 3.0 <https://github.com/haskell/cabal/issues/6125#issuecomment-1379878419>`_.
769 .. pkg-field:: data-dir: directory
771 The directory where Cabal looks for data files to install, relative
772 to the source directory. By default, Cabal will look in the source
775 .. pkg-field:: extra-source-files: filename list
777 A list of additional files to be included in source distributions built with :ref:`setup-sdist`.
778 As with :pkg-field:`data-files` it can use a limited form of ``*`` wildcards in file names.
779 Files listed here are tracked by ``cabal build``; changes in these files cause (partial) rebuilds.
781 .. pkg-field:: extra-doc-files: filename list
784 A list of additional files to be included in source distributions,
785 and also copied to the html directory when Haddock documentation is
786 generated. As with :pkg-field:`data-files` it can use a limited form of
787 ``*`` wildcards in file names.
789 .. pkg-field:: extra-tmp-files: filename list
791 A list of additional files or directories to be removed by
792 :ref:`setup-clean`. These would typically be additional files created by
793 additional hooks, such as the scheme described in the section on
794 `system-dependent parameters`_.
799 .. pkg-section:: library name
800 :synopsis: Library build information.
802 Build information for libraries.
804 Currently, there can only be one publicly exposed library in a
805 package, and its name is the same as package name set by global
806 :pkg-field:`name` field. In this case, the ``name`` argument to
807 the :pkg-section:`library` section must be omitted.
809 Starting with Cabal 2.0, private internal sub-library components
810 can be defined by setting the ``name`` field to a name
811 different from the current package's name; see section on
812 :ref:`Internal Libraries <sublibs>` for more information.
814 The library section should contain the following fields:
816 .. pkg-field:: exposed-modules: identifier list
818 :required: if this package contains a library
820 A list of modules added by this package.
822 .. pkg-field:: virtual-modules: identifier list
825 A list of virtual modules provided by this package. Virtual modules
826 are modules without a source file. See for example the ``GHC.Prim``
827 module from the ``ghc-prim`` package. Modules listed here will not be
828 built, but still end up in the list of ``exposed-modules`` in the
829 installed package info when the package is registered in the package
832 .. pkg-field:: exposed: boolean
836 Some Haskell compilers (notably GHC) support the notion of packages
837 being "exposed" or "hidden" which means the modules they provide can
838 be easily imported without always having to specify which package
839 they come from. However this only works effectively if the modules
840 provided by all exposed packages do not overlap (otherwise a module
841 import would be ambiguous).
843 Almost all new libraries use hierarchical module names that do not
844 clash, so it is very uncommon to have to use this field. However it
845 may be necessary to set ``exposed: False`` for some old libraries
846 that use a flat module namespace or where it is known that the
847 exposed modules would clash with other common modules.
849 .. pkg-field:: visibility: visibility specifiers
854 ``private`` for internal libraries. Cannot be set for main
855 (unnamed) library, which is always public.
857 Can be ``public`` or ``private``.
858 Makes it possible to have multiple public libraries in a single package.
859 If set to ``public``, depending on this library from another package is
860 allowed. If set to ``private``, depending on this library is allowed only
861 from the same package.
863 See section on :ref:`Internal Libraries <sublibs>` for examples and more
866 .. pkg-field:: reexported-modules: exportlist
869 Supported only in GHC 7.10 and later. A list of modules to
870 *reexport* from this package. The syntax of this field is
871 ``orig-pkg:Name as NewName`` to reexport module ``Name`` from
872 ``orig-pkg`` with the new name ``NewName``. We also support
873 abbreviated versions of the syntax: if you omit ``as NewName``,
874 we'll reexport without renaming; if you omit ``orig-pkg``, then we
875 will automatically figure out which package to reexport from, if
878 Reexported modules are useful for compatibility shims when a package
879 has been split into multiple packages, and they have the useful
880 property that if a package provides a module, and another package
881 reexports it under the same name, these are not considered a
882 conflict (as would be the case with a stub module.) They can also be
883 used to resolve name conflicts.
885 .. pkg-field:: signatures: signature list
888 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.
890 Module signatures are part of the Backpack_ extension to
891 the Haskell module system.
893 Packages that do not export any modules and only export required signatures
894 are called "signature-only packages", and their signatures are subjected to
896 <https://wiki.haskell.org/Module_signature#How_to_use_a_signature_package>`__.
900 The library section may also contain build information fields (see the
901 section on `build information`_).
905 **Internal Libraries**
907 Cabal 2.0 and later support "internal libraries", which are extra named
908 libraries (as opposed to the usual unnamed library section). For
909 example, suppose that your test suite needs access to some internal
910 modules in your library, which you do not otherwise want to export. You
911 could put these modules in an internal library, which the main library
912 and the test suite :pkg-field:`build-depends` upon. Then your Cabal file might
913 look something like this:
921 license-file: LICENSE
925 exposed-modules: Foo.Internal
926 -- NOTE: no explicit constraints on base needed
927 -- as they're inherited from the 'library' stanza
929 default-language: Haskell2010
932 exposed-modules: Foo.Public
933 build-depends: foo-internal, base >= 4.3 && < 5
934 default-language: Haskell2010
937 type: exitcode-stdio-1.0
939 -- NOTE: no constraints on 'foo-internal' as same-package
940 -- dependencies implicitly refer to the same package instance
941 build-depends: foo-internal, base
942 default-language: Haskell2010
944 Internal libraries are also useful for packages that define multiple
945 executables, but do not define a publicly accessible library. Internal
946 libraries are only visible internally in the package (so they can only
947 be added to the :pkg-field:`build-depends` of same-package libraries,
948 executables, test suites, etc.) Internal libraries locally shadow any
949 packages which have the same name; consequently, don't name an internal
950 library with the same name as an external dependency if you need to be
951 able to refer to the external dependency in a
952 :pkg-field:`build-depends` declaration.
954 Shadowing can be used to vendor an external dependency into a package
955 and thus emulate *private dependencies*. Below is an example based on
956 a real-world use case:
961 name: haddock-library
963 license: BSD-3-Clause
968 , bytestring ^>= 0.10.2.0
969 , containers ^>= 0.4.2.1 || ^>= 0.5.0.0
970 , transformers ^>= 0.5.0.0
975 build-depends: attoparsec
978 Documentation.Haddock
980 default-language: Haskell2010
985 , bytestring ^>= 0.10.2.0
986 , deepseq ^>= 1.4.0.0
988 hs-source-dirs: vendor/attoparsec-0.13.1.0
990 -- NB: haddock-library needs only small part of lib:attoparsec
991 -- internally, so we only bundle that subset here
993 Data.Attoparsec.ByteString
994 Data.Attoparsec.Combinator
997 Data.Attoparsec.Internal
999 ghc-options: -funbox-strict-fields -Wall -fwarn-tabs -O2
1001 default-language: Haskell2010
1004 For packages using ``cabal-version: 3.4`` or higher, the syntax to
1005 specify an internal library in a ``build-depends:`` section is
1006 ``package-name:internal-library-name``.
1008 **Multiple public libraries**
1010 Cabal 3.0 and later support exposing multiple libraries from a single package
1011 through the field :pkg-field:`library:visibility`.
1012 Having multiple public libraries is useful for separating the unit of
1013 distribution (package) from the unit of buildable code (library).
1014 For more information about the rationale and some examples, see
1015 `this blog post <https://fgaz.me/posts/2019-11-14-cabal-multiple-libraries/>`__.
1018 TODO inline the blog post
1021 Opening an interpreter session
1022 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1024 While developing a package, it is often useful to make its code
1025 available inside an interpreter session. This can be done with the
1028 .. code-block:: console
1032 The name comes from the acronym
1033 `REPL <http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop>`__,
1034 which stands for "read-eval-print-loop". By default ``cabal repl`` loads
1035 the first component in a package. If the package contains several named
1036 components, the name can be given as an argument to ``repl``. The name
1037 can be also optionally prefixed with the component's type for
1038 disambiguation purposes. Example:
1040 .. code-block:: console
1043 $ cabal repl exe:foo
1044 $ cabal repl test:bar
1045 $ cabal repl bench:baz
1047 Freezing dependency versions
1048 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1050 If a package is built in several different environments, such as a
1051 development environment, a staging environment and a production
1052 environment, it may be necessary or desirable to ensure that the same
1053 dependency versions are selected in each environment. This can be done
1054 with the ``freeze`` command:
1056 .. code-block:: console
1060 The command writes the selected version for all dependencies to the
1061 ``cabal.config`` file. All environments which share this file will use
1062 the dependency versions specified in it.
1064 Generating dependency version bounds
1065 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1067 Cabal also has the ability to suggest dependency version bounds that
1068 conform to the `Package Versioning Policy`_, which is
1069 a recommended versioning system for publicly released Cabal packages.
1070 This is done by running the ``gen-bounds`` command:
1072 .. code-block:: console
1076 For example, given the following dependencies without bounds specified in
1077 :pkg-field:`build-depends`:
1086 ``gen-bounds`` might suggest changing them to the following:
1091 base >= 4.15.0 && < 4.16,
1092 mtl >= 2.2.2 && < 2.3,
1093 transformers >= 0.5.6 && < 0.6,
1096 Listing outdated dependency version bounds
1097 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1099 Manually updating dependency version bounds in a ``.cabal`` file or a
1100 freeze file can be tedious, especially when there's a lot of
1101 dependencies. The ``cabal outdated`` command is designed to help with
1102 that. It will print a list of packages for which there is a new
1103 version on Hackage that is outside the version bound specified in the
1104 ``build-depends`` field. The ``outdated`` command can also be
1105 configured to act on the freeze file (both old- and v2-style) and
1106 ignore major (or all) version bumps on Hackage for a subset of
1111 .. code-block:: console
1115 Outdated dependencies:
1116 haskell-src-exts <1.17 (latest: 1.19.1)
1117 language-javascript <0.6 (latest: 0.6.0.9)
1118 unix ==2.7.2.0 (latest: 2.7.2.1)
1120 $ cabal outdated --simple-output
1125 $ cabal outdated --ignore=haskell-src-exts
1126 Outdated dependencies:
1127 language-javascript <0.6 (latest: 0.6.0.9)
1128 unix ==2.7.2.0 (latest: 2.7.2.1)
1130 $ cabal outdated --ignore=haskell-src-exts,language-javascript,unix
1131 All dependencies are up to date.
1133 $ cabal outdated --ignore=haskell-src-exts,language-javascript,unix -q
1137 $ cd /some/other/package
1138 $ cabal outdated --freeze-file
1139 Outdated dependencies:
1140 HTTP ==4000.3.3 (latest: 4000.3.4)
1141 HUnit ==1.3.1.1 (latest: 1.5.0.0)
1143 $ cabal outdated --freeze-file --ignore=HTTP --minor=HUnit
1144 Outdated dependencies:
1145 HUnit ==1.3.1.1 (latest: 1.3.1.2)
1147 See `the command documentation <cabal-commands.html#cabal-outdated>`__ for a
1148 list of available flags.
1153 .. pkg-section:: executable name
1154 :synopsis: Executable build info section.
1156 Executable sections (if present) describe executable programs contained
1157 in the package and must have an argument after the section label, which
1158 defines the name of the executable. This is a freeform argument but may
1161 The executable may be described using the following fields, as well as
1162 build information fields (see the section on `build information`_).
1164 .. pkg-field:: main-is: filename (required)
1166 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1167 module. Note that it is the ``.hs`` filename that must be listed,
1168 even if that file is generated using a preprocessor. The source file
1169 must be relative to one of the directories listed in
1170 :pkg-field:`hs-source-dirs`. Further, while the name of the file may
1171 vary, the module itself must be named ``Main``.
1173 Starting with ``cabal-version: 1.18`` this field supports
1174 specifying a C, C++, or objC source file as the main entry point.
1176 .. pkg-field:: scope: token
1179 Whether the executable is ``public`` (default) or ``private``, i.e. meant to
1180 be run by other programs rather than the user. Private executables are
1181 installed into `$libexecdir/$libexecsubdir`.
1186 You can have Cabal build and run your executables by using the ``run``
1189 .. code-block:: console
1191 $ cabal run EXECUTABLE [-- EXECUTABLE_FLAGS]
1193 This command will configure, build and run the executable
1194 ``EXECUTABLE``. The double dash separator is required to distinguish
1195 executable flags from ``run``'s own flags. If there is only one
1196 executable defined in the whole package, the executable's name can be
1197 omitted. See the output of ``cabal help run`` for a list of options you
1198 can pass to ``cabal run``.
1203 .. pkg-section:: test-suite name
1204 :synopsis: Test suite build information.
1206 Test suite sections (if present) describe package test suites and must
1207 have an argument after the section label, which defines the name of the
1208 test suite. This is a freeform argument, but may not contain spaces. It
1209 should be unique among the names of the package's other test suites, the
1210 package's executables, and the package itself. Using test suite sections
1211 requires at least Cabal version 1.9.2.
1213 The test suite may be described using the following fields, as well as
1214 build information fields (see the section on `build information`_).
1216 .. pkg-field:: type: interface (required until ``cabal-version`` 3.8)
1218 The interface type and version of the test suite. Cabal supports two
1219 test suite interfaces, called ``exitcode-stdio-1.0`` (default since ``cabal-version`` 3.8) and
1220 ``detailed-0.9``. Each of these types may require or disallow other
1221 fields as described below.
1223 Test suites using the ``exitcode-stdio-1.0`` (default since ``cabal-version`` 3.8) interface are executables
1224 that indicate test failure with a non-zero exit code when run; they may
1225 provide human-readable log information through the standard output and
1226 error channels. The ``exitcode-stdio-1.0`` type requires the ``main-is``
1229 .. pkg-field:: main-is: filename
1230 :synopsis: Module containing tests main function.
1232 :required: ``exitcode-stdio-1.0``
1233 :disallowed: ``detailed-0.9``
1235 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1236 module. Note that it is the ``.hs`` filename that must be listed,
1237 even if that file is generated using a preprocessor. The source file
1238 must be relative to one of the directories listed in
1239 :pkg-field:`hs-source-dirs`. This field is analogous to the ``main-is`` field
1240 of an executable section.
1242 Test suites using the ``detailed-0.9`` interface are modules exporting
1243 the symbol ``tests :: IO [Test]``. The ``Test`` type is exported by the
1244 module ``Distribution.TestSuite`` provided by Cabal. For more details,
1245 see the example below.
1247 The ``detailed-0.9`` interface allows Cabal and other test agents to
1248 inspect a test suite's results case by case, producing detailed human-
1249 and machine-readable log files. The ``detailed-0.9`` interface requires
1250 the :pkg-field:`test-module` field.
1252 .. pkg-field:: test-module: identifier
1254 :required: ``detailed-0.9``
1255 :disallowed: ``exitcode-stdio-1.0``
1257 The module exporting the ``tests`` symbol.
1259 .. pkg-field:: code-generators
1261 An optional list of preprocessors which can generate new modules
1262 for use in the test-suite.
1264 A list of executabes (possibly brought into scope by
1265 :pkg-field:`build-tool-depends`) that are run after all other
1266 preprocessors. These executables are invoked as so: ``exe-name
1267 TARGETDIR [SOURCEDIRS] -- [GHCOPTIONS]``. The arguments are, in order a target dir for
1268 output, a sequence of all source directories with source files of
1269 local lib components that the given test stanza dependens on, and
1270 following a double dash, all options cabal would pass to ghc for a
1271 build. They are expected to output a newline-seperated list of
1272 generated modules which have been written to the targetdir
1273 (excepting, if written, the main module). This can
1274 be used for driving doctests and other discover-style tests generated
1278 Example: Package using ``exitcode-stdio-1.0`` interface
1279 """""""""""""""""""""""""""""""""""""""""""""""""""""""
1281 The example package description and executable source file below
1282 demonstrate the use of the ``exitcode-stdio-1.0`` interface.
1284 .. code-block:: cabal
1290 License: BSD-3-Clause
1294 type: exitcode-stdio-1.0
1295 main-is: test-foo.hs
1296 build-depends: base >= 4 && < 5
1297 default-language: Haskell2010
1299 .. code-block:: haskell
1300 :caption: test-foo.hs
1304 import System.Exit (exitFailure)
1307 putStrLn "This test always fails!"
1310 Example: Package using ``detailed-0.9`` interface
1311 """""""""""""""""""""""""""""""""""""""""""""""""
1313 The example package description and test module source file below
1314 demonstrate the use of the ``detailed-0.9`` interface. The test module
1315 also develops a simple implementation of the interface set by
1316 ``Distribution.TestSuite``, but in actual usage the implementation would
1317 be provided by the library that provides the testing facility.
1319 .. code-block:: cabal
1325 License: BSD-3-Clause
1331 build-depends: base >= 4 && < 5, Cabal >= 1.9.2 && < 2
1332 default-language: Haskell2010
1335 .. code-block:: haskell
1338 module Bar ( tests ) where
1340 import Distribution.TestSuite
1343 tests = return [ Test succeeds, Test fails ]
1345 succeeds = TestInstance
1346 { run = return $ Finished Pass
1350 , setOption = \_ _ -> Right succeeds
1352 fails = TestInstance
1353 { run = return $ Finished $ Fail "Always fails!"
1357 , setOption = \_ _ -> Right fails
1363 You can have Cabal run your test suites using its built-in test runner:
1367 $ cabal configure --enable-tests
1371 See the output of ``cabal help test`` for a list of options you can pass
1377 .. pkg-section:: benchmark name
1379 :synopsis: Benchmark build information.
1381 Benchmark sections (if present) describe benchmarks contained in the
1382 package and must have an argument after the section label, which defines
1383 the name of the benchmark. This is a freeform argument, but may not
1384 contain spaces. It should be unique among the names of the package's
1385 other benchmarks, the package's test suites, the package's executables,
1386 and the package itself. Using benchmark sections requires at least Cabal
1389 The benchmark may be described using the following fields, as well as
1390 build information fields (see the section on `build information`_).
1392 .. pkg-field:: type: interface (required until ``cabal-version`` 3.8)
1394 The interface type and version of the benchmark. At the moment Cabal
1395 only support one benchmark interface, called ``exitcode-stdio-1.0``.
1397 Benchmarks using the ``exitcode-stdio-1.0`` (default since ``cabal-version`` 3.8) interface are executables
1398 that indicate failure to run the benchmark with a non-zero exit code
1399 when run; they may provide human-readable information through the
1400 standard output and error channels.
1402 .. pkg-field:: main-is: filename
1404 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1405 module. Note that it is the ``.hs`` filename that must be listed,
1406 even if that file is generated using a preprocessor. The source file
1407 must be relative to one of the directories listed in
1408 :pkg-field:`hs-source-dirs`. This field is analogous to the ``main-is``
1409 field of an executable section. Further, while the name of the file may
1410 vary, the module itself must be named ``Main``.
1413 """""""""""""""""""""""""""""""""""""""""""""""""""""""
1415 .. code-block:: cabal
1417 :name: foo-bench.cabal
1422 License: BSD-3-Clause
1426 type: exitcode-stdio-1.0
1427 main-is: bench-foo.hs
1428 build-depends: base >= 4 && < 5, time >= 1.1 && < 1.7
1429 default-language: Haskell2010
1431 .. code-block:: haskell
1432 :caption: bench-foo.hs
1434 {-# LANGUAGE BangPatterns #-}
1437 import Data.Time.Clock
1441 fib n = fib (n-1) + fib (n-2)
1444 start <- getCurrentTime
1446 end <- getCurrentTime
1447 putStrLn $ "fib 20 took " ++ show (diffUTCTime end start)
1452 You can have Cabal run your benchmark using its built-in benchmark
1457 $ cabal configure --enable-benchmarks
1461 See the output of ``cabal help bench`` for a list of options you can
1462 pass to ``cabal bench``.
1467 Foreign libraries are system libraries intended to be linked against
1468 programs written in C or other "foreign" languages. They
1469 come in two primary flavours: dynamic libraries (``.so`` files on Linux,
1470 ``.dylib`` files on OSX, ``.dll`` files on Windows, etc.) are linked against
1471 executables when the executable is run (or even lazily during
1472 execution), while static libraries (``.a`` files on Linux/OSX, ``.lib``
1473 files on Windows) get linked against the executable at compile time.
1475 Foreign libraries only work with GHC 7.8 and later.
1477 A typical stanza for a foreign library looks like
1481 foreign-library myforeignlib
1483 lib-version-info: 6:3:2
1487 mod-def-file: MyForeignLib.def
1489 other-modules: MyForeignLib.SomeModule
1490 MyForeignLib.SomeOtherModule
1491 build-depends: base >=4.7 && <4.9
1493 c-sources: csrc/MyForeignLibWrapper.c
1494 default-language: Haskell2010
1497 .. pkg-section:: foreign-library name
1499 :synopsis: Foreign library build information.
1501 Build information for `foreign libraries`_.
1503 .. pkg-field:: type: foreign library type
1505 Cabal recognizes ``native-static`` and ``native-shared`` here, although
1506 we currently only support building `native-shared` libraries.
1508 .. pkg-field:: options: foreign library option list
1510 Options for building the foreign library, typically specific to the
1511 specified type of foreign library. Currently we only support
1512 ``standalone`` here. A standalone dynamic library is one that does not
1513 have any dependencies on other (Haskell) shared libraries; without
1514 the ``standalone`` option the generated library would have dependencies
1515 on the Haskell runtime library (``libHSrts``), the base library
1516 (``libHSbase``), etc. Currently, ``standalone`` *must* be used on Windows
1517 and *must not* be used on any other platform.
1519 .. pkg-field:: mod-def-file: filename
1521 This option can only be used when creating dynamic Windows libraries
1522 (that is, when using ``native-shared`` and the ``os`` is ``Windows``). If
1523 used, it must be a path to a *module definition file*. The details of
1524 module definition files are beyond the scope of this document; see the
1525 `GHC <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/win32-dlls.html>`_
1526 manual for some details and some further pointers.
1528 .. pkg-field:: lib-version-info: current:revision:age
1530 This field is currently only used on Linux.
1532 This field specifies a Libtool-style version-info field that sets
1533 an appropriate ABI version for the foreign library. Note that the
1534 three numbers specified in this field do not directly specify the
1535 actual ABI version: ``6:3:2`` results in library version ``4.2.3``.
1537 With this field set, the SONAME of the library is set, and symlinks
1540 How you should bump this field on an ABI change depends on the
1541 breakage you introduce:
1543 - Programs using the previous version may use the new version as
1544 drop-in replacement, and programs using the new version can also
1545 work with the previous one. In other words, no recompiling nor
1546 relinking is needed. In this case, bump ``revision`` only, don't
1547 touch current nor age.
1548 - Programs using the previous version may use the new version as
1549 drop-in replacement, but programs using the new version may use
1550 APIs not present in the previous one. In other words, a program
1551 linking against the new version may fail with "unresolved
1552 symbols" if linking against the old version at runtime: set
1553 revision to 0, bump current and age.
1554 - Programs may need to be changed, recompiled, and relinked in
1555 order to use the new version. Bump current, set revision and age
1558 Also refer to the Libtool documentation on the version-info field.
1560 .. pkg-field:: lib-version-linux: version
1562 This field is only used on Linux.
1564 Specifies the library ABI version directly for foreign libraries
1565 built on Linux: so specifying ``4.2.3`` causes a library
1566 ``libfoo.so.4.2.3`` to be built with SONAME ``libfoo.so.4``, and
1567 appropriate symlinks ``libfoo.so.4`` and ``libfoo.so`` to be
1570 Note that typically foreign libraries should export a way to initialize
1571 and shutdown the Haskell runtime. In the example above, this is done by
1572 the ``csrc/MyForeignLibWrapper.c`` file, which might look something like
1579 HsBool myForeignLibInit(void){
1581 char *argv[] = { "+RTS", "-A32m", NULL };
1582 char **pargv = argv;
1584 // Initialize Haskell runtime
1585 hs_init(&argc, &pargv);
1587 // do any other initialization here and
1588 // return false if there was a problem
1589 return HS_BOOL_TRUE;
1592 void myForeignLibExit(void){
1596 With modern ghc regular libraries are installed in directories that contain
1597 package keys. This isn't usually a problem because the package gets registered
1598 in ghc's package DB and so we can figure out what the location of the library
1599 is. Foreign libraries however don't get registered, which means that we'd have
1600 to have a way of finding out where a platform library got installed (other than by
1601 searching the ``lib/`` directory). Instead, we install foreign libraries in
1606 .. pkg-section:: None
1608 The following fields may be optionally present in a library, executable,
1609 test suite or benchmark section, and give information for the building
1610 of the corresponding library or executable. See also the sections on
1611 `system-dependent parameters`_ and `configurations`_ for a way to supply
1612 system-dependent values for these fields.
1614 .. pkg-field:: build-depends: library list
1616 Declares the *library* dependencies required to build the current
1617 package component; see :pkg-field:`build-tool-depends` for
1618 declaring build-time *tool* dependencies. External library
1619 dependencies should be annotated with a version constraint.
1623 External libraries are identified by the package's name they're
1624 provided by, optionally followed by a colon and the library name
1625 (available from ``cabal-version: 3.0``).
1626 If the library name is absent, the main (unnamed) library will be used.
1627 To refer to the main (unnamed) library explicitly, use the name of the
1628 package (``foo:foo``).
1629 Multiple libraries from the same package can be specified with the shorthand
1630 syntax ``pkg:{lib1,lib2}```.
1632 See section on :ref:`Internal Libraries <sublibs>` for examples and more
1635 **Version Constraints**
1637 Version constraints use the operators ``==, >=, >, <, <=`` and a
1638 version number. Multiple constraints can be combined using ``&&`` or
1639 ``||``. If no version constraint is specified, any version is
1640 assumed to be acceptable. For example:
1647 foo >= 1.2.3 && < 1.3,
1650 Dependencies like ``foo >= 1.2.3 && < 1.3`` turn out to be very
1651 common because it is recommended practice for package versions to
1652 correspond to API versions (see PVP_).
1654 Since Cabal 1.6, there is a special wildcard syntax to help with
1659 build-depends: foo ==1.2.*
1661 It is only syntactic sugar. It is exactly equivalent to
1662 ``foo >= 1.2 && < 1.3``.
1666 A potential pitfall of the wildcard syntax is that the
1667 constraint ``nats == 1.0.*`` doesn't match the release
1668 ``nats-1`` because the version ``1`` is lexicographically less
1669 than ``1.0``. This is not an issue with the caret-operator
1670 ``^>=`` described below.
1672 Starting with Cabal 2.0, there's a new version operator to express
1673 PVP_-style major upper bounds conveniently, and is inspired by similar
1674 syntactic sugar found in other language ecosystems where it's often
1675 called the "Caret" operator:
1683 This allows to assert the positive knowledge that this package is
1684 *known* to be semantically compatible with the releases
1685 ``foo-1.2.3.4`` and ``bar-1`` respectively. The information
1686 encoded via such ``^>=``-assertions is used by the cabal solver to
1687 infer version constraints describing semantically compatible
1688 version ranges according to the PVP_ contract (see below).
1690 Another way to say this is that ``foo < 1.3`` expresses *negative*
1691 information, i.e. "``foo-1.3`` or ``foo-1.4.2`` will *not* be
1692 compatible"; whereas ``foo ^>= 1.2.3.4`` asserts the *positive*
1693 information that "``foo-1.2.3.4`` is *known* to be compatible" and (in
1694 the absence of additional information) according to the PVP_
1695 contract we can (positively) infer right away that all versions
1696 satisfying ``foo >= 1.2.3.4 && < 1.3`` will be compatible as well.
1700 More generally, the PVP_ contract implies that we can safely
1701 relax the lower bound to ``>= 1.2``, because if we know that
1702 ``foo-1.2.3.4`` is semantically compatible, then so is
1703 ``foo-1.2`` (if it typechecks). But we'd need to perform
1704 additional static analysis (i.e. perform typechecking) in order
1705 to know if our package in the role of an API consumer will
1706 successfully typecheck against the dependency ``foo-1.2``. But
1707 since we cannot do this analysis during constraint solving and
1708 to keep things simple, we pragmatically use ``foo >= 1.2.3.4``
1709 as the initially inferred approximation for the lower bound
1710 resulting from the assertion ``foo ^>= 1.2.3.4``. If further
1711 evidence becomes available that e.g. ``foo-1.2`` typechecks,
1712 one can simply revise the dependency specification to include
1713 the assertion ``foo ^>= 1.2``.
1715 The subtle but important difference in signaling allows tooling to
1716 treat explicitly expressed ``<``-style constraints and inferred
1717 (``^>=``-style) upper bounds differently. For instance,
1718 :cfg-field:`allow-newer`'s ``^``-modifier allows to relax only
1719 ``^>=``-style bounds while leaving explicitly stated
1720 ``<``-constraints unaffected.
1722 Ignoring the signaling intent, the default syntactic desugaring rules are
1724 - ``^>= x`` == ``>= x && < x.1``
1725 - ``^>= x.y`` == ``>= x.y && < x.(y+1)``
1726 - ``^>= x.y.z`` == ``>= x.y.z && < x.(y+1)``
1727 - ``^>= x.y.z.u`` == ``>= x.y.z.u && < x.(y+1)``
1732 One might expect the desugaring to truncate all version
1733 components below (and including) the patch-level, i.e.
1734 ``^>= x.y.z.u`` == ``>= x.y.z && < x.(y+1)``,
1735 as the major and minor version components alone are supposed to
1736 uniquely identify the API according to the PVP_. However, by
1737 designing ``^>=`` to be closer to the ``>=`` operator, we avoid
1738 the potentially confusing effect of ``^>=`` being more liberal
1739 than ``>=`` in the presence of patch-level versions.
1741 Consequently, the example declaration above is equivalent to
1746 foo >= 1.2.3.4 && < 1.3,
1751 Prior to Cabal 1.8, ``build-depends`` specified in each
1752 section were global to all sections. This was unintentional, but
1753 some packages were written to depend on it, so if you need your
1754 :pkg-field:`build-depends` to be local to each section, you must specify
1755 at least ``Cabal-Version: >= 1.8`` in your ``.cabal`` file.
1759 Cabal 1.20 experimentally supported module thinning and
1760 renaming in ``build-depends``; however, this support has since been
1761 removed and should not be used.
1763 Starting with Cabal 3.0, a set notation for the ``==`` and ``^>=`` operator
1764 is available. For instance,
1768 tested-with: GHC == 8.6.3, GHC == 8.4.4, GHC == 8.2.2, GHC == 8.0.2,
1769 GHC == 7.10.3, GHC == 7.8.4, GHC == 7.6.3, GHC == 7.4.2
1771 build-depends: network ^>= 2.6.3.6 || ^>= 2.7.0.2 || ^>= 2.8.0.0 || ^>= 3.0.1.0
1773 can be then written in a more convenient and concise form
1777 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 }
1779 build-depends: network ^>= { 2.6.3.6, 2.7.0.2, 2.8.0.0, 3.0.1.0 }
1782 .. pkg-field:: other-modules: identifier list
1784 A list of modules used by the component but not exposed to users.
1785 For a library component, these would be hidden modules of the
1786 library. For an executable, these would be auxiliary modules to be
1787 linked with the file named in the ``main-is`` field.
1791 Every module in the package *must* be listed in one of
1792 :pkg-field:`other-modules`, :pkg-field:`library:exposed-modules` or
1793 :pkg-field:`executable:main-is` fields.
1795 .. pkg-field:: hs-source-dir: directory list
1801 Root directories for the module hierarchy.
1803 Deprecated in favor of :pkg-field:`hs-source-dirs`.
1805 .. pkg-field:: hs-source-dirs: directory list
1809 Root directories for the module hierarchy.
1813 Components can share source directories but modules found there will be
1814 recompiled even if other components already built them, i.e., if a
1815 library and an executable share a source directory and the executable
1816 depends on the library and imports its ``Foo`` module, ``Foo`` will be
1817 compiled twice, once as part of the library and again for the executable.
1819 .. pkg-field:: default-extensions: identifier list
1822 A list of Haskell extensions used by every module. These determine
1823 corresponding compiler options enabled for all files. Extension
1824 names are the constructors of the
1825 `Extension <https://hackage.haskell.org/package/Cabal/docs/Language-Haskell-Extension.html#t:Extension>`__
1826 type. For example, ``CPP`` specifies that Haskell source files are
1827 to be preprocessed with a C preprocessor.
1829 .. pkg-field:: other-extensions: identifier list
1832 A list of Haskell extensions used by some (but not necessarily all)
1833 modules. From GHC version 6.6 onward, these may be specified by
1834 placing a ``LANGUAGE`` pragma in the source files affected e.g.
1836 .. code-block:: haskell
1838 {-# LANGUAGE CPP, MultiParamTypeClasses #-}
1840 In Cabal-1.24 the dependency solver will use this and
1841 :pkg-field:`default-extensions` information. Cabal prior to 1.24 will abort
1842 compilation if the current compiler doesn't provide the extensions.
1844 If you use some extensions conditionally, using CPP or conditional
1845 module lists, it is good to replicate the condition in
1846 :pkg-field:`other-extensions` declarations:
1850 other-extensions: CPP
1852 other-extensions: PolyKinds
1854 You could also omit the conditionally used extensions, as they are
1855 for information only, but it is recommended to replicate them in
1856 :pkg-field:`other-extensions` declarations.
1858 .. pkg-field:: default-language: identifier
1863 .. pkg-field:: other-languages: identifier
1868 .. pkg-field:: extensions: identifier list
1872 Deprecated in favor of :pkg-field:`default-extensions`.
1874 .. pkg-field:: build-tool-depends: package:executable list
1877 A list of Haskell executables needed to build this component. Executables are provided
1878 during the whole duration of the component, so this field can be used for executables
1879 needed during :pkg-section:`test-suite` as well.
1881 Each is specified by the package containing the executable and the name of the
1882 executable itself, separated by a colon, and optionally followed by a version bound.
1884 All executables defined in the given Cabal file are termed as *internal* dependencies
1885 as opposed to the rest which are *external* dependencies.
1887 Each of the two is handled differently:
1889 1. External dependencies can (and should) contain a version bound like conventional
1890 :pkg-field:`build-depends` dependencies.
1891 2. Internal dependencies should not contain a version bound, as they will be always
1892 resolved within the same configuration of the package in the build plan.
1893 Specifically, version bounds that include the package's version will be warned for
1894 being extraneous, and version bounds that exclude the package's version will raise
1895 an error for being impossible to follow.
1897 For example (1) using a test-suite to make sure README.md Haskell snippets are tested using
1898 `markdown-unlit <http://hackage.haskell.org/package/markdown-unlit>`__:
1902 build-tool-depends: markdown-unlit:markdown-unlit >= 0.5.0 && < 0.6
1904 For example (2) using a test-suite to test executable behaviour in the same package:
1908 build-tool-depends: mypackage:executable
1910 Cabal tries to make sure that all specified programs are atomically built and prepended
1911 on the ``PATH`` shell variable before building the component in question, but can only do
1912 so for Nix-style builds. Specifically:
1914 a) For Nix-style local builds, both internal and external dependencies.
1915 b) For old-style builds, only for internal dependencies [#old-style-build-tool-depends]_.
1916 It's up to the user to provide needed executables in this case under ``PATH``.
1921 :pkg-field:`build-tool-depends` was added in Cabal 2.0, and it will
1922 be ignored (with a warning) with old versions of Cabal. See
1923 :pkg-field:`build-tools` for more information about backwards
1926 .. pkg-field:: build-tools: program list
1930 Deprecated in favor of :pkg-field:`build-tool-depends`, but :ref:`see below for backwards compatibility information <buildtoolsbc>`.
1932 A list of Haskell programs needed to build this component.
1933 Each may be followed by an optional version bound.
1934 Confusingly, each program in the list either refer to one of three things:
1936 1. Another executables in the same package (supported since Cabal 1.12)
1938 2. Tool name contained in Cabal's :ref:`hard-coded set of common tools <buildtoolsmap>`
1940 3. A pre-built executable that should already be on the ``PATH``
1941 (supported since Cabal 2.0)
1943 These cases are listed in order of priority:
1944 an executable in the package will override any of the hard-coded packages with the same name,
1945 and a hard-coded package will override any executable on the ``PATH``.
1947 In the first two cases, the list entry is desugared into a :pkg-field:`build-tool-depends` entry.
1948 In the first case, the entry is desugared into a :pkg-field:`build-tool-depends` entry by prefixing with ``$pkg:``.
1949 In the second case, it is desugared by looking up the package and executable name in a hard-coded table.
1950 In either case, the optional version bound is passed through unchanged.
1951 Refer to the documentation for :pkg-field:`build-tool-depends` to understand the desugared field's meaning, along with restrictions on version bounds.
1955 **Backward Compatibility**
1957 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.
1958 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.
1959 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``.
1963 **Set of Known Tool Names**
1965 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::
1967 build-tools: alex >= 3.2.1 && < 3.3, happy >= 1.19.5 && < 1.20
1969 is simply desugared into the equivalent specification::
1971 build-tool-depends: alex:alex >= 3.2.1 && < 3.3, happy:happy >= 1.19.5 && < 1.20
1973 +--------------------------+-----------------------------------+-----------------+
1974 | :pkg-field:`build-tools` | desugared | Note |
1975 | identifier | :pkg-field:`build-tool-depends` | |
1977 +==========================+===================================+=================+
1978 | ``alex`` | ``alex:alex`` | |
1979 +--------------------------+-----------------------------------+-----------------+
1980 | ``c2hs`` | ``c2hs:c2hs`` | |
1981 +--------------------------+-----------------------------------+-----------------+
1982 | ``cpphs`` | ``cpphs:cpphs`` | |
1983 +--------------------------+-----------------------------------+-----------------+
1984 | ``greencard`` | ``greencard:greencard`` | |
1985 +--------------------------+-----------------------------------+-----------------+
1986 | ``haddock`` | ``haddock:haddock`` | |
1987 +--------------------------+-----------------------------------+-----------------+
1988 | ``happy`` | ``happy:happy`` | |
1989 +--------------------------+-----------------------------------+-----------------+
1990 | ``hsc2hs`` | ``hsc2hs:hsc2hs`` | |
1991 +--------------------------+-----------------------------------+-----------------+
1992 | ``hscolour`` | ``hscolour:hscolour`` | |
1993 +--------------------------+-----------------------------------+-----------------+
1994 | ``hspec-discover`` | ``hspec-discover:hspec-discover`` | since Cabal 2.0 |
1995 +--------------------------+-----------------------------------+-----------------+
1997 This built-in set can be programmatically extended via ``Custom`` setup scripts; this, however, is of limited use since the Cabal solver cannot access information injected by ``Custom`` setup scripts.
1999 .. pkg-field:: buildable: boolean
2003 Is the component buildable? Like some of the other fields below,
2004 this field is more useful with the slightly more elaborate form of
2005 the simple build infrastructure described in the section on
2006 `system-dependent parameters`_.
2008 .. pkg-field:: ghc-options: token list
2010 Additional options for GHC. You can often achieve the same effect
2011 using the :pkg-field:`default-extensions` field, which is preferred.
2013 Options required only by one module may be specified by placing an
2014 ``OPTIONS_GHC`` pragma in the source file affected.
2016 As with many other fields, whitespace can be escaped by using
2017 Haskell string syntax. Example:
2018 ``ghc-options: -Wcompat "-with-rtsopts=-T -I1" -Wall``.
2020 .. pkg-field:: ghc-prof-options: token list
2022 Additional options for GHC when the package is built with profiling
2025 Note that as of Cabal-1.24, the default profiling detail level
2026 defaults to ``exported-functions`` for libraries and
2027 ``toplevel-functions`` for executables. For GHC these correspond to
2028 the flags ``-fprof-auto-exported`` and ``-fprof-auto-top``. Prior to
2029 Cabal-1.24 the level defaulted to ``none``. These levels can be
2030 adjusted by the person building the package with the
2031 ``--profiling-detail`` and ``--library-profiling-detail`` flags.
2033 It is typically better for the person building the package to pick
2034 the profiling detail level rather than for the package author. So
2035 unless you have special needs it is probably better not to specify
2036 any of the GHC ``-fprof-auto*`` flags here. However if you wish to
2037 override the profiling detail level, you can do so using the
2038 :pkg-field:`ghc-prof-options` field: use ``-fno-prof-auto`` or one of the
2039 other ``-fprof-auto*`` flags.
2041 .. pkg-field:: ghc-shared-options: token list
2043 Additional options for GHC when the package is built as shared
2044 library. The options specified via this field are combined with the
2045 ones specified via :pkg-field:`ghc-options`, and are passed to GHC during
2046 both the compile and link phases.
2048 .. pkg-field:: ghcjs-options: token list
2050 Like :pkg-field:`ghc-options` but applies to GHCJS
2052 .. pkg-field:: ghcjs-prof-options: token list
2054 Like :pkg-field:`ghc-prof-options` but applies to GHCJS
2056 .. pkg-field:: ghcjs-shared-options: token list
2058 Like :pkg-field:`ghc-shared-options` but applies to GHCJS
2060 .. pkg-field:: includes: filename list
2062 A list of header files to be included in any compilations via C.
2063 This field applies to both header files that are already installed
2064 on the system and to those coming with the package to be installed.
2065 The former files should be found in absolute paths, while the latter
2066 files should be found in paths relative to the top of the source
2067 tree or relative to one of the directories listed in
2068 :pkg-field:`include-dirs`.
2070 These files typically contain function prototypes for foreign
2071 imports used by the package. This is in contrast to
2072 :pkg-field:`install-includes`, which lists header files that are intended
2073 to be exposed to other packages that transitively depend on this
2076 .. pkg-field:: install-includes: filename list
2078 A list of header files from this package to be installed into
2079 ``$libdir/includes`` when the package is installed. Files listed in
2080 :pkg-field:`install-includes` should be found in relative to the top of the
2081 source tree or relative to one of the directories listed in
2082 :pkg-field:`include-dirs`.
2084 :pkg-field:`install-includes` is typically used to name header files that
2085 contain prototypes for foreign imports used in Haskell code in this
2086 package, for which the C implementations are also provided with the
2087 package. For example, here is a ``.cabal`` file for a hypothetical
2088 ``bindings-clib`` package that bundles the C source code for ``clib``::
2092 install-includes: clib.h
2094 Now any package that depends (directly or transitively) on the
2095 ``bindings-clib`` library can use ``clib.h``.
2097 Note that in order for files listed in :pkg-field:`install-includes` to be
2098 usable when compiling the package itself, they need to be listed in
2099 the :pkg-field:`includes` field as well.
2101 .. pkg-field:: include-dirs: directory list
2103 A list of directories to search for header files, when preprocessing
2104 with ``c2hs``, ``hsc2hs``, ``cpphs`` or the C preprocessor, and also
2105 when compiling via C. Directories can be absolute paths (e.g., for
2106 system directories) or paths that are relative to the top of the
2107 source tree. Cabal looks in these directories when attempting to
2108 locate files listed in :pkg-field:`includes` and
2109 :pkg-field:`install-includes`.
2111 .. pkg-field:: c-sources: filename list
2113 A list of C source files to be compiled and linked with the Haskell
2116 .. pkg-field:: cxx-sources: filename list
2119 A list of C++ source files to be compiled and linked with the Haskell
2120 files. Useful for segregating C and C++ sources when supplying different
2121 command-line arguments to the compiler via the :pkg-field:`cc-options`
2122 and the :pkg-field:`cxx-options` fields. The files listed in the
2123 :pkg-field:`cxx-sources` can reference files listed in the
2124 :pkg-field:`c-sources` field and vice-versa. The object files will be linked
2127 .. pkg-field:: asm-sources: filename list
2130 A list of assembly source files to be compiled and linked with the
2133 .. pkg-field:: cmm-sources: filename list
2136 A list of C-- source files to be compiled and linked with the Haskell
2139 .. pkg-field:: js-sources: filename list
2141 A list of JavaScript source files to be linked with the Haskell
2142 files (only for JavaScript targets).
2144 .. pkg-field:: extra-libraries: token list
2146 A list of extra libraries to link with (when not linking fully static
2149 .. pkg-field:: extra-libraries-static: token list
2151 A list of extra libraries to link with (when linking fully static
2154 .. pkg-field:: extra-ghci-libraries: token list
2156 A list of extra libraries to be used instead of 'extra-libraries'
2157 when the package is loaded with GHCi.
2159 .. pkg-field:: extra-bundled-libraries: token list
2162 A list of libraries that are supposed to be copied from the build
2163 directory alongside the produced Haskell libraries. Note that you
2164 are under the obligation to produce those libraries in the build
2165 directory (e.g. via a custom setup). Libraries listed here will
2166 be included when ``copy``-ing packages and be listed in the
2167 ``hs-libraries`` of the package configuration in the package database.
2168 Library names must either be prefixed with "HS" or "C" and corresponding
2169 library file names must match:
2171 - Libraries with name "HS<library-name>":
2172 - `libHS<library-name>.a`
2173 - `libHS<library-name>-ghc<ghc-flavour><ghc-version>.<dyn-library-extension>*`
2174 - Libraries with name "C<library-name>":
2175 - `libC<library-name>.a`
2176 - `lib<library-name>.<dyn-library-extension>*`
2178 .. pkg-field:: extra-lib-dirs: directory list
2180 A list of directories to search for libraries (when not linking fully static
2183 .. pkg-field:: extra-lib-dirs-static: directory list
2185 A list of directories to search for libraries (when linking fully static
2188 .. pkg-field:: extra-library-flavours: notsure
2192 .. pkg-field:: extra-dynamic-library-flavours: notsure
2196 .. pkg-field:: cc-options: token list
2198 Command-line arguments to be passed to the C compiler. Since the
2199 arguments are compiler-dependent, this field is more useful with the
2200 setup described in the section on `system-dependent parameters`_.
2202 .. pkg-field:: cpp-options: token list
2204 Command-line arguments for pre-processing Haskell code. Applies to
2205 Haskell source and other pre-processed Haskell source like .hsc
2206 .chs. Does not apply to C code, that's what cc-options is for.
2208 .. pkg-field:: cxx-options: token list
2211 Command-line arguments to be passed to the compiler when compiling
2212 C++ code. The C++ sources to which these command-line arguments
2213 should be applied can be specified with the :pkg-field:`cxx-sources`
2214 field. Command-line options for C and C++ can be passed separately to
2215 the compiler when compiling both C and C++ sources by segregating the C
2216 and C++ sources with the :pkg-field:`c-sources` and
2217 :pkg-field:`cxx-sources` fields respectively, and providing different
2218 command-line arguments with the :pkg-field:`cc-options` and the
2219 :pkg-field:`cxx-options` fields.
2221 .. pkg-field:: cmm-options: token list
2224 Command-line arguments to be passed to the compiler when compiling
2225 C-- code. See also :pkg-field:`cmm-sources`.
2227 .. pkg-field:: asm-options: token list
2230 Command-line arguments to be passed to the assembler when compiling
2231 assembler code. See also :pkg-field:`asm-sources`.
2233 .. pkg-field:: ld-options: token list
2235 Command-line arguments to be passed to the linker. Since the
2236 arguments are compiler-dependent, this field is more useful with the
2237 setup described in the section on `system-dependent parameters`_.
2239 .. pkg-field:: hsc2hs-options: token list
2242 Command-line arguments to be passed to ``hsc2hs``.
2244 .. pkg-field:: pkgconfig-depends: package list
2247 `pkg-config <http://www.freedesktop.org/wiki/Software/pkg-config/>`__
2248 packages, needed to build this package. They can be annotated with
2249 versions, e.g. ``gtk+-2.0 >= 2.10, cairo >= 1.0``. If no version
2250 constraint is specified, any version is assumed to be acceptable.
2251 Cabal uses ``pkg-config`` to find if the packages are available on
2252 the system and to find the extra compilation and linker options
2253 needed to use the packages.
2255 If you need to bind to a C library that supports ``pkg-config`` then
2256 it is much preferable to use this field rather than hard code options
2257 into the other fields. ``pkg-config --list-all`` will show you all
2258 supported libraries. Depending on your system you may need to adjust
2259 ``PKG_CONFIG_PATH``.
2261 .. pkg-field:: frameworks: token list
2263 On Darwin/MacOS X, a list of frameworks to link to. See Apple's
2264 developer documentation for more details on frameworks. This entry
2265 is ignored on all other platforms.
2267 .. pkg-field:: extra-framework-dirs: directory list
2270 On Darwin/MacOS X, a list of directories to search for frameworks.
2271 This entry is ignored on all other platforms.
2273 .. pkg-field:: mixins: mixin list
2276 Supported only in GHC 8.2 and later. A list of packages mentioned in the
2277 :pkg-field:`build-depends` field, each optionally accompanied by a list of
2278 module and module signature renamings. A valid mixin obeys the
2283 Mixin ::= PackageName IncludeRenaming
2284 IncludeRenaming ::= ModuleRenaming { "requires" ModuleRenaming }
2287 | "(" Renaming "," ... "," Renaming ")"
2288 | "hiding" "(" ModuleName "," ... "," ModuleName ")"
2291 | ModuleName "as" ModuleName
2293 The simplest mixin syntax is simply the name of a package mentioned in the
2294 :pkg-field:`build-depends` field. For example:
2304 But this doesn't have any effect. More interesting is to use the mixin
2305 entry to rename one or more modules from the package, like this:
2311 foo (Foo.Bar as AnotherFoo.Bar, Foo.Baz as AnotherFoo.Baz)
2313 Note that renaming a module like this will hide all the modules
2314 that are not explicitly named.
2316 Modules can also be hidden:
2322 foo hiding (Foo.Bar)
2324 Hiding modules exposes everything that is not explicitly hidden.
2328 Cabal files with :pkg-field:`cabal-version` < 3.0 suffer from an
2329 infelicity in how the entries of :pkg-field:`mixins` are parsed: an
2330 entry will fail to parse if the provided renaming clause has whitespace
2331 after the opening parenthesis.
2333 See issues :issue:`5150`, :issue:`4864`, and :issue:`5293`.
2335 There can be multiple mixin entries for a given package, in effect creating
2336 multiple copies of the dependency:
2342 foo (Foo.Bar as AnotherFoo.Bar, Foo.Baz as AnotherFoo.Baz),
2343 foo (Foo.Bar as YetAnotherFoo.Bar)
2345 The ``requires`` clause is used to rename the module signatures required by
2352 foo (Foo.Bar as AnotherFoo.Bar) requires (Foo.SomeSig as AnotherFoo.SomeSig)
2354 Signature-only packages don't have any modules, so only the signatures can
2355 be renamed, with the following syntax:
2361 sigonly requires (SigOnly.SomeSig as AnotherSigOnly.SomeSig)
2363 See the :pkg-field:`library:signatures` field for more details.
2365 Mixin packages are part of the Backpack_ extension to the
2366 Haskell module system.
2368 The matching of the module signatures required by a
2369 :pkg-field:`build-depends` dependency with the implementation modules
2370 present in another dependency is triggered by a coincidence of names. When
2371 the names of the signature and of the implementation are already the same,
2372 the matching is automatic. But when the names don't coincide, or we want to
2373 instantiate a signature in two different ways, adding mixin entries that
2374 perform renamings becomes necessary.
2378 Backpack_ has the limitation that implementation modules that instantiate
2379 signatures required by a :pkg-field:`build-depends` dependency can't
2380 reside in the same component that has the dependency. They must reside
2381 in a different package dependency, or at least in a separate internal
2387 Library and executable sections may include conditional blocks, which
2388 test for various system parameters and configuration flags. The flags
2389 mechanism is rather generic, but most of the time a flag represents
2390 certain feature, that can be switched on or off by the package user.
2391 Here is an example package description file using configurations:
2393 Example: A package containing a library and executable programs
2394 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2401 License: BSD-3-Clause
2403 Synopsis: Test package to test configurations
2408 Description: Enable debug support
2413 Description: Include API for web frontend.
2418 description: Whether to build against @directory >= 1.2@
2419 -- This is an automatic flag which the solver will
2420 -- assign automatically while searching for a solution
2423 Build-Depends: base >= 4.2 && < 4.9
2424 Exposed-Modules: Testing.Test1
2425 Default-Extensions: CPP
2426 Default-Language: Haskell2010
2430 CPP-Options: -DDEBUG
2432 CC-Options: "-DDEBUG"
2434 CC-Options: "-DNDEBUG"
2436 if flag(WebFrontend)
2437 Build-Depends: cgi >= 0.42 && < 0.44
2438 Other-Modules: Testing.WebStuff
2439 CPP-Options: -DWEBFRONTEND
2441 if flag(NewDirectory)
2442 build-depends: directory >= 1.2 && < 1.4
2443 Build-Depends: time >= 1.0 && < 1.9
2445 build-depends: directory == 1.1.*
2446 Build-Depends: old-time >= 1.0 && < 1.2
2450 Other-Modules: Testing.Test1
2451 Build-Depends: base >= 4.2 && < 4.9
2452 Default-Language: Haskell2010
2455 CC-Options: "-DDEBUG"
2456 CPP-Options: -DDEBUG
2461 Flags, conditionals, library and executable sections use layout to
2462 indicate structure. This is very similar to the Haskell layout rule.
2463 Entries in a section have to all be indented to the same level which
2464 must be more than the section header. Tabs are not allowed to be used
2467 As an alternative to using layout you can also use explicit braces
2468 ``{}``. In this case the indentation of entries in a section does not
2469 matter, though different fields within a block must be on different
2470 lines. Here is a bit of the above example again, using braces:
2472 Example: Using explicit braces rather than indentation for layout
2473 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2480 License: BSD-3-Clause
2482 Synopsis: Test package to test configurations
2487 Description: Enable debug support
2493 Build-Depends: base >= 4.2 && < 4.9
2494 Exposed-Modules: Testing.Test1
2495 Default-Extensions: CPP
2496 Default-language: Haskell2010
2498 CPP-Options: -DDEBUG
2500 CC-Options: "-DDEBUG"
2502 CC-Options: "-DNDEBUG"
2510 .. pkg-section:: flag name
2511 :synopsis: Flag declaration.
2513 Flag section declares a flag which can be used in `conditional blocks`_.
2515 Flag names are case-insensitive and must match ``[[:alnum:]_][[:alnum:]_-]*``
2516 regular expression, or expressed as ABNF_:
2518 .. code-block:: abnf
2520 flag-name = (UALNUM / "_") *(UALNUM / "_" / "-")
2522 UALNUM = UALPHA / DIGIT
2523 UALPHA = ... ; set of alphabetic Unicode code-points
2527 Hackage accepts ASCII-only flags, ``[a-zA-Z0-9_][a-zA-Z0-9_-]*`` regexp.
2529 .. pkg-field:: description: freeform
2531 The description of this flag.
2533 .. pkg-field:: default: boolean
2537 The default value of this flag.
2541 This value may be :ref:`overridden in several
2542 ways <controlling flag assignments>`. The
2543 rationale for having flags default to True is that users usually
2544 want new features as soon as they are available. Flags representing
2545 features that are not (yet) recommended for most users (such as
2546 experimental features or debugging support) should therefore
2547 explicitly override the default to False.
2549 .. pkg-field:: manual: boolean
2554 By default, Cabal will first try to satisfy dependencies with the
2555 default flag value and then, if that is not possible, with the
2556 negated value. However, if the flag is manual, then the default
2557 value (which can be overridden by commandline flags) will be used.
2562 Conditional blocks may appear anywhere inside a library or executable
2563 section. They have to follow rather strict formatting rules. Conditional
2564 blocks must always be of the shape
2569 property-descriptions-or-conditionals
2576 property-descriptions-or-conditionals
2578 property-descriptions-or-conditionals
2580 Note that the ``if`` and the condition have to be all on the same line.
2582 Since Cabal 2.2 conditional blocks support ``elif`` construct.
2587 property-descriptions-or-conditionals
2589 property-descriptions-or-conditionals
2591 property-descriptions-or-conditionals
2598 Conditions can be formed using boolean tests and the boolean operators
2599 ``||`` (disjunction / logical "or"), ``&&`` (conjunction / logical
2600 "and"), or ``!`` (negation / logical "not"). The unary ``!`` takes
2601 highest precedence, ``||`` takes lowest. Precedence levels may be
2602 overridden through the use of parentheses. For example,
2603 ``os(darwin) && !arch(i386) || os(freebsd)`` is equivalent to
2604 ``(os(darwin) && !(arch(i386))) || os(freebsd)``.
2606 The following tests are currently supported.
2609 Tests if the current operating system is *name*. The argument is
2610 tested against ``System.Info.os`` on the target system. There is
2611 unfortunately some disagreement between Haskell implementations
2612 about the standard values of ``System.Info.os``. Cabal canonicalises
2613 it so that in particular ``os(windows)`` works on all
2614 implementations. If the canonicalised os names match, this test
2615 evaluates to true, otherwise false. The match is case-insensitive.
2616 :samp:`arch({name})`
2617 Tests if the current architecture is *name*. The argument is matched
2618 against ``System.Info.arch`` on the target system. If the arch names
2619 match, this test evaluates to true, otherwise false. The match is
2621 :samp:`impl({compiler})`
2622 Tests for the configured Haskell implementation. An optional version
2623 constraint may be specified (for example ``impl(ghc >= 6.6.1)``). If
2624 the configured implementation is of the right type and matches the
2625 version constraint, then this evaluates to true, otherwise false.
2626 The match is case-insensitive.
2628 Note that including a version constraint in an ``impl`` test causes
2629 it to check for two properties:
2631 - The current compiler has the specified name, and
2633 - The compiler's version satisfied the specified version constraint
2635 As a result, ``!impl(ghc >= x.y.z)`` is not entirely equivalent to
2636 ``impl(ghc < x.y.z)``. The test ``!impl(ghc >= x.y.z)`` checks that:
2638 - The current compiler is not GHC, or
2640 - The version of GHC is earlier than version x.y.z.
2642 :samp:`flag({name})`
2643 Evaluates to the current assignment of the flag of the given name.
2644 Flag names are case insensitive. Testing for flags that have not
2645 been introduced with a flag section is an error.
2647 Constant value true.
2649 Constant value false.
2651 .. _resolution-of-conditions-and-flags:
2653 Resolution of Conditions and Flags
2654 """"""""""""""""""""""""""""""""""
2656 If a package descriptions specifies configuration flags the package user
2657 can :ref:`control these in several ways <controlling flag assignments>`. If the
2658 user does not fix the value of a flag, Cabal will try to find a flag
2659 assignment in the following way.
2661 - For each flag specified, it will assign its default value, evaluate
2662 all conditions with this flag assignment, and check if all
2663 dependencies can be satisfied. If this check succeeded, the package
2664 will be configured with those flag assignments.
2666 - If dependencies were missing, the last flag (as by the order in which
2667 the flags were introduced in the package description) is tried with
2668 its alternative value and so on. This continues until either an
2669 assignment is found where all dependencies can be satisfied, or all
2670 possible flag assignments have been tried.
2672 To put it another way, Cabal does a complete backtracking search to find
2673 a satisfiable package configuration. It is only the dependencies
2674 specified in the :pkg-field:`build-depends` field in conditional blocks that
2675 determine if a particular flag assignment is satisfiable
2676 (:pkg-field:`build-tools` are not considered). The order of the declaration and
2677 the default value of the flags determines the search order. Flags
2678 overridden on the command line fix the assignment of that flag, so no
2679 backtracking will be tried for that flag.
2681 If no suitable flag assignment could be found, the configuration phase
2682 will fail and a list of missing dependencies will be printed. Note that
2683 this resolution process is exponential in the worst case (i.e., in the
2684 case where dependencies cannot be satisfied). There are some
2685 optimizations applied internally, but the overall complexity remains
2688 Meaning of field values when using conditionals
2689 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2691 During the configuration phase, a flag assignment is chosen, all
2692 conditionals are evaluated, and the package description is combined into
2693 a flat package descriptions. If the same field is declared both inside
2694 a conditional and outside then they are combined using the following rules.
2696 - Boolean fields are combined using conjunction (logical "and").
2698 - List fields are combined by appending the inner items to the outer
2703 other-extensions: CPP
2705 other-extensions: MultiParamTypeClasses
2707 when compiled using GHC will be combined to
2711 other-extensions: CPP, MultiParamTypeClasses
2713 Similarly, if two conditional sections appear at the same nesting
2714 level, properties specified in the latter will come after properties
2715 specified in the former.
2717 - All other fields must not be specified in ambiguous ways. For example
2722 if flag(useothermain)
2723 Main-is: OtherMain.hs
2725 will lead to an error. Instead use
2729 if flag(useothermain)
2730 Main-is: OtherMain.hs
2739 .. pkg-section:: common name
2741 :synopsis: Common build info section
2743 Starting with Cabal-2.2 it's possible to use common build info stanzas.
2748 build-depends: base ^>= 4.11
2752 build-depends: tasty ^>= 0.12.0.1
2756 exposed-modules: Foo
2757 default-language: Haskell2010
2760 import: deps, test-deps
2761 type: exitcode-stdio-1.0
2764 default-language: Haskell2010
2766 - You can use `build information`_ fields in common stanzas.
2768 - Common stanzas must be defined before use.
2770 - Common stanzas can import other common stanzas.
2772 - You can import multiple stanzas at once. Stanza names must be separated by commas.
2774 - ``import`` must be the first field in a section. Since Cabal 3.0 imports
2775 are also allowed inside conditionals.
2779 The name `import` was chosen, because there is ``includes`` field.
2781 .. pkg-section:: None
2783 .. pkg-field:: import: token-list
2790 .. pkg-section:: source-repository
2793 It is often useful to be able to specify a source revision control
2794 repository for a package. Cabal lets you specify this information in
2795 a relatively structured form which enables other tools to interpret and
2796 make effective use of the information. For example the information
2797 should be sufficient for an automatic tool to checkout the sources.
2799 Cabal supports specifying different information for various common
2800 source control systems. Obviously not all automated tools will support
2801 all source control systems.
2803 Cabal supports specifying repositories for different use cases. By
2804 declaring which case we mean automated tools can be more useful. There
2805 are currently two kinds defined:
2807 - The ``head`` kind refers to the latest development branch of the
2808 package. This may be used for example to track activity of a project
2809 or as an indication to outside developers what sources to get for
2810 making new contributions.
2812 - The ``this`` kind refers to the branch and tag of a repository that
2813 contains the sources for this version or release of a package. For
2814 most source control systems this involves specifying a tag, id or
2815 hash of some form and perhaps a branch. The purpose is to be able to
2816 reconstruct the sources corresponding to a particular package
2817 version. This might be used to indicate what sources to get if
2818 someone needs to fix a bug in an older branch that is no longer an
2821 You can specify one kind or the other or both. As an example here are
2822 the repositories for the Cabal library. Note that the ``this`` kind of
2823 repository specifies a tag.
2827 source-repository head
2829 location: https://github.com/haskell/cabal
2831 source-repository this
2833 location: https://github.com/haskell/cabal
2836 The exact fields are as follows:
2838 .. pkg-field:: type: token
2840 The name of the source control system used for this repository. The
2841 currently recognised types are:
2847 - ``mercurial`` (or alias ``hg``)
2848 - ``bazaar`` (or alias ``bzr``)
2852 This field is required.
2854 .. pkg-field:: location: URL
2856 The location of the repository. The exact form of this field depends
2857 on the repository type. For example:
2859 - for darcs: ``http://code.haskell.org/foo/``
2860 - for git: ``git://github.com/foo/bar.git``
2861 - for CVS: ``anoncvs@cvs.foo.org:/cvs``
2863 This field is required.
2865 .. pkg-field:: module: token
2867 CVS requires a named module, as each CVS server can host multiple
2870 This field is required for the CVS repository type and should not be
2873 .. pkg-field:: branch: token
2875 Many source control systems support the notion of a branch, as a
2876 distinct concept from having repositories in separate locations. For
2877 example CVS, SVN and git use branches while darcs uses different
2878 locations for different branches. If you need to specify a branch to
2879 identify a your repository then specify it in this field.
2881 This field is optional.
2883 .. pkg-field:: tag: token
2885 A tag identifies a particular state of a source repository. The tag
2886 can be used with a ``this`` repository kind to identify the state of
2887 a repository corresponding to a particular package version or
2888 release. The exact form of the tag depends on the repository type.
2890 This field is required for the ``this`` repository kind.
2892 .. pkg-field:: subdir: directory
2894 Some projects put the sources for multiple packages under a single
2895 source repository. This field lets you specify the relative path
2896 from the root of the repository to the top directory for the
2897 package, i.e. the directory containing the package's ``.cabal``
2900 This field is optional. It defaults to empty which corresponds to the
2901 root directory of the repository.
2903 Downloading a package's source
2904 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2906 The ``cabal get`` command allows to access a package's source code -
2907 either by unpacking a tarball downloaded from Hackage (the default) or
2908 by checking out a working copy from the package's source repository.
2912 $ cabal get [FLAGS] PACKAGES
2914 The ``get`` command supports the following options:
2916 ``-d --destdir`` *PATH*
2917 Where to place the package source, defaults to (a subdirectory of)
2918 the current directory.
2919 ``-s --source-repository`` *[head\|this\|...]*
2920 Clone the package's source repository using the appropriate version
2921 control system. The optional argument allows to choose a specific
2923 ``--index-state`` *[HEAD\|@<unix-timestamp>\|<iso8601-utc-timestamp>]*
2924 Use source package index state as it existed at a previous time. Accepts
2925 unix-timestamps (e.g. ``@1474732068``), ISO8601 UTC timestamps (e.g.
2926 ``2016-09-24T17:47:48Z``), or ``HEAD`` (default).
2927 This determines which package versions are available as well as which
2928 ``.cabal`` file revision is selected (unless ``--pristine`` is used).
2929 ``--only-package-description``
2930 Unpack only the package description file. A synonym,
2931 ``--package-description-only``, is provided for convenience.
2933 Unpack the original pristine tarball, rather than updating the
2934 ``.cabal`` file with the latest revision from the package archive.
2936 Custom setup scripts
2937 --------------------
2939 Since Cabal 1.24, custom ``Setup.hs`` are required to accurately track
2940 their dependencies by declaring them in the ``.cabal`` file rather than
2941 rely on dependencies being implicitly in scope. Please refer to
2942 `this article <https://www.well-typed.com/blog/2015/07/cabal-setup-deps/>`__
2945 As of Cabal library version 3.0, ``defaultMain*`` variants implement support
2946 for response files. Custom ``Setup.hs`` files that do not use one of these
2947 main functions are required to implement their own support, such as by using
2948 ``GHC.ResponseFile.getArgsWithResponseFiles``.
2950 Declaring a ``custom-setup`` stanza also enables the generation of
2951 ``MIN_VERSION_package_(A,B,C)`` CPP macros for the Setup component.
2953 .. pkg-section:: custom-setup
2954 :synopsis: Custom Setup.hs build information.
2957 The optional :pkg-section:`custom-setup` stanza contains information needed
2958 for the compilation of custom ``Setup.hs`` scripts,
2964 base >= 4.5 && < 4.11,
2965 Cabal >= 1.14 && < 1.25
2967 .. pkg-field:: setup-depends: package list
2970 The dependencies needed to compile ``Setup.hs``. See the
2971 :pkg-field:`build-depends` field for a description of the syntax expected by
2974 If the field is not specified the implicit package set will be used.
2975 The package set contains packages bundled with GHC (i.e. ``base``,
2976 ``bytestring``) and specifically ``Cabal``.
2977 The specific bounds are put on ``Cabal`` dependency:
2978 lower-bound is inferred from :pkg-field:`cabal-version`,
2979 and the upper-bound is ``< 1.25``.
2981 ``Cabal`` version is additionally restricted by GHC,
2982 with absolute minimum being ``1.20``, and for example ``Custom``
2983 builds with GHC-8.10 require at least ``Cabal-3.2``.
2986 Backward compatibility and ``custom-setup``
2987 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2989 Versions prior to Cabal 1.24 don't recognise ``custom-setup`` stanzas,
2990 and will behave agnostic to them (except for warning about an unknown
2991 section). Consequently, versions prior to Cabal 1.24 can't ensure the
2992 declared dependencies ``setup-depends`` are in scope, and instead
2993 whatever is registered in the current package database environment
2994 will become eligible (and resolved by the compiler) for the
2995 ``Setup.hs`` module.
2997 The availability of the
2998 ``MIN_VERSION_package_(A,B,C)`` CPP macros
2999 inside ``Setup.hs`` scripts depends on the condition that either
3001 - a ``custom-setup`` section has been declared (or ``cabal build`` is being
3002 used which injects an implicit hard-coded ``custom-setup`` stanza if it's missing), or
3003 - GHC 8.0 or later is used (which natively injects package version CPP macros)
3005 Consequently, if you need to write backward compatible ``Setup.hs``
3006 scripts using CPP, you should declare a ``custom-setup`` stanza and
3007 use the pattern below:
3009 .. code-block:: haskell
3011 {-# LANGUAGE CPP #-}
3012 import Distribution.Simple
3014 #if defined(MIN_VERSION_Cabal)
3015 -- version macros are available and can be used as usual
3016 # if MIN_VERSION_Cabal(a,b,c)
3017 -- code specific to lib:Cabal >= a.b.c
3019 -- code specific to lib:Cabal < a.b.c
3022 # warning Enabling heuristic fall-back. Please upgrade cabal-install to 1.24 or later if Setup.hs fails to compile.
3024 -- package version macros not available; except for exotic environments,
3025 -- you can heuristically assume that lib:Cabal's version is correlated
3026 -- with __GLASGOW_HASKELL__, and specifically since we can assume that
3027 -- GHC < 8.0, we can assume that lib:Cabal is version 1.22 or older.
3032 The simplified (heuristic) CPP pattern shown below is useful if all you need
3033 is to distinguish ``Cabal < 2.0`` from ``Cabal >= 2.0``.
3035 .. code-block:: haskell
3037 {-# LANGUAGE CPP #-}
3038 import Distribution.Simple
3040 #if !defined(MIN_VERSION_Cabal)
3041 # define MIN_VERSION_Cabal(a,b,c) 0
3044 #if MIN_VERSION_Cabal(2,0,0)
3045 -- code for lib:Cabal >= 2.0
3047 -- code for lib:Cabal < 2.0
3054 Autogenerated modules and includes
3055 ----------------------------------
3057 .. pkg-section:: None
3059 Modules that are built automatically at setup, created with a custom
3060 setup script, must appear on :pkg-field:`other-modules` for the library,
3061 executable, test-suite or benchmark stanzas or also on
3062 :pkg-field:`library:exposed-modules` for libraries to be used, but are not
3063 really on the package when distributed. This makes commands like sdist fail
3064 because the file is not found.
3066 These special modules must appear again on the :pkg-field:`autogen-modules`
3067 field of the stanza that is using them, besides :pkg-field:`other-modules` or
3068 :pkg-field:`library:exposed-modules`. With this there is no need to create
3069 complex build hooks for this poweruser case.
3071 .. pkg-field:: autogen-modules: module list
3074 .. todo:: document autogen-modules field
3076 Right now :pkg-field:`executable:main-is` modules are not supported on
3077 :pkg-field:`autogen-modules`.
3082 default-language: Haskell2010
3093 default-language: Haskell2010
3102 .. pkg-field:: autogen-includes: filename list
3105 A list of header files from this package which are autogenerated
3106 (e.g. by a ``configure`` script). Autogenerated header files are not
3107 packaged by ``sdist`` command.
3114 .. pkg-field:: virtual-modules: module list
3120 .. _accessing-data-files:
3122 Accessing data files from package code
3123 --------------------------------------
3125 The placement on the target system of files listed in
3126 the :pkg-field:`data-files` field varies between systems, and in some cases
3127 one can even move packages around after installation
3128 (see :ref:`prefix independence`). To
3129 enable packages to find these files in a portable way, Cabal generates a
3130 module called :file:`Paths_{pkgname}` (with any hyphens in *pkgname*
3131 replaced by underscores) during building, so that it may be imported by
3132 modules of the package. This module defines a function
3134 .. code-block:: haskell
3136 getDataFileName :: FilePath -> IO FilePath
3138 If the argument is a filename listed in the :pkg-field:`data-files` field, the
3139 result is the name of the corresponding file on the system on which the
3144 If you decide to import the :file:`Paths_{pkgname}` module then it
3145 *must* be listed in the :pkg-field:`other-modules` field just like any other
3146 module in your package and on :pkg-field:`autogen-modules` as the file is
3149 The :file:`Paths_{pkgname}` module is not platform independent, as any
3150 other autogenerated module, so it does not get included in the source
3151 tarballs generated by ``sdist``.
3153 The :file:`Paths_{pkgname}` module also includes some other useful
3154 functions and values, which record the version of the package and some
3155 other directories which the package has been configured to be installed
3156 into (e.g. data files live in ``getDataDir``):
3158 .. code-block:: haskell
3162 getBinDir :: IO FilePath
3163 getLibDir :: IO FilePath
3164 getDynLibDir :: IO FilePath
3165 getDataDir :: IO FilePath
3166 getLibexecDir :: IO FilePath
3167 getSysconfDir :: IO FilePath
3169 The actual location of all these directories can be individually
3170 overridden at runtime using environment variables of the form
3171 ``pkg_name_var``, where ``pkg_name`` is the name of the package with all
3172 hyphens converted into underscores, and ``var`` is either ``bindir``,
3173 ``libdir``, ``dynlibdir``, ``datadir``, ``libexedir`` or ``sysconfdir``. For example,
3174 the configured data directory for ``pretty-show`` is controlled with the
3175 ``pretty_show_datadir`` environment variable.
3177 Accessing the package version
3178 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3180 The auto generated :file:`PackageInfo_{pkgname}` module exports the constant
3181 ``version ::`` `Version <http://hackage.haskell.org/package/base/docs/Data-Version.html>`__
3182 which is defined as the version of your package as specified in the
3185 Accessing package-related informations
3186 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3188 The auto generated :file:`PackageInfo_{pkgname}` module exports the following
3189 package-related constants:
3191 .. code-block:: haskell
3199 Unlike :file:`Paths_{pkgname}` (see <#accessing-data-files-from-package-code>),
3200 :file:`PackageInfo_{pkgname}` is system- and path-independent. It aims to be
3201 easier to work with for hash-based tools such as Nix.
3203 .. _system-dependent parameters:
3205 System-dependent parameters
3206 ---------------------------
3208 For some packages, especially those interfacing with C libraries,
3209 implementation details and the build procedure depend on the build
3210 environment. The ``build-type`` ``Configure`` can be used to handle many
3211 such situations. In this case, ``Setup.hs`` should be:
3213 .. code-block:: haskell
3215 import Distribution.Simple
3216 main = defaultMainWithHooks autoconfUserHooks
3218 Most packages, however, would probably do better using the ``Simple``
3219 build type and `configurations`_.
3221 The :pkg-field:`build-type` ``Configure`` differs from ``Simple`` in two ways:
3223 - The package root directory must contain a shell script called
3224 ``configure``. The configure step will run the script. This
3225 ``configure`` script may be produced by
3226 `autoconf <http://www.gnu.org/software/autoconf/>`__ or may be
3227 hand-written. The ``configure`` script typically discovers
3228 information about the system and records it for later steps, e.g. by
3229 generating system-dependent header files for inclusion in C source
3230 files and preprocessed Haskell source files. (Clearly this won't work
3231 for Windows without MSYS or Cygwin: other ideas are needed.)
3233 - If the package root directory contains a file called
3234 *package*\ ``.buildinfo`` after the configuration step, subsequent
3235 steps will read it to obtain additional settings for `build
3236 information`_ fields,to be merged with the ones
3237 given in the ``.cabal`` file. In particular, this file may be
3238 generated by the ``configure`` script mentioned above, allowing these
3239 settings to vary depending on the build environment.
3241 Note that the package's ``extra-source-files`` are available to the
3242 ``configure`` script when it is executed. In typical ``autoconf`` fashion,
3243 ``--host`` flag will be passed to the ``configure`` script to indicate the host
3244 platform when cross-compiling. Moreover, various bits of build configuration
3245 will be passed via environment variables:
3247 - ``CC`` will reflect the path to the C compiler
3248 - ``CFLAGS`` will reflect the path to the C compiler
3249 - ``CABAL_FLAGS`` will contain the Cabal flag assignment of the current
3250 package using traditional Cabal flag syntax (e.g. ``+flagA -flagB``)
3251 - ``CABAL_FLAG_<flag>`` will be set to either ``0`` or ``1`` depending upon
3252 whether flag ``<flag>`` is enabled. Note that any any non-alpha-numeric
3253 characters in the flag name are replaced with ``_``.
3255 The build information file should have the following structure:
3259 ``executable:`` *name* *buildinfo*
3261 ``executable:`` *name* *buildinfo* ...
3263 where each *buildinfo* consists of settings of fields listed in the
3264 section on `build information`_. The first one (if
3265 present) relates to the library, while each of the others relate to the
3266 named executable. (The names must match the package description, but you
3267 don't have to have entries for all of them.)
3269 Neither of these files is required. If they are absent, this setup
3270 script is equivalent to ``defaultMain``.
3272 Example: Using autoconf
3273 ^^^^^^^^^^^^^^^^^^^^^^^
3275 This example is for people familiar with the
3276 `autoconf <http://www.gnu.org/software/autoconf/>`__ tools.
3278 In the X11 package, the file ``configure.ac`` contains:
3280 .. code-block:: shell
3282 AC_INIT([Haskell X11 package], [1.1], [libraries@haskell.org], [X11])
3284 # Safety check: Ensure that we are in the correct source directory.
3285 AC_CONFIG_SRCDIR([X11.cabal])
3287 # Header file to place defines in
3288 AC_CONFIG_HEADERS([include/HsX11Config.h])
3290 # Check for X11 include paths and libraries
3292 AC_TRY_CPP([#include <X11/Xlib.h>],,[no_x=yes])
3294 # Build the package if we found X11 stuff
3295 if test "$no_x" = yes
3296 then BUILD_PACKAGE_BOOL=False
3297 else BUILD_PACKAGE_BOOL=True
3299 AC_SUBST([BUILD_PACKAGE_BOOL])
3301 AC_CONFIG_FILES([X11.buildinfo])
3304 Then the setup script will run the ``configure`` script, which checks
3305 for the presence of the X11 libraries and substitutes for variables in
3306 the file ``X11.buildinfo.in``:
3310 buildable: @BUILD_PACKAGE_BOOL@
3311 cc-options: @X_CFLAGS@
3312 ld-options: @X_LIBS@
3314 This generates a file ``X11.buildinfo`` supplying the parameters needed
3320 cc-options: -I/usr/X11R6/include
3321 ld-options: -L/usr/X11R6/lib
3323 The ``configure`` script also generates a header file
3324 ``include/HsX11Config.h`` containing C preprocessor defines recording
3325 the results of various tests. This file may be included by C source
3326 files and preprocessed Haskell source files in the package.
3330 Packages using these features will also need to list additional
3331 files such as ``configure``, templates for ``.buildinfo`` files, files
3332 named only in ``.buildinfo`` files, header files and so on in the
3333 :pkg-field:`extra-source-files` field to ensure that they are included in
3334 source distributions. They should also list files and directories generated
3335 by ``configure`` in the :pkg-field:`extra-tmp-files` field to ensure that
3336 they are removed by ``setup clean``.
3338 Quite often the files generated by ``configure`` need to be listed
3339 somewhere in the package description (for example, in the
3340 :pkg-field:`install-includes` field). However, we usually don't want generated
3341 files to be included in the source tarball. The solution is again
3342 provided by the ``.buildinfo`` file. In the above example, the following
3343 line should be added to ``X11.buildinfo``:
3347 install-includes: HsX11Config.h
3349 In this way, the generated ``HsX11Config.h`` file won't be included in
3350 the source tarball in addition to ``HsX11Config.h.in``, but it will be
3351 copied to the right location during the install process. Packages that
3352 use custom ``Setup.hs`` scripts can update the necessary fields
3353 programmatically instead of using the ``.buildinfo`` file.
3355 Conditional compilation
3356 -----------------------
3358 Sometimes you want to write code that works with more than one version
3359 of a dependency. You can specify a range of versions for the dependency
3360 in the :pkg-field:`build-depends`, but how do you then write the code that can
3361 use different versions of the API?
3363 Haskell lets you preprocess your code using the C preprocessor (either
3364 the real C preprocessor, or ``cpphs``). To enable this, add
3365 ``extensions: CPP`` to your package description. When using CPP, Cabal
3366 provides some pre-defined macros to let you test the version of
3367 dependent packages; for example, suppose your package works with either
3368 version 3 or version 4 of the ``base`` package, you could select the
3369 available version in your Haskell modules like this:
3373 #if MIN_VERSION_base(4,0,0)
3374 ... code that works with base-4 ...
3376 ... code that works with base-3 ...
3379 In general, Cabal supplies a macro
3380 ``MIN_VERSION_``\ *``package``*\ ``_(A,B,C)`` for each package depended
3381 on via :pkg-field:`build-depends`. This macro is true if the actual version of
3382 the package in use is greater than or equal to ``A.B.C`` (using the
3383 conventional ordering on version numbers, which is lexicographic on the
3384 sequence, but numeric on each component, so for example 1.2.0 is greater
3387 Since version 1.20, the ``MIN_TOOL_VERSION_``\ *``tool``*
3388 family of macros lets you condition on the version of build tools used to
3389 build the program (e.g. ``hsc2hs``).
3391 Since version 1.24, the macro ``CURRENT_COMPONENT_ID``, which
3392 expands to the string of the component identifier that uniquely
3393 identifies this component. Furthermore, if the package is a library,
3394 the macro ``CURRENT_PACKAGE_KEY`` records the identifier that was passed
3395 to GHC for use in symbols and for type equality.
3397 Since version 2.0, the macro ``CURRENT_PACKAGE_VERSION`` expands
3398 to the string version number of the current package.
3400 Cabal places the definitions of these macros into an
3401 automatically-generated header file, which is included when
3402 preprocessing Haskell source code by passing options to the C
3405 Cabal also allows to detect when the source code is being used for
3406 generating documentation. The ``__HADDOCK_VERSION__`` macro is defined
3407 only when compiling via Haddock_
3408 instead of a normal Haskell compiler. The value of the
3409 ``__HADDOCK_VERSION__`` macro is defined as ``A*1000 + B*10 + C``, where
3410 ``A.B.C`` is the Haddock version. This can be useful for working around
3411 bugs in Haddock or generating prettier documentation in some special
3414 .. _more-complex-packages:
3416 More complex packages
3417 ---------------------
3419 For packages that don't fit the simple schemes described above, you have
3422 - By using the :pkg-field:`build-type` ``Custom``, you can supply your own
3423 ``Setup.hs`` file, and customize the simple build infrastructure
3424 using *hooks*. These allow you to perform additional actions before
3425 and after each command is run, and also to specify additional
3426 preprocessors. A typical ``Setup.hs`` may look like this:
3428 .. code-block:: haskell
3430 import Distribution.Simple
3431 main = defaultMainWithHooks simpleUserHooks { postHaddock = posthaddock }
3433 posthaddock args flags desc info = ....
3435 See ``UserHooks`` in
3436 `Distribution.Simple <https://hackage.haskell.org/package/Cabal/docs/Distribution-Simple.html>`__
3437 for the details, but note that this interface is experimental, and
3438 likely to change in future releases.
3440 If you use a custom ``Setup.hs`` file you should strongly consider
3441 adding a :pkg-section:`custom-setup` stanza with a
3442 :pkg-field:`custom-setup:setup-depends` field to ensure that your setup
3443 script does not break with future dependency versions.
3445 - You could delegate all the work to ``make``, though this is unlikely
3446 to be very portable. Cabal supports this with the :pkg-field:`build-type`
3447 ``Make`` and a trivial setup library
3448 `Distribution.Make <https://hackage.haskell.org/package/Cabal/docs/Distribution-Make.html>`__,
3449 which simply parses the command line arguments and invokes ``make``.
3450 Here ``Setup.hs`` should look like this:
3452 .. code-block:: haskell
3454 import Distribution.Make
3457 The root directory of the package should contain a ``configure``
3458 script, and, after that has run, a ``Makefile`` with a default target
3459 that builds the package, plus targets ``install``, ``register``,
3460 ``unregister``, ``clean``, ``dist`` and ``docs``. Some options to
3461 commands are passed through as follows:
3463 - The ``--with-hc-pkg``, ``--prefix``, ``--bindir``, ``--libdir``,
3464 ``--dynlibdir``, ``--datadir``, ``--libexecdir`` and ``--sysconfdir`` options to
3465 the ``configure`` command are passed on to the ``configure``
3466 script. In addition the value of the ``--with-compiler`` option is
3467 passed in a ``--with-hc`` option and all options specified with
3468 ``--configure-option=`` are passed on.
3470 - The ``--destdir`` option to the ``copy`` command becomes a setting
3471 of a ``destdir`` variable on the invocation of ``make copy``. The
3472 supplied ``Makefile`` should provide a ``copy`` target, which will
3473 probably look like this:
3475 .. code-block:: make
3478 $(MAKE) install prefix=$(destdir)/$(prefix) \
3479 bindir=$(destdir)/$(bindir) \
3480 libdir=$(destdir)/$(libdir) \
3481 dynlibdir=$(destdir)/$(dynlibdir) \
3482 datadir=$(destdir)/$(datadir) \
3483 libexecdir=$(destdir)/$(libexecdir) \
3484 sysconfdir=$(destdir)/$(sysconfdir) \
3486 - Finally, with the :pkg-field:`build-type` ``Custom``, you can also write your
3487 own setup script from scratch, and you may use the Cabal
3488 library for all or part of the work. One option is to copy the source
3489 of ``Distribution.Simple``, and alter it for your needs. Good luck.
3496 Cabal and GHC jointly support Backpack, an extension to Haskell's module
3497 system which makes it possible to parametrize a package over some
3498 modules, which can be instantiated later arbitrarily by a user. This
3499 means you can write a library to be agnostic over some data
3500 representation, and then instantiate it several times with different
3501 data representations. Like C++ templates, instantiated packages are
3502 recompiled for each instantiation, which means you do not pay any
3503 runtime cost for parametrizing packages in this way. Backpack modules
3504 are somewhat experimental; while fully supported by cabal-install, they are currently
3505 `not supported by Stack <https://github.com/commercialhaskell/stack/issues/2540>`__.
3507 A Backpack package is defined by use of the
3508 :pkg-field:`library:signatures` field, or by (transitive) dependency on
3509 a package that defines some requirements. To define a parametrized
3510 package, define a signature file (file extension ``hsig``) that
3511 specifies the signature of the module you want to parametrize over, and
3512 add it to your Cabal file in the :pkg-field:`library:signatures` field.
3514 .. code-block:: haskell
3521 concat :: [Str] -> Str
3523 .. code-block:: cabal
3524 :caption: parametrized.cabal
3532 exposed-modules: MyModule
3534 You can define any number of regular modules (e.g., ``MyModule``) that
3535 import signatures and use them as regular modules.
3537 If you are familiar with ML modules, you might now expect there to be
3538 some way to apply the parametrized package with an implementation of
3539 the ``Str`` module to get a concrete instantiation of the package.
3540 Backpack operates slightly differently with a concept of *mix-in
3541 linking*, where you provide an implementation of ``Str`` simply by
3542 bringing another module into scope with the same name as the
3543 requirement. For example, if you had a package ``str-impl`` that provided a
3544 module named ``Str``, instantiating ``parametrized`` is as simple as
3545 just depending on both ``str-impl`` and ``parametrized``:
3547 .. code-block:: cabal
3548 :caption: combined.cabal
3554 build-depends: base, str-impl, parametrized
3556 Note that due to technical limitations, you cannot directly define
3557 ``Str`` in the ``combined`` library; it must be placed in its own
3558 library (you can use :ref:`Internal Libraries <sublibs>` to conveniently
3559 define a sub-library).
3561 However, a more common situation is that your names don't match up
3562 exactly. The :pkg-field:`library:mixins` field can be used to rename
3563 signatures and modules to line up names as necessary. If you have
3564 a requirement ``Str`` and an implementation ``Data.Text``, you can
3565 line up the names in one of two ways:
3567 * Rename the requirement to match the implementation:
3568 ``mixins: parametrized requires (Str as Data.Text)``
3569 * Rename the implementation to match the requirement:
3570 ``mixins: text (Data.Text as Str)``
3572 The :pkg-field:`library:mixins` field can also be used to disambiguate
3573 between multiple instantiations of the same package; for each
3574 instantiation of the package, give it a separate entry in mixins with
3575 the requirements and provided modules renamed to be distinct.
3577 .. code-block:: cabal
3581 name: double-combined
3584 build-depends: base, text, bytestring, parametrized
3586 parametrized (MyModule as MyModule.Text) requires (Str as Data.Text),
3587 parametrized (MyModule as MyModule.BS) requires (Str as Data.ByteString)
3589 Intensive use of Backpack sometimes involves creating lots of small
3590 parametrized libraries; :ref:`Internal Libraries <sublibs>` can be used
3591 to define all of these libraries in a single package without having to
3592 create many separate Cabal packages. You may also find it useful to use
3593 :pkg-field:`library:reexported-modules` to reexport instantiated
3594 libraries to Backpack-unware users (e.g., Backpack can be used entirely
3595 as an implementation detail.)
3597 Backpack imposes a limitation on Template Haskell that goes beyond the usual TH
3598 stage restriction: it's not possible to splice TH code imported from a
3599 compilation unit that is still "indefinite", that is, a unit for which some
3600 module signatures still haven't been matched with implementations. The reason
3601 is that indefinite units are typechecked, but not compiled, so there's no
3602 actual TH code to run while splicing. Splicing TH code from a definite
3603 compilation unit into an indefinite one works normally.
3605 For more information about Backpack, check out the
3606 `GHC wiki page <https://gitlab.haskell.org/ghc/ghc/-/wikis/backpack>`__.
3608 .. include:: references.inc
3610 .. rubric:: Footnotes
3612 .. [#old-style-build-tool-depends]
3614 Some packages (ab)use :pkg-field:`build-depends` on old-style builds, but this has a few major drawbacks:
3616 - 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.
3617 - it may or may not place the executable on ``PATH``.
3618 - it does not ensure the correct version of the package is installed, so you might end up overwriting versions with each other.