Update Structured.hs
[cabal.git] / doc / cabal-commands.rst
blob1b4d5a6247d1c8403a75a964918caf00b25131cf
1 cabal-install Commands
2 ======================
4 We now give an in-depth description of all the commands, describing the
5 arguments and flags they accept.
7 cabal v2-configure
8 -------------------
10 ``cabal v2-configure`` takes a set of arguments and writes a
11 ``cabal.project.local`` file based on the flags passed to this command.
12 ``cabal v2-configure FLAGS; cabal v2-build`` is roughly equivalent to
13 ``cabal v2-build FLAGS``, except that with ``v2-configure`` the flags
14 are persisted to all subsequent calls to ``v2-build``.
16 ``cabal v2-configure`` is intended to be a convenient way to write out
17 a ``cabal.project.local`` for simple configurations; e.g.,
18 ``cabal v2-configure -w ghc-7.8`` would ensure that all subsequent
19 builds with ``cabal v2-build`` are performed with the compiler
20 ``ghc-7.8``. For more complex configuration, we recommend writing the
21 ``cabal.project.local`` file directly (or placing it in
22 ``cabal.project``!)
24 ``cabal v2-configure`` inherits options from ``Cabal``. semantics:
26 -  Any flag accepted by ``./Setup configure``.
28 -  Any flag accepted by ``cabal configure`` beyond
29    ``./Setup configure``, namely ``--cabal-lib-version``,
30    ``--constraint``, ``--preference`` and ``--solver.``
32 -  Any flag accepted by ``cabal install`` beyond ``./Setup configure``.
34 -  Any flag accepted by ``./Setup haddock``.
36 The options of all of these flags apply only to *local* packages in a
37 project; this behavior is different than that of ``cabal install``,
38 which applies flags to every package that would be built. The motivation
39 for this is to avoid an innocuous addition to the flags of a package
40 resulting in a rebuild of every package in the store (which might need
41 to happen if a flag actually applied to every transitive dependency). To
42 apply options to an external package, use a ``package`` stanza in a
43 ``cabal.project`` file.
45 There are two ways of modifying the ``cabal.project.local`` file through
46 ``cabal v2-configure``, either by appending new configurations to it, or 
47 by simply overwriting it all. Overwriting is the default behaviour, as
48 such, there's a flag ``--enable-append`` to append the new configurations
49 instead. Since overwriting is rather destructive in nature, a backup system
50 is in place, which moves the old configuration to a ``cabal.project.local~``
51 file, this feature can also be disabled by using the ``--enable-overwrite``
52 flag.
54 cabal v2-update
55 ----------------
57 ``cabal v2-update`` updates the state of the package index. If the
58 project contains multiple remote package repositories it will update
59 the index of all of them (e.g. when using overlays).
61 Some examples:
65     $ cabal v2-update                  # update all remote repos
66     $ cabal v2-update head.hackage     # update only head.hackage
68 cabal v2-build
69 ---------------
71 ``cabal v2-build`` takes a set of targets and builds them. It
72 automatically handles building and installing any dependencies of these
73 targets.
75 A target can take any of the following forms:
77 -  A package target: ``package``, which specifies that all enabled
78    components of a package to be built. By default, test suites and
79    benchmarks are *not* enabled, unless they are explicitly requested
80    (e.g., via ``--enable-tests``.)
82 -  A component target: ``[package:][ctype:]component``, which specifies
83    a specific component (e.g., a library, executable, test suite or
84    benchmark) to be built.
86 -  All packages: ``all``, which specifies all packages within the project.
88 -  Components of a particular type: ``package:ctypes``, ``all:ctypes``:
89    which specifies all components of the given type. Where valid
90    ``ctypes`` are:
92      - ``libs``, ``libraries``,
93      - ``flibs``, ``foreign-libraries``,
94      - ``exes``, ``executables``,
95      - ``tests``,
96      - ``benches``, ``benchmarks``.
98 -  A module target: ``[package:][ctype:]module``, which specifies that the
99    component of which the given module is a part of will be built.
101 -  A filepath target: ``[package:][ctype:]filepath``, which specifies that the
102    component of which the given filepath is a part of will be built.
104 In component targets, ``package:`` and ``ctype:`` (valid component types
105 are ``lib``, ``flib``, ``exe``, ``test`` and ``bench``) can be used to
106 disambiguate when multiple packages define the same component, or the
107 same component name is used in a package (e.g., a package ``foo``
108 defines both an executable and library named ``foo``). We always prefer
109 interpreting a target as a package name rather than as a component name.
111 Some example targets:
115     $ cabal v2-build lib:foo-pkg       # build the library named foo-pkg
116     $ cabal v2-build foo-pkg:foo-tests # build foo-tests in foo-pkg
117     $ cabal v2-build src/Lib.s         # build the library component to
118                                        # which "src/Lib.hs" belongs
119     $ cabal v2-build app/Main.hs       # build the executable component of
120                                        # "app/Main.hs"
121     $ cabal v2-build Lib               # build the library component to
122                                        # which the module "Lib" belongs
124 Beyond a list of targets, ``cabal v2-build`` accepts all the flags that
125 ``cabal v2-configure`` takes. Most of these flags are only taken into
126 consideration when building local packages; however, some flags may
127 cause extra store packages to be built (for example,
128 ``--enable-profiling`` will automatically make sure profiling libraries
129 for all transitive dependencies are built and installed.)
131 In addition ``cabal v2-build`` accepts these flags:
133 - ``--only-configure``: When given we will forego performing a full build and
134   abort after running the configure phase of each target package.
137 cabal v2-repl
138 --------------
140 ``cabal v2-repl TARGET`` loads all of the modules of the target into
141 GHCi as interpreted bytecode. In addition to ``cabal v2-build``'s flags,
142 it takes an additional ``--repl-options`` flag.
144 To avoid ``ghci`` specific flags from triggering unneeded global rebuilds these
145 flags are now stripped from the internal configuration. As a result
146 ``--ghc-options`` will no longer (reliably) work to pass flags to ``ghci`` (or
147 other repls). Instead, you should use the new ``--repl-options`` flag to
148 specify these options to the invoked repl. (This flag also works on ``cabal
149 repl`` and ``Setup repl`` on sufficiently new versions of Cabal.)
151 Currently, it is not supported to pass multiple targets to ``v2-repl``
152 (``v2-repl`` will just successively open a separate GHCi session for
153 each target.)
155 It also provides a way to experiment with libraries without needing to download
156 them manually or to install them globally.
158 This command opens a REPL with the current default target loaded, and a version
159 of the ``vector`` package matching that specification exposed.
163     $ cabal v2-repl --build-depends "vector >= 0.12 && < 0.13"
165 Both of these commands do the same thing as the above, but only exposes ``base``,
166 ``vector``, and the ``vector`` package's transitive dependencies even if the user
167 is in a project context.
171     $ cabal v2-repl --ignore-project --build-depends "vector >= 0.12 && < 0.13"
172     $ cabal v2-repl --project='' --build-depends "vector >= 0.12 && < 0.13"
174 This command would add ``vector``, but not (for example) ``primitive``, because
175 it only includes the packages specified on the command line (and ``base``, which
176 cannot be excluded for technical reasons).
180     $ cabal v2-repl --build-depends vector --no-transitive-deps
182 cabal v2-run
183 -------------
185 ``cabal v2-run [TARGET [ARGS]]`` runs the executable specified by the
186 target, which can be a component, a package or can be left blank, as
187 long as it can uniquely identify an executable within the project.
188 Tests and benchmarks are also treated as executables.
190 See `the v2-build section <#cabal-v2-build>`__ for the target syntax.
192 Except in the case of the empty target, the strings after it will be
193 passed to the executable as arguments.
195 If one of the arguments starts with ``-`` it will be interpreted as
196 a cabal flag, so if you need to pass flags to the executable you
197 have to separate them with ``--``.
201     $ cabal v2-run target -- -a -bcd --argument
203 ``v2-run`` also supports running script files that use a certain format. With
204 a script that looks like:
208     #!/usr/bin/env cabal
209     {- cabal:
210     build-depends: base ^>= 4.11
211                 , shelly ^>= 1.8.1
212     -}
214     main :: IO ()
215     main = do
216         ...
218 It can either be executed like any other script, using ``cabal`` as an
219 interpreter, or through this command:
223     $ cabal v2-run script.hs
224     $ cabal v2-run script.hs -- --arg1 # args are passed like this
226 cabal v2-freeze
227 ----------------
229 ``cabal v2-freeze`` writes out a **freeze file** which records all of
230 the versions and flags that are picked by the solver under the
231 current index and flags.  Default name of this file is
232 ``cabal.project.freeze`` but in combination with a
233 ``--project-file=my.project`` flag (see :ref:`project-file
234 <cmdoption-project-file>`)
235 the name will be ``my.project.freeze``.
236 A freeze file has the same syntax as ``cabal.project`` and looks
237 something like this:
239 .. highlight:: cabal
243     constraints: HTTP ==4000.3.3,
244                  HTTP +warp-tests -warn-as-error -network23 +network-uri -mtl1 -conduit10,
245                  QuickCheck ==2.9.1,
246                  QuickCheck +templatehaskell,
247                  -- etc...
250 For end-user executables, it is recommended that you distribute the
251 ``cabal.project.freeze`` file in your source repository so that all
252 users see a consistent set of dependencies. For libraries, this is not
253 recommended: users often need to build against different versions of
254 libraries than what you developed against.
256 cabal v2-bench
257 ---------------
259 ``cabal v2-bench [TARGETS] [OPTIONS]`` runs the specified benchmarks
260 (all the benchmarks in the current package by default), first ensuring
261 they are up to date.
263 cabal v2-test
264 --------------
266 ``cabal v2-test [TARGETS] [OPTIONS]`` runs the specified test suites
267 (all the test suites in the current package by default), first ensuring
268 they are up to date.
270 cabal v2-haddock
271 -----------------
273 ``cabal v2-haddock [FLAGS] [TARGET]`` builds Haddock documentation for
274 the specified packages within the project.
276 If a target is not a library :cfg-field:`haddock-benchmarks`,
277 :cfg-field:`haddock-executables`, :cfg-field:`haddock-internal`,
278 :cfg-field:`haddock-tests` will be implied as necessary.
280 cabal v2-exec
281 ---------------
283 ``cabal v2-exec [FLAGS] [--] COMMAND [--] [ARGS]`` runs the specified command
284 using the project's environment. That is, passing the right flags to compiler
285 invocations and bringing the project's executables into scope.
287 cabal v2-install
288 -----------------
290 ``cabal v2-install [FLAGS] PACKAGES`` builds the specified packages and
291 symlinks/copies their executables in ``installdir`` (usually ``~/.cabal/bin``).
293 For example this command will build the latest ``cabal-install`` and symlink
294 its ``cabal`` executable:
298     $ cabal v2-install cabal-install
300 In addition, it's possible to use ``cabal v2-install`` to install components
301 of a local project. For example, with an up-to-date Git clone of the Cabal
302 repository, this command will build cabal-install HEAD and symlink the
303 ``cabal`` executable:
307     $ cabal v2-install exe:cabal
309 Where symlinking is not possible (eg. on some Windows versions) the ``copy``
310 method is used by default. You can specify the install method
311 by using ``--install-method`` flag:
315     $ cabal v2-install exe:cabal --install-method=copy --installdir=$HOME/bin
317 Note that copied executables are not self-contained, since they might use
318 data-files from the store.
320 Adding libraries to GHC package environments
321 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
323 It is also possible to "install" libraries using the ``--lib`` flag. For
324 example, this command will build the latest Cabal library and install it:
328     $ cabal v2-install --lib Cabal
330 This works by managing GHC package environment files. By default, it is writing
331 to the global environment in ``~/.ghc/$ARCH-$OS-$GHCVER/environments/default``.
332 ``v2-install`` provides the ``--package-env`` flag to control which of these
333 environments is modified.
335 This command will modify the environment file in the current directory:
339     $ cabal v2-install --lib Cabal --package-env .
341 This command will modify the environment file in the ``~/foo`` directory:
345     $ cabal v2-install --lib Cabal --package-env foo/
347 Do note that the results of the previous two commands will be overwritten by
348 the use of other v2-style commands, so it is not recommended to use them inside
349 a project directory.
351 This command will modify the environment in the ``local.env`` file in the
352 current directory:
356     $ cabal v2-install --lib Cabal --package-env local.env
358 This command will modify the ``myenv`` named global environment:
362     $ cabal v2-install --lib Cabal --package-env myenv
364 If you wish to create a named environment file in the current directory where
365 the name does not contain an extension, you must reference it as ``./myenv``.
367 You can learn more about how to use these environments in `this section of the
368 GHC manual <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/packages.html#package-environments>`_.
370 cabal v2-clean
371 ---------------
373 ``cabal v2-clean [FLAGS]`` cleans up the temporary files and build artifacts
374 stored in the ``dist-newstyle`` folder.
376 By default, it removes the entire folder, but it can also spare the configuration
377 and caches if the ``--save-config`` option is given, in which case it only removes
378 the build artefacts (``.hi``, ``.o`` along with any other temporary files generated
379 by the compiler, along with the build output).
381 cabal v2-sdist
382 ---------------
384 ``cabal v2-sdist [FLAGS] [TARGETS]`` takes the crucial files needed to build ``TARGETS``
385 and puts them into an archive format ready for upload to Hackage. These archives are stable
386 and two archives of the same format built from the same source will hash to the same value.
388 ``cabal v2-sdist`` takes the following flags:
390 - ``-l``, ``--list-only``: Rather than creating an archive, lists files that would be included.
391   Output is to ``stdout`` by default. The file paths are relative to the project's root
392   directory.
394 - ``-o``, ``--output-directory``: Sets the output dir, if a non-default one is desired. The default is
395   ``dist-newstyle/sdist/``. ``--output-directory -`` will send output to ``stdout``
396   unless multiple archives are being created.
398 - ``--null-sep``: Only used with ``--list-only``. Separates filenames with a NUL
399   byte instead of newlines.
401 ``v2-sdist`` is inherently incompatible with sdist hooks (which were removed in `Cabal-3.0`),
402 not due to implementation but due to fundamental core invariants
403 (same source code should result in the same tarball, byte for byte)
404 that must be satisfied for it to function correctly in the larger v2-build ecosystem.
405 ``autogen-modules`` is able to replace uses of the hooks to add generated modules, along with
406 the custom publishing of Haddock documentation to Hackage.
408 .. warning::
410   Packages that use Backpack will stop working if uploaded to
411   Hackage, due to `issue #6005 <https://github.com/haskell/cabal/issues/6005>`_.
412   While this is happening, we recommend not uploading these packages
413   to Hackage (and instead referencing the package directly
414   as a ``source-repository-package``).