Skip to content

Commit 7d99572

Browse files
committed
Fix #6691 ghc-options: ensure configurability not overlooked in online docs
Also corrects the tutorial for the position after the change in the default for `apply-ghc-options` from Stack 0.1.6.0. Also makes some stylistic changes to the content of this part of the tutorial-like guide.
1 parent 60e10c2 commit 7d99572

File tree

1 file changed

+76
-51
lines changed

1 file changed

+76
-51
lines changed

doc/tutorial/cabal_flags_and_ghc_options.md

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
# 10. Cabal flags and GHC options
44

5-
There are two common ways to alter how a package will install: with Cabal flags
6-
and with GHC options.
5+
There are two common ways to affect how a package will be built: with Cabal
6+
flags and with GHC options.
77

8-
## Cabal flag management
8+
## Cabal flags
99

10-
To change a Cabal flag setting, we can use the command line `--flag` option. The
11-
`yackage` package has an `upload` flag that is enabled by default. We can
12-
command:
10+
Cabal flags can be set or unset at the command line or as a project-specific
11+
Stack option.
12+
13+
To set or unset a Cabal flag at the command line, we can use the `--flag`
14+
option. The `yackage` package has an `upload` flag that is enabled by default.
15+
We can command:
1316

1417
~~~text
1518
stack build --flag yackage:-upload
@@ -38,64 +41,84 @@ option documentation.
3841

3942
## GHC options
4043

41-
GHC options follow a similar logic as in managing Cabal flags, with a few
42-
nuances to adjust for common use cases. Let's consider the command:
44+
GHC options can be specified at the command line or as an non-project specific
45+
Stack option.
46+
47+
At the command line, consider the command:
4348

4449
~~~text
4550
stack build --ghc-options="-Wall -Werror"
4651
~~~
4752

48-
This will set the `-Wall -Werror` options for all *local targets*. Note that
49-
this will not affect extra-dep and snapshot packages at all. This design
53+
or, equivalently:
54+
55+
~~~text
56+
stack build --ghc-options=--pedantic
57+
~~~
58+
59+
By default, this will set GHC's `-Wall` and `-Werror` options for all *project
60+
packages*. This will not, however, affect other packages at all. This design
5061
provides us with reproducible and fast builds.
5162

52-
(By the way: the above GHC options have a special convenience flag:
53-
`--pedantic`.)
63+
??? question "Can GHC options for other packages be specified at the command line?"
5464

55-
There's one extra nuance about command line GHC options: Since they only apply
56-
to local targets, if you change your local targets, they will no longer apply
57-
to other packages. Let's play around with an example from the `wai` repository,
58-
which includes the `wai` and `warp` packages, the latter depending on the
59-
former. If we command again:
65+
Yes, GHC options can be specified at the command line for all packages or
66+
only project packages that are targets. For further information, see the
67+
documentation for the
68+
[apply-ghc-options](../configure/yaml/non-project.md#apply-ghc-options)
69+
non-project specific configuration option.
6070

61-
~~~text
62-
stack build --ghc-options=-O0 wai
63-
~~~
71+
??? question "What if GHC options specified at the command line apply only to targets?"
6472

65-
It will build all of the dependencies of `wai`, and then build `wai` with all
66-
optimizations disabled. Now let's add in `warp` as well. Command:
73+
By changing the default using the
74+
[apply-ghc-options](../configure/yaml/non-project.md#apply-ghc-options)
75+
configuration option, it is possble to specify that GHC options at the
76+
command line apply only to project packages that are *targets*. If this is
77+
done and you change your targets, the options will no longer apply to other
78+
project packages.
6779

68-
~~~text
69-
stack build --ghc-options=-O0 wai warp
70-
~~~
80+
Let us consider an example from the `wai` repository, which includes the
81+
`wai` and `warp` packages, the latter depending on the former. If we
82+
command:
7183

72-
This builds the additional dependencies for `warp`, and then builds `warp` with
73-
optimizations disabled. Importantly: it does not rebuild `wai`, since `wai`'s
74-
configuration has not been altered. Now the surprising case. Command:
84+
~~~text
85+
stack build --ghc-options=-O0 wai
86+
~~~
7587

76-
~~~text
77-
stack build --ghc-options=-O0 warp
78-
wai-3.0.3.0-5a49351d03cba6cbaf906972d788e65d: unregistering (flags changed from ["--ghc-options","-O0"] to [])
79-
warp-3.1.3-a91c7c3108f63376877cb3cd5dbe8a7a: unregistering (missing dependencies: wai)
80-
wai-3.0.3.0: configure
81-
~~~
88+
Stack will build all of the dependencies of `wai` (inclduding `warp`) and then
89+
build `wai` with all GHC optimizations disabled.
8290

83-
You may expect this to be a no-op: neither `wai` nor `warp` has changed.
84-
However, Stack will instead recompile `wai` with optimizations enabled again,
85-
and then rebuild `warp` (with optimizations disabled) against this newly built
86-
`wai`. The reason: reproducible builds. If we'd never built `wai` or `warp`
87-
before, trying to build `warp` would necessitate building all of its
88-
dependencies, and it would do so with default GHC options (optimizations
89-
enabled). This dependency would include `wai`. So when we command:
91+
Now let's add `warp` as a target. If we command:
9092

91-
~~~text
92-
stack build --ghc-options=-O0 warp
93-
~~~
93+
~~~text
94+
stack build --ghc-options=-O0 wai warp
95+
~~~
96+
97+
this builds the additional dependencies for `warp`, and then builds `warp`
98+
with GHC optimizations disabled. Importantly, Stack does not rebuild `wai`,
99+
since `wai`'s configuration has not been altered.
100+
101+
Now the surprising case. If we command:
102+
103+
~~~text
104+
stack build --ghc-options=-O0 warp
105+
~~~
106+
107+
you may expect this to do nothing, as neither `wai` nor `warp` has changed.
108+
However, Stack will rebuild `wai` with GHC optimizations enabled again, and
109+
then rebuild `warp` (with optimizations disabled) against this newly-built
110+
`wai`. The reason is reproducible builds. If we had never built `wai` or
111+
`warp` before, trying to build `warp` would require building all of its
112+
dependencies, and it would do so with default GHC options (that is, GHC
113+
optimizations enabled). These dependencies would include `wai`. So when we
114+
command:
115+
116+
~~~text
117+
stack build --ghc-options=-O0 warp
118+
~~~
94119

95-
We want its behavior to be unaffected by any previous build steps we took.
96-
While this specific corner case does catch people by surprise, the overall goal
97-
of reproducible builds is - in the Stack maintainers' views - worth the
98-
confusion.
120+
we want Stack's behavior to be unaffected by any previous build steps we
121+
took.
99122

100123
If you have GHC options that you will be applying regularly when building your
101124
packages, you can add them to your Stack project-level configuration file
@@ -105,7 +128,9 @@ For more information, see the
105128
[ghc-options](../configure/yaml/non-project.md#ghc-options) non-project specific
106129
configuration option documentation.
107130

108-
!!! note
131+
??? question "Can Stack be configured to specify GHC options for specific packages?"
109132

110-
That's it, the heavy content of this guide is done! Everything from here on
111-
out is simple explanations of commands. Congratulations!
133+
Yes, Stack can be configured to specify GHC options for specific packages,
134+
either globally or at the project level. For more information, see the
135+
[ghc-options](../configure/yaml/non-project.md#ghc-options) non-project
136+
specific configuration option documentation.

0 commit comments

Comments
 (0)