Add `Distribution.Client.Main`
[cabal.git] / doc / developing-packages.rst
blob28f2c7847df6251950718324a2ab27f296d154bb
1 Quickstart
2 ==========
4 .. TIP::
5     If this is your first time using `cabal` you should check out the :doc:`Getting Started guide <getting-started>`.
7 Starting from scratch, we're going to walk you through creating a simple
8 Haskell application.
10 **TL;DR;** ``mkdir proglet && cd proglet && cabal init --simple --exe && cabal run proglet``
13 Introduction
14 ------------
16 Every application needs a name, we'll call ours "proglet" and start by
17 creating an empty directory.
19 .. highlight:: console
23     $ mkdir proglet
24     $ cd proglet/
27 .. _init quickstart:
29 Using ``cabal init``
30 --------------------
32 The ``cabal init`` command creates the necessary files for a Cabal package,
33 it has both an ``--interactive`` (default) and ``--non-interactive``
34 mode. The interactive mode will walk you through many of the package
35 options and metadata, the non-interactive mode will simply pick reasonable
36 defaults which is sufficient if you're just trying something out.
38 .. highlight:: console
42     $ cabal init --non-interactive
43     # You can also use -n which is the short version of --non-interactive
45 If you want, you can also try out the interactive mode, for now chose
46 "Executable" when asked what type of package you want to build.
48 .. highlight:: console
52     $ cabal init
53     ...
54     What does the package build:
55        1) Executable
56        2) Library
57        3) Library and Executable
58        4) Test suite
59     Your choice?
61 One of the important questions is whether the package contains a library
62 and/or an executable. Libraries are collections of Haskell modules that
63 can be re-used by other Haskell libraries and programs, while executables
64 are standalone programs. Test suites can both depend on a library or be
65 standalonely generated.
67 For the moment these are the only choices. For more complex packages
68 (e.g. a library and multiple executables) the ``.cabal``
69 file can be edited afterwards.
71 After you make your selection (executable; library; library
72 and executable; or: test suite) cabal asks us a number of questions starting with
73 which version of the cabal specification to use, our package's name
74 (for example, "proglet"), and our package's version.
78     Generating CHANGELOG.md...
79     Generating Main.hs...
80     Generating proglet.cabal...
82 Use the ``ls`` command to see the created files:
86    $ ls
87    CHANGELOG.md  Main.hs  proglet.cabal
90 Running the program
91 -------------------
93 Now that we have our Haskell code and the extra files that Cabal needs we
94 can build and run our application.
98    $ cabal build
99    Resolving dependencies...
100    ...
101    Linking /path/to/proglet ...
103    $ cabal run proglet
104    ...
105    Hello, Haskell!
107 Since we have an executable we can use ``cabal run proglet`` which will build
108 our executable (and re-build it if we've made any changes) and then run the
109 binary. The ``cabal run`` command works for any ``component-name`` (tests for
110 example), not just the main executable.
113 About the Cabal package structure
114 ---------------------------------
116 It is assumed that all the files that make up a package live under a common
117 root directory (apart from external dependencies). This simple example has
118 all the package files in one directory, but most packages use one or more
119 subdirectories.
121 Cabal needs one extra file in the package's root directory:
123 -  ``proglet.cabal``: contains package metadata and build information.
126 Editing the .cabal file
127 -----------------------
129 .. highlight:: cabal
131 Load up the ``.cabal`` file in a text editor. The first part of the
132 ``.cabal`` file has the package metadata and towards the end of the file
133 you will find the :pkg-section:`executable` or :pkg-section:`library`
134 section.
136 You will see that the fields that have yet to be filled in are commented
137 out. Cabal files use "``--``" Haskell-style comment syntax.
139 .. NOTE::
140    Comments are only allowed on lines on their own. Trailing comments on
141    other lines are not allowed because they could be confused with program
142    options.
147     executable proglet
148       main-is: Main.hs
149       -- other-modules:
150       -- other-extensions:
151       build-depends: base >=4.11 && <4.12
152       -- hs-source-dirs:
153       default-language: Haskell2010
156 If you selected earlier to create a library package then your ``.cabal``
157 file will have a section that looks like this:
161     library
162       exposed-modules: MyLib
163       -- other-modules:
164       -- build-depends:
165       build-depends: base >=4.11 && <4.12
166       -- hs-source-dirs:
167       default-language: Haskell2010
170 The build information fields listed (but commented out) are just the few
171 most important and common fields. There are many others that are covered
172 later in this chapter.
174 Most of the build information fields are the same between libraries and
175 executables. The difference is that libraries have a number of "exposed"
176 modules that make up the public interface of the library, while
177 executables have a file containing a ``Main`` module.
179 The name of a library always matches the name of the package, so it is
180 not specified in the library section. Executables often follow the name
181 of the package too, but this is not required and the name is given
182 explicitly.
185 Modules included in the package
186 -------------------------------
188 For an executable, ``cabal init`` creates the ``Main.hs`` file which
189 contains your program's ``Main`` module. It will also fill in the
190 :pkg-field:`executable:main-is` field with the file name of your program's
191 ``Main`` module, including the ``.hs`` (or ``.lhs``) extension. Other
192 modules included in the executable should be listed in the
193 :pkg-field:`other-modules` field.
195 For a library, ``cabal init`` looks in the project directory for files
196 that look like Haskell modules and adds all the modules to the
197 :pkg-field:`library:exposed-modules` field. For modules that do not form part
198 of your package's public interface, you can move those modules to the
199 :pkg-field:`other-modules` field. Either way, all modules in the library need
200 to be listed.
203 Modules imported from other packages
204 ------------------------------------
206 While your library or executable may include a number of modules, it
207 almost certainly also imports a number of external modules from the
208 standard libraries or other pre-packaged libraries. (These other
209 libraries are of course just Cabal packages that contain a library.)
211 You have to list all of the library packages that your library or
212 executable imports modules from. Or to put it another way: you have to
213 list all the other packages that your package depends on.
215 For example, suppose the example ``Proglet`` module imports the module
216 ``Data.Map``. The ``Data.Map`` module comes from the ``containers``
217 package, so we must list it:
221     library
222       exposed-modules:     Proglet
223       other-modules:
224       build-depends:       containers, base >=4.11 && <4.12
226 In addition, almost every package also depends on the ``base`` library
227 package because it exports the standard ``Prelude`` module plus other
228 basic modules like ``Data.List``.
230 You will notice that we have listed ``base >=4.11 && <4.12``. This gives a
231 constraint on the version of the base package that our package will work
232 with. The most common kinds of constraints are:
234 -  ``pkgname >=n``
235 -  ``pkgname ^>=n`` (since Cabal 2.0)
236 -  ``pkgname >=n && <m``
237 -  ``pkgname ==n.*`` (since Cabal 1.6)
239 The last is just shorthand, for example ``base ==4.*`` means exactly
240 the same thing as ``base >=4 && <5``. Please refer to the documentation
241 on the :pkg-field:`build-depends` field for more information.
243 Also, you can factor out shared ``build-depends`` (and other fields such
244 as ``ghc-options``) into a ``common`` stanza which you can ``import`` in
245 your libraries and executable sections. For example:
249     common shared-properties
250       default-language: Haskell2010
251       build-depends:
252         base == 4.*
253       ghc-options:
254         -Wall
256     library
257       import: shared-properties
258       exposed-modules:
259         Proglet
261 Note that the ``import`` **must** be the first thing in the stanza. For more
262 information see the :ref:`common-stanzas` section.
264 .. _building-packages:
266 Building the package
267 --------------------
269 For simple packages that's it! We can now try building the package,
270 which also downloads and builds all required dependencies:
272 .. code-block:: console
274     $ cabal build
276 If the package contains an executable, you can run it with:
278 .. code-block:: console
280    $ cabal run
282 and the executable can also be installed for convenience:
284 .. code-block:: console
286     $ cabal install
288 When installed, the executable program lands in a special directory
289 for binaries that may or may not already be on your system's ``PATH``.
290 If it is, the executable can be run by typing its filename on commandline.
291 For installing libraries see the :ref:`adding-libraries` section.
293 Next steps
294 ----------
296 What we have covered so far should be enough for very simple packages
297 that you use on your own system.
299 The next few sections cover more details needed for more complex
300 packages and details needed for distributing packages to other people.
302 The previous chapter covers building and installing packages -- your own
303 packages or ones developed by other people.
306 Package concepts
307 ================
309 Before diving into the details of writing packages it helps to
310 understand a bit about packages in the Haskell world and the particular
311 approach that Cabal takes.
313 The point of packages
314 ---------------------
316 Packages are a mechanism for organising and distributing code. Packages
317 are particularly suited for "programming in the large", that is building
318 big systems by using and re-using code written by different people at
319 different times.
321 People organise code into packages based on functionality and
322 dependencies. Social factors are also important: most packages have a
323 single author, or a relatively small team of authors.
325 Packages are also used for distribution: the idea is that a package can
326 be created in one place and be moved to a different computer and be
327 usable in that different environment. There are a surprising number of
328 details that have to be got right for this to work, and a good package
329 system helps to simplify this process and make it reliable.
331 Packages come in two main flavours: libraries of reusable code, and
332 complete programs. Libraries present a code interface, an API, while
333 programs can be run directly. In the Haskell world, library packages
334 expose a set of Haskell modules as their public interface. Cabal
335 packages can contain a library or executables or both.
337 Some programming languages have packages as a builtin language concept.
338 For example in Java, a package provides a local namespace for types and
339 other definitions. In the Haskell world, packages are not a part of the
340 language itself. Haskell programs consist of a number of modules, and
341 packages just provide a way to partition the modules into sets of
342 related functionality. Thus the choice of module names in Haskell is
343 still important, even when using packages.
345 Package names and versions
346 --------------------------
348 All packages have a name, e.g. "HUnit". Package names are assumed to be
349 unique. Cabal package names may contain letters, numbers and hyphens,
350 but not spaces and may also not contain a hyphened section consisting of
351 only numbers. The namespace for Cabal packages is flat, not
352 hierarchical.
354 Packages also have a version, e.g "1.1". This matches the typical way in
355 which packages are developed. Strictly speaking, each version of a
356 package is independent, but usually they are very similar. Cabal package
357 versions follow the conventional numeric style, consisting of a sequence
358 of digits such as "1.0.1" or "2.0". There are a range of common
359 conventions for "versioning" packages, that is giving some meaning to
360 the version number in terms of changes in the package, such as
361 e.g. `SemVer <http://semver.org>`__; however, for packages intended to be
362 distributed via Hackage Haskell's `Package Versioning Policy <https://pvp.haskell.org/>`_ applies
363 (see also the `PVP/SemVer FAQ section <https://pvp.haskell.org/faq/#semver>`__).
365 The combination of package name and version is called the *package ID*
366 and is written with a hyphen to separate the name and version, e.g.
367 "HUnit-1.1".
369 For Cabal packages, the combination of the package name and version
370 *uniquely* identifies each package. Or to put it another way: two
371 packages with the same name and version are considered to *be* the same.
373 Strictly speaking, the package ID only identifies each Cabal *source*
374 package; the same Cabal source package can be configured and built in
375 different ways. There is a separate installed package ID that uniquely
376 identifies each installed package instance. Most of the time however,
377 users need not be aware of this detail.
379 Kinds of package: Cabal vs GHC vs system
380 ----------------------------------------
382 It can be slightly confusing at first because there are various
383 different notions of package floating around. Fortunately the details
384 are not very complicated.
386 Cabal packages
387     Cabal packages are really source packages. That is they contain
388     Haskell (and sometimes C) source code.
390     Cabal packages can be compiled to produce GHC packages. They can
391     also be translated into operating system packages.
393 GHC packages
394     This is GHC's view on packages. GHC only cares about library
395     packages, not executables. Library packages have to be registered
396     with GHC for them to be available in GHCi or to be used when
397     compiling other programs or packages.
399     The low-level tool ``ghc-pkg`` is used to register GHC packages and
400     to get information on what packages are currently registered.
402     You never need to make GHC packages manually. When you build and
403     install a Cabal package containing a library then it gets registered
404     with GHC automatically.
406     Haskell implementations other than GHC have essentially the same
407     concept of registered packages. For the most part, Cabal hides the
408     slight differences.
410 Operating system packages
411     On operating systems like Linux and Mac OS X, the system has a
412     specific notion of a package and there are tools for installing and
413     managing packages.
415     The Cabal package format is designed to allow Cabal packages to be
416     translated, mostly-automatically, into operating system packages.
417     They are usually translated 1:1, that is a single Cabal package
418     becomes a single system package.
420     It is also possible to make Windows installers from Cabal packages,
421     though this is typically done for a program together with all of its
422     library dependencies, rather than packaging each library separately.
424 Unit of distribution
425 --------------------
427 The Cabal package is the unit of distribution. This means that
428 each Cabal package can be distributed on its own, in source or binary
429 form. There may be dependencies between packages, but there is
430 usually a degree of flexibility in which versions of packages can work
431 together so distributing them independently makes sense.
433 It is perhaps easiest to see what being "the unit of distribution"
434 means by contrast to an alternative approach. Many projects are made up
435 of several interdependent packages and during development these might
436 all be kept under one common directory tree and be built and tested
437 together. When it comes to distribution however, rather than
438 distributing them all together in a single tarball, it is required that
439 they each be distributed independently in their own tarballs.
441 Cabal's approach is to say that if you can specify a dependency on a
442 package then that package should be able to be distributed
443 independently. Or to put it the other way round, if you want to
444 distribute it as a single unit, then it should be a single package.
446 Explicit dependencies and automatic package management
447 ------------------------------------------------------
449 Cabal takes the approach that all packages dependencies are specified
450 explicitly and specified in a declarative way. The point is to enable
451 automatic package management. This means tools like ``cabal`` can
452 resolve dependencies and install a package plus all of its dependencies
453 automatically. Alternatively, it is possible to mechanically (or mostly
454 mechanically) translate Cabal packages into system packages and let the
455 system package manager install dependencies automatically.
457 It is important to track dependencies accurately so that packages can
458 reliably be moved from one system to another system and still be able to
459 build it there. Cabal is therefore relatively strict about specifying
460 dependencies. For example Cabal's default build system will not even let
461 code build if it tries to import a module from a package that isn't
462 listed in the ``.cabal`` file, even if that package is actually
463 installed. This helps to ensure that there are no "untracked
464 dependencies" that could cause the code to fail to build on some other
465 system.
467 The explicit dependency approach is in contrast to the traditional
468 "./configure" approach where instead of specifying dependencies
469 declaratively, the ``./configure`` script checks if the dependencies are
470 present on the system. Some manual work is required to transform a
471 ``./configure`` based package into a Linux distribution package (or
472 similar). This conversion work is usually done by people other than the
473 package author(s). The practical effect of this is that only the most
474 popular packages will benefit from automatic package management.
475 Instead, Cabal forces the original author to specify the dependencies
476 but the advantage is that every package can benefit from automatic
477 package management.
479 The "./configure" approach tends to encourage packages that adapt
480 themselves to the environment in which they are built, for example by
481 disabling optional features so that they can continue to work when a
482 particular dependency is not available. This approach makes sense in a
483 world where installing additional dependencies is a tiresome manual
484 process and so minimising dependencies is important. The automatic
485 package management view is that packages should just declare what they
486 need and the package manager will take responsibility for ensuring that
487 all the dependencies are installed.
489 Sometimes of course optional features and optional dependencies do make
490 sense. Cabal packages can have optional features and varying
491 dependencies. These conditional dependencies are still specified in a
492 declarative way however and remain compatible with automatic package
493 management. The need to remain compatible with automatic package
494 management means that Cabal's conditional dependencies system is a bit
495 less flexible than with the "./configure" approach.
497 .. note::
498    `GNU autoconf places restrictions on paths, including the
499    path that the user builds a package from.
500    <https://www.gnu.org/software/autoconf/manual/autoconf.html#File-System-Conventions>`_
501    Package authors using ``build-type: configure`` should be aware of
502    these restrictions; because users may be unexpectedly constrained and
503    face mysterious errors, it is recommended that ``build-type: configure``
504    is only used where strictly necessary.
506 Portability
507 -----------
509 One of the purposes of Cabal is to make it easier to build packages on
510 different platforms (operating systems and CPU architectures), with
511 different compiler versions and indeed even with different Haskell
512 implementations. (Yes, there are Haskell implementations other than
513 GHC!)
515 Cabal provides abstractions of features present in different Haskell
516 implementations and wherever possible it is best to take advantage of
517 these to increase portability. Where necessary however it is possible to
518 use specific features of specific implementations.
520 For example a package author can list in the package's ``.cabal`` what
521 language extensions the code uses. This allows Cabal to figure out if
522 the language extension is supported by the Haskell implementation that
523 the user picks. Additionally, certain language extensions such as
524 Template Haskell require special handling from the build system and by
525 listing the extension it provides the build system with enough
526 information to do the right thing.
528 Another similar example is linking with foreign libraries. Rather than
529 specifying GHC flags directly, the package author can list the libraries
530 that are needed and the build system will take care of using the right
531 flags for the compiler. Additionally this makes it easier for tools to
532 discover what system C libraries a package needs, which is useful for
533 tracking dependencies on system libraries (e.g. when translating into
534 Linux distribution packages).
536 In fact both of these examples fall into the category of explicitly
537 specifying dependencies. Not all dependencies are other Cabal packages.
538 Foreign libraries are clearly another kind of dependency. It's also
539 possible to think of language extensions as dependencies: the package
540 depends on a Haskell implementation that supports all those extensions.
542 Where compiler-specific options are needed however, there is an "escape
543 hatch" available. The developer can specify implementation-specific
544 options and more generally there is a configuration mechanism to
545 customise many aspects of how a package is built depending on the
546 Haskell implementation, the operating system, computer architecture and
547 user-specified configuration flags.