2
2
3
3
# 10. Cabal flags and GHC options
4
4
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.
7
7
8
- ## Cabal flag management
8
+ ## Cabal flags
9
9
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:
13
16
14
17
~~~ text
15
18
stack build --flag yackage:-upload
@@ -38,64 +41,84 @@ option documentation.
38
41
39
42
## GHC options
40
43
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:
43
48
44
49
~~~ text
45
50
stack build --ghc-options="-Wall -Werror"
46
51
~~~
47
52
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
50
61
provides us with reproducible and fast builds.
51
62
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?"
54
64
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.
60
70
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?"
64
72
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.
67
79
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:
71
83
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
+ ~~~
75
87
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.
82
90
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:
90
92
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
+ ~~~
94
119
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.
99
122
100
123
If you have GHC options that you will be applying regularly when building your
101
124
packages, you can add them to your Stack project-level configuration file
@@ -105,7 +128,9 @@ For more information, see the
105
128
[ ghc-options] ( ../configure/yaml/non-project.md#ghc-options ) non-project specific
106
129
configuration option documentation.
107
130
108
- !!! note
131
+ ??? question "Can Stack be configured to specify GHC options for specific packages?"
109
132
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