Skip to content

Commit 6716142

Browse files
committed
Reword common stanzas in the cabal file generated by cabal init
Following #11231, this commit reworks the common stanzas present in the cabal file generated by `cabal init`.
1 parent 8365cec commit 6716142

File tree

2 files changed

+123
-74
lines changed

2 files changed

+123
-74
lines changed

cabal-install/src/Distribution/Client/Init/FileCreators.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,16 @@ writeProject (ProjectSettings opts pkgDesc libTarget exeTarget testTarget)
7373

7474
let pkgFields = mkPkgDescription opts pkgDesc
7575
commonStanza = mkCommonStanza opts
76+
extensionsStanza = mkExtensionsStanza opts
77+
ghcOptionsStanza = mkGhcOptionsStanza opts
7678

7779
libStanza <- prepareLibTarget opts libTarget
7880
exeStanza <- prepareExeTarget opts exeTarget
7981
testStanza <- prepareTestTarget opts testTarget
8082

8183
(reusedCabal, cabalContents) <-
8284
writeCabalFile opts $
83-
pkgFields ++ [commonStanza, libStanza, exeStanza, testStanza]
85+
pkgFields ++ [extensionsStanza, ghcOptionsStanza, commonStanza, libStanza, exeStanza, testStanza]
8486

8587
when (null $ _pkgSynopsis pkgDesc) $
8688
message opts T.Warning "No synopsis given. You should edit the .cabal file and add one."

cabal-install/src/Distribution/Client/Init/Format.hs

Lines changed: 120 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ module Distribution.Client.Init.Format
2626
, mkExeStanza
2727
, mkTestStanza
2828
, mkPkgDescription
29+
, mkExtensionsStanza
30+
, mkGhcOptionsStanza
31+
, mkRtsOptionsStanza
2932
) where
3033

3134
import Distribution.CabalSpecVersion
@@ -133,90 +136,134 @@ mkCommonStanza opts = case specHasCommonStanzas $ _optCabalSpec opts of
133136
[text "warnings"]
134137
[field "ghc-options" text "-Wall" [] False opts]
135138

139+
mkGhcOptionsStanza :: WriteOpts -> PrettyField FieldAnnotation
140+
mkGhcOptionsStanza opts = case specHasCommonStanzas $ _optCabalSpec opts of
141+
NoCommonStanzas -> PrettyEmpty
142+
_ ->
143+
PrettySection
144+
annNoComments
145+
"common"
146+
[text "ghc-options"]
147+
[ field "ghc-options" text "-Wall -Widentities -Wcompat" [] False opts
148+
]
149+
150+
mkRtsOptionsStanza :: WriteOpts -> PrettyField FieldAnnotation
151+
mkRtsOptionsStanza opts = case specHasCommonStanzas $ _optCabalSpec opts of
152+
NoCommonStanzas -> PrettyEmpty
153+
_ ->
154+
PrettySection
155+
annNoComments
156+
"common"
157+
[text "rts-options"]
158+
[ field "ghc-options" text "-rtsopts -threaded \"-with-rtsopts=-N -T\"" [] False opts
159+
]
160+
161+
mkExtensionsStanza :: WriteOpts -> PrettyField FieldAnnotation
162+
mkExtensionsStanza opts = case specHasCommonStanzas $ _optCabalSpec opts of
163+
NoCommonStanzas -> PrettyEmpty
164+
_ ->
165+
PrettySection
166+
annNoComments
167+
"common"
168+
[text "extensions"]
169+
[ field "default-extensions" text "" [] False opts
170+
, field "default-language" text "GHC2021" [] False opts
171+
]
172+
173+
insertCommonStanzas :: WriteOpts -> [PrettyField FieldAnnotation]
174+
insertCommonStanzas opts =
175+
case specHasCommonStanzas $ _optCabalSpec opts of
176+
NoCommonStanzas -> [PrettyEmpty]
177+
_ ->
178+
[ field
179+
"import"
180+
(hsep . map text)
181+
["extensions"]
182+
["Common language extensions"]
183+
False
184+
opts
185+
, field
186+
"import"
187+
(hsep . map text)
188+
["ghc-options"]
189+
["Common compiler warnings and optimisations"]
190+
False
191+
opts
192+
, field
193+
"import"
194+
(hsep . map text)
195+
["rts-options"]
196+
["Common RTS options"]
197+
False
198+
opts
199+
]
200+
136201
mkLibStanza :: WriteOpts -> LibTarget -> PrettyField FieldAnnotation
137202
mkLibStanza opts (LibTarget srcDirs lang expMods otherMods exts deps tools) =
138203
PrettySection
139204
annNoComments
140205
(toUTF8BS "library")
141206
[]
142-
[ case specHasCommonStanzas $ _optCabalSpec opts of
143-
NoCommonStanzas -> PrettyEmpty
144-
_ ->
145-
field
146-
"import"
147-
(hsep . map text)
148-
["warnings"]
149-
["Import common warning flags."]
150-
False
151-
opts
152-
, field
153-
"exposed-modules"
154-
formatExposedModules
155-
(toList expMods)
156-
["Modules exported by the library."]
157-
True
158-
opts
159-
, field
160-
"other-modules"
161-
formatOtherModules
162-
otherMods
163-
["Modules included in this library but not exported."]
164-
True
165-
opts
166-
, field
167-
"other-extensions"
168-
formatOtherExtensions
169-
exts
170-
["LANGUAGE extensions used by modules in this package."]
171-
True
172-
opts
173-
, field
174-
"build-depends"
175-
formatDependencyList
176-
deps
177-
["Other library packages from which modules are imported."]
178-
True
179-
opts
180-
, field
181-
"hs-source-dirs"
182-
formatHsSourceDirs
183-
(makeSymbolicPath <$> srcDirs)
184-
["Directories containing source files."]
185-
True
186-
opts
187-
, field
188-
(buildToolTag opts)
189-
formatDependencyList
190-
tools
191-
["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."]
192-
False
193-
opts
194-
, field
195-
"default-language"
196-
id
197-
lang
198-
["Base language which the package is written in."]
199-
True
200-
opts
201-
]
207+
(insertCommonStanzas opts ++ [
208+
field
209+
"exposed-modules"
210+
formatExposedModules
211+
(toList expMods)
212+
["Modules exported by the library."]
213+
True
214+
opts
215+
, field
216+
"other-modules"
217+
formatOtherModules
218+
otherMods
219+
["Modules included in this library but not exported."]
220+
True
221+
opts
222+
, field
223+
"other-extensions"
224+
formatOtherExtensions
225+
exts
226+
["LANGUAGE extensions used by modules in this package."]
227+
True
228+
opts
229+
, field
230+
"build-depends"
231+
formatDependencyList
232+
deps
233+
["Other library packages from which modules are imported."]
234+
True
235+
opts
236+
, field
237+
"hs-source-dirs"
238+
formatHsSourceDirs
239+
(makeSymbolicPath <$> srcDirs)
240+
["Directories containing source files."]
241+
True
242+
opts
243+
, field
244+
(buildToolTag opts)
245+
formatDependencyList
246+
tools
247+
["Extra tools (e.g. alex, hsc2hs, ...) needed to build the source."]
248+
False
249+
opts
250+
, field
251+
"default-language"
252+
id
253+
lang
254+
["Base language which the package is written in."]
255+
True
256+
opts
257+
])
202258

203259
mkExeStanza :: WriteOpts -> ExeTarget -> PrettyField FieldAnnotation
204260
mkExeStanza opts (ExeTarget exeMain appDirs lang otherMods exts deps tools) =
205261
PrettySection
206262
annNoComments
207263
(toUTF8BS "executable")
208264
[exeName]
209-
[ case specHasCommonStanzas $ _optCabalSpec opts of
210-
NoCommonStanzas -> PrettyEmpty
211-
_ ->
212-
field
213-
"import"
214-
(hsep . map text)
215-
["warnings"]
216-
["Import common warning flags."]
217-
False
218-
opts
219-
, field
265+
(insertCommonStanzas opts ++ [
266+
field
220267
"main-is"
221268
unsafeFromHs
222269
exeMain
@@ -265,7 +312,7 @@ mkExeStanza opts (ExeTarget exeMain appDirs lang otherMods exts deps tools) =
265312
["Base language which the package is written in."]
266313
True
267314
opts
268-
]
315+
])
269316
where
270317
exeName = pretty $ _optPkgName opts
271318

@@ -275,7 +322,7 @@ mkTestStanza opts (TestTarget testMain dirs lang otherMods exts deps tools) =
275322
annNoComments
276323
(toUTF8BS "test-suite")
277324
[suiteName]
278-
[ case specHasCommonStanzas $ _optCabalSpec opts of
325+
(insertCommonStanzas ++ [ case specHasCommonStanzas $ _optCabalSpec opts of
279326
NoCommonStanzas -> PrettyEmpty
280327
_ ->
281328
field

0 commit comments

Comments
 (0)