Skip to content

Commit b2d1901

Browse files
committed
Rework material on library components
1 parent 15b5b05 commit b2d1901

File tree

1 file changed

+104
-100
lines changed

1 file changed

+104
-100
lines changed

doc/cabal-package-description-file.rst

Lines changed: 104 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
Package Description — <package>.cabal File
22
==========================================
33

4-
The package description file, commonly known as "the Cabal file",
5-
describes the contents of a package.
6-
The Cabal package is the unit of distribution. When installed, its
7-
purpose is to make available:
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:
87

9-
- One or more Haskell programs (executables).
8+
- Haskell programs (executables); and/or
109

11-
- One or more libraries, exposing a number of Haskell modules.
10+
- libraries, exposing a number of Haskell modules.
1211

13-
However having both a library and executables in a package does not work
14-
very well; if the executables depend on the library, they must
15-
explicitly list all the modules they directly or indirectly import from
16-
that library. Fortunately, starting with Cabal 1.8.0.4, executables can
17-
also declare the package that they are in as a dependency, and Cabal
18-
will treat them as if they were in another package that depended on the
19-
library.
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.
2015

2116
Internally, the package may consist of much more than a bunch of Haskell
2217
modules: it may also have C source code and header files, source code
@@ -803,18 +798,65 @@ Library
803798

804799
Build information for libraries.
805800

806-
A package can have one default library, which is identified by omitting the
807-
``name`` argument.
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
805+
unnamed library.
808806

809-
Starting with Cabal 2.0, sub-library components can be defined by setting
810-
the ``name`` field to a name different from the current package's name; see
811-
section on :ref:`Sublibraries <sublibs>` for more information. By
812-
default, these sub-libraries are private and internal. Since Cabal 3.0,
813-
these sub-libraries can also be exposed and used by other packages. See the
814-
:pkg-field:`library:visibility` field and :ref:`Multiple Public Libraries
815-
<publicsublibs>` for more information.
807+
.. Note::
808+
809+
The 'cabal' executable provided by the 'cabal-install' package will not
810+
accept dependencies on sublibraries of packages with no unnamed library.
811+
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
815+
internal library.
816+
817+
A sublibrary cannot have the same name as its package.
818+
819+
.. Note::
820+
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
823+
names clashed.
824+
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
827+
public.
828+
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.
834+
835+
.. Note::
836+
837+
Before version 3.0 of the Cabal specification, all sublibraries were
838+
internal libraries. Before version 2.0, a package could not include
839+
sublibraries.
840+
841+
See :ref:`Sublibraries - Examples <sublibs>` for examples.
842+
843+
A library section should contain the following fields:
844+
845+
.. pkg-field:: visibility: visibility specifiers
846+
847+
:since: 3.0
848+
849+
:default:
850+
``private`` for sublibraries. Cannot be set for the main library, which
851+
is always public.
816852

817-
The library section should contain the following fields:
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
856+
packages.
857+
858+
See the :pkg-field:`build-depends` field for the syntax to specify a
859+
dependency on a library component.
818860

819861
.. pkg-field:: exposed-modules: identifier list
820862

@@ -849,23 +891,6 @@ The library section should contain the following fields:
849891
that use a flat module namespace or where it is known that the
850892
exposed modules would clash with other common modules.
851893

852-
.. pkg-field:: visibility: visibility specifiers
853-
854-
:since: 3.0
855-
856-
:default:
857-
``private`` for sublibraries. Cannot be set for main
858-
(unnamed) library, which is always public.
859-
860-
Can be ``public`` or ``private``.
861-
Makes it possible to have multiple public libraries in a single package.
862-
If set to ``public``, depending on this library from another package is
863-
allowed. If set to ``private``, depending on this library is allowed only
864-
from the same package.
865-
866-
See section on :ref:`Sublibraries <sublibs>` for examples and more
867-
information.
868-
869894
.. pkg-field:: reexported-modules: exportlist
870895
:since: 1.22
871896

@@ -905,22 +930,21 @@ section on `build information`_).
905930

906931
.. _sublibs:
907932

908-
**Sublibraries**
933+
**Sublibraries - Examples**
909934

910-
Cabal 2.0 and later support "sublibraries", which are extra named
911-
libraries (as opposed to the usual unnamed library section). For
912-
example, suppose that your test suite needs access to some internal
913-
modules in your library, which you do not otherwise want to export. You
914-
could put these modules in a sublibrary, which the main library
915-
and the test suite :pkg-field:`build-depends` upon. Then your Cabal file might
916-
look something like this:
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
940+
this:
917941

918942
::
919943

920-
cabal-version: 2.0
944+
cabal-version: 3.4
921945
name: foo
922946
version: 0.1.0.0
923-
license: BSD3
947+
license: BSD-3-Clause
924948
license-file: LICENSE
925949
build-type: Simple
926950

@@ -933,34 +957,27 @@ look something like this:
933957

934958
library
935959
exposed-modules: Foo.Public
936-
build-depends: foo-internal, base >= 4.3 && < 5
960+
build-depends: foo:foo-internal, base >= 4.3 && < 5
937961
default-language: Haskell2010
938962

939963
test-suite test-foo
940964
type: exitcode-stdio-1.0
941965
main-is: test-foo.hs
942966
-- NOTE: no constraints on 'foo-internal' as same-package
943967
-- dependencies implicitly refer to the same package instance
944-
build-depends: foo-internal, base
968+
build-depends: foo:foo-internal, base
945969
default-language: Haskell2010
946970

947-
Sublibraries are also useful for packages that define multiple
948-
executables, but do not define a publicly accessible library. Internal
949-
libraries are only visible internally in the package (so they can only
950-
be added to the :pkg-field:`build-depends` of same-package libraries,
951-
executables, test suites, etc.) Sublibraries locally shadow any
952-
packages which have the same name; consequently, don't name an internal
953-
library with the same name as an external dependency if you need to be
954-
able to refer to the external dependency in a
955-
:pkg-field:`build-depends` declaration.
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.
956973

957-
Shadowing can be used to vendor an external dependency into a package
958-
and thus emulate *private dependencies*. Below is an example based on
959-
a real-world use case:
974+
Internal libraries can be used to incorporate (vendor or bundle) an external
975+
dependency into a package, effectively simulating *private dependencies*. Below
976+
is an example:
960977

961978
::
962979

963-
cabal-version: 3.0
980+
cabal-version: 3.4
964981
name: haddock-library
965982
version: 1.6.0
966983
license: BSD-3-Clause
@@ -975,7 +992,7 @@ a real-world use case:
975992
hs-source-dirs: src
976993

977994
-- internal sub-lib
978-
build-depends: attoparsec
995+
build-depends: haddock-library:attoparsec
979996

980997
exposed-modules:
981998
Documentation.Haddock
@@ -1003,25 +1020,6 @@ a real-world use case:
10031020

10041021
default-language: Haskell2010
10051022

1006-
.. note::
1007-
For packages using ``cabal-version: 3.4`` or higher, the syntax to
1008-
specify a sublibrary in a ``build-depends:`` section is
1009-
``package-name:internal-library-name``.
1010-
1011-
.. _publicsublibs:
1012-
1013-
**Multiple Public Libraries**
1014-
1015-
Cabal 3.0 and later support exposing multiple libraries from a single package
1016-
through the field :pkg-field:`library:visibility`.
1017-
Having multiple public libraries is useful for separating the unit of
1018-
distribution (package) from the unit of buildable code (library).
1019-
For more information about the rationale and some examples, see
1020-
`this blog post <https://fgaz.me/posts/2019-11-14-cabal-multiple-libraries/>`__.
1021-
1022-
..
1023-
TODO inline the blog post
1024-
10251023
Executables
10261024
^^^^^^^^^^^
10271025

@@ -1456,24 +1454,30 @@ system-dependent values for these fields.
14561454

14571455
.. pkg-field:: build-depends: library list
14581456

1459-
Declares the *library* dependencies required to build the current
1460-
package component; see :pkg-field:`build-tool-depends` for
1461-
declaring build-time *tool* dependencies. External library
1462-
dependencies should be annotated with a version constraint.
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.
14631461

14641462
**Library Names**
14651463

1466-
External libraries are identified by the package's name they're
1467-
provided by, optionally followed by a colon and the library name
1468-
(available from ``cabal-version: 3.0``).
1469-
If the library name is absent, the main (unnamed) library will be used.
1470-
To refer to the main (unnamed) library explicitly, use the name of the
1471-
package (``foo:foo``).
1472-
Multiple libraries from the same package can be specified with the shorthand
1473-
syntax ``pkg:{lib1,lib2}```.
1474-
1475-
See section on :ref:`Multiple Public Libraries <publicsublibs>` for examples and more
1476-
information.
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}``.
1471+
1472+
.. Note::
1473+
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.
1478+
1479+
See the section on :pkg-section:`library` for information about how a
1480+
package can specify library components.
14771481

14781482
**Version Constraints**
14791483

0 commit comments

Comments
 (0)