.. _using-ghc:
Using GHC
=========
.. index::
single: GHC, using
single: using GHC
Getting started: compiling programs
-----------------------------------
In this chapter you'll find a complete reference to the GHC command-line
syntax, including all 400+ flags. It's a large and complex system, and
there are lots of details, so it can be quite hard to figure out how to
get started. With that in mind, this introductory section provides a
quick introduction to the basic usage of GHC for compiling a Haskell
program, before the following sections dive into the full syntax.
Let's create a Hello World program, and compile and run it. First,
create a file :file:`hello.hs` containing the Haskell code: ::
main = putStrLn "Hello, World!"
To compile the program, use GHC like this:
.. code-block:: sh
$ ghc hello.hs
(where ``$`` represents the prompt: don't type it). GHC will compile the
source file :file:`hello.hs`, producing an object file :file:`hello.o` and an
interface file :file:`hello.hi`, and then it will link the object file to
the libraries that come with GHC to produce an executable called
:file:`hello` on Unix/Linux/Mac, or :file:`hello.exe` on Windows.
By default GHC will be very quiet about what it is doing, only printing
error messages. If you want to see in more detail what's going on behind
the scenes, add :ghc-flag:`-v` to the command line.
Then we can run the program like this:
.. code-block:: sh
$ ./hello
Hello World!
If your program contains multiple modules, then you only need to tell
GHC the name of the source file containing the ``Main`` module, and GHC
will examine the ``import`` declarations to find the other modules that
make up the program and find their source files. This means that, with
the exception of the ``Main`` module, every source file should be named
after the module name that it contains (with dots replaced by directory
separators). For example, the module ``Data.Person`` would be in the
file ``Data/Person.hs`` on Unix/Linux/Mac, or ``Data\Person.hs`` on
Windows.
Options overview
----------------
GHC's behaviour is controlled by options, which for historical reasons
are also sometimes referred to as command-line flags or arguments.
Options can be specified in three ways:
Command-line arguments
~~~~~~~~~~~~~~~~~~~~~~
.. index::
single: structure, command-line
single: command-line; arguments
single: arguments; command-line
An invocation of GHC takes the following form:
.. code-block:: none
ghc [argument...]
Command-line arguments are either options or file names.
Command-line options begin with ``-``. They may *not* be grouped:
``-vO`` is different from ``-v -O``. Options need not precede filenames:
e.g., ``ghc *.o -o foo``. All options are processed and then applied to
all files; you cannot, for example, invoke
``ghc -c -O1 Foo.hs -O2 Bar.hs`` to apply different optimisation levels
to the files ``Foo.hs`` and ``Bar.hs``.
In addition to passing arguments via the command-line, arguments can be passed
via GNU-style response files. For instance,
.. code-block:: bash
$ cat response-file
-O1
Hello.hs
-o Hello
$ ghc @response-file
.. note::
.. index::
single: command-line; order of arguments
Note that command-line options are *order-dependent*, with arguments being
evaluated from left-to-right. This can have seemingly strange effects in the
presence of flag implication. For instance, consider
:ghc-flag:`-fno-specialise <-fspecialise>` and :ghc-flag:`-O1` (which implies
:ghc-flag:`-fspecialise`). These two command lines mean very different
things:
``-fno-specialise -O1``
``-fspecialise`` will be enabled as the ``-fno-specialise`` is overridden
by the ``-O1``.
``-O1 -fno-specialise``
``-fspecialise`` will not be enabled, since the ``-fno-specialise``
overrides the ``-fspecialise`` implied by ``-O1``.
.. _source-file-options:
Command line options in source files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. index::
single: source-file options
Sometimes it is useful to make the connection between a source file and
the command-line options it requires quite tight. For instance, if a
Haskell source file deliberately uses name shadowing, it should be
compiled with the ``-Wno-name-shadowing`` option. Rather than
maintaining the list of per-file options in a ``Makefile``, it is
possible to do this directly in the source file using the
``OPTIONS_GHC`` :ref:`pragma ` ::
{-# OPTIONS_GHC -Wno-name-shadowing #-}
module X where
...
``OPTIONS_GHC`` is a *file-header pragma* (see :ref:`options-pragma`).
Only *dynamic* flags can be used in an ``OPTIONS_GHC`` pragma (see
:ref:`mode-dynamic-flags`).
Note that your command shell does not get to the source file options,
they are just included literally in the array of command-line arguments
the compiler maintains internally, so you'll be desperately disappointed
if you try to glob etc. inside ``OPTIONS_GHC``.
.. note::
The contents of ``OPTIONS_GHC`` are appended to the command-line
options, so options given in the source file override those given on the
command-line.
It is not recommended to move all the contents of your Makefiles into
your source files, but in some circumstances, the ``OPTIONS_GHC`` pragma
is the Right Thing. (If you use :ghc-flag:`-keep-hc-file` and have ``OPTION`` flags in
your module, the ``OPTIONS_GHC`` will get put into the generated ``.hc`` file).
Setting options in GHCi
~~~~~~~~~~~~~~~~~~~~~~~
Options may also be modified from within GHCi, using the :ghci-cmd:`:set`
command.
.. _mode-dynamic-flags:
Dynamic and Mode options
------------------------
.. index::
single: dynamic; options
single: mode; options
Each of GHC's command line options is classified as dynamic or mode:
Mode: A mode may be used on the command line only.
You can pass only one mode flag.
For example, :ghc-flag:`--make` or :ghc-flag:`-E`.
The available modes are listed in :ref:`modes`.
Dynamic: A dynamic flag may be used on the command line,
in a ``OPTIONS_GHC`` pragma in a source
file, or set using :ghci-cmd:`:set` in GHCi.
The flag reference tables (:ref:`flag-reference`) lists the status of
each flag.
.. _file-suffixes:
Meaningful file suffixes
------------------------
.. index::
single: suffixes, file
single: file suffixes for GHC
File names with "meaningful" suffixes (e.g., ``.lhs`` or ``.o``) cause
the "right thing" to happen to those files.
``.hs``
A Haskell module.
``.lhs``
.. index::
single: lhs file extension
A “literate Haskell” module.
``.hspp``
A file created by the preprocessor.
``.hi``
A Haskell interface file, probably compiler-generated.
``.hie``
An extended Haskell interface file, produced by the Haskell compiler.
``.hc``
Intermediate C file produced by the Haskell compiler.
``.c``
A C file not produced by the Haskell compiler.
``.ll``
An llvm-intermediate-language source file, usually produced by the
compiler.
``.bc``
An llvm-intermediate-language bitcode file, usually produced by the
compiler.
``.s``
An assembly-language source file, usually produced by the compiler.
``.o``
An object file, produced by an assembler.
Files with other suffixes (or without suffixes) are passed straight to
the linker.
.. _modes:
Modes of operation
------------------
.. index::
single: help options
GHC's behaviour is firstly controlled by a mode flag. Only one of these
flags may be given, but it does not necessarily need to be the first
option on the command-line. For instance,
.. code-block:: none
$ ghc Main.hs --make -o my-application
If no mode flag is present, then GHC will enter :ghc-flag:`--make` mode
(:ref:`make-mode`) if there are any Haskell source files given on the
command line, or else it will link the objects named on the command line
to produce an executable.
The available mode flags are:
.. ghc-flag:: --interactive
:shortdesc: Interactive mode - normally used by just running ``ghci``;
see :ref:`ghci` for details.
:type: mode
:category: modes
.. index::
single: interactive mode
single: GHCi
Interactive mode, which is also available as :program:`ghci`. Interactive
mode is described in more detail in :ref:`ghci`.
.. ghc-flag:: --run ⟨file⟩
:shortdesc: Run a Haskell program.
:type: mode
:category: modes
.. index::
single: run mode
single: GHCi
Run a script's ``main`` entry-point. Similar to ``runghc``/``runhaskell`` this will by
default use the bytecode interpreter. If the command-line contains a ``--``
argument then all arguments that follow will be passed to the script. All
arguments that precede ``--`` are interpreted as GHC arguments.
.. ghc-flag:: --make
:shortdesc: Build a multi-module Haskell program, automatically figuring out
dependencies. Likely to be much easier, and faster, than using
``make``; see :ref:`make-mode` for details.
:type: mode
:category: modes
.. index::
single: make mode; of GHC
In this mode, GHC will build a multi-module Haskell program
automatically, figuring out dependencies for itself. If you have a
straightforward Haskell program, this is likely to be much easier,
and faster, than using :command:`make`. Make mode is described in
:ref:`make-mode`.
This mode is the default if there are any Haskell source files
mentioned on the command line, and in this case the :ghc-flag:`--make`
option can be omitted.
.. ghc-flag:: -e ⟨expr⟩
:shortdesc: Evaluate ``expr``; see :ref:`eval-mode` for details.
:type: mode
:category: modes
.. index::
single: eval mode; of GHC
Expression-evaluation mode. This is very similar to interactive
mode, except that there is a single expression to evaluate (⟨expr⟩)
which is given on the command line. This flag may be given multiple
times, in which case each expression is evaluated sequentially.
See :ref:`eval-mode` for more details.
.. ghc-flag:: -E
:shortdesc: Stop after preprocessing (``.hspp`` file)
:type: mode
:category: phases
Stop after preprocessing (``.hspp`` file)
.. ghc-flag:: -C
:shortdesc: Stop after generating C (``.hc`` file)
:type: mode
:category: phases
Stop after generating C (``.hc`` file)
.. ghc-flag:: -S
:shortdesc: Stop after generating assembly (``.s`` file)
:type: mode
:category: phases
Stop after generating assembly (``.s`` file)
.. ghc-flag:: -c
:shortdesc: Stop after generating object (``.o``) file
:type: mode
:category: phases
Stop after generating object (``.o``) file
This is the traditional batch-compiler mode, in which GHC can
compile source files one at a time, or link objects together into an
executable. See :ref:`options-order`.
.. ghc-flag:: --merge-objs
:shortdesc: Merge a set of objects into a GHCi library.
:type: mode
:category: phases
Merge a set of static object files into a library optimised for loading in
GHCi. See :ref:`building-ghci-libraries`.
.. ghc-flag:: -M
:shortdesc: generate dependency information suitable for use in a
``Makefile``; see :ref:`makefile-dependencies` for details.
:type: mode
:category: modes
.. index::
single: dependency-generation mode; of GHC
Dependency-generation mode. In this mode, GHC can be used to
generate dependency information suitable for use in a ``Makefile``.
See :ref:`makefile-dependencies`.
.. ghc-flag:: --frontend ⟨module⟩
:shortdesc: run GHC with the given frontend plugin; see
:ref:`frontend_plugins` for details.
:type: mode
:category: modes
.. index::
single: frontend plugins; using
Run GHC using the given frontend plugin. See :ref:`frontend_plugins` for
details.
.. ghc-flag:: -shared
:shortdesc: Create a shared object.
:type: mode
:category: modes
.. index::
single: DLL-creation mode
single: Shared-object creation mode
Create a shared object (or, on Windows, DLL). See :ref:`win32-dlls-create`.
.. ghc-flag:: --help
-?
:shortdesc: Display help
:type: mode
:category: modes
Cause GHC to spew a long usage message to standard output and then
exit.
.. ghc-flag:: --show-iface ⟨file⟩
:shortdesc: display the contents of an interface file.
:type: mode
:category: modes
Read the interface in ⟨file⟩ and dump it as text to ``stdout``. For
example ``ghc --show-iface M.hi``.
.. ghc-flag:: --supported-extensions
--supported-languages
:shortdesc: display the supported language extensions
:type: mode
:category: modes
Print the supported language extensions.
.. ghc-flag:: --show-options
:shortdesc: display the supported command line options
:type: mode
:category: modes
Print the supported command line options. This flag can be used for
autocompletion in a shell.
.. ghc-flag:: --info
:shortdesc: display information about the compiler
:type: mode
:category: modes
Print information about the compiler.
.. ghc-flag:: --version
-V
:shortdesc: display GHC version
:type: mode
:category: modes
Print a one-line string including GHC's version number.
.. ghc-flag:: --numeric-version
:shortdesc: display GHC version (numeric only)
:type: mode
:category: modes
Print GHC's numeric version number only.
.. ghc-flag:: --print-booter-version
:shortdesc: display bootstrap compiler version
:type: mode
:category: modes
Print the numeric version of the GHC binary used to
bootstrap the build of this compiler.
.. ghc-flag:: --print-build-platform
:shortdesc: display platform on which GHC was built
:type: mode
:category: modes
Print the target string of the build platform, on which GHC was built,
as generated by GNU Autotools.
The format is ``cpu-manufacturer-operating_system-(kernel)``, e.g.,
``x86_64-unknown-linux``.
.. ghc-flag:: --print-c-compiler-flags
:shortdesc: C compiler flags used to build GHC
:type: mode
:category: modes
List the flags passed to the C compiler during GHC build.
.. ghc-flag:: --print-c-compiler-link-flags
:shortdesc: C linker flags used to build GHC
:type: mode
:category: modes
List the flags passed to the C compiler for the linking step
during GHC build.
.. ghc-flag:: --print-debug-on
:shortdesc: print whether GHC was built with ``-DDEBUG``
:type: mode
:category: modes
Print ``True`` if GHC was built with ``-DDebug`` flag.
This enables assertions and extra debug code.
The flag can be set in ``GhcStage1HcOpts`` and/or ``GhcStage2HcOpts``
and is automatically set for ``devel1`` and ``devel2`` build flavors.
.. ghc-flag:: --print-global-package-db
:shortdesc: display GHC's global package database directory
:type: mode
:category: modes
Print the path to GHC's global package database directory.
A package database stores details about installed packages as a directory
containing a file for each package.
This flag prints the path to the global database shipped with GHC, and
looks something like ``/usr/lib/ghc/package.conf.d`` on Unix.
There may be other package databases, e.g., the user package databse.
For more details see :ref:`package-databases`.
.. ghc-flag:: --print-have-interpreter
:shortdesc: display whether GHC was built with interactive support
:type: mode
:category: modes
Print ``YES`` if GHC was compiled to include the interpreter, ``NO`` otherwise.
If this GHC does not have the interpreter included, running it in interactive
mode (see :ghc-flag:`--interactive`) will throw an error.
This only pertains the use of GHC interactively, not any separate GHCi binaries
(see :ref:`ghci`).
.. ghc-flag:: --print-have-native-code-generator
:shortdesc: display whether target platform has NCG support
:type: mode
:category: modes
Print ``YES`` if native code generator supports the target platform,
``NO`` otherwise.
(See :ref:`native-code-gen`)
.. ghc-flag:: --print-host-platform
:shortdesc: display host platform of GHC
:type: mode
:category: modes
Print the target string of the host platform, i.e.,
the one on which GHC is supposed to run, as generated by GNU Autotools.
The format is ``cpu-manufacturer-operating_system-(kernel)``, e.g.,
``x86_64-unknown-linux``.
.. ghc-flag:: --print-leading-underscore
:shortdesc: display use of leading underscores on symbol names
:type: mode
:category: modes
Print ``YES`` if GHC was compiled to use symbols with leading underscores
in object files, ``NO`` otherwise.
This is usually atarget platform dependent.
.. ghc-flag:: --print-libdir
:shortdesc: display GHC library directory
:type: mode
:category: modes
.. index::
single: libdir
Print the path to GHC's library directory. This is the top of the
directory tree containing GHC's libraries, interfaces, and include
files (usually something like ``/usr/local/lib/ghc-5.04`` on Unix).
This is the value of ``$libdir`` in the package
configuration file (see :ref:`packages`).
.. ghc-flag:: --print-ld-flags
:shortdesc: display linker flags used to compile GHC
:type: mode
:category: modes
Print linke flags used to compile GHC.
.. ghc-flag:: --print-object-splitting-supported
:shortdesc: display whether GHC supports object splitting
:type: mode
:category: modes
Print ``YES`` if GHC was compiled with support for splitting generated
object files into smaller objects, ``NO`` otherwise.
This feature uses platform specific techniques and may not be available on
all platforms.
See :ghc-flag:`-split-objs` for details.
.. ghc-flag:: --print-project-git-commit-id
:shortdesc: display Git commit id GHC is built from
:type: mode
:category: modes
Print the Git commit id from which this GHC was built.
This can be used to trace the current binary back to a specific
revision, which is especially useful during development on GHC itself.
It is set by the configure script.
.. ghc-flag:: --print-project-version
:shortdesc: display GHC version
:type: mode
:category: modes
Print the version set in the configure script during build.
This is simply the GHC version.
.. ghc-flag:: --print-rts-ways
:shortdesc: display which way RTS was built
:type: mode
:category: modes
Packages, like the Runtime System, can be built in a number of ways:
- profiling - with profiling support
- dynamic - with dynamic linking
- logging - RTS event logging
- threaded - mulithreaded RTS
- debug - RTS with debug information
Various combinations of these flavours are possible.
.. ghc-flag:: --print-stage
:shortdesc: display ``stage`` number of GHC
:type: mode
:category: modes
GHC is built using GHC itself and this build happens in stages,
which are numbered.
- Stage 0 is the GHC you have installed. The "GHC you have installed" is also called "the bootstrap compiler".
- Stage 1 is the first GHC we build, using stage 0. Stage 1 is then used to build the packages.
- Stage 2 is the second GHC we build, using stage 1. This is the one we normally install when you say make install.
- Stage 3 is optional, but is sometimes built to test stage 2.
Stage 1 does not support interactive execution (GHCi) and Template Haskell.
.. ghc-flag:: --print-support-smp
:shortdesc: display whether GHC was compiled with SMP support
:type: mode
:category: modes
Print ``YES`` if GHC was built with multiporcessor support, ``NO`` otherwise.
.. ghc-flag:: --print-tables-next-to-code
:shortdesc: display whether GHC was compiled with ``--enable-tables-next-to-code``
:type: mode
:category: modes
Print ``YES`` if GHC was built with the flag ``--enable-tables-next-to-code``, ``NO`` otherwise.
This option is on by default, as it generates a more efficient code layout.
.. ghc-flag:: --print-target-platform
:shortdesc: display target platform of GHC
:type: mode
:category: modes
Print the target string of the target platform, i.e.,
the one on which generated binaries will run, as generated by GNU Autotools.
The format is ``cpu-manufacturer-operating_system-(kernel)``, e.g.,
``x86_64-unknown-linux``.
.. ghc-flag:: --print-unregisterised
:shortdesc: display whether this GHC was built in unregisterised mode
:type: mode
:category: modes
Print ``YES`` if this GHC was built in unregisterised mode, ``NO`` otherwise.
"Unregisterised" means that GHC will disable most platform-specific tricks
and optimisations. Only the LLVM and C code generators will be available.
See :ref:`unreg` for more details.
.. _make-mode:
Using ``ghc`` ``--make``
~~~~~~~~~~~~~~~~~~~~~~~~
.. index::
single: --make; mode of GHC
single: separate compilation
In this mode, GHC will build a multi-module Haskell program by following
dependencies from one or more root modules (usually just ``Main``). For
example, if your ``Main`` module is in a file called :file:`Main.hs`, you
could compile and link the program like this:
.. code-block:: none
ghc --make Main.hs
In fact, GHC enters make mode automatically if there are any Haskell
source files on the command line and no other mode is specified, so in
this case we could just type
.. code-block:: none
ghc Main.hs
Any number of source file names or module names may be specified; GHC
will figure out all the modules in the program by following the imports
from these initial modules. It will then attempt to compile each module
which is out of date, and finally, if there is a ``Main`` module, the
program will also be linked into an executable.
The main advantages to using ``ghc --make`` over traditional
``Makefile``\s are:
- GHC doesn't have to be restarted for each compilation, which means it
can cache information between compilations. Compiling a multi-module
program with ``ghc --make`` can be up to twice as fast as running
``ghc`` individually on each source file.
- You don't have to write a ``Makefile``.
.. index::
single: Makefiles; avoiding
- GHC re-calculates the dependencies each time it is invoked, so the
dependencies never get out of sync with the source.
- Using the :ghc-flag:`-j[⟨n⟩]` flag, you can compile modules in parallel.
Specify ``-j ⟨n⟩`` to compile ⟨n⟩ jobs in parallel. If ⟨n⟩ is omitted, then
it defaults to the number of processors.
Any of the command-line options described in the rest of this chapter
can be used with ``--make``, but note that any options you give on the
command line will apply to all the source files compiled, so if you want
any options to apply to a single source file only, you'll need to use an
``OPTIONS_GHC`` pragma (see :ref:`source-file-options`).
If the program needs to be linked with additional objects (say, some
auxiliary C code), then the object files can be given on the command
line and GHC will include them when linking the executable.
For backward compatibility with existing make scripts, when used in
combination with :ghc-flag:`-c`, the linking phase is omitted (same as
``--make -no-link``).
Note that GHC can only follow dependencies if it has the source file
available, so if your program includes a module for which there is no
source file, even if you have an object and an interface file for the
module, then GHC will complain. The exception to this rule is for
package modules, which may or may not have source files.
The source files for the program don't all need to be in the same
directory; the :ghc-flag:`-i` option can be used to add directories to the
search path (see :ref:`search-path`).
.. ghc-flag:: -j[⟨n⟩]
:shortdesc: When compiling with :ghc-flag:`--make`, compile ⟨n⟩ modules
in parallel.
:type: dynamic
:category: misc
Perform compilation in parallel when possible. GHC will use up to ⟨N⟩
threads during compilation. If N is omitted, then it defaults to the
number of processors. Note that compilation of a module may not begin
until its dependencies have been built.
GHC Jobserver Protocol
~~~~~~~~~~~~~~~~~~~~~~
The GHC Jobserver Protocol was specified in `GHC proposal #540 `__.
This protocol allows
a server to dynamically invoke many instances of a client process,
while restricting all of those instances to use no more than capabilities.
This is achieved by coordination over a system semaphore (either a POSIX
semaphore in the case of Linux and Darwin, or a Win32 semaphore
in the case of Windows platforms).
There are two kinds of participants in the GHC Jobserver protocol:
- The *jobserver* creates a system semaphore with a certain number of
available tokens.
Each time the jobserver wants to spawn a new jobclient subprocess, it **must**
first acquire a single token from the semaphore, before spawning
the subprocess. This token **must** be released once the subprocess terminates.
Once work is finished, the jobserver **must** destroy the semaphore it created.
- A *jobclient* is a subprocess spawned by the jobserver or another jobclient.
Each jobclient starts with one available token (its *implicit token*,
which was acquired by the parent which spawned it), and can request more
tokens through the Jobserver Protocol by waiting on the semaphore.
Each time a jobclient wants to spawn a new jobclient subprocess, it **must**
pass on a single token to the child jobclient. This token can either be the
jobclient's implicit token, or another token which the jobclient acquired
from the semaphore.
Each jobclient **must** release exactly as many tokens as it has acquired from
the semaphore (this does not include the implicit tokens).
GHC itself acts as a jobclient which can be enabled by using the flag ``-jsem``.
.. ghc-flag:: -jsem
:shortdesc: When compiling with :ghc-flag:`--make`, coordinate with
other processes through the semaphore ⟨sem⟩ to compile
modules in parallel.
:type: dynamic
:category: misc
Perform compilation in parallel when possible, coordinating with other
processes through the semaphore ⟨sem⟩ (specified as a string).
Error if the semaphore doesn't exist.
Use of ``-jsem`` will override use of :ghc-flag:``-j[⟨n⟩]``,
and vice-versa.
.. _multi-home-units:
Multiple Home Units
~~~~~~~~~~~~~~~~~~~
The compiler also has support for building multiple units in a single compiler
invocation. In modern projects it is common to work on multiple interdependent
packages at once, using the support for multiple home units you can load all
these local packages into one ghc session and quickly get feedback about how
changes affect other dependent packages.
In order to specify multiple units, the `-unit @⟨filename⟩`:ghc-flag: is given multiple times
with a response file containing the arguments for each unit. The response file contains
a newline separated list of arguments.
.. code-block:: none
ghc -unit @unitA -unit @unitB
where the ``unitA`` response file contains the normal arguments that you would
pass to ``--make`` mode.
.. code-block:: none
-this-unit-id a-0.1.0.0
-i
-isrc
A1
A2
...
Then when the compiler starts in ``--make`` mode it will compile both units ``a`` and ``b``.
There is also very basic support for multiple home units in GHCi, at the moment you can start
a GHCi session with multiple units but only the `:reload`:ghci-cmd: is supported.
.. ghc-flag:: -unit @⟨filename⟩
:shortdesc: Specify the options to build a specific unit.
:type: dynamic
:category: misc
This option is passed multiple times to inform the compiler about all the
home units which it will compile. The options for each unit are supplied
in a response file which contains a newline separated list of normal arguments.
There are a few extra flags which have been introduced to make working with multiple
units easier.
.. ghc-flag:: -working-dir ⟨dir⟩
:shortdesc: Specify the directory a unit is expected to be compiled in.
:type: dynamic
:category:
It is common to assume that a package is compiled in the directory where its
cabal file resides. Thus, all paths used in the compiler are assumed to be relative
to this directory. When there are multiple home units the compiler is often
not operating in the standard directory and instead where the cabal.project
file is located. In this case the `-working-dir` option can be passed which specifies
the path from the current directory to the directory the unit assumes to be its root,
normally the directory which contains the cabal file.
When the flag is passed, any relative paths used by the compiler are offset
by the working directory. Notably this includes `-i`:ghc-flag: and `-I⟨dir⟩`:ghc-flag: flags.
This option can also be queried by the ``getPackageRoot`` Template Haskell
function. It is intended to be used with helper functions such as ``makeRelativeToProject``
which make relative filepaths relative to the compilation directory rather than
the directory which contains the .cabal file.
.. ghc-flag:: -this-package-name ⟨unit-id⟩
:shortdesc: The name of the package which this module would be part of when installed.
:type: dynamic
:category:
This flag papers over the awkward interaction of the `PackageImports`:extension:
and multiple home units. When using ``PackageImports`` you can specify the name
of the package in an import to disambiguate between modules which appear in multiple
packages with the same name.
This flag allows a home unit to be given a package name so that you can also
disambiguate between multiple home units which provide modules with the same name.
.. ghc-flag:: -hidden-module ⟨module name⟩
:shortdesc: A module which should not be visible outside its unit.
:type: dynamic
:category:
This flag can be supplied multiple times in order to specify which modules
in a home unit should not be visible outside of the unit it belongs to.
The main use of this flag is to be able to recreate the difference between
an exposed and hidden module for installed packages.
.. ghc-flag:: -reexported-module ⟨module name⟩
:shortdesc: A module which should be reexported from this unit.
:type: dynamic
:category:
This flag can be supplied multiple times in order to specify which modules
are not defined in a unit but should be reexported. The effect is that other
units will see this module as if it was defined in this unit.
The use of this flag is to be able to replicate the reexported modules
feature of packages with multiple home units.
The home unit closure requirement
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
There is one very important closure property which you must ensure when using
multiple home units.
Any external unit must not depend on any home unit.
This closure property is checked by the compiler but it's up to the tool invoking
GHC to ensure that the supplied list of home units obeys this invariant.
For example, if we have three units, ``p``, ``q`` and ``r``, where ``p`` depends on ``q`` and
``q`` depends on ``r``, then the closure property states that if we load ``p`` and ``r`` as
home units then we must also load ``q``, because ``q`` depends on the home unit ``r`` and we need
``q`` because ``p`` depends on it.
.. _eval-mode:
Expression evaluation mode
~~~~~~~~~~~~~~~~~~~~~~~~~~
This mode is very similar to interactive mode, except that there is a
single expression to evaluate which is specified on the command line as
an argument to the ``-e`` option:
.. code-block:: none
ghc -e expr
Haskell source files may be named on the command line, and they will be
loaded exactly as in interactive mode. The expression is evaluated in
the context of the loaded modules.
For example, to load and run a Haskell program containing a module
``Main``, we might say:
.. code-block:: none