14.4 KB
Newer Older
1 2 3

Andrey Mokhov's avatar
Andrey Mokhov committed
4 5 6 7
Hadrian is a new build system for the [Glasgow Haskell Compiler][ghc]. It is
based on the [Shake][shake] library and we hope that it will replace the current
[Make-based build system][make] starting from GHC 8.8. If you are curious about
the rationale behind the
8 9 10 11 12 13 14 15 16 17
project and the architecture of the build system you can find more details in
this [Haskell Symposium 2016 paper]( and this
[Haskell eXchange 2016 talk][talk].

The new build system can work side-by-side with the existing build system, since it
places all build artefacts in a dedicated directory (called `_build` by default).

Your first build

Andrey Mokhov's avatar
Andrey Mokhov committed
Hadrian has not entirely caught up with the Make build system. Not afraid?
19 20
Then put on the helmet and run the following commands from the root of the GHC
21 22

./boot && ./configure
hadrian/build -j
25 26 27 28 29

or on Windows:

./boot && ./configure --enable-tarballs-autodownload
31 32 33
hadrian/build.bat -j

34 35 36 37
Here flag `-j` enables parallelism and is optional. We will further refer to the
build script simply as `build`. Note that Hadrian can also run the `boot` and
`configure` scripts automatically for you if you pass the flag `--configure`,
or simply `-c`. See the overview of command line flags below.
38 39 40


* If the default build script doesn't work, you might want to try another one,
42 43
e.g. based on Cabal sandboxes (`build-cabal.*`), Stack (`build-stack.*`) or Nix
44 45 46 47 48

* On Windows, if you do not want to install MSYS, you can use the Stack-based
build script (Stack provides a managed MSYS environment), as described in
[these instructions][windows-build]. If you don't mind installing MSYS yourself
or already have it, you can use the Cabal-based build script.

50 51 52 53 54
* Hadrian is written in Haskell and depends on `shake` (plus a few packages that
`shake` depends on), `mtl`, `quickcheck`, and GHC core libraries.

* If you have never built GHC before, start with the
[preparation guide][ghc-preparation].

56 57 58 59 60 61 62
Cheatsheet for Make users

You can find a cheatsheet-style document that shows the Hadrian equivalents of
commands that GHC users are used to run with the Make build system

63 64
Using the build system
65 66
Once your first build is successful, simply run `build` to rebuild after some
changes. Build results are placed into `_build` by default.
67 68 69 70 71 72

#### Command line flags

In addition to standard Shake flags (try `--help`), the build system
currently supports several others:

73 74 75
* `--build-root=PATH` or `-oPATH`: specify the directory in which you want to
store all build products. By default Hadrian builds everything in the `_build/`
subdirectory of the GHC source tree. Unlike the Make build system, Hadrian
Andrey Mokhov's avatar
Andrey Mokhov committed
doesn't have any "inplace" logic left any more. This option is therefore useful
77 78 79
for GHC developers who want to build GHC in different ways or at different
commits, from the same source directory, and have the build products sit in
different, isolated folders.
80 81

* `--configure` or `-c`: use this flag to run the `boot` and `configure` scripts
82 83
automatically, so that you don't have to remember to run them manually as you
normally do when using Make (typically only in the first build):
84 85 86 87
    ./configure # On Windows run ./configure --enable-tarballs-autodownload
Andrey Mokhov's avatar
Andrey Mokhov committed
88 89 90 91 92 93 94 95
    Beware that with this flag Hadrian may do network I/O on Windows to download
    necessary tarballs, which may sometimes be undesirable.

* `--flavour=FLAVOUR`: choose a build flavour. The following settings are
currently supported: `default`, `quick`, `quickest`, `perf`, `prof`, `devel1`
and `devel2`. As an example, the `quickest` flavour adds `-O0` flag to all GHC
invocations and builds libraries only in the `vanilla` way, which speeds up
builds by 3-4x. Build flavours are documented

Andrey Mokhov's avatar
Andrey Mokhov committed
98 99 100 101 102
* `--freeze1`: freeze Stage1 GHC, i.e. do not rebuild it even if some of its
source files are out-of-date. This allows to significantly reduce the rebuild
time when you are working on a feature that affects both Stage1 and Stage2
compilers, but may lead to incorrect build results. To unfreeze Stage1 GHC
simply drop the `--freeze1` flag and Hadrian will rebuild all out-of-date files.

104 105 106
* `--freeze2`: just like `--freeze1` but tell Hadrian to additionally freeze
Stage2 GHC.

Andrey Mokhov's avatar
Andrey Mokhov committed
107 108
* `--integer-simple`: build GHC using the `integer-simple` integer library
(instead of `integer-gmp`).

110 111
* `--color` and `--no-color`: choose whether to use colors when printing build
progress info. By default, Hadrian tries to determine if the terminal supports
colored output, and proceeds accordingly.

Andrey Mokhov's avatar
Andrey Mokhov committed
114 115 116 117
* `--progress-info=STYLE`: choose how build progress info is printed. There are
four settings: `none`, `brief` (one line per build command; this is the default
setting), `normal` (typically a box per build command), and `unicorn` (when
`normal` just won't do).

Andrey Mokhov's avatar
Andrey Mokhov committed
119 120
* `--verbose`: run Hadrian in verbose mode. In particular this prints diagnostic
messages by Shake oracles.

122 123 124
* `--lint`: run [Shake Lint]( during the
build to check that the build system is well formed. Note that the Lint check
currently fails under certain circumstances, as discussed in
[this ticket](

127 128 129 130 131
#### Expressions

Hadrian expressions are used extensively for specifying build settings. For an
explanation of how they work, see the [documentation](doc/

132 133
#### User settings

Andrey Mokhov's avatar
Andrey Mokhov committed
134 135 136
The Make-based build system uses `mk/` to specify user build settings.
Hadrian uses `hadrian/UserSettings.hs` for the same purpose, see

138 139
#### Building libraries and executables

Andrey Mokhov's avatar
Andrey Mokhov committed
You can build a specific library or executable for a given stage by running
141 142
`build stage<N>:<lib|exe>:<package name>`. Examples:

Andrey Mokhov's avatar
Andrey Mokhov committed
143 144 145
# Build the Stage1 GHC compiler, and place the binary to the directory
# _build/stage0/bin/ghc (because it is built by the Stage0 compiler).
146 147
build stage1:exe:ghc-bin

Andrey Mokhov's avatar
Andrey Mokhov committed
148 149
# Build the Stage2 GHC compiler, and place the binary to the directory
# _build/stage1/bin/ghc (because it is built by the Stage1 compiler).
150 151
build stage2:exe:ghc-bin

Andrey Mokhov's avatar
Andrey Mokhov committed
152 153
# Build the `ghc` library with the bootstrapping (Stage0) compiler, and register
# it in the package database stored in the directory _build/stage0/lib.
154 155
build stage0:lib:ghc

Andrey Mokhov's avatar
Andrey Mokhov committed
156 157
# Build the Cabal library with the Stage1 compiler and register it
# in the package database stored in the directory _build/stage1/lib.

Andrey Mokhov's avatar
Andrey Mokhov committed
159 160
# Build the `text` library with the Stage2 compiler and register it
# in the package database stored in the directory _build/stage2/lib.
161 162
build stage2:lib:text

Andrey Mokhov's avatar
Andrey Mokhov committed
163 164 165
# Build Haddock using the Stage1 compiler and place the binary into the
# directory _build/stage1/haddock.
build stage1:exe:haddock
166 167

168 169
#### Fast feedback using ghci

Running the `./hadrian/ghci` script will load the main compiler into
171 172 173 174
a ghci session. This is intended for fast development feedback, modules are only
typechecked so it isn't possible to run any functions in the repl.

176 177 178 179 180

You can also use this target with `ghcid`.

ghcid --command="./hadrian/ghci"
182 183 184 185 186 187 188

The first time this command is run hadrian will need to compile a few dependencies
which will take 1-2 minutes. Loading GHC into GHCi itself takes about 30 seconds and
reloads after that take in the region of 1-5 seconds depending on which modules
need to be recompiled.

189 190 191 192 193
You can further speed up the script by passing `-j` as an argument. This will
have the effect of passing `-j` to both hadrian and ghci so they will both
build in parallel.

./hadrian/ghci -j8
195 196

197 198
#### Testing

Andrey Mokhov's avatar
Andrey Mokhov committed
199 200 201 202
To run GHC testsuite, use `build test`. See
[doc/](doc/ to learn about all associated command line
flags, as well as about the equivalents of the features that the Make build
system offers.

Andrey Mokhov's avatar
Andrey Mokhov committed
`build selftest` runs tests of the build system. The current test coverage
205 206
is close to zero (see [#197][test-issue]).

207 208 209 210
#### Clean and full rebuild

* `build clean` removes all build artefacts.

Andrey Mokhov's avatar
Andrey Mokhov committed
* `build -B` forces Shake to rerun all rules, even if the previous build results
212 213
are still up-to-date.

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
#### Staged compilation

GHC is a self-hosted compiler and consequently the build proceeds in several

1. The build begins with a user-provided installation of GHC called the
   stage0 (or bootstrap) compiler which is used (via the `build.*.sh` scripts)
   to build Hadrian.
1. Hadrian uses the stage0 compiler to build a stage1 compiler (somewhat
   confusingly found in `_build/stage0/bin/ghc`), linking against the stage0
   compiler's core libraries (e.g. `base`).
1. The stage1 compiler is used to build new core libraries (found in
1. The stage1 compiler is used to build a stage2 compiler (found in
   `_build/stage1/bin/ghc`), linking against these new core libraries.
1. Optionally (see the [Building Stage3](#building-stage3) section below) the
   stage2 compiler can be used to build a stage3 compiler (found in
   `build/stage2/bin/ghc`) as a further smoke-test.

Note that the stage directories in the `_build` directory can be thought of as
named after the stage that was used to *build* the artifacts in each directory.

These stages can be summarized graphically:

![an overview of the stages of a Hadrian compilation](doc/staged-compilation.svg)

240 241
#### Documentation

Andrey Mokhov's avatar
Andrey Mokhov committed
242 243 244
To build GHC documentation, run `build docs`. Note that finer-grain
documentation targets (e.g. building only HTML documentation or only the GHC
User's Guide) are currently not supported.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

#### Source distribution

To build a GHC source distribution tarball, run `build source-dist`.

#### Binary distribution

To build a GHC binary distribution, run `build binary-dist`. The resulting
tarball contains just enough to support the

``` sh
$ ./configure [--prefix=PATH] && make install

workflow, for now.

261 262 263 264 265 266 267 268 269 270 271 272 273
### Building and installing GHC

You can get Hadrian to build _and_ install a binary distribution in one go
with the following command:

``` sh
$ build install --prefix=/some/absolute/path

This builds everything that would be shipped in a bindist, without creating
the archive, and just runs `./configure --prefix=PATH` and `make install`
to get GHC installed installed at `/some/absolute/path`.

Andrey Mokhov's avatar
Andrey Mokhov committed
#### Building Stage3

Andrey Mokhov's avatar
Andrey Mokhov committed
276 277 278 279
It is possible to define a build flavour that builds a Stage3 compiler, which is
a compiler built using Stage2. This is useful for cross-compilation. Detailed
instructions can be found in the corresponding
[part of the user settings manual](doc/

281 282 283 284 285 286 287 288 289 290
#### Integrating Hadrian into other tooling

The `tool-args` target is designed to allow hadrian to be integrated into other
tooling which uses the GHC API.
`tool-args` prints out a list of flags which hadrian will use to compile
a module in the `compiler` directory. Using these flags you can then set up
a GHC API session with the correct environment to load a module into your own
GHC session. This is how `haskell-ide-engine` is able to support hadrian.

> ./hadrian/build tool-args
292 293 294 295
-hide-all-packages -no-user-package-db -package-db _build/stage0/lib/packag...

The `./hadrian/ghci` script is implemented using this target.

298 299 300 301

Here are a few simple suggestions that might help you fix the build:

Andrey Mokhov's avatar
Andrey Mokhov committed
303 304
* If Hadrian fails with the message
  `Configuration file hadrian/cfg/system.config is missing`, you have probably
  forgotten to pass the `--configure` flag during the first build.

* With the `--configure` (`-c`) flag, Hadrian sometimes fetches a wrong
  Happy saying something like: `HappyTemplate-arrays-coerce: openFile: does not exist`
  (as mentioned [here](, in
310 311
  which case you might be better off running `./configure` manually before Hadrian.

312 313 314 315
* The call to `build test` sometimes fails with
  `libCffi_p.a: copyFile: does not exist` (as noticed
  The workaround is to `rm _build/stage1/libffi/build/inst/lib/libffi.a` and
316 317
  start over.

If you need help in debugging Hadrian, read the
319 320 321
and Shake's [debugging tutorial](
If nothing helps, don't hesitate to create a GHC issue.
322 323 324 325 326 327 328 329 330

Current limitations
The new build system still lacks many important features:
* Dynamic linking on Windows is not supported [#343][dynamic-windows-issue].

How to contribute

Andrey Mokhov's avatar
Andrey Mokhov committed
331 332 333 334 335 336
The best way to contribute is to try Hadrian, report the issues you found, and
attempt to fix them. Documentation patches are particularly welcome. Please
note: the codebase is still unstable and we expect some further refactoring. In
particular, we would like to separate the general-purpose "Hadrian library" for
building Haskell projects using Shake (`src/Hadrian/*`) from GHC specifics; see
[this issue](
337 338 339 340


Andrey Mokhov's avatar
Andrey Mokhov committed
341 342 343 344 345 346 347 348 349 350 351 352
The list of people who helped create Hadrian is long, and we hope that it will
soon become even longer! The project was first developed in a separate GitHub
repository, where you can find the list of original
[contributors][contributors]. They had to stare at Makefiles for months, so give
them all a round of applause. Simon Peyton Jones and Simon Marlow helped with
deciphering these Makefiles, and Neil Mitchell patiently explained how to
translate Makefiles to (much nicer) Shakefiles.

The initial development of Hadrian was funded by Newcastle University, EPSRC and
Microsoft Research. Other organisations that contributed at various stages of
the project are Haskell.Org and Google (both through supporting summer student
projects), as well as Well-Typed.
353 354 355

357 358
359 360
362 363 364 365