1 Package Description — <package>.cabal File
2 ==========================================
4 The package description file, commonly known as "the Cabal file", describes the
5 contents of a package. The Cabal package is the unit of distribution. When
6 installed, its purpose is to make available one or more:
8 - Haskell programs (executables); and/or
10 - libraries, exposing a number of Haskell modules.
12 Public library components can be depended upon by other Cabal packages and all
13 library components (both public and private) can be depended upon by other
14 components of the same package.
16 Internally, the package may consist of much more than a bunch of Haskell
17 modules: it may also have C source code and header files, source code
18 meant for preprocessing, documentation, test cases, auxiliary tools etc.
20 A package is identified by a globally-unique *package name*, which
21 consists of one or more alphanumeric words separated by hyphens. To
22 avoid ambiguity, each of these words should contain at least one letter.
23 Chaos will result if two distinct packages with the same name are
24 installed on the same system. A particular version of the package is
25 distinguished by a *version number*, consisting of a sequence of one or
26 more integers separated by dots. These can be combined to form a single
27 text string called the *package ID*, using a hyphen to separate the name
28 from the version, e.g. "``HUnit-1.1``".
32 Packages are not part of the Haskell language; they simply
33 populate the hierarchical space of module names. In GHC 6.6 and later a
34 program may contain multiple modules with the same name if they come
35 from separate packages; in all other current Haskell systems packages
36 may not overlap in the modules they provide, including hidden modules.
41 Suppose you have a directory hierarchy containing the source files that
42 make up your package. You will need to add two more files to the root
43 directory of the package:
45 :file:`{package-name}.cabal`
46 a Unicode UTF-8 text file containing a package description. For
47 details of the syntax of this file, see the section on
48 `package descriptions`_.
51 a single-module Haskell program to perform various setup tasks (with
52 the interface described in the section on :ref:`setup-commands`).
53 This module should import only modules that will be present in all Haskell
54 implementations, including modules of the Cabal library. The content of
55 this file is determined by the :pkg-field:`build-type` setting in the
56 ``.cabal`` file. In most cases it will be trivial, calling on the Cabal
57 library to do most of the work.
59 Once you have these, you can create a source bundle of this directory
60 for distribution. Building of the package is demonstrated in the section
61 :ref:`building-packages`.
63 One of the purposes of Cabal is to make it easier to build a package
64 with different Haskell implementations. So it provides abstractions of
65 features present in different Haskell implementations and wherever
66 possible it is best to take advantage of these to increase portability.
67 Where necessary however it is possible to use specific features of
68 specific implementations. For example one of the pieces of information a
69 package author can put in the package's ``.cabal`` file is what language
70 extensions the code uses. This is far preferable to specifying flags for
71 a specific compiler as it allows Cabal to pick the right flags for the
72 Haskell implementation that the user picks. It also allows Cabal to
73 figure out if the language extension is even supported by the Haskell
74 implementation that the user picks. Where compiler-specific options are
75 needed however, there is an "escape hatch" available. The developer can
76 specify implementation-specific options and more generally there is a
77 configuration mechanism to customise many aspects of how a package is
78 built depending on the Haskell implementation, the Operating system,
79 computer architecture and user-specified configuration flags.
87 default-language: Haskell2010
88 build-depends: base >= 4 && < 5
90 extensions: ForeignFunctionInterface
93 build-depends: Win32 >= 2.1 && < 2.6
95 Example: A package containing a simple library
96 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
98 The HUnit package contains a file ``HUnit.cabal`` containing:
105 synopsis: A unit testing framework for Haskell
106 homepage: http://hunit.sourceforge.net/
108 author: Dean Herington
109 license: BSD-3-Clause
110 license-file: LICENSE
114 build-depends: base >= 2 && < 4
115 exposed-modules: Test.HUnit.Base, Test.HUnit.Lang,
116 Test.HUnit.Terminal, Test.HUnit.Text, Test.HUnit
117 default-extensions: CPP
118 default-language: Haskell2010
120 and the following ``Setup.hs``:
122 .. code-block:: haskell
124 import Distribution.Simple
127 Example: A package containing executable programs
128 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135 synopsis: Small package with two programs
136 author: Angela Author
137 license: BSD-3-Clause
141 build-depends: HUnit >= 1.1.1 && < 1.2
143 hs-source-dirs: prog1
144 default-language: Haskell2010
147 -- A different main.hs because of hs-source-dirs.
149 build-depends: HUnit >= 1.1.1 && < 1.2
150 hs-source-dirs: prog2
152 default-language: Haskell2010
154 with ``Setup.hs`` the same as above.
156 Example: A package containing a library and executable programs
157 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
164 synopsis: Package with library and two programs
165 license: BSD-3-Clause
166 author: Angela Author
170 build-depends: HUnit >= 1.1.1 && < 1.2
172 exposed-modules: A, B, C
173 default-language: Haskell2010
177 hs-source-dirs: prog1
179 default-language: Haskell2010
182 -- A different main.hs because of hs-source-dirs.
184 -- No bound on a library provided by the same package.
185 build-depends: TestPackage
186 hs-source-dirs: prog2
188 default-language: Haskell2010
190 with ``Setup.hs`` the same as above. Note that any library modules
191 required (directly or indirectly) by an executable must be listed again.
193 The trivial setup script used in these examples uses the *simple build
194 infrastructure* provided by the Cabal library (see
195 `Distribution.Simple <https://hackage.haskell.org/package/Cabal/docs/Distribution-Simple.html>`__).
196 The simplicity lies in its interface rather that its implementation. It
197 automatically handles preprocessing with standard preprocessors, and
198 builds packages for all the Haskell implementations.
200 The simple build infrastructure can also handle packages where building
201 is governed by system-dependent parameters, if you specify a little more
202 (see the section on `system-dependent parameters`_).
203 A few packages require `more elaborate solutions <more complex packages>`_.
210 The package description file must have a name ending in "``.cabal``". It
211 must be a Unicode text file encoded using valid UTF-8. There must be
212 exactly one such file in the directory. The first part of the name is
213 usually the package name, and some of the tools that operate on Cabal
214 packages require this; specifically, Hackage rejects packages which
215 don't follow this rule.
217 In the package description file, lines whose first non-whitespace
218 characters are "``--``" are treated as comments and ignored.
220 This file should contain a number global property descriptions and
223 - The `package properties`_ describe the package
224 as a whole, such as name, license, author, etc.
226 - Optionally, a number of *configuration flags* can be declared. These
227 can be used to enable or disable certain features of a package. (see
228 the section on `configurations`_).
230 - The (optional) library section specifies the `library`_ properties and
231 relevant `build information`_.
233 - Following is an arbitrary number of executable sections which describe
234 an executable program and relevant `build information`_.
236 Each section consists of a number of property descriptions in the form
237 of field/value pairs, with a syntax roughly like mail message headers.
239 - Case is not significant in field names, but is significant in field
242 - To continue a field value, indent the next line relative to the field
245 - Field names may be indented, but all field values in the same section
246 must use the same indentation.
248 - Tabs are *not* allowed as indentation characters due to a missing
249 standard interpretation of tab width.
251 - Before Cabal 3.0, to get a blank line in a field value, use an indented "``.``"
253 The syntax of the value depends on the field. Field types include:
255 *token*, *filename*, *directory*
256 Either a sequence of one or more non-space non-comma characters, or
257 a quoted string in Haskell 98 lexical syntax. The latter can be used
258 for escaping whitespace, for example:
259 ``ghc-options: -Wall "-with-rtsopts=-T -I1"``. Unless otherwise
260 stated, relative filenames and directories are interpreted from the
261 package root directory.
262 *freeform*, *URL*, *address*
263 An arbitrary, uninterpreted string.
265 A letter followed by zero or more alphanumerics or underscores.
267 A compiler flavor (one of: ``GHC``, ``UHC`` or ``LHC``)
268 followed by a version range. For example, ``GHC ==6.10.3``, or
269 ``LHC >=0.6 && <0.8``.
271 Modules and preprocessors
272 ^^^^^^^^^^^^^^^^^^^^^^^^^
274 Haskell module names listed in the :pkg-field:`library:exposed-modules` and
275 :pkg-field:`library:other-modules` fields may correspond to Haskell source
276 files, i.e. with names ending in "``.hs``" or "``.lhs``", or to inputs for
277 various Haskell preprocessors. The simple build infrastructure understands the
280 - ``.gc`` (:hackage-pkg:`greencard`)
281 - ``.chs`` (:hackage-pkg:`c2hs`)
282 - ``.hsc`` (:hackage-pkg:`hsc2hs`)
283 - ``.y`` and ``.ly`` (happy_)
285 - ``.cpphs`` (cpphs_)
287 When building, Cabal will automatically run the appropriate preprocessor
288 and compile the Haskell module it produces. For the ``c2hs`` and
289 ``hsc2hs`` preprocessors, Cabal will also automatically add, compile and
290 link any C sources generated by the preprocessor (produced by
291 ``hsc2hs``'s ``#def`` feature or ``c2hs``'s auto-generated wrapper
292 functions). Dependencies on pre-processors are specified via the
293 :pkg-field:`build-tools` or :pkg-field:`build-tool-depends` fields.
295 Some fields take lists of values, which are optionally separated by
296 commas, except for the :pkg-field:`build-depends` field, where the commas are
299 Some fields are marked as required. All others are optional, and unless
300 otherwise specified have empty default values.
305 These fields may occur in the first top-level properties section and
306 describe the package as a whole:
308 .. pkg-field:: name: package-name (required)
310 The unique name of the package, without the version number.
312 As pointed out in the section on `package descriptions`_, some
313 tools require the package-name specified for this field to match
314 the package description's file-name :file:`{package-name}.cabal`.
316 Package names are case-sensitive and must match the regular expression
317 (i.e. alphanumeric "words" separated by dashes; each alphanumeric
318 word must contain at least one letter):
319 ``[[:digit:]]*[[:alpha:]][[:alnum:]]*(-[[:digit:]]*[[:alpha:]][[:alnum:]]*)*``.
321 Or, expressed in ABNF_:
325 package-name = package-name-part *("-" package-name-part)
326 package-name-part = *DIGIT UALPHA *UALNUM
328 UALNUM = UALPHA / DIGIT
329 UALPHA = ... ; set of alphabetic Unicode code-points
333 Hackage restricts package names to the ASCII subset.
335 .. pkg-field:: version: numbers (required)
337 The package version number, usually consisting of a sequence of
338 natural numbers separated by dots, i.e. as the regular
339 expression ``[0-9]+([.][0-9]+)*`` or expressed in ABNF_:
343 package-version = 1*DIGIT *("." 1*DIGIT)
345 .. pkg-field:: cabal-version: x.y[.z]
347 The version of the Cabal specification that this package
348 description uses. The Cabal specification does slowly evolve (see
349 also :ref:`spec-history`), introducing new features and
350 occasionally changing the meaning of existing features.
351 Specifying which version of the specification you are using
352 enables programs which process the package description to know
353 what syntax to expect and what each part means.
355 The version number you specify will affect both compatibility and
356 behaviour. Most tools (including the Cabal library and the ``cabal``
357 program) understand a range of versions of the Cabal specification.
358 Older tools will of course only work with older versions of the
359 Cabal specification that was known at the time. Most of the time,
360 tools that are too old will recognise this fact and produce a
361 suitable error message. Likewise, ``cabal check`` will tell you
362 whether the version number is sufficiently high for the features
363 you use in the package description.
365 As for behaviour, new versions of the Cabal specification can change the
366 meaning of existing syntax. This means if you want to take advantage
367 of the new meaning or behaviour then you must specify the newer
368 Cabal version. Tools are expected to use the meaning and behaviour
369 appropriate to the version given in the package description.
371 In particular, the syntax of package descriptions changed
372 significantly with Cabal version 1.2 and the :pkg-field:`cabal-version`
373 field is now required. Files written in the old syntax are still
374 recognized, so if you require compatibility with very old Cabal
375 versions then you may write your package description file using the
376 old syntax. Please consult the user's guide of an older Cabal
377 version for a description of that syntax.
379 Starting with ``cabal-version: 2.2`` this field is only valid if
380 fully contained in the very first line of a package description
381 and ought to adhere to the ABNF_ grammar
385 newstyle-spec-version-decl = "cabal-version" *WS ":" *WS newstyle-spec-version *WS
387 newstyle-spec-version = NUM "." NUM [ "." NUM ]
389 NUM = DIGIT0 / DIGITP 1*DIGIT0
397 For package descriptions using a format prior to
398 ``cabal-version: 1.12`` the legacy syntax resembling a version
401 .. code-block:: cabal
403 cabal-version: >= 1.10
407 This legacy syntax is supported up until ``cabal-version: >=
408 2.0`` it is however strongly recommended to avoid using the
409 legacy syntax. See also :issue:`4899`.
413 .. pkg-field:: build-type: identifier
415 :default: ``Custom`` or ``Simple``
417 The type of build used by this package. Build types are the
419 `BuildType <https://hackage.haskell.org/package/Cabal-syntax/docs/Distribution-Types-BuildType.html#t:BuildType>`__
420 type. This field is optional and when missing, its default value
421 is inferred according to the following rules:
423 - When :pkg-field:`cabal-version` is set to ``2.2`` or higher,
424 the default is ``Simple`` unless a :pkg-section:`custom-setup`
425 exists, in which case the inferred default is ``Custom``.
427 - For lower :pkg-field:`cabal-version` values, the default is
428 ``Custom`` unconditionally.
430 If the build type is anything other than ``Custom``, then the
431 ``Setup.hs`` file *must* be exactly the standardized content
432 discussed below. This is because in these cases, ``cabal`` will
433 ignore the ``Setup.hs`` file completely, whereas other methods of
434 package management, such as ``runhaskell Setup.hs [CMD]``, still
435 rely on the ``Setup.hs`` file.
437 For build type ``Simple``, the contents of ``Setup.hs`` must be:
439 .. code-block:: haskell
441 import Distribution.Simple
444 For build type ``Configure`` (see the section on `system-dependent
445 parameters`_ below), the contents of
446 ``Setup.hs`` must be:
448 .. code-block:: haskell
450 import Distribution.Simple
451 main = defaultMainWithHooks autoconfUserHooks
453 For build type ``Make`` (see the section on `more complex packages`_ below),
454 the contents of ``Setup.hs`` must be:
456 .. code-block:: haskell
458 import Distribution.Make
461 For build type ``Custom``, the file ``Setup.hs`` can be customized,
462 and will be used both by ``cabal`` and other tools.
464 For most packages, the build type ``Simple`` is sufficient.
466 .. pkg-field:: license: SPDX expression
470 The type of license under which this package is distributed.
472 Starting with ``cabal-version: 2.2`` the ``license`` field takes a
473 (case-sensitive) SPDX expression such as
475 .. code-block:: cabal
477 license: Apache-2.0 AND (MIT OR GPL-2.0-or-later)
479 See `SPDX IDs: How to use <https://spdx.org/ids-how>`__ for more
480 examples of SPDX expressions.
483 `list of SPDX license identifiers <https://spdx.org/licenses/>`__
484 is a function of the :pkg-field:`cabal-version` value as defined
485 in the following table:
487 +--------------------------+--------------------+
488 | Cabal specification | SPDX license list |
489 | version | version |
491 +==========================+====================+
492 | ``cabal-version: 2.2`` | ``3.0 2017-12-28`` |
493 +--------------------------+--------------------+
494 | ``cabal-version: 2.4`` | ``3.2 2018-07-10`` |
495 +--------------------------+--------------------+
497 **Pre-SPDX Legacy Identifiers**
499 The license identifier in the table below are defined for
500 ``cabal-version: 2.0`` and previous versions of the Cabal
503 +--------------------------+-----------------+
504 | :pkg-field:`license` | Note |
507 +==========================+=================+
511 +--------------------------+-----------------+
515 +--------------------------+-----------------+
516 | ``AGPL`` | since 1.18 |
518 +--------------------------+-----------------+
519 | ``BSD2`` | since 1.20 |
520 +--------------------------+-----------------+
522 +--------------------------+-----------------+
524 +--------------------------+-----------------+
525 | ``ISC`` | since 1.22 |
526 +--------------------------+-----------------+
527 | ``MPL-2.0`` | since 1.20 |
528 +--------------------------+-----------------+
531 +--------------------------+-----------------+
532 | ``PublicDomain`` | |
533 +--------------------------+-----------------+
534 | ``AllRightsReserved`` | |
535 +--------------------------+-----------------+
536 | ``OtherLicense`` | |
537 +--------------------------+-----------------+
540 .. pkg-field:: license-file: filename
542 See :pkg-field:`license-files`.
544 .. pkg-field:: license-files: filename list
547 The name of a file(s) containing the precise copyright license for
548 this package. The license file(s) will be installed with the
551 If you have multiple license files then use the :pkg-field:`license-files`
552 field instead of (or in addition to) the :pkg-field:`license-file` field.
554 .. pkg-field:: copyright: freeform
556 The content of a copyright notice, typically the name of the holder
557 of the copyright on the package and the year(s) from which copyright
558 is claimed. For example::
560 copyright: (c) 2006-2007 Joe Bloggs
562 .. pkg-field:: author: freeform
564 The original author of the package.
566 Remember that ``.cabal`` files are Unicode, using the UTF-8
569 .. pkg-field:: maintainer: address
571 The current maintainer or maintainers of the package. This is an
572 e-mail address to which users should send bug reports, feature
573 requests and patches.
575 .. pkg-field:: stability: freeform
577 The stability level of the package, e.g. ``alpha``,
578 ``experimental``, ``provisional``, ``stable``.
580 .. pkg-field:: homepage: URL
582 The package homepage.
584 .. pkg-field:: bug-reports: URL
586 The URL where users should direct bug reports. This would normally
589 - A ``mailto:`` URL, e.g. for a person or a mailing list.
591 - An ``http:`` (or ``https:``) URL for an online bug tracking
594 For example Cabal itself uses a web-based bug tracking system
598 bug-reports: https://github.com/haskell/cabal/issues
600 .. pkg-field:: package-url: URL
602 The location of a source bundle for the package. The distribution
603 should be a Cabal package.
605 .. pkg-field:: synopsis: freeform
607 A very short description of the package, for use in a table of
608 packages. This is your headline, so keep it short (one line) but as
609 informative as possible. Save space by not including the package
610 name or saying it's written in Haskell.
612 .. pkg-field:: description: freeform
614 Description of the package. This may be several paragraphs, and
615 should be aimed at a Haskell programmer who has never heard of your
618 For library packages, this field is used as prologue text by
619 :ref:`setup-haddock` and thus may contain the same markup as Haddock_
620 documentation comments.
622 .. pkg-field:: category: freeform
624 A classification category for future use by the package catalogue
625 Hackage_. These categories have not
626 yet been specified, but the upper levels of the module hierarchy
629 .. pkg-field:: tested-with: compiler list
631 A list of compilers and versions against which the package has been
632 tested (or at least built). The value of this field is not used by Cabal
633 and is rather intended as extra metadata for use by third party
634 tooling, such as e.g. CI tooling.
636 Here's a typical usage example:
640 tested-with: GHC == 9.0.1, GHC == 8.10.4, GHC == 8.8.4,
641 GHC == 8.6.5, GHC == 8.4.4, GHC == 8.2.2, GHC == 8.0.2,
642 GHC == 7.10.3, GHC == 7.8.4, GHC == 7.6.3, GHC == 7.4.2
644 The same can be spread over several lines, for instance:
648 tested-with: GHC == 9.0.1
660 The separating comma can also be dropped altogether:
677 However, this alternative might
678 `disappear <https://github.com/haskell/cabal/issues/4894#issuecomment-909008657>`__
681 Starting with :pkg-field:`cabal-version` 3.0,
682 there are further conveniences.
684 1. A preceding ``,`` is allowed, so a bullet-list style
685 is possible (recommended):
703 2. A concise set notation syntax is available:
707 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 }
709 .. pkg-field:: data-files: filename list
711 A list of files to be installed for run-time use by the package.
712 This is useful for packages that use a large amount of static data,
713 such as tables of values or code templates. Cabal provides a way to
714 `find these files at run-time <#accessing-data-files-from-package-code>`_.
716 A limited form of ``*`` wildcards in file names, for example
717 ``data-files: images/*.png`` matches all the ``.png`` files in the
718 ``images`` directory. ``data-files: audio/**/*.mp3`` matches all
719 the ``.mp3`` files in the ``audio`` directory, including
722 The specific limitations of this wildcard syntax are
724 - ``*`` wildcards are only allowed in place of the file name, not
725 in the directory name or file extension. It must replace the
726 whole file name (e.g., ``*.html`` is allowed, but
727 ``chapter-*.html`` is not). If a wildcard is used, it must be
728 used with an extension, so ``data-files: data/*`` is not
731 - Prior to Cabal 2.4, when matching a wildcard plus extension, a
732 file's full extension must match exactly, so ``*.gz`` matches
733 ``foo.gz`` but not ``foo.tar.gz``. This restriction has been
734 lifted when ``cabal-version: 2.4`` or greater so that ``*.gz``
735 does match ``foo.tar.gz``
737 - ``*`` wildcards will not match if the file name is empty (e.g.,
738 ``*.html`` will not match ``foo/.html``).
740 - ``**`` wildcards can only appear as the final path component
741 before the file name (e.g., ``data/**/images/*.jpg`` is not
744 - Prior to Cabal 3.8, if a ``**`` wildcard is used, then
745 the file name must include a ``*`` wildcard (e.g.,
746 ``data/**/README.rst`` was not allowed). As of ``cabal-version:
747 3.8`` or greater, this restriction is lifted.
749 - A wildcard that does not match any files is an error.
751 The reason for providing only a very limited form of wildcard is to
752 concisely express the common case of a large number of related files
753 of the same file type without making it too easy to accidentally
754 include unwanted files.
756 On efficiency: if you use ``**`` patterns, the directory tree will
757 be walked starting with the parent directory of the ``**``. If
758 that's the root of the project, this might include ``.git/``,
759 ``dist-newstyle/``, or other large directories! To avoid this
760 behaviour, put the files that wildcards will match against in
763 ``**`` wildcards are available starting in Cabal 2.4
764 and `bug-free since Cabal 3.0 <https://github.com/haskell/cabal/issues/6125#issuecomment-1379878419>`_.
766 .. pkg-field:: data-dir: directory
768 The directory where Cabal looks for data files to install, relative
769 to the source directory. By default, Cabal will look in the source
772 .. pkg-field:: extra-source-files: filename list
774 A list of additional files to be included in source distributions built with :ref:`setup-sdist`.
775 As with :pkg-field:`data-files` it can use a limited form of ``*`` wildcards in file names.
776 Files listed here are tracked by ``cabal build``; changes in these files cause (partial) rebuilds.
778 .. pkg-field:: extra-doc-files: filename list
781 A list of additional files to be included in source distributions,
782 and also copied to the html directory when Haddock documentation is
783 generated. As with :pkg-field:`data-files` it can use a limited form of
784 ``*`` wildcards in file names.
786 .. pkg-field:: extra-tmp-files: filename list
788 A list of additional files or directories to be removed by
789 :ref:`setup-clean`. These would typically be additional files created by
790 additional hooks, such as the scheme described in the section on
791 `system-dependent parameters`_.
796 .. pkg-section:: library name
797 :synopsis: Library build information.
799 Build information for libraries.
801 A package can include zero or more library components. A library can be
802 unnamed or named (using the ``name`` argument). It can also be depended upon
803 only by components in the same package (private) or by those components and
804 components in other packages (public). A package can have no more than one
809 The 'cabal' executable provided by the 'cabal-install' package will not
810 accept dependencies on sublibraries of packages with no unnamed library.
812 This guide refers to an unnamed library as the main library and a named
813 library as a sublibrary (such components may be considered as subidiary, or
814 ancillary, to the main library). It refers to a private sublibrary as an
817 A sublibrary cannot have the same name as its package.
821 Before version 3.4 of the Cabal specification, a private sublibrary could
822 shadow a dependency on the main library of another package, if their
825 A main library is always public and a sublibrary is private by default.
826 See the :pkg-field:`library:visibility` field for setting a sublibrary as
829 Being able to include more than one public library in a package allows the
830 separation of the unit of distribution (the package) from the unit of
831 buildable code (the library). This is useful for Haskell projects with many
832 libraries that are distributed together as it avoids duplication and
833 potential inconsistencies.
837 Before version 3.0 of the Cabal specification, all sublibraries were
838 internal libraries. Before version 2.0, a package could not include
841 See :ref:`Sublibraries - Examples <sublibs>` for examples.
843 A library section should contain the following fields:
845 .. pkg-field:: visibility: visibility specifiers
850 ``private`` for sublibraries. Cannot be set for the main library, which
853 Can be set to ``private`` or ``public``. A ``private`` library component can
854 only be depended on by other components of the same package. A ``public``
855 component can be depended on by those components and by components of other
858 See the :pkg-field:`build-depends` field for the syntax to specify a
859 dependency on a library component.
861 .. pkg-field:: exposed-modules: identifier list
863 :required: if this package contains a library
865 A list of modules added by this package.
867 .. pkg-field:: virtual-modules: identifier list
870 A list of virtual modules provided by this package. Virtual modules
871 are modules without a source file. See for example the ``GHC.Prim``
872 module from the ``ghc-prim`` package. Modules listed here will not be
873 built, but still end up in the list of ``exposed-modules`` in the
874 installed package info when the package is registered in the package
877 .. pkg-field:: exposed: boolean
881 Some Haskell compilers (notably GHC) support the notion of packages
882 being "exposed" or "hidden" which means the modules they provide can
883 be easily imported without always having to specify which package
884 they come from. However this only works effectively if the modules
885 provided by all exposed packages do not overlap (otherwise a module
886 import would be ambiguous).
888 Almost all new libraries use hierarchical module names that do not
889 clash, so it is very uncommon to have to use this field. However it
890 may be necessary to set ``exposed: False`` for some old libraries
891 that use a flat module namespace or where it is known that the
892 exposed modules would clash with other common modules.
894 .. pkg-field:: reexported-modules: exportlist
897 Supported only in GHC 7.10 and later. A list of modules to
898 *reexport* from this package. The syntax of this field is
899 ``orig-pkg:Name as NewName`` to reexport module ``Name`` from
900 ``orig-pkg`` with the new name ``NewName``. We also support
901 abbreviated versions of the syntax: if you omit ``as NewName``,
902 we'll reexport without renaming; if you omit ``orig-pkg``, then we
903 will automatically figure out which package to reexport from, if
906 Reexported modules are useful for compatibility shims when a package
907 has been split into multiple packages, and they have the useful
908 property that if a package provides a module, and another package
909 reexports it under the same name, these are not considered a
910 conflict (as would be the case with a stub module.) They can also be
911 used to resolve name conflicts.
913 .. pkg-field:: signatures: signature list
916 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.
918 Module signatures are part of the :ref:`Backpack` extension to
919 the Haskell module system.
921 Packages that do not export any modules and only export required signatures
922 are called "signature-only packages", and their signatures are subjected to
924 <https://wiki.haskell.org/Module_signature#How_to_use_a_signature_package>`__.
928 The library section may also contain build information fields (see the
929 section on `build information`_).
933 **Sublibraries - Examples**
935 An example of the use of a private sublibrary (an internal library) is a test
936 suite that needs access to some internal modules in the package's main library,
937 which you do not otherwise want to expose. You could put those modules in an
938 internal library, which the main library and the test suite
939 :pkg-field:`build-depends` upon. Your Cabal file might then look something like
947 license: BSD-3-Clause
948 license-file: LICENSE
952 exposed-modules: Foo.Internal
953 -- NOTE: no explicit constraints on base needed
954 -- as they're inherited from the 'library' stanza
956 default-language: Haskell2010
959 exposed-modules: Foo.Public
960 build-depends: foo:foo-internal, base >= 4.3 && < 5
961 default-language: Haskell2010
964 type: exitcode-stdio-1.0
966 -- NOTE: no constraints on 'foo-internal' as same-package
967 -- dependencies implicitly refer to the same package instance
968 build-depends: foo:foo-internal, base
969 default-language: Haskell2010
971 Another example of the use of internal libraries is a package that includes one
972 or more executables but does not include a public library.
974 Internal libraries can be used to incorporate (vendor or bundle) an external
975 dependency into a package, effectively simulating *private dependencies*. Below
981 name: haddock-library
983 license: BSD-3-Clause
988 , bytestring ^>= 0.10.2.0
989 , containers ^>= 0.4.2.1 || ^>= 0.5.0.0
990 , transformers ^>= 0.5.0.0
995 build-depends: haddock-library:attoparsec
998 Documentation.Haddock
1000 default-language: Haskell2010
1005 , bytestring ^>= 0.10.2.0
1006 , deepseq ^>= 1.4.0.0
1008 hs-source-dirs: vendor/attoparsec-0.13.1.0
1010 -- NB: haddock-library needs only small part of lib:attoparsec
1011 -- internally, so we only bundle that subset here
1013 Data.Attoparsec.ByteString
1014 Data.Attoparsec.Combinator
1017 Data.Attoparsec.Internal
1019 ghc-options: -funbox-strict-fields -Wall -fwarn-tabs -O2
1021 default-language: Haskell2010
1026 A package description can contain multiple executable sections.
1027 The documentation of the `cabal run <cabal-commands.html#cabal-run>`__ command
1028 contains detailed information on how to run an executable.
1030 .. pkg-section:: executable name
1031 :synopsis: Executable build info section.
1033 Executable sections (if present) describe executable programs contained
1034 in the package and must have an argument after the section label, which
1035 defines the name of the executable. This is a freeform argument but may
1038 The executable may be described using the following fields, as well as
1039 build information fields (see the section on `build information`_).
1041 .. pkg-field:: main-is: filename (required)
1043 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1044 module. Note that it is the ``.hs`` filename that must be listed,
1045 even if that file is generated using a preprocessor. The source file
1046 must be relative to one of the directories listed in
1047 :pkg-field:`hs-source-dirs`. Further, while the name of the file may
1048 vary, the module itself must be named ``Main``.
1050 Starting with ``cabal-version: 1.18`` this field supports
1051 specifying a C, C++, or objC source file as the main entry point.
1053 .. pkg-field:: scope: token
1056 Whether the executable is ``public`` (default) or ``private``, i.e. meant to
1057 be run by other programs rather than the user. Private executables are
1058 installed into `$libexecdir/$libexecsubdir`.
1064 A package description can contain multiple test suite sections.
1065 The documentation of the `cabal test <cabal-commands.html#cabal-test>`__ command
1066 contains detailed information on how to run test suites.
1068 .. pkg-section:: test-suite name
1069 :synopsis: Test suite build information.
1071 Test suite sections (if present) describe package test suites and must
1072 have an argument after the section label, which defines the name of the
1073 test suite. This is a freeform argument, but may not contain spaces. It
1074 should be unique among the names of the package's other test suites, the
1075 package's executables, and the package itself. Using test suite sections
1076 requires at least Cabal version 1.9.2.
1078 The test suite may be described using the following fields, as well as
1079 build information fields (see the section on `build information`_).
1081 .. pkg-field:: type: interface (required until ``cabal-version`` 3.8)
1083 The interface type and version of the test suite. Cabal supports two
1084 test suite interfaces, called ``exitcode-stdio-1.0`` (default since ``cabal-version`` 3.8) and
1085 ``detailed-0.9``. Each of these types may require or disallow other
1086 fields as described below.
1088 Test suites using the ``exitcode-stdio-1.0`` (default since ``cabal-version`` 3.8) interface are executables
1089 that indicate test failure with a non-zero exit code when run; they may
1090 provide human-readable log information through the standard output and
1091 error channels. The ``exitcode-stdio-1.0`` type requires the ``main-is``
1094 .. pkg-field:: main-is: filename
1095 :synopsis: Module containing tests main function.
1097 :required: ``exitcode-stdio-1.0``
1098 :disallowed: ``detailed-0.9``
1100 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1101 module. Note that it is the ``.hs`` filename that must be listed,
1102 even if that file is generated using a preprocessor. The source file
1103 must be relative to one of the directories listed in
1104 :pkg-field:`hs-source-dirs`. This field is analogous to the ``main-is`` field
1105 of an executable section.
1107 Test suites using the ``detailed-0.9`` interface are modules exporting
1108 the symbol ``tests :: IO [Test]``. The ``Test`` type is exported by the
1109 module ``Distribution.TestSuite`` provided by Cabal. For more details,
1110 see the example below.
1112 The ``detailed-0.9`` interface allows Cabal and other test agents to
1113 inspect a test suite's results case by case, producing detailed human-
1114 and machine-readable log files. The ``detailed-0.9`` interface requires
1115 the :pkg-field:`test-module` field.
1117 .. pkg-field:: test-module: identifier
1119 :required: ``detailed-0.9``
1120 :disallowed: ``exitcode-stdio-1.0``
1122 The module exporting the ``tests`` symbol.
1124 .. pkg-field:: code-generators
1126 An optional list of preprocessors which can generate new modules
1127 for use in the test-suite.
1129 A list of executabes (possibly brought into scope by
1130 :pkg-field:`build-tool-depends`) that are run after all other
1131 preprocessors. These executables are invoked as so: ``exe-name
1132 TARGETDIR [SOURCEDIRS] -- [GHCOPTIONS]``. The arguments are, in order a target dir for
1133 output, a sequence of all source directories with source files of
1134 local lib components that the given test stanza depends on, and
1135 following a double dash, all options cabal would pass to ghc for a
1136 build. They are expected to output a newline-seperated list of
1137 generated modules which have been written to the targetdir
1138 (excepting, if written, the main module). This can
1139 be used for driving doctests and other discover-style tests generated
1143 Example: Package using ``exitcode-stdio-1.0`` interface
1144 """""""""""""""""""""""""""""""""""""""""""""""""""""""
1146 The example package description and executable source file below
1147 demonstrate the use of the ``exitcode-stdio-1.0`` interface.
1149 .. code-block:: cabal
1155 License: BSD-3-Clause
1159 type: exitcode-stdio-1.0
1160 main-is: test-foo.hs
1161 build-depends: base >= 4 && < 5
1162 default-language: Haskell2010
1164 .. code-block:: haskell
1165 :caption: test-foo.hs
1169 import System.Exit (exitFailure)
1172 putStrLn "This test always fails!"
1175 Example: Package using ``detailed-0.9`` interface
1176 """""""""""""""""""""""""""""""""""""""""""""""""
1178 The example package description and test module source file below
1179 demonstrate the use of the ``detailed-0.9`` interface. The test module
1180 also develops a simple implementation of the interface set by
1181 ``Distribution.TestSuite``, but in actual usage the implementation would
1182 be provided by the library that provides the testing facility.
1184 .. code-block:: cabal
1190 License: BSD-3-Clause
1196 build-depends: base >= 4 && < 5, Cabal >= 1.9.2 && < 2
1197 default-language: Haskell2010
1200 .. code-block:: haskell
1203 module Bar ( tests ) where
1205 import Distribution.TestSuite
1208 tests = return [ Test succeeds, Test fails ]
1210 succeeds = TestInstance
1211 { run = return $ Finished Pass
1215 , setOption = \_ _ -> Right succeeds
1217 fails = TestInstance
1218 { run = return $ Finished $ Fail "Always fails!"
1222 , setOption = \_ _ -> Right fails
1228 A package description can contain multiple benchmark sections.
1229 The documentation of the `cabal bench <cabal-commands.html#cabal-bench>`__ command
1230 contains detailed information on how to run benchmarks.
1232 .. pkg-section:: benchmark name
1234 :synopsis: Benchmark build information.
1236 Benchmark sections (if present) describe benchmarks contained in the
1237 package and must have an argument after the section label, which defines
1238 the name of the benchmark. This is a freeform argument, but may not
1239 contain spaces. It should be unique among the names of the package's
1240 other benchmarks, the package's test suites, the package's executables,
1241 and the package itself. Using benchmark sections requires at least Cabal
1244 The benchmark may be described using the following fields, as well as
1245 build information fields (see the section on `build information`_).
1247 .. pkg-field:: type: interface (required until ``cabal-version`` 3.8)
1249 The interface type and version of the benchmark. At the moment Cabal
1250 only support one benchmark interface, called ``exitcode-stdio-1.0``.
1252 Benchmarks using the ``exitcode-stdio-1.0`` (default since ``cabal-version`` 3.8) interface are executables
1253 that indicate failure to run the benchmark with a non-zero exit code
1254 when run; they may provide human-readable information through the
1255 standard output and error channels.
1257 .. pkg-field:: main-is: filename
1259 The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
1260 module. Note that it is the ``.hs`` filename that must be listed,
1261 even if that file is generated using a preprocessor. The source file
1262 must be relative to one of the directories listed in
1263 :pkg-field:`hs-source-dirs`. This field is analogous to the ``main-is``
1264 field of an executable section. Further, while the name of the file may
1265 vary, the module itself must be named ``Main``.
1268 """""""""""""""""""""""""""""""""""""""""""""""""""""""
1270 .. code-block:: cabal
1272 :name: foo-bench.cabal
1277 License: BSD-3-Clause
1281 type: exitcode-stdio-1.0
1282 main-is: bench-foo.hs
1283 build-depends: base >= 4 && < 5, time >= 1.1 && < 1.7
1284 default-language: Haskell2010
1286 .. code-block:: haskell
1287 :caption: bench-foo.hs
1289 {-# LANGUAGE BangPatterns #-}
1292 import Data.Time.Clock
1296 fib n = fib (n-1) + fib (n-2)
1299 start <- getCurrentTime
1301 end <- getCurrentTime
1302 putStrLn $ "fib 20 took " ++ show (diffUTCTime end start)
1308 Foreign libraries are system libraries intended to be linked against
1309 programs written in C or other "foreign" languages. They
1310 come in two primary flavours: dynamic libraries (``.so`` files on Linux,
1311 ``.dylib`` files on OSX, ``.dll`` files on Windows, etc.) are linked against
1312 executables when the executable is run (or even lazily during
1313 execution), while static libraries (``.a`` files on Linux/OSX, ``.lib``
1314 files on Windows) get linked against the executable at compile time.
1316 Foreign libraries only work with GHC 7.8 and later.
1318 A typical stanza for a foreign library looks like
1322 foreign-library myforeignlib
1324 lib-version-info: 6:3:2
1328 mod-def-file: MyForeignLib.def
1330 other-modules: MyForeignLib.SomeModule
1331 MyForeignLib.SomeOtherModule
1332 build-depends: base >=4.7 && <4.9
1334 c-sources: csrc/MyForeignLibWrapper.c
1335 default-language: Haskell2010
1338 .. pkg-section:: foreign-library name
1340 :synopsis: Foreign library build information.
1342 Build information for `foreign libraries`_.
1344 .. pkg-field:: type: foreign library type
1346 Cabal recognizes ``native-static`` and ``native-shared`` here, although
1347 we currently only support building `native-shared` libraries.
1349 .. pkg-field:: options: foreign library option list
1351 Options for building the foreign library, typically specific to the
1352 specified type of foreign library. Currently we only support
1353 ``standalone`` here. A standalone dynamic library is one that does not
1354 have any dependencies on other (Haskell) shared libraries; without
1355 the ``standalone`` option the generated library would have dependencies
1356 on the Haskell runtime library (``libHSrts``), the base library
1357 (``libHSbase``), etc. Currently, ``standalone`` *must* be used on Windows
1358 and *must not* be used on any other platform.
1360 .. pkg-field:: mod-def-file: filename
1362 This option can only be used when creating dynamic Windows libraries
1363 (that is, when using ``native-shared`` and the ``os`` is ``Windows``). If
1364 used, it must be a path to a *module definition file*. The details of
1365 module definition files are beyond the scope of this document; see the
1366 `GHC <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/win32-dlls.html>`_
1367 manual for some details and some further pointers.
1369 .. pkg-field:: lib-version-info: current:revision:age
1371 This field is currently only used on Linux.
1373 This field specifies a Libtool-style version-info field that sets
1374 an appropriate ABI version for the foreign library. Note that the
1375 three numbers specified in this field do not directly specify the
1376 actual ABI version: ``6:3:2`` results in library version ``4.2.3``.
1378 With this field set, the SONAME of the library is set, and symlinks
1381 How you should bump this field on an ABI change depends on the
1382 breakage you introduce:
1384 - Programs using the previous version may use the new version as
1385 drop-in replacement, and programs using the new version can also
1386 work with the previous one. In other words, no recompiling nor
1387 relinking is needed. In this case, bump ``revision`` only, don't
1388 touch current nor age.
1389 - Programs using the previous version may use the new version as
1390 drop-in replacement, but programs using the new version may use
1391 APIs not present in the previous one. In other words, a program
1392 linking against the new version may fail with "unresolved
1393 symbols" if linking against the old version at runtime: set
1394 revision to 0, bump current and age.
1395 - Programs may need to be changed, recompiled, and relinked in
1396 order to use the new version. Bump current, set revision and age
1399 Also refer to the Libtool documentation on the version-info field.
1401 .. pkg-field:: lib-version-linux: version
1403 This field is only used on Linux.
1405 Specifies the library ABI version directly for foreign libraries
1406 built on Linux: so specifying ``4.2.3`` causes a library
1407 ``libfoo.so.4.2.3`` to be built with SONAME ``libfoo.so.4``, and
1408 appropriate symlinks ``libfoo.so.4`` and ``libfoo.so`` to be
1411 Note that typically foreign libraries should export a way to initialize
1412 and shutdown the Haskell runtime. In the example above, this is done by
1413 the ``csrc/MyForeignLibWrapper.c`` file, which might look something like
1420 HsBool myForeignLibInit(void){
1422 char *argv[] = { "+RTS", "-A32m", NULL };
1423 char **pargv = argv;
1425 // Initialize Haskell runtime
1426 hs_init(&argc, &pargv);
1428 // do any other initialization here and
1429 // return false if there was a problem
1430 return HS_BOOL_TRUE;
1433 void myForeignLibExit(void){
1437 With modern ghc regular libraries are installed in directories that contain
1438 package keys. This isn't usually a problem because the package gets registered
1439 in ghc's package DB and so we can figure out what the location of the library
1440 is. Foreign libraries however don't get registered, which means that we'd have
1441 to have a way of finding out where a platform library got installed (other than by
1442 searching the ``lib/`` directory). Instead, we install foreign libraries in
1447 .. pkg-section:: None
1449 The following fields may be optionally present in a library, executable,
1450 test suite or benchmark section, and give information for the building
1451 of the corresponding library or executable. See also the sections on
1452 `system-dependent parameters`_ and `configurations`_ for a way to supply
1453 system-dependent values for these fields.
1455 .. pkg-field:: build-depends: library list
1457 Declares the dependencies on *library* components required to build the
1458 current package component. See :pkg-field:`build-tool-depends` for declaring
1459 dependencies on build-time *tools*. Dependencies on libraries from another
1460 package should be annotated with a version constraint.
1464 A library is identified by the name of its package, optionally followed by a
1465 colon and the library's name (for example, ``my-package:my-library``). If a
1466 library name is omitted, the package's main library will be used. To refer
1467 expressly to a package's main library, use the name of the package as the
1468 library name (for example, ``my-package:my-package``). More than one library
1469 from the same package can be specified with the shorthand syntax
1470 ``my-package:{my-library1,my-library2}``.
1474 Before version 3.4 of the Cabal specification, from version 2.0, a
1475 private sublibrary (an internal library) was identified by only the name
1476 of the sublibrary. An internal library could shadow a dependency on the
1477 main library of another package, if the names clashed.
1479 See the section on :pkg-section:`library` for information about how a
1480 package can specify library components.
1482 **Version Constraints**
1484 Version constraints use the operators ``==, >=, >, <, <=`` and a
1485 version number. Multiple constraints can be combined using ``&&`` or
1490 Even though there is no ``/=`` operator, by combining operators we can
1491 skip over one or more versions, to skip a deprecated version or to skip
1492 versions that narrow the constraint solving more than we'd like.
1494 For example, the ``time =1.12.*`` series depends on ``base >=4.13 && <5``
1495 but ``time-1.12.3`` bumps the lower bound on base to ``>=4.14``. If we
1496 still want to compile with a ``ghc-8.8.*`` version of GHC that ships with
1497 ``base-4.13`` and with later GHC versions, then we can use ``time >=1.12
1498 && (time <1.12.3 || time >1.12.3)``.
1500 Hackage shows deprecated and preferred versions for packages, such as for
1501 `containers <https://hackage.haskell.org/package/containers/preferred>`_
1502 and `aeson <https://hackage.haskell.org/package/aeson/preferred>`_ for
1503 example. Deprecating package versions is not the same deprecating a
1504 package as a whole, for which hackage keeps a `deprecated packages list
1505 <https://hackage.haskell.org/packages/deprecated>`_.
1507 If no version constraint is specified, any version is assumed to be
1508 acceptable. For example:
1515 foo >= 1.2.3 && < 1.3,
1518 Dependencies like ``foo >= 1.2.3 && < 1.3`` turn out to be very
1519 common because it is recommended practice for package versions to
1520 correspond to API versions (see PVP_).
1522 Since Cabal 1.6, there is a special wildcard syntax to help with
1527 build-depends: foo ==1.2.*
1529 It is only syntactic sugar. It is exactly equivalent to
1530 ``foo >= 1.2 && < 1.3``.
1534 A potential pitfall of the wildcard syntax is that the
1535 constraint ``nats == 1.0.*`` doesn't match the release
1536 ``nats-1`` because the version ``1`` is lexicographically less
1537 than ``1.0``. This is not an issue with the caret-operator
1538 ``^>=`` described below.
1540 Starting with Cabal 2.0, there's a new version operator to express
1541 PVP_-style major upper bounds conveniently, and is inspired by similar
1542 syntactic sugar found in other language ecosystems where it's often
1543 called the "Caret" operator:
1551 This allows to assert the positive knowledge that this package is
1552 *known* to be semantically compatible with the releases
1553 ``foo-1.2.3.4`` and ``bar-1`` respectively. The information
1554 encoded via such ``^>=``-assertions is used by the cabal solver to
1555 infer version constraints describing semantically compatible
1556 version ranges according to the PVP_ contract (see below).
1558 Another way to say this is that ``foo < 1.3`` expresses *negative*
1559 information, i.e. "``foo-1.3`` or ``foo-1.4.2`` will *not* be
1560 compatible"; whereas ``foo ^>= 1.2.3.4`` asserts the *positive*
1561 information that "``foo-1.2.3.4`` is *known* to be compatible" and (in
1562 the absence of additional information) according to the PVP_
1563 contract we can (positively) infer right away that all versions
1564 satisfying ``foo >= 1.2.3.4 && < 1.3`` will be compatible as well.
1568 More generally, the PVP_ contract implies that we can safely
1569 relax the lower bound to ``>= 1.2``, because if we know that
1570 ``foo-1.2.3.4`` is semantically compatible, then so is
1571 ``foo-1.2`` (if it typechecks). But we'd need to perform
1572 additional static analysis (i.e. perform typechecking) in order
1573 to know if our package in the role of an API consumer will
1574 successfully typecheck against the dependency ``foo-1.2``. But
1575 since we cannot do this analysis during constraint solving and
1576 to keep things simple, we pragmatically use ``foo >= 1.2.3.4``
1577 as the initially inferred approximation for the lower bound
1578 resulting from the assertion ``foo ^>= 1.2.3.4``. If further
1579 evidence becomes available that e.g. ``foo-1.2`` typechecks,
1580 one can simply revise the dependency specification to include
1581 the assertion ``foo ^>= 1.2``.
1583 The subtle but important difference in signaling allows tooling to
1584 treat explicitly expressed ``<``-style constraints and inferred
1585 (``^>=``-style) upper bounds differently. For instance,
1586 :cfg-field:`allow-newer`'s ``^``-modifier allows to relax only
1587 ``^>=``-style bounds while leaving explicitly stated
1588 ``<``-constraints unaffected.
1590 Ignoring the signaling intent, the default syntactic desugaring rules are
1592 - ``^>= x`` == ``>= x && < x.1``
1593 - ``^>= x.y`` == ``>= x.y && < x.(y+1)``
1594 - ``^>= x.y.z`` == ``>= x.y.z && < x.(y+1)``
1595 - ``^>= x.y.z.u`` == ``>= x.y.z.u && < x.(y+1)``
1600 One might expect the desugaring to truncate all version
1601 components below (and including) the patch-level, i.e.
1602 ``^>= x.y.z.u`` == ``>= x.y.z && < x.(y+1)``,
1603 as the major and minor version components alone are supposed to
1604 uniquely identify the API according to the PVP_. However, by
1605 designing ``^>=`` to be closer to the ``>=`` operator, we avoid
1606 the potentially confusing effect of ``^>=`` being more liberal
1607 than ``>=`` in the presence of patch-level versions.
1609 Consequently, the example declaration above is equivalent to
1614 foo >= 1.2.3.4 && < 1.3,
1619 Prior to Cabal 1.8, ``build-depends`` specified in each
1620 section were global to all sections. This was unintentional, but
1621 some packages were written to depend on it, so if you need your
1622 :pkg-field:`build-depends` to be local to each section, you must specify
1623 at least ``Cabal-Version: >= 1.8`` in your ``.cabal`` file.
1627 Cabal 1.20 experimentally supported module thinning and
1628 renaming in ``build-depends``; however, this support has since been
1629 removed and should not be used.
1631 Starting with Cabal 3.0, a set notation for the ``==`` and ``^>=`` operator
1632 is available. For instance,
1636 tested-with: GHC == 8.6.3, GHC == 8.4.4, GHC == 8.2.2, GHC == 8.0.2,
1637 GHC == 7.10.3, GHC == 7.8.4, GHC == 7.6.3, GHC == 7.4.2
1639 build-depends: network ^>= 2.6.3.6 || ^>= 2.7.0.2 || ^>= 2.8.0.0 || ^>= 3.0.1.0
1641 can be then written in a more convenient and concise form
1645 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 }
1647 build-depends: network ^>= { 2.6.3.6, 2.7.0.2, 2.8.0.0, 3.0.1.0 }
1650 .. pkg-field:: other-modules: identifier list
1652 A list of modules used by the component but not exposed to users.
1653 For a library component, these would be hidden modules of the
1654 library. For an executable, these would be auxiliary modules to be
1655 linked with the file named in the ``main-is`` field.
1659 Every module in the package *must* be listed in one of
1660 :pkg-field:`other-modules`, :pkg-field:`library:exposed-modules` or
1661 :pkg-field:`executable:main-is` fields.
1663 .. pkg-field:: hs-source-dir: directory list
1669 Root directories for the module hierarchy.
1671 Deprecated in favor of :pkg-field:`hs-source-dirs`.
1673 .. pkg-field:: hs-source-dirs: directory list
1677 Root directories for the module hierarchy.
1681 Components can share source directories but modules found there will be
1682 recompiled even if other components already built them, i.e., if a
1683 library and an executable share a source directory and the executable
1684 depends on the library and imports its ``Foo`` module, ``Foo`` will be
1685 compiled twice, once as part of the library and again for the executable.
1687 .. pkg-field:: default-extensions: identifier list
1690 A list of Haskell extensions used by every module. These determine
1691 corresponding compiler options enabled for all files. Extension
1692 names are the constructors of the
1693 `Extension <https://hackage.haskell.org/package/Cabal-syntax/docs/Language-Haskell-Extension.html#t:Extension>`__
1694 type. For example, ``CPP`` specifies that Haskell source files are
1695 to be preprocessed with a C preprocessor.
1697 .. pkg-field:: other-extensions: identifier list
1700 A list of Haskell extensions used by some (but not necessarily all)
1701 modules. From GHC version 6.6 onward, these may be specified by
1702 placing a ``LANGUAGE`` pragma in the source files affected e.g.
1704 .. code-block:: haskell
1706 {-# LANGUAGE CPP, MultiParamTypeClasses #-}
1708 In Cabal-1.24 the dependency solver will use this and
1709 :pkg-field:`default-extensions` information. Cabal prior to 1.24 will abort
1710 compilation if the current compiler doesn't provide the extensions.
1712 If you use some extensions conditionally, using CPP or conditional
1713 module lists, it is good to replicate the condition in
1714 :pkg-field:`other-extensions` declarations:
1718 other-extensions: CPP
1720 other-extensions: PolyKinds
1722 You could also omit the conditionally used extensions, as they are
1723 for information only, but it is recommended to replicate them in
1724 :pkg-field:`other-extensions` declarations.
1726 .. pkg-field:: default-language: identifier
1729 Specifies a language standard or a group of language extensions to be activated for the project. In the case of GHC, `see here for details <https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/control.html#controlling-extensions>`__.
1731 The possible values are:
1733 - ``GHC2021`` (only available for GHC version newer than ``9.2``)
1737 .. pkg-field:: other-languages: identifier
1742 .. pkg-field:: extensions: identifier list
1746 Deprecated in favor of :pkg-field:`default-extensions`.
1748 .. pkg-field:: build-tool-depends: package:executable list
1751 A list of Haskell executables needed to build this component. Executables are provided
1752 during the whole duration of the component, so this field can be used for executables
1753 needed during :pkg-section:`test-suite` as well.
1755 Each is specified by the package containing the executable and the name of the
1756 executable itself, separated by a colon, and optionally followed by a version bound.
1758 All executables defined in the given Cabal file are termed as *internal* dependencies
1759 as opposed to the rest which are *external* dependencies.
1761 Each of the two is handled differently:
1763 1. External dependencies can (and should) contain a version bound like conventional
1764 :pkg-field:`build-depends` dependencies.
1765 2. Internal dependencies should not contain a version bound, as they will be always
1766 resolved within the same configuration of the package in the build plan.
1767 Specifically, version bounds that include the package's version will be warned for
1768 being extraneous, and version bounds that exclude the package's version will raise
1769 an error for being impossible to follow.
1771 For example (1) using a test-suite to make sure README.md Haskell snippets are tested using
1772 `markdown-unlit <http://hackage.haskell.org/package/markdown-unlit>`__:
1776 build-tool-depends: markdown-unlit:markdown-unlit >= 0.5.0 && < 0.6
1778 For example (2) using a test-suite to test executable behaviour in the same package:
1782 build-tool-depends: mypackage:executable
1784 Cabal tries to make sure that all specified programs are atomically built and prepended
1785 on the ``PATH`` shell variable before building the component in question, but can only do
1786 so for Nix-style builds. Specifically:
1788 a) For Nix-style local builds, both internal and external dependencies.
1789 b) For old-style builds, only for internal dependencies [#old-style-build-tool-depends]_.
1790 It's up to the user to provide needed executables in this case under ``PATH``.
1795 :pkg-field:`build-tool-depends` was added in Cabal 2.0, and it will
1796 be ignored (with a warning) with old versions of Cabal. See
1797 :pkg-field:`build-tools` for more information about backwards
1800 .. pkg-field:: build-tools: program list
1804 Deprecated in favor of :pkg-field:`build-tool-depends`, but :ref:`see below for backwards compatibility information <buildtoolsbc>`.
1806 A list of Haskell programs needed to build this component.
1807 Each may be followed by an optional version bound.
1808 Confusingly, each program in the list either refer to one of three things:
1810 1. Another executables in the same package (supported since Cabal 1.12)
1812 2. Tool name contained in Cabal's :ref:`hard-coded set of common tools <buildtoolsmap>`
1814 3. A pre-built executable that should already be on the ``PATH``
1815 (supported since Cabal 2.0)
1817 These cases are listed in order of priority:
1818 an executable in the package will override any of the hard-coded packages with the same name,
1819 and a hard-coded package will override any executable on the ``PATH``.
1821 In the first two cases, the list entry is desugared into a :pkg-field:`build-tool-depends` entry.
1822 In the first case, the entry is desugared into a :pkg-field:`build-tool-depends` entry by prefixing with ``$pkg:``.
1823 In the second case, it is desugared by looking up the package and executable name in a hard-coded table.
1824 In either case, the optional version bound is passed through unchanged.
1825 Refer to the documentation for :pkg-field:`build-tool-depends` to understand the desugared field's meaning, along with restrictions on version bounds.
1829 **Backward Compatibility**
1831 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.
1832 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.
1833 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``.
1837 **Set of Known Tool Names**
1839 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::
1841 build-tools: alex >= 3.2.1 && < 3.3, happy >= 1.19.5 && < 1.20
1843 is simply desugared into the equivalent specification::
1845 build-tool-depends: alex:alex >= 3.2.1 && < 3.3, happy:happy >= 1.19.5 && < 1.20
1847 +--------------------------+-----------------------------------+-----------------+
1848 | :pkg-field:`build-tools` | desugared | Note |
1849 | identifier | :pkg-field:`build-tool-depends` | |
1851 +==========================+===================================+=================+
1852 | ``alex`` | ``alex:alex`` | |
1853 +--------------------------+-----------------------------------+-----------------+
1854 | ``c2hs`` | ``c2hs:c2hs`` | |
1855 +--------------------------+-----------------------------------+-----------------+
1856 | ``cpphs`` | ``cpphs:cpphs`` | |
1857 +--------------------------+-----------------------------------+-----------------+
1858 | ``greencard`` | ``greencard:greencard`` | |
1859 +--------------------------+-----------------------------------+-----------------+
1860 | ``haddock`` | ``haddock:haddock`` | |
1861 +--------------------------+-----------------------------------+-----------------+
1862 | ``happy`` | ``happy:happy`` | |
1863 +--------------------------+-----------------------------------+-----------------+
1864 | ``hsc2hs`` | ``hsc2hs:hsc2hs`` | |
1865 +--------------------------+-----------------------------------+-----------------+
1866 | ``hscolour`` | ``hscolour:hscolour`` | |
1867 +--------------------------+-----------------------------------+-----------------+
1868 | ``hspec-discover`` | ``hspec-discover:hspec-discover`` | since Cabal 2.0 |
1869 +--------------------------+-----------------------------------+-----------------+
1871 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.
1873 .. pkg-field:: buildable: boolean
1877 Is the component buildable? Like some of the other fields below,
1878 this field is more useful with the slightly more elaborate form of
1879 the simple build infrastructure described in the section on
1880 `system-dependent parameters`_.
1882 .. pkg-field:: ghc-options: token list
1884 Additional options for GHC. You can often achieve the same effect
1885 using the :pkg-field:`default-extensions` field, which is preferred.
1887 Options required only by one module may be specified by placing an
1888 ``OPTIONS_GHC`` pragma in the source file affected.
1890 As with many other fields, whitespace can be escaped by using
1891 Haskell string syntax. Example:
1892 ``ghc-options: -Wcompat "-with-rtsopts=-T -I1" -Wall``.
1894 .. pkg-field:: ghc-prof-options: token list
1896 Additional options for GHC when the package is built with profiling
1899 Note that as of Cabal-1.24, the default profiling detail level
1900 defaults to ``exported-functions`` for libraries and
1901 ``toplevel-functions`` for executables. For GHC these correspond to
1902 the flags ``-fprof-auto-exported`` and ``-fprof-auto-top``. Prior to
1903 Cabal-1.24 the level defaulted to ``none``. These levels can be
1904 adjusted by the person building the package with the
1905 ``--profiling-detail`` and ``--library-profiling-detail`` flags.
1907 It is typically better for the person building the package to pick
1908 the profiling detail level rather than for the package author. So
1909 unless you have special needs it is probably better not to specify
1910 any of the GHC ``-fprof-auto*`` flags here. However if you wish to
1911 override the profiling detail level, you can do so using the
1912 :pkg-field:`ghc-prof-options` field: use ``-fno-prof-auto`` or one of the
1913 other ``-fprof-auto*`` flags.
1915 .. pkg-field:: ghc-shared-options: token list
1917 Additional options for GHC when the package is built as shared
1918 library. The options specified via this field are combined with the
1919 ones specified via :pkg-field:`ghc-options`, and are passed to GHC during
1920 both the compile and link phases.
1922 .. pkg-field:: ghcjs-options: token list
1924 Like :pkg-field:`ghc-options` but applies to GHCJS
1926 .. pkg-field:: ghcjs-prof-options: token list
1928 Like :pkg-field:`ghc-prof-options` but applies to GHCJS
1930 .. pkg-field:: ghcjs-shared-options: token list
1932 Like :pkg-field:`ghc-shared-options` but applies to GHCJS
1934 .. pkg-field:: includes: filename list
1936 A list of header files to be included in any compilations via C.
1937 This field applies to both header files that are already installed
1938 on the system and to those coming with the package to be installed.
1939 The former files should be found in absolute paths, while the latter
1940 files should be found in paths relative to the top of the source
1941 tree or relative to one of the directories listed in
1942 :pkg-field:`include-dirs`.
1944 These files typically contain function prototypes for foreign
1945 imports used by the package. This is in contrast to
1946 :pkg-field:`install-includes`, which lists header files that are intended
1947 to be exposed to other packages that transitively depend on this
1950 .. pkg-field:: install-includes: filename list
1952 A list of header files from this package to be installed into
1953 ``$libdir/includes`` when the package is installed. Files listed in
1954 :pkg-field:`install-includes` should be found in relative to the top of the
1955 source tree or relative to one of the directories listed in
1956 :pkg-field:`include-dirs`.
1958 :pkg-field:`install-includes` is typically used to name header files that
1959 contain prototypes for foreign imports used in Haskell code in this
1960 package, for which the C implementations are also provided with the
1961 package. For example, here is a ``.cabal`` file for a hypothetical
1962 ``bindings-clib`` package that bundles the C source code for ``clib``::
1966 install-includes: clib.h
1968 Now any package that depends (directly or transitively) on the
1969 ``bindings-clib`` library can use ``clib.h``.
1971 Note that in order for files listed in :pkg-field:`install-includes` to be
1972 usable when compiling the package itself, they need to be listed in
1973 the :pkg-field:`includes` field as well.
1975 .. pkg-field:: include-dirs: directory list
1977 A list of directories to search for header files, when preprocessing
1978 with ``c2hs``, ``hsc2hs``, ``cpphs`` or the C preprocessor, and also
1979 when compiling via C. Directories can be absolute paths (e.g., for
1980 system directories) or paths that are relative to the top of the
1981 source tree. Cabal looks in these directories when attempting to
1982 locate files listed in :pkg-field:`includes` and
1983 :pkg-field:`install-includes`.
1985 .. pkg-field:: c-sources: filename list
1987 A list of C source files to be compiled and linked with the Haskell
1990 .. pkg-field:: cxx-sources: filename list
1993 A list of C++ source files to be compiled and linked with the Haskell
1994 files. Useful for segregating C and C++ sources when supplying different
1995 command-line arguments to the compiler via the :pkg-field:`cc-options`
1996 and the :pkg-field:`cxx-options` fields. The files listed in the
1997 :pkg-field:`cxx-sources` can reference files listed in the
1998 :pkg-field:`c-sources` field and vice-versa. The object files will be linked
2001 .. pkg-field:: asm-sources: filename list
2004 A list of assembly source files to be compiled and linked with the
2007 .. pkg-field:: cmm-sources: filename list
2010 A list of C-- source files to be compiled and linked with the Haskell
2013 .. pkg-field:: js-sources: filename list
2015 A list of JavaScript source files to be linked with the Haskell
2016 files (only for JavaScript targets).
2018 .. pkg-field:: extra-libraries: token list
2020 A list of extra libraries to link with (when not linking fully static
2023 .. pkg-field:: extra-libraries-static: token list
2025 A list of extra libraries to link with (when linking fully static
2028 .. pkg-field:: extra-ghci-libraries: token list
2030 A list of extra libraries to be used instead of 'extra-libraries'
2031 when the package is loaded with GHCi.
2033 .. pkg-field:: extra-bundled-libraries: token list
2036 A list of libraries that are supposed to be copied from the build
2037 directory alongside the produced Haskell libraries. Note that you
2038 are under the obligation to produce those libraries in the build
2039 directory (e.g. via a custom setup). Libraries listed here will
2040 be included when ``copy``-ing packages and be listed in the
2041 ``hs-libraries`` of the package configuration in the package database.
2042 Library names must either be prefixed with "HS" or "C" and corresponding
2043 library file names must match:
2045 - Libraries with name "HS<library-name>":
2046 - `libHS<library-name>.a`
2047 - `libHS<library-name>-ghc<ghc-flavour><ghc-version>.<dyn-library-extension>*`
2048 - Libraries with name "C<library-name>":
2049 - `libC<library-name>.a`
2050 - `lib<library-name>.<dyn-library-extension>*`
2052 .. pkg-field:: extra-lib-dirs: directory list
2054 A list of directories to search for libraries (when not linking fully static
2057 .. pkg-field:: extra-lib-dirs-static: directory list
2059 A list of directories to search for libraries (when linking fully static
2062 .. pkg-field:: extra-library-flavours: notsure
2066 .. pkg-field:: extra-dynamic-library-flavours: notsure
2070 .. pkg-field:: cc-options: token list
2072 Command-line arguments to be passed to the C compiler. Since the
2073 arguments are compiler-dependent, this field is more useful with the
2074 setup described in the section on `system-dependent parameters`_.
2076 .. pkg-field:: cpp-options: token list
2078 Command-line arguments for pre-processing Haskell code. Applies to
2079 Haskell source and other pre-processed Haskell source like .hsc
2080 .chs. Does not apply to C code, that's what cc-options is for.
2082 .. pkg-field:: cxx-options: token list
2085 Command-line arguments to be passed to the compiler when compiling
2086 C++ code. The C++ sources to which these command-line arguments
2087 should be applied can be specified with the :pkg-field:`cxx-sources`
2088 field. Command-line options for C and C++ can be passed separately to
2089 the compiler when compiling both C and C++ sources by segregating the C
2090 and C++ sources with the :pkg-field:`c-sources` and
2091 :pkg-field:`cxx-sources` fields respectively, and providing different
2092 command-line arguments with the :pkg-field:`cc-options` and the
2093 :pkg-field:`cxx-options` fields.
2095 .. pkg-field:: cmm-options: token list
2098 Command-line arguments to be passed to the compiler when compiling
2099 C-- code. See also :pkg-field:`cmm-sources`.
2101 .. pkg-field:: asm-options: token list
2104 Command-line arguments to be passed to the assembler when compiling
2105 assembler code. See also :pkg-field:`asm-sources`.
2107 .. pkg-field:: ld-options: token list
2109 Command-line arguments to be passed to the linker. Since the
2110 arguments are compiler-dependent, this field is more useful with the
2111 setup described in the section on `system-dependent parameters`_.
2113 .. pkg-field:: hsc2hs-options: token list
2116 Command-line arguments to be passed to ``hsc2hs``.
2118 .. pkg-field:: pkgconfig-depends: package list
2121 `pkg-config <http://www.freedesktop.org/wiki/Software/pkg-config/>`__
2122 packages, needed to build this package. They can be annotated with
2123 versions, e.g. ``gtk+-2.0 >= 2.10, cairo >= 1.0``. If no version
2124 constraint is specified, any version is assumed to be acceptable.
2125 Cabal uses ``pkg-config`` to find if the packages are available on
2126 the system and to find the extra compilation and linker options
2127 needed to use the packages.
2129 If you need to bind to a C library that supports ``pkg-config`` then
2130 it is much preferable to use this field rather than hard code options
2131 into the other fields. ``pkg-config --list-all`` will show you all
2132 supported libraries. Depending on your system you may need to adjust
2133 ``PKG_CONFIG_PATH``.
2135 .. pkg-field:: frameworks: token list
2137 On Darwin/MacOS X, a list of frameworks to link to. See Apple's
2138 developer documentation for more details on frameworks. This entry
2139 is ignored on all other platforms.
2141 .. pkg-field:: extra-framework-dirs: directory list
2144 On Darwin/MacOS X, a list of directories to search for frameworks.
2145 This entry is ignored on all other platforms.
2147 .. pkg-field:: mixins: mixin list
2150 Supported only in GHC 8.2 and later. A list of packages mentioned in the
2151 :pkg-field:`build-depends` field, each optionally accompanied by a list of
2152 module and module signature renamings. A valid mixin obeys the
2157 Mixin ::= PackageName IncludeRenaming
2158 IncludeRenaming ::= ModuleRenaming { "requires" ModuleRenaming }
2161 | "(" Renaming "," ... "," Renaming ")"
2162 | "hiding" "(" ModuleName "," ... "," ModuleName ")"
2165 | ModuleName "as" ModuleName
2167 The simplest mixin syntax is simply the name of a package mentioned in the
2168 :pkg-field:`build-depends` field. For example:
2178 But this doesn't have any effect. More interesting is to use the mixin
2179 entry to rename one or more modules from the package, like this:
2185 foo (Foo.Bar as AnotherFoo.Bar, Foo.Baz as AnotherFoo.Baz)
2187 Note that renaming a module like this will hide all the modules
2188 that are not explicitly named.
2190 Modules can also be hidden:
2196 foo hiding (Foo.Bar)
2198 Hiding modules exposes everything that is not explicitly hidden.
2202 Cabal files with :pkg-field:`cabal-version` < 3.0 suffer from an
2203 infelicity in how the entries of :pkg-field:`mixins` are parsed: an
2204 entry will fail to parse if the provided renaming clause has whitespace
2205 after the opening parenthesis.
2207 See issues :issue:`5150`, :issue:`4864`, and :issue:`5293`.
2209 There can be multiple mixin entries for a given package, in effect creating
2210 multiple copies of the dependency:
2216 foo (Foo.Bar as AnotherFoo.Bar, Foo.Baz as AnotherFoo.Baz),
2217 foo (Foo.Bar as YetAnotherFoo.Bar)
2219 The ``requires`` clause is used to rename the module signatures required by
2226 foo (Foo.Bar as AnotherFoo.Bar) requires (Foo.SomeSig as AnotherFoo.SomeSig)
2228 Signature-only packages don't have any modules, so only the signatures can
2229 be renamed, with the following syntax:
2235 sigonly requires (SigOnly.SomeSig as AnotherSigOnly.SomeSig)
2237 See the :pkg-field:`library:signatures` field for more details.
2239 Mixin packages are part of the :ref:`Backpack` extension to the
2240 Haskell module system.
2242 The matching of the module signatures required by a
2243 :pkg-field:`build-depends` dependency with the implementation modules
2244 present in another dependency is triggered by a coincidence of names. When
2245 the names of the signature and of the implementation are already the same,
2246 the matching is automatic. But when the names don't coincide, or we want to
2247 instantiate a signature in two different ways, adding mixin entries that
2248 perform renamings becomes necessary.
2252 :ref:`Backpack` has the limitation that implementation modules that instantiate
2253 signatures required by a :pkg-field:`build-depends` dependency can't
2254 reside in the same component that has the dependency. They must reside
2255 in a different package dependency, or at least in a separate internal
2261 Library and executable sections may include conditional blocks, which
2262 test for various system parameters and configuration flags. The flags
2263 mechanism is rather generic, but most of the time a flag represents
2264 certain feature, that can be switched on or off by the package user.
2265 Here is an example package description file using configurations:
2267 Example: A package containing a library and executable programs
2268 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2275 License: BSD-3-Clause
2277 Synopsis: Test package to test configurations
2282 Description: Enable debug support
2287 Description: Include API for web frontend.
2292 description: Whether to build against @directory >= 1.2@
2293 -- This is an automatic flag which the solver will
2294 -- assign automatically while searching for a solution
2297 Build-Depends: base >= 4.2 && < 4.9
2298 Exposed-Modules: Testing.Test1
2299 Default-Extensions: CPP
2300 Default-Language: Haskell2010
2304 CPP-Options: -DDEBUG
2306 CC-Options: "-DDEBUG"
2308 CC-Options: "-DNDEBUG"
2310 if flag(WebFrontend)
2311 Build-Depends: cgi >= 0.42 && < 0.44
2312 Other-Modules: Testing.WebStuff
2313 CPP-Options: -DWEBFRONTEND
2315 if flag(NewDirectory)
2316 build-depends: directory >= 1.2 && < 1.4
2317 Build-Depends: time >= 1.0 && < 1.9
2319 build-depends: directory == 1.1.*
2320 Build-Depends: old-time >= 1.0 && < 1.2
2324 Other-Modules: Testing.Test1
2325 Build-Depends: base >= 4.2 && < 4.9
2326 Default-Language: Haskell2010
2329 CC-Options: "-DDEBUG"
2330 CPP-Options: -DDEBUG
2335 Flags, conditionals, library and executable sections use layout to
2336 indicate structure. This is very similar to the Haskell layout rule.
2337 Entries in a section have to all be indented to the same level which
2338 must be more than the section header. Tabs are not allowed to be used
2341 As an alternative to using layout you can also use explicit braces
2342 ``{}``. In this case the indentation of entries in a section does not
2343 matter, though different fields within a block must be on different
2344 lines. Here is a bit of the above example again, using braces:
2346 Example: Using explicit braces rather than indentation for layout
2347 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2354 License: BSD-3-Clause
2356 Synopsis: Test package to test configurations
2361 Description: Enable debug support
2367 Build-Depends: base >= 4.2 && < 4.9
2368 Exposed-Modules: Testing.Test1
2369 Default-Extensions: CPP
2370 Default-language: Haskell2010
2372 CPP-Options: -DDEBUG
2374 CC-Options: "-DDEBUG"
2376 CC-Options: "-DNDEBUG"
2384 .. pkg-section:: flag name
2385 :synopsis: Flag declaration.
2387 Flag section declares a flag which can be used in `conditional blocks`_.
2389 Flag names are case-insensitive and must match ``[[:alnum:]_][[:alnum:]_-]*``
2390 regular expression, or expressed as ABNF_:
2392 .. code-block:: abnf
2394 flag-name = (UALNUM / "_") *(UALNUM / "_" / "-")
2396 UALNUM = UALPHA / DIGIT
2397 UALPHA = ... ; set of alphabetic Unicode code-points
2401 Hackage accepts ASCII-only flags, ``[a-zA-Z0-9_][a-zA-Z0-9_-]*`` regexp.
2403 .. pkg-field:: description: freeform
2405 The description of this flag.
2407 .. pkg-field:: default: boolean
2411 The default value of this flag.
2415 This value may be :ref:`overridden in several
2416 ways <controlling flag assignments>`. The
2417 rationale for having flags default to True is that users usually
2418 want new features as soon as they are available. Flags representing
2419 features that are not (yet) recommended for most users (such as
2420 experimental features or debugging support) should therefore
2421 explicitly override the default to False.
2423 .. pkg-field:: manual: boolean
2428 By default, Cabal will first try to satisfy dependencies with the
2429 default flag value and then, if that is not possible, with the
2430 negated value. However, if the flag is manual, then the default
2431 value (which can be overridden by commandline flags) will be used.
2433 .. _conditional-blocks:
2438 Conditional blocks may appear anywhere inside a component or common
2439 section. They have to follow rather strict formatting rules. Conditional
2440 blocks must always be of the shape
2445 property-descriptions-or-conditionals
2452 property-descriptions-or-conditionals
2454 property-descriptions-or-conditionals
2456 Note that the ``if`` and the condition have to be all on the same line.
2458 Since Cabal 2.2 conditional blocks support ``elif`` construct.
2463 property-descriptions-or-conditionals
2465 property-descriptions-or-conditionals
2467 property-descriptions-or-conditionals
2474 Conditions can be formed using boolean tests and the boolean operators
2475 ``||`` (disjunction / logical "or"), ``&&`` (conjunction / logical
2476 "and"), or ``!`` (negation / logical "not"). The unary ``!`` takes
2477 highest precedence, ``||`` takes lowest. Precedence levels may be
2478 overridden through the use of parentheses. For example,
2479 ``os(darwin) && !arch(i386) || os(freebsd)`` is equivalent to
2480 ``(os(darwin) && !(arch(i386))) || os(freebsd)``.
2482 The following tests are currently supported.
2485 Tests if the current operating system is *name*. The argument is
2486 tested against ``System.Info.os`` on the target system. There is
2487 unfortunately some disagreement between Haskell implementations
2488 about the standard values of ``System.Info.os``. Cabal canonicalises
2489 it so that in particular ``os(windows)`` works on all
2490 implementations. If the canonicalised os names match, this test
2491 evaluates to true, otherwise false. The match is case-insensitive.
2492 :samp:`arch({name})`
2493 Tests if the current architecture is *name*. *name* should be the name of
2494 one of the nullary constructors of ``Distribution.System.Arch`` (e.g.
2495 ``x86_64``, ``aarch64`` or ``i386``), otherwise it will be treated as an
2496 'other architecture' of the given *name*. It will be compared with
2497 ``Distribution.System.buildArch``, which is derived from
2498 ``System.Info.arch`` (certain architectures are treated as synonymous; e.g.
2499 ``aarch64`` / ``arm64`` or ``powerpc64`` / ``powerpc64le`` are not
2500 distinguished). For a match, this test evaluates to true, otherwise false.
2501 The match is case-insensitive.
2502 :samp:`impl({compiler})`
2503 Tests for the configured Haskell implementation. An optional version
2504 constraint may be specified (for example ``impl(ghc >= 6.6.1)``). If
2505 the configured implementation is of the right type and matches the
2506 version constraint, then this evaluates to true, otherwise false.
2507 The match is case-insensitive.
2509 Note that including a version constraint in an ``impl`` test causes
2510 it to check for two properties:
2512 - The current compiler has the specified name, and
2514 - The compiler's version satisfied the specified version constraint
2516 As a result, ``!impl(ghc >= x.y.z)`` is not entirely equivalent to
2517 ``impl(ghc < x.y.z)``. The test ``!impl(ghc >= x.y.z)`` checks that:
2519 - The current compiler is not GHC, or
2521 - The version of GHC is earlier than version x.y.z.
2523 :samp:`flag({name})`
2524 Evaluates to the current assignment of the flag of the given name.
2525 Flag names are case insensitive. Testing for flags that have not
2526 been introduced with a flag section is an error.
2528 Constant value true.
2530 Constant value false.
2532 .. _resolution-of-conditions-and-flags:
2534 Resolution of Conditions and Flags
2535 """"""""""""""""""""""""""""""""""
2537 If a package descriptions specifies configuration flags the package user
2538 can :ref:`control these in several ways <controlling flag assignments>`. If the
2539 user does not fix the value of a flag, Cabal will try to find a flag
2540 assignment in the following way.
2542 - For each flag specified, it will assign its default value, evaluate
2543 all conditions with this flag assignment, and check if all
2544 dependencies can be satisfied. If this check succeeded, the package
2545 will be configured with those flag assignments.
2547 - If dependencies were missing, the last flag (as by the order in which
2548 the flags were introduced in the package description) is tried with
2549 its alternative value and so on. This continues until either an
2550 assignment is found where all dependencies can be satisfied, or all
2551 possible flag assignments have been tried.
2553 To put it another way, Cabal does a complete backtracking search to find
2554 a satisfiable package configuration. It is only the dependencies
2555 specified in the :pkg-field:`build-depends` field in conditional blocks that
2556 determine if a particular flag assignment is satisfiable
2557 (:pkg-field:`build-tools` are not considered). The order of the declaration and
2558 the default value of the flags determines the search order. Flags
2559 overridden on the command line fix the assignment of that flag, so no
2560 backtracking will be tried for that flag.
2562 If no suitable flag assignment could be found, the configuration phase
2563 will fail and a list of missing dependencies will be printed. Note that
2564 this resolution process is exponential in the worst case (i.e., in the
2565 case where dependencies cannot be satisfied). There are some
2566 optimizations applied internally, but the overall complexity remains
2569 Meaning of field values when using conditionals
2570 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2572 During the configuration phase, a flag assignment is chosen, all
2573 conditionals are evaluated, and the package description is combined into
2574 a flat package descriptions. If the same field is declared both inside
2575 a conditional and outside then they are combined using the following rules.
2577 - Boolean fields are combined using conjunction (logical "and").
2579 - List fields are combined by appending the inner items to the outer
2584 other-extensions: CPP
2586 other-extensions: MultiParamTypeClasses
2588 when compiled using GHC will be combined to
2592 other-extensions: CPP, MultiParamTypeClasses
2594 Similarly, if two conditional sections appear at the same nesting
2595 level, properties specified in the latter will come after properties
2596 specified in the former.
2598 - All other fields must not be specified in ambiguous ways. For example
2603 if flag(useothermain)
2604 Main-is: OtherMain.hs
2606 will lead to an error. Instead use
2610 if flag(useothermain)
2611 Main-is: OtherMain.hs
2620 .. pkg-section:: common name
2622 :synopsis: Common build info section
2624 Starting with Cabal-2.2 it's possible to use common build info stanzas.
2629 build-depends: base ^>= 4.11
2633 build-depends: tasty ^>= 0.12.0.1
2637 exposed-modules: Foo
2638 default-language: Haskell2010
2641 import: deps, test-deps
2642 type: exitcode-stdio-1.0
2645 default-language: Haskell2010
2647 - You can use `build information`_ fields in common stanzas.
2649 - Common stanzas must be defined before use.
2651 - Common stanzas can import other common stanzas.
2653 - You can import multiple stanzas at once. Stanza names must be separated by commas.
2655 - ``import`` must be the first field in a section. Since Cabal 3.0 imports
2656 are also allowed inside conditionals.
2660 The name `import` was chosen, because there is ``includes`` field.
2662 .. pkg-section:: None
2664 .. pkg-field:: import: token-list
2671 .. pkg-section:: source-repository
2674 It is often useful to be able to specify a source revision control
2675 repository for a package. Cabal lets you specify this information in
2676 a relatively structured form which enables other tools to interpret and
2677 make effective use of the information. For example the information
2678 should be sufficient for an automatic tool to checkout the sources.
2680 Cabal supports specifying different information for various common
2681 source control systems. Obviously not all automated tools will support
2682 all source control systems.
2684 Cabal supports specifying repositories for different use cases. By
2685 declaring which case we mean automated tools can be more useful. There
2686 are currently two kinds defined:
2688 - The ``head`` kind refers to the latest development branch of the
2689 package. This may be used for example to track activity of a project
2690 or as an indication to outside developers what sources to get for
2691 making new contributions.
2693 - The ``this`` kind refers to the branch and tag of a repository that
2694 contains the sources for this version or release of a package. For
2695 most source control systems this involves specifying a tag, id or
2696 hash of some form and perhaps a branch. The purpose is to be able to
2697 reconstruct the sources corresponding to a particular package
2698 version. This might be used to indicate what sources to get if
2699 someone needs to fix a bug in an older branch that is no longer an
2702 You can specify one kind or the other or both. As an example here are
2703 the repositories for the Cabal library. Note that the ``this`` kind of
2704 repository specifies a tag.
2708 source-repository head
2710 location: https://github.com/haskell/cabal
2712 source-repository this
2714 location: https://github.com/haskell/cabal
2717 The exact fields are as follows:
2719 .. pkg-field:: type: token
2721 The name of the source control system used for this repository. The
2722 currently recognised types are:
2728 - ``mercurial`` (or alias ``hg``)
2729 - ``bazaar`` (or alias ``bzr``)
2733 This field is required.
2735 .. pkg-field:: location: URL
2737 The location of the repository. The exact form of this field depends
2738 on the repository type. For example:
2740 - for darcs: ``http://code.haskell.org/foo/``
2741 - for git: ``git://github.com/foo/bar.git``
2742 - for CVS: ``anoncvs@cvs.foo.org:/cvs``
2744 This field is required.
2746 .. pkg-field:: module: token
2748 CVS requires a named module, as each CVS server can host multiple
2751 This field is required for the CVS repository type and should not be
2754 .. pkg-field:: branch: token
2756 Many source control systems support the notion of a branch, as a
2757 distinct concept from having repositories in separate locations. For
2758 example CVS, SVN and git use branches while darcs uses different
2759 locations for different branches. If you need to specify a branch to
2760 identify a your repository then specify it in this field.
2762 This field is optional.
2764 .. pkg-field:: tag: token
2766 A tag identifies a particular state of a source repository. The tag
2767 can be used with a ``this`` repository kind to identify the state of
2768 a repository corresponding to a particular package version or
2769 release. The exact form of the tag depends on the repository type.
2771 This field is required for the ``this`` repository kind.
2773 .. pkg-field:: subdir: directory
2775 Some projects put the sources for multiple packages under a single
2776 source repository. This field lets you specify the relative path
2777 from the root of the repository to the top directory for the
2778 package, i.e. the directory containing the package's ``.cabal``
2781 This field is optional. It defaults to empty which corresponds to the
2782 root directory of the repository.
2785 Custom setup scripts
2786 --------------------
2788 Since Cabal 1.24, custom ``Setup.hs`` are required to accurately track
2789 their dependencies by declaring them in the ``.cabal`` file rather than
2790 rely on dependencies being implicitly in scope. Please refer to
2791 `this article <https://www.well-typed.com/blog/2015/07/cabal-setup-deps/>`__
2794 As of Cabal library version 3.0, ``defaultMain*`` variants implement support
2795 for response files. Custom ``Setup.hs`` files that do not use one of these
2796 main functions are required to implement their own support, such as by using
2797 ``GHC.ResponseFile.getArgsWithResponseFiles``.
2799 Declaring a ``custom-setup`` stanza also enables the generation of
2800 ``MIN_VERSION_package_(A,B,C)`` CPP macros for the Setup component.
2802 .. pkg-section:: custom-setup
2803 :synopsis: Custom Setup.hs build information.
2806 The optional :pkg-section:`custom-setup` stanza contains information needed
2807 for the compilation of custom ``Setup.hs`` scripts,
2813 base >= 4.5 && < 4.11,
2814 Cabal >= 1.14 && < 1.25
2816 .. pkg-field:: setup-depends: package list
2819 The dependencies needed to compile ``Setup.hs``. See the
2820 :pkg-field:`build-depends` field for a description of the syntax expected by
2823 If the field is not specified the implicit package set will be used.
2824 The package set contains packages bundled with GHC (i.e. ``base``,
2825 ``bytestring``) and specifically ``Cabal``.
2826 The specific bounds are put on ``Cabal`` dependency:
2827 lower-bound is inferred from :pkg-field:`cabal-version`,
2828 and the upper-bound is ``< 1.25``.
2830 ``Cabal`` version is additionally restricted by GHC,
2831 with absolute minimum being ``1.20``, and for example ``Custom``
2832 builds with GHC-8.10 require at least ``Cabal-3.2``.
2835 Backward compatibility and ``custom-setup``
2836 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2838 Versions prior to Cabal 1.24 don't recognise ``custom-setup`` stanzas,
2839 and will behave agnostic to them (except for warning about an unknown
2840 section). Consequently, versions prior to Cabal 1.24 can't ensure the
2841 declared dependencies ``setup-depends`` are in scope, and instead
2842 whatever is registered in the current package database environment
2843 will become eligible (and resolved by the compiler) for the
2844 ``Setup.hs`` module.
2846 The availability of the
2847 ``MIN_VERSION_package_(A,B,C)`` CPP macros
2848 inside ``Setup.hs`` scripts depends on the condition that either
2850 - a ``custom-setup`` section has been declared (or ``cabal build`` is being
2851 used which injects an implicit hard-coded ``custom-setup`` stanza if it's missing), or
2852 - GHC 8.0 or later is used (which natively injects package version CPP macros)
2854 Consequently, if you need to write backward compatible ``Setup.hs``
2855 scripts using CPP, you should declare a ``custom-setup`` stanza and
2856 use the pattern below:
2858 .. code-block:: haskell
2860 {-# LANGUAGE CPP #-}
2861 import Distribution.Simple
2863 #if defined(MIN_VERSION_Cabal)
2864 -- version macros are available and can be used as usual
2865 # if MIN_VERSION_Cabal(a,b,c)
2866 -- code specific to lib:Cabal >= a.b.c
2868 -- code specific to lib:Cabal < a.b.c
2871 # warning Enabling heuristic fall-back. Please upgrade cabal-install to 1.24 or later if Setup.hs fails to compile.
2873 -- package version macros not available; except for exotic environments,
2874 -- you can heuristically assume that lib:Cabal's version is correlated
2875 -- with __GLASGOW_HASKELL__, and specifically since we can assume that
2876 -- GHC < 8.0, we can assume that lib:Cabal is version 1.22 or older.
2881 The simplified (heuristic) CPP pattern shown below is useful if all you need
2882 is to distinguish ``Cabal < 2.0`` from ``Cabal >= 2.0``.
2884 .. code-block:: haskell
2886 {-# LANGUAGE CPP #-}
2887 import Distribution.Simple
2889 #if !defined(MIN_VERSION_Cabal)
2890 # define MIN_VERSION_Cabal(a,b,c) 0
2893 #if MIN_VERSION_Cabal(2,0,0)
2894 -- code for lib:Cabal >= 2.0
2896 -- code for lib:Cabal < 2.0
2903 Autogenerated modules and includes
2904 ----------------------------------
2906 .. pkg-section:: None
2908 Modules that are built automatically at setup, created with a custom
2909 setup script, must appear on :pkg-field:`other-modules` for the library,
2910 executable, test-suite or benchmark stanzas or also on
2911 :pkg-field:`library:exposed-modules` for libraries to be used, but are not
2912 really on the package when distributed. This makes commands like sdist fail
2913 because the file is not found.
2915 These special modules must appear again on the :pkg-field:`autogen-modules`
2916 field of the stanza that is using them, besides :pkg-field:`other-modules` or
2917 :pkg-field:`library:exposed-modules`. With this there is no need to create
2918 complex build hooks for this poweruser case.
2920 .. pkg-field:: autogen-modules: module list
2923 .. todo:: document autogen-modules field
2925 Right now :pkg-field:`executable:main-is` modules are not supported on
2926 :pkg-field:`autogen-modules`.
2931 default-language: Haskell2010
2942 default-language: Haskell2010
2951 .. pkg-field:: autogen-includes: filename list
2954 A list of header files from this package which are autogenerated
2955 (e.g. by a ``configure`` script). Autogenerated header files are not
2956 packaged by ``sdist`` command.
2959 .. _accessing-data-files:
2961 Accessing data files from package code
2962 --------------------------------------
2964 The placement on the target system of files listed in
2965 the :pkg-field:`data-files` field varies between systems, and in some cases
2966 one can even move packages around after installation
2967 (see :ref:`prefix independence`). To
2968 enable packages to find these files in a portable way, Cabal generates a
2969 module called :file:`Paths_{pkgname}` (with any hyphens in *pkgname*
2970 replaced by underscores) during building, so that it may be imported by
2971 modules of the package. This module defines a function
2973 .. code-block:: haskell
2975 getDataFileName :: FilePath -> IO FilePath
2977 If the argument is a filename listed in the :pkg-field:`data-files` field, the
2978 result is the name of the corresponding file on the system on which the
2983 If you decide to import the :file:`Paths_{pkgname}` module then it
2984 *must* be listed in the :pkg-field:`other-modules` field just like any other
2985 module in your package and on :pkg-field:`autogen-modules` as the file is
2988 The :file:`Paths_{pkgname}` module is not platform independent, as any
2989 other autogenerated module, so it does not get included in the source
2990 tarballs generated by ``sdist``.
2992 The :file:`Paths_{pkgname}` module also includes some other useful
2993 functions and values, which record the version of the package and some
2994 other directories which the package has been configured to be installed
2995 into (e.g. data files live in ``getDataDir``):
2997 .. code-block:: haskell
3001 getBinDir :: IO FilePath
3002 getLibDir :: IO FilePath
3003 getDynLibDir :: IO FilePath
3004 getDataDir :: IO FilePath
3005 getLibexecDir :: IO FilePath
3006 getSysconfDir :: IO FilePath
3008 The actual location of all these directories can be individually
3009 overridden at runtime using environment variables of the form
3010 ``pkg_name_var``, where ``pkg_name`` is the name of the package with all
3011 hyphens converted into underscores, and ``var`` is either ``bindir``,
3012 ``libdir``, ``dynlibdir``, ``datadir``, ``libexedir`` or ``sysconfdir``. For example,
3013 the configured data directory for ``pretty-show`` is controlled with the
3014 ``pretty_show_datadir`` environment variable.
3016 Accessing the package version
3017 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3019 The auto generated :file:`PackageInfo_{pkgname}` module exports the constant
3020 ``version ::`` `Version <http://hackage.haskell.org/package/base/docs/Data-Version.html>`__
3021 which is defined as the version of your package as specified in the
3024 Accessing package-related informations
3025 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3027 The auto generated :file:`PackageInfo_{pkgname}` module exports the following
3028 package-related constants:
3030 .. code-block:: haskell
3038 Unlike :file:`Paths_{pkgname}` (see <#accessing-data-files-from-package-code>),
3039 :file:`PackageInfo_{pkgname}` is system- and path-independent. It aims to be
3040 easier to work with for hash-based tools such as Nix.
3042 .. _system-dependent parameters:
3044 System-dependent parameters
3045 ---------------------------
3047 For some packages, especially those interfacing with C libraries,
3048 implementation details and the build procedure depend on the build
3049 environment. The ``build-type`` ``Configure`` can be used to handle many
3050 such situations. In this case, ``Setup.hs`` should be:
3052 .. code-block:: haskell
3054 import Distribution.Simple
3055 main = defaultMainWithHooks autoconfUserHooks
3057 Most packages, however, would probably do better using the ``Simple``
3058 build type and `configurations`_.
3060 The :pkg-field:`build-type` ``Configure`` differs from ``Simple`` in two ways:
3062 - The package root directory must contain a shell script called
3063 ``configure``. The configure step will run the script. This
3064 ``configure`` script may be produced by
3065 `autoconf <http://www.gnu.org/software/autoconf/>`__ or may be
3066 hand-written. The ``configure`` script typically discovers
3067 information about the system and records it for later steps, e.g. by
3068 generating system-dependent header files for inclusion in C source
3069 files and preprocessed Haskell source files. (Clearly this won't work
3070 for Windows without MSYS or Cygwin: other ideas are needed.)
3072 - If the package root directory contains a file called
3073 *package*\ ``.buildinfo`` after the configuration step, subsequent
3074 steps will read it to obtain additional settings for `build
3075 information`_ fields,to be merged with the ones
3076 given in the ``.cabal`` file. In particular, this file may be
3077 generated by the ``configure`` script mentioned above, allowing these
3078 settings to vary depending on the build environment.
3080 Note that the package's ``extra-source-files`` are available to the
3081 ``configure`` script when it is executed. In typical ``autoconf`` fashion,
3082 ``--host`` flag will be passed to the ``configure`` script to indicate the host
3083 platform when cross-compiling. Moreover, various bits of build configuration
3084 will be passed via environment variables:
3086 - ``CC`` will reflect the path to the C compiler
3087 - ``CFLAGS`` will reflect the path to the C compiler
3088 - ``CABAL_FLAGS`` will contain the Cabal flag assignment of the current
3089 package using traditional Cabal flag syntax (e.g. ``+flagA -flagB``)
3090 - ``CABAL_FLAG_<flag>`` will be set to either ``0`` or ``1`` depending upon
3091 whether flag ``<flag>`` is enabled. Note that any any non-alpha-numeric
3092 characters in the flag name are replaced with ``_``.
3094 The build information file should have the following structure:
3098 ``executable:`` *name* *buildinfo*
3100 ``executable:`` *name* *buildinfo* ...
3102 where each *buildinfo* consists of settings of fields listed in the
3103 section on `build information`_. The first one (if
3104 present) relates to the library, while each of the others relate to the
3105 named executable. (The names must match the package description, but you
3106 don't have to have entries for all of them.)
3108 Neither of these files is required. If they are absent, this setup
3109 script is equivalent to ``defaultMain``.
3111 Example: Using autoconf
3112 ^^^^^^^^^^^^^^^^^^^^^^^
3114 This example is for people familiar with the
3115 `autoconf <http://www.gnu.org/software/autoconf/>`__ tools.
3117 In the X11 package, the file ``configure.ac`` contains:
3119 .. code-block:: shell
3121 AC_INIT([Haskell X11 package], [1.1], [libraries@haskell.org], [X11])
3123 # Safety check: Ensure that we are in the correct source directory.
3124 AC_CONFIG_SRCDIR([X11.cabal])
3126 # Header file to place defines in
3127 AC_CONFIG_HEADERS([include/HsX11Config.h])
3129 # Check for X11 include paths and libraries
3131 AC_TRY_CPP([#include <X11/Xlib.h>],,[no_x=yes])
3133 # Build the package if we found X11 stuff
3134 if test "$no_x" = yes
3135 then BUILD_PACKAGE_BOOL=False
3136 else BUILD_PACKAGE_BOOL=True
3138 AC_SUBST([BUILD_PACKAGE_BOOL])
3140 AC_CONFIG_FILES([X11.buildinfo])
3143 Then the setup script will run the ``configure`` script, which checks
3144 for the presence of the X11 libraries and substitutes for variables in
3145 the file ``X11.buildinfo.in``:
3149 buildable: @BUILD_PACKAGE_BOOL@
3150 cc-options: @X_CFLAGS@
3151 ld-options: @X_LIBS@
3153 This generates a file ``X11.buildinfo`` supplying the parameters needed
3159 cc-options: -I/usr/X11R6/include
3160 ld-options: -L/usr/X11R6/lib
3162 The ``configure`` script also generates a header file
3163 ``include/HsX11Config.h`` containing C preprocessor defines recording
3164 the results of various tests. This file may be included by C source
3165 files and preprocessed Haskell source files in the package.
3169 Packages using these features will also need to list additional
3170 files such as ``configure``, templates for ``.buildinfo`` files, files
3171 named only in ``.buildinfo`` files, header files and so on in the
3172 :pkg-field:`extra-source-files` field to ensure that they are included in
3173 source distributions. They should also list files and directories generated
3174 by ``configure`` in the :pkg-field:`extra-tmp-files` field to ensure that
3175 they are removed by ``setup clean``.
3177 Quite often the files generated by ``configure`` need to be listed
3178 somewhere in the package description (for example, in the
3179 :pkg-field:`install-includes` field). However, we usually don't want generated
3180 files to be included in the source tarball. The solution is again
3181 provided by the ``.buildinfo`` file. In the above example, the following
3182 line should be added to ``X11.buildinfo``:
3186 install-includes: HsX11Config.h
3188 In this way, the generated ``HsX11Config.h`` file won't be included in
3189 the source tarball in addition to ``HsX11Config.h.in``, but it will be
3190 copied to the right location during the install process. Packages that
3191 use custom ``Setup.hs`` scripts can update the necessary fields
3192 programmatically instead of using the ``.buildinfo`` file.
3194 Conditional compilation
3195 -----------------------
3197 Sometimes you want to write code that works with more than one version
3198 of a dependency. You can specify a range of versions for the dependency
3199 in the :pkg-field:`build-depends`, but how do you then write the code that can
3200 use different versions of the API?
3202 Haskell lets you preprocess your code using the C preprocessor (either
3203 the real C preprocessor, or ``cpphs``). To enable this, add
3204 ``extensions: CPP`` to your package description. When using CPP, Cabal
3205 provides some pre-defined macros to let you test the version of
3206 dependent packages; for example, suppose your package works with either
3207 version 3 or version 4 of the ``base`` package, you could select the
3208 available version in your Haskell modules like this:
3212 #if MIN_VERSION_base(4,0,0)
3213 ... code that works with base-4 ...
3215 ... code that works with base-3 ...
3218 In general, Cabal supplies a macro
3219 ``MIN_VERSION_``\ *``package``*\ ``_(A,B,C)`` for each package depended
3220 on via :pkg-field:`build-depends`. This macro is true if the actual version of
3221 the package in use is greater than or equal to ``A.B.C`` (using the
3222 conventional ordering on version numbers, which is lexicographic on the
3223 sequence, but numeric on each component, so for example 1.2.0 is greater
3226 Since version 1.20, the ``MIN_TOOL_VERSION_``\ *``tool``*
3227 family of macros lets you condition on the version of build tools used to
3228 build the program (e.g. ``hsc2hs``).
3230 Since version 1.24, the macro ``CURRENT_COMPONENT_ID``, which
3231 expands to the string of the component identifier that uniquely
3232 identifies this component. Furthermore, if the package is a library,
3233 the macro ``CURRENT_PACKAGE_KEY`` records the identifier that was passed
3234 to GHC for use in symbols and for type equality.
3236 Since version 2.0, the macro ``CURRENT_PACKAGE_VERSION`` expands
3237 to the string version number of the current package.
3239 Cabal places the definitions of these macros into an
3240 automatically-generated header file, which is included when
3241 preprocessing Haskell source code by passing options to the C
3244 Cabal also allows to detect when the source code is being used for
3245 generating documentation. The ``__HADDOCK_VERSION__`` macro is defined
3246 only when compiling via Haddock_
3247 instead of a normal Haskell compiler. The value of the
3248 ``__HADDOCK_VERSION__`` macro is defined as ``A*1000 + B*10 + C``, where
3249 ``A.B.C`` is the Haddock version. This can be useful for working around
3250 bugs in Haddock or generating prettier documentation in some special
3253 .. _more-complex-packages:
3255 More complex packages
3256 ---------------------
3258 For packages that don't fit the simple schemes described above, you have
3261 - By using the :pkg-field:`build-type` ``Custom``, you can supply your own
3262 ``Setup.hs`` file, and customize the simple build infrastructure
3263 using *hooks*. These allow you to perform additional actions before
3264 and after each command is run, and also to specify additional
3265 preprocessors. A typical ``Setup.hs`` may look like this:
3267 .. code-block:: haskell
3269 import Distribution.Simple
3270 main = defaultMainWithHooks simpleUserHooks { postHaddock = posthaddock }
3272 posthaddock args flags desc info = ....
3274 See ``UserHooks`` in
3275 `Distribution.Simple <https://hackage.haskell.org/package/Cabal/docs/Distribution-Simple.html>`__
3276 for the details, but note that this interface is experimental, and
3277 likely to change in future releases.
3279 If you use a custom ``Setup.hs`` file you should strongly consider
3280 adding a :pkg-section:`custom-setup` stanza with a
3281 :pkg-field:`custom-setup:setup-depends` field to ensure that your setup
3282 script does not break with future dependency versions.
3284 - You could delegate all the work to ``make``, though this is unlikely
3285 to be very portable. Cabal supports this with the :pkg-field:`build-type`
3286 ``Make`` and a trivial setup library
3287 `Distribution.Make <https://hackage.haskell.org/package/Cabal/docs/Distribution-Make.html>`__,
3288 which simply parses the command line arguments and invokes ``make``.
3289 Here ``Setup.hs`` should look like this:
3291 .. code-block:: haskell
3293 import Distribution.Make
3296 The root directory of the package should contain a ``configure``
3297 script, and, after that has run, a ``Makefile`` with a default target
3298 that builds the package, plus targets ``install``, ``register``,
3299 ``unregister``, ``clean``, ``dist`` and ``docs``. Some options to
3300 commands are passed through as follows:
3302 - The ``--with-hc-pkg``, ``--prefix``, ``--bindir``, ``--libdir``,
3303 ``--dynlibdir``, ``--datadir``, ``--libexecdir`` and ``--sysconfdir`` options to
3304 the ``configure`` command are passed on to the ``configure``
3305 script. In addition the value of the ``--with-compiler`` option is
3306 passed in a ``--with-hc`` option and all options specified with
3307 ``--configure-option=`` are passed on.
3309 - The ``--destdir`` option to the ``copy`` command becomes a setting
3310 of a ``destdir`` variable on the invocation of ``make copy``. The
3311 supplied ``Makefile`` should provide a ``copy`` target, which will
3312 probably look like this:
3314 .. code-block:: make
3317 $(MAKE) install prefix=$(destdir)/$(prefix) \
3318 bindir=$(destdir)/$(bindir) \
3319 libdir=$(destdir)/$(libdir) \
3320 dynlibdir=$(destdir)/$(dynlibdir) \
3321 datadir=$(destdir)/$(datadir) \
3322 libexecdir=$(destdir)/$(libexecdir) \
3323 sysconfdir=$(destdir)/$(sysconfdir) \
3325 - Finally, with the :pkg-field:`build-type` ``Custom``, you can also write your
3326 own setup script from scratch, and you may use the Cabal
3327 library for all or part of the work. One option is to copy the source
3328 of ``Distribution.Simple``, and alter it for your needs. Good luck.
3330 .. include:: references.inc
3332 .. rubric:: Footnotes
3334 .. [#old-style-build-tool-depends]
3336 Some packages (ab)use :pkg-field:`build-depends` on old-style builds, but this has a few major drawbacks:
3338 - 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.
3339 - it may or may not place the executable on ``PATH``.
3340 - it does not ensure the correct version of the package is installed, so you might end up overwriting versions with each other.