Suppose you have a directory hierarchy containing the source files that make up your package. You will need to add two more files to the root directory of the package:
a text file containing a package description (for details of the syntax of this file, see Section 2.1), and
a single-module Haskell program to perform various setup tasks (with the interface described in Section 3). This module should import only modules that will be present in all Haskell implementations, including modules of the Cabal library. In most cases it will be trivial, calling on the Cabal library to do most of the work.
Once you have these, you can create a source bundle of this directory for distribution. Building of the package is discussed in Section 3.
Example 1. A package containing a simple library
The HUnit package contains a file HUnit.cabal containing:
Name: HUnit Version: 1.1 License: BSD3 Author: Dean Herington Homepage: http://hunit.sourceforge.net/ Category: Testing Build-Depends: base Synopsis: Unit testing framework for Haskell Exposed-modules: Test.HUnit, Test.HUnit.Base, Test.HUnit.Lang, Test.HUnit.Terminal, Test.HUnit.Text Extensions: CPP
and the following Setup.hs:
import Distribution.Simple main = defaultMain
Example 2. A package containing executable programs
Name: TestPackage Version: 0.0 License: BSD3 Author: Angela Author Synopsis: Small package with two programs Build-Depends: HUnit Executable: program1 Main-Is: Main.hs Hs-Source-Dirs: prog1 Executable: program2 Main-Is: Main.hs Hs-Source-Dirs: prog2 Other-Modules: Utils
with Setup.hs the same as above.
Example 3. A package containing a library and executable programs
Name: TestPackage Version: 0.0 License: BSD3 Author: Angela Author Synopsis: Package with library and two programs Build-Depends: HUnit Exposed-Modules: A, B, C Executable: program1 Main-Is: Main.hs Hs-Source-Dirs: prog1 Other-Modules: A, B Executable: program2 Main-Is: Main.hs Hs-Source-Dirs: prog2 Other-Modules: A, C, Utils
with Setup.hs the same as above. Note that any library modules required (directly or indirectly) by an executable must be listed again.
The trivial setup script used in these examples uses the simple build infrastructure provided by the Cabal library (see Distribution.Simple). The simplicity lies in its interface rather that its implementation. It automatically handles preprocessing with standard preprocessors, and builds packages for all the Haskell implementations (except nhc98, for now).
The simple build infrastructure can also handle packages where building is governed by system-dependent parameters, if you specify a little more (see Section 2.2). A few packages require more elaborate solutions (see Section 2.4).
The package description file should have a name ending in ".cabal". There must be exactly one such file in the directory. The first part of the name is immaterial, but it is conventional to use the package name.
In the package description file, lines beginning with "--" are treated as comments and ignored.
This file should contain one or more stanzas separated by blank lines:
The first stanza describes the package as a whole (see Section 2.1.1), as well as an optional library (see Section 2.1.2) and relevant build information (see Section 2.1.4).
Each subsequent stanza (if any) describes an executable program (see Section 2.1.3) and relevant build information (see Section 2.1.4).
Each stanza consists of a number of field/value pairs, with a syntax like mail message headers.
case is not significant in field names
to continue a field value, indent the next line
to get a blank line in a field value, use an indented "."
The syntax of the value depends on the field. Field types include:
Either a sequence of one or more non-space non-comma characters, or a quoted string in Haskell 98 lexical syntax. Unless otherwise stated, relative filenames and directories are interpreted from the package root directory.
An arbitrary, uninterpreted string.
A letter followed by zero or more alphanumerics or underscores.
Module names may correspond to Haskell source files, i.e. with names ending in ".hs" or ".lhs", or to inputs for various Haskell preprocessors. The simple build infrastructure understands ".gc" (GreenCard), ".chs" (c2hs), ".hsc" (hsc2hs), ".y" and ".ly" (happy), ".x" (alex) and ".cpphs" (cpphs). In such cases the appropriate preprocessor will be run automatically as required. |
Some fields take lists of values, which are optionally separated by commas, except for the build-depends field, where the commas are mandatory.
Some fields are marked as required. All others are optional, and unless otherwise specified have empty default values.
These fields may occur in the first stanza, and describe the package as a whole:
The unique name of the package (see Section 1), without the version number.
The package version number, usually consisting of a sequence of natural numbers separated by dots.
The type of license under which this package is distributed. License names are the constants of the License type.
The name of a file containing the precise license for this package.
The content of a copyright notice, typically the name of the holder of the copyright on the package and the year(s) from which copyright is claimed.
The original author of the package.
The current maintainer or maintainers of the package. This is an e-mail address to which users should send bug reports, feature requests and patches.
The stability level of the package, e.g. alpha, experimental, provisional, stable.
The package homepage.
The location of a source bundle for the package. The distribution should be a Cabal package.
A very short description of the package, for use in a table of packages. This is your headline, so keep it short (one line) but as informative as possible. Save space by not including the package name or saying it's written in Haskell.
Description of the package. This may be several paragraphs, and should be aimed at a Haskell programmer who has never heard of your package before.
A classification category for future use by the package catalogue Hackage. These categories have not yet been specified, but the upper levels of the module hierarchy make a good start.
A list of compilers and versions against which the package has been tested (or at least built).
A list of packages, possibly annotated with versions, needed to build this one, e.g. foo > 1.2, bar. If no version constraint is specified, any version is assumed to be acceptable.
A list of additional files to be included in source distributions built with setup sdist (see Section 3.10).
If the package contains a library, the first stanza should also contain the following field:
A list of modules added by this package.
The first stanza may also contain build information fields (see Section 2.1.4) relating to the library.
Subsequent stanzas (if present) describe executable programs contained in the package, using the following fields, as well as build information fields (see Section 2.1.4).
The name of the executable program.
The name of the source file containing the Main module, relative to one of the directories listed in hs-source-dirs.
These stanzas may also contain build information fields (see Section 2.1.4) relating to the executable.
The following fields may be optionally present in any stanza, and give information for the building of the corresponding library or executable. See also Section 2.2 for a way to supply system-dependent values for these fields.
Is the component buildable? Like some of the other fields below, this field is more useful with the slightly more elaborate form of the simple build infrastructure described in Section 2.2.
A list of modules used by the component but not exposed to users. For a library component, these would be hidden modules of the library. For an executable, these would be auxiliary modules to be linked with the file named in the main-is field.
Root directories for the module hierarchy.
For backwards compatibility, the old variant hs-source-dir is also recognized.
A list of Haskell extensions used by every module. Extension names are the constructors of the Extension type. These determine corresponding compiler options. In particular, CPP specifies that Haskell source files are to be preprocessed with a C preprocessor.
Extensions used only by one module may be specified by placing a LANGUAGE pragma in the source file affected, e.g.:
{-# LANGUAGE CPP, MultiParamTypeClasses #-}
Additional options for GHC. You can often achieve the same effect using the extensions field, which is preferred.
Options required only by one module may be specified by placing an OPTIONS_GHC pragma in the source file affected.
Additional options for GHC when the package is built with profiling enabled.
Additional options for Hugs. You can often achieve the same effect using the extensions field, which is preferred.
Options required only by one module may be specified by placing an OPTIONS_HUGS pragma in the source file affected.
Additional options for nhc98. You can often achieve the same effect using the extensions field, which is preferred.
Options required only by one module may be specified by placing an OPTIONS_NHC pragma in the source file affected.
A list of header files from standard include directories or those listed in include-dirs, to be included in any compilations via C. These files typically contain function prototypes for foreign imports used by the package.
A list of directories to search for header files, both when using a C preprocessor and when compiling via C.
A list of C source files to be compiled and linked with the Haskell files.
If you use this field, you should also name the C files in CFILES pragmas in the Haskell source files that use them, e.g.:
{-# CFILES dir/file1.c dir/file2.c #-}These are ignored by the compilers, but needed by Hugs.
A list of extra libraries to link with.
A list of directories to search for libraries.
Command-line arguments to be passed to the C compiler. Since the arguments are compiler-dependent, this field is more useful with the setup described in Section 2.2.
Command-line arguments to be passed to the linker. Since the arguments are compiler-dependent, this field is more useful with the setup described in Section 2.2.
On Darwin/MacOS X, a list of frameworks to link to. See Apple's developer documentation for more details on frameworks. This entry is ignored on all other platforms.
For some packages, especially those interfacing with C libraries, implementation details and the build procedure depend on the build environment. The simple build infrastructure can handle many such situations using a slightly longer Setup.hs:
import Distribution.Simple main = defaultMainWithHooks defaultUserHooks
This program differs from defaultMain in two ways:
If the package root directory contains a file called configure, the configure step will run that. This configure program may be a script produced by the autoconf system, or may be hand-written. This program typically discovers information about the system and records it for later steps, e.g. by generating system-dependent header files for inclusion in C source files and preprocessed Haskell source files. (Clearly this won't work for Windows without MSYS or Cygwin: other ideas are needed.)
If the package root directory contains a file called package.buildinfo after the configuration step, subsequent steps will read it to obtain additional settings for build information fields (see Section 2.1.4), to be merged with the ones given in the .cabal file. In particular, this file may be generated by the configure script mentioned above, allowing these settings to vary depending on the build environment.
The build information file should have the following structure:
buildinfo executable: name buildinfo executable: name buildinfo ...
where each buildinfo consists of settings of fields listed in Section 2.1.4. The first one (if present) relates to the library, while each of the others relate to the named executable. (The names must match the package description, but you don't have to have entries for all of them.)
Neither of these files is required. If they are absent, this setup script is equivalent to defaultMain.
Example 4. Using autoconf
(This example is for people familiar with the autoconf tools.)
In the X11 package, the file configure.ac contains:
AC_INIT([Haskell X11 package], [1.1], [libraries@haskell.org], [X11]) # Safety check: Ensure that we are in the correct source directory. AC_CONFIG_SRCDIR([X11.cabal]) # Header file to place defines in AC_CONFIG_HEADERS([include/HsX11Config.h]) # Check for X11 include paths and libraries AC_PATH_XTRA AC_TRY_CPP([#include <X11/Xlib.h>],,[no_x=yes]) # Build the package if we found X11 stuff if test "$no_x" = yes then BUILD_PACKAGE_BOOL=False else BUILD_PACKAGE_BOOL=True fi AC_SUBST([BUILD_PACKAGE_BOOL]) AC_CONFIG_FILES([X11.buildinfo]) AC_OUTPUT
Then the setup script will run the configure script, which checks for the presence of the X11 libraries and substitutes for variables in the file X11.buildinfo.in:
buildable: @BUILD_PACKAGE_BOOL@ cc-options: @X_CFLAGS@ ld-options: @X_LIBS@
This generates a file X11.buildinfo supplying the parameters needed by later stages:
buildable: True cc-options: -I/usr/X11R6/include ld-options: -L/usr/X11R6/lib
The configure script also generates a header file include/HsX11Config.h containing C preprocessor defines recording the results of various tests. This file may be included by C source files and preprocessed Haskell source files in the package.
Packages using these features will also need to list additional files such as configure, templates for .buildinfo files, files named only in .buildinfo files, header files and so on in the other-files field, to ensure that they are included in source distributions. |
Cabal will automatically handle certain preprocessors including GreenCard, hsc2hs, happy, and alex. To use these preprocessors, just specify the module name (without any extension) in the exposed-modules or extra-modules fields. No further steps are required. Cabal will look for modules with extensions corresponding to the preprocessors it knows about (".y" for Happy files, ".gc" for GreenCard files, etc.), run the appropriate preprocessor on those files, then compile the output.
For packages that don't fit the simple schemes described above, you have a few options:
You can customize the simple build infrastructure using hooks. These allow you to perform additional actions before and after each command is run, and also to specify additional preprocessors. See UserHooks in Distribution.Simple for the details, but note that this interface is experimental, and likely to change in future releases.
You could delegate all the work to make, though this is unlikely to be very portable. Cabal supports this with a trivial setup library Distribution.Make, which simply parses the command line arguments and invokes make. Here Setup.hs looks like
import Distribution.Make main = defaultMain
The root directory of the package should contain a configure script, and, after that has run, a Makefile with a default target that builds the package, plus targets install, register, unregister, clean, dist and docs. Some options to commands are passed through as follows:
The --with-hc, --with-hc-pkg and --prefix options to the configure command are passed on to the configure script.
the --copy-prefix option to the copy command becomes a setting of a prefix variable on the invocation of make install.
You can write your own setup script conforming to the interface of Section 3, possibly using the Cabal library for part of the work. One option is to copy the source of Distribution.Simple, and alter it for your needs. Good luck.