-
Notifications
You must be signed in to change notification settings - Fork 846
/
Copy pathBuild.hs
437 lines (412 loc) · 16.3 KB
/
Build.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
-- | Build the project.
module Stack.Build
( buildCmd
, build
, buildLocalTargets
, loadPackage
, mkBaseConfigOpts
, splitObjsWarning
) where
import Data.Attoparsec.Args ( EscapingMode (Escaping), parseArgs )
import qualified Data.Either.Extra as EE
import Data.List ( (\\) )
import Data.List.Extra ( groupSort )
import qualified Data.Map as Map
import qualified Data.Set as Set
-- import qualified Distribution.PackageDescription as C
-- import Distribution.Types.Dependency ( Dependency (..), depLibraries )
import Distribution.Version ( mkVersion )
import RIO.NonEmpty ( nonEmpty )
import qualified RIO.NonEmpty as NE
import Stack.Build.ConstructPlan ( constructPlan )
import Stack.Build.Execute ( executePlan, preFetch, printPlan )
import Stack.Build.Installed ( getInstalled, toInstallMap )
import Stack.Build.Source ( localDependencies, projectLocalPackages )
import Stack.Build.Target ( NeedTargets (..) )
import Stack.FileWatch ( fileWatch, fileWatchPoll )
import Stack.Package ( buildableExes, resolvePackage )
import Stack.Prelude hiding ( loadPackage )
import Stack.Runners ( ShouldReexec (..), withConfig, withEnvConfig )
import Stack.Setup ( withNewLocalBuildTargets )
import Stack.Types.Build
( Plan (..), Task (..), TaskType (..), taskLocation
, taskProvides
)
import Stack.Types.Build.Exception
( BuildException (..), BuildPrettyException (..) )
import Stack.Types.BuildConfig ( HasBuildConfig, configFileL )
import Stack.Types.BuildOpts ( BuildOpts (..) )
import Stack.Types.BuildOptsCLI
( BuildCommand (..), BuildOptsCLI (..), FileWatchOpts (..) )
import Stack.Types.BuildOptsMonoid
( buildOptsMonoidBenchmarksL, buildOptsMonoidHaddockL
, buildOptsMonoidInstallExesL, buildOptsMonoidTestsL
)
import Stack.Types.Compiler ( getGhcVersion )
import Stack.Types.CompilerPaths ( HasCompiler, cabalVersionL )
import Stack.Types.ComponentUtils
( StackUnqualCompName, unqualCompToString )
import Stack.Types.Config ( Config (..), HasConfig (..), buildOptsL )
import Stack.Types.ConfigureOpts ( BaseConfigOpts (..) )
import Stack.Types.EnvConfig
( EnvConfig (..), HasEnvConfig (..), HasSourceMap
, actualCompilerVersionL, installationRootDeps
, installationRootLocal, packageDatabaseDeps
, packageDatabaseExtra, packageDatabaseLocal
)
import Stack.Types.GlobalOpts ( globalOptsBuildOptsMonoidL )
import Stack.Types.NamedComponent ( exeComponents )
import Stack.Types.Package
( InstallLocation (..), LocalPackage (..), Package (..)
, PackageConfig (..), lpFiles, lpFilesForComponents
)
import Stack.Types.Platform ( HasPlatform (..) )
import Stack.Types.Runner ( Runner, globalOptsL )
import Stack.Types.SourceMap
( SMTargets (..), SourceMap (..), Target (..) )
import System.Terminal ( fixCodePage )
newtype CabalVersionPrettyException
= CabalVersionNotSupported Version
deriving (Show, Typeable)
instance Pretty CabalVersionPrettyException where
pretty (CabalVersionNotSupported cabalVer) =
"[S-5973]"
<> line
<> fillSep
[ flow "Stack builds with the version of the Cabal package that comes \
\with the specified version of GHC. However, Stack no longer \
\supports such Cabal versions before 2.2. Version"
, fromString $ versionString cabalVer
, flow "was found. To fix this, either use Stack"
, downgradeRecommendation
, flow "or earlier or use a snapshot that specifies a version of GHC \
\that is 8.4 or later. Stackage LTS Haskell 12.0"
, parens (style Shell "lts-12.0")
, flow "or later or Nightly 2018-03-13"
, parens (style Shell "nightly-2018-03-13")
, flow "or later specify such GHC versions."
]
where
-- Due to a bug, Stack 2.15.1 does not support Cabal < 2.
downgradeRecommendation = if cabalVer < mkVersion [2]
then "2.15.3 or 2.13.1"
else "2.15.3"
instance Exception CabalVersionPrettyException
-- | Helper for build and install commands
buildCmd :: BuildOptsCLI -> RIO Runner ()
buildCmd opts = do
when (any (("-prof" `elem`) . fromRight [] . parseArgs Escaping) opts.ghcOptions) $
prettyThrowIO GHCProfOptionInvalid
local (over globalOptsL modifyGO) $
case opts.fileWatch of
FileWatchPoll -> withFileWatchHook fileWatchPoll
FileWatch -> withFileWatchHook fileWatch
NoFileWatch -> inner Nothing
where
withFileWatchHook fileWatchAction =
-- This loads the full configuration in order to obtain the file-watch-hook
-- setting. That is likely not the most efficient approach.
withConfig YesReexec $ withEnvConfig NeedTargets opts $
fileWatchAction (inner . Just)
inner ::
Maybe (Set (Path Abs File) -> IO ())
-> RIO Runner ()
inner setLocalFiles = withConfig YesReexec $ withEnvConfig NeedTargets opts $
Stack.Build.build setLocalFiles
-- Read the build command from the CLI and enable it to run
modifyGO =
case opts.command of
Test -> set
(globalOptsBuildOptsMonoidL . buildOptsMonoidTestsL)
(Just True)
Haddock -> set
(globalOptsBuildOptsMonoidL . buildOptsMonoidHaddockL)
(Just True)
Bench -> set
(globalOptsBuildOptsMonoidL . buildOptsMonoidBenchmarksL)
(Just True)
Install -> set
(globalOptsBuildOptsMonoidL . buildOptsMonoidInstallExesL)
(Just True)
Build -> id -- Default case is just Build
-- | Build.
--
-- If a buildLock is passed there is an important contract here. That lock must
-- protect the snapshot, and it must be safe to unlock it if there are no further
-- modifications to the snapshot to be performed by this build.
build :: HasEnvConfig env
=> Maybe (Set (Path Abs File) -> IO ()) -- ^ callback after discovering all local files
-> RIO env ()
build msetLocalFiles = do
mcp <- view $ configL . to (.modifyCodePage)
ghcVersion <- view $ actualCompilerVersionL . to getGhcVersion
fixCodePage mcp ghcVersion $ do
bopts <- view buildOptsL
sourceMap <- view $ envConfigL . to (.sourceMap)
locals <- projectLocalPackages
depsLocals <- localDependencies
boptsCli <- view $ envConfigL . to (.buildOptsCLI)
-- Set local files, necessary for file watching
configFile <- view configFileL
let allLocals = locals <> depsLocals
-- We are indifferent as to whether the configuration file is a
-- user-specifc global or a project-level one.
eitherConfigFile = EE.fromEither configFile
for_ msetLocalFiles $ \setLocalFiles -> do
files <-
if boptsCli.watchAll
then sequence [lpFiles lp | lp <- allLocals]
else forM allLocals $ \lp -> do
let pn = lp.package.name
case Map.lookup pn sourceMap.targets.targets of
Nothing ->
pure Set.empty
Just (TargetAll _) ->
lpFiles lp
Just (TargetComps components) ->
lpFilesForComponents components lp
liftIO $ setLocalFiles $ Set.insert eitherConfigFile $ Set.unions files
checkComponentsBuildable allLocals
installMap <- toInstallMap sourceMap
(installedMap, globalDumpPkgs, snapshotDumpPkgs, localDumpPkgs) <-
getInstalled installMap
baseConfigOpts <- mkBaseConfigOpts boptsCli
plan <- constructPlan
baseConfigOpts
localDumpPkgs
loadPackage
sourceMap
installedMap
boptsCli.initialBuildSteps
allowLocals <- view $ configL . to (.allowLocals)
unless allowLocals $ case justLocals plan of
[] -> pure ()
localsIdents -> throwM $ LocalPackagesPresent localsIdents
checkCabalVersion
haddockCompsSupported <- warnAboutHaddockComps bopts
let disableHaddockComps =
local $ over buildOptsL $ \bo ->
bo
{ haddockExecutables = False
, haddockTests = False
, haddockBenchmarks = False
}
withHaddockCompsGuarded = if haddockCompsSupported
then id
else disableHaddockComps
warnAboutSplitObjs bopts
warnIfExecutablesWithSameNameCouldBeOverwritten locals plan
when bopts.preFetch $
preFetch plan
if boptsCli.dryrun
then
printPlan plan
else
withHaddockCompsGuarded $ executePlan
boptsCli
baseConfigOpts
locals
globalDumpPkgs
snapshotDumpPkgs
localDumpPkgs
installedMap
sourceMap.targets.targets
plan
buildLocalTargets ::
HasEnvConfig env
=> NonEmpty Text
-> RIO env (Either SomeException ())
buildLocalTargets targets =
tryAny $ withNewLocalBuildTargets (NE.toList targets) $ build Nothing
justLocals :: Plan -> [PackageIdentifier]
justLocals =
map taskProvides .
filter ((== Local) . taskLocation) .
Map.elems .
(.tasks)
checkCabalVersion :: HasEnvConfig env => RIO env ()
checkCabalVersion = do
cabalVer <- view cabalVersionL
when (cabalVer < mkVersion [2, 2]) $
prettyThrowM $ CabalVersionNotSupported cabalVer
-- | See https://github.com/commercialhaskell/stack/issues/1198.
warnIfExecutablesWithSameNameCouldBeOverwritten ::
HasTerm env
=> [LocalPackage]
-> Plan
-> RIO env ()
warnIfExecutablesWithSameNameCouldBeOverwritten locals plan = do
logDebug "Checking if we are going to build multiple executables with the same name"
forM_ (Map.toList warnings) $ \(exe, (toBuild, otherLocals)) -> do
let exe_s
| length toBuild > 1 = flow "several executables with the same name:"
| otherwise = "executable"
exesText pkgs =
fillSep $ punctuate
","
[ style
PkgComponent
(fromString $ packageNameString p <> ":" <> unqualCompToString exe)
| p <- pkgs
]
prettyWarnL $
[ "Building"
, exe_s
, exesText toBuild <> "."
]
<> [ fillSep
[ flow "Only one of them will be available via"
, style Shell "stack exec"
, flow "or locally installed."
]
| length toBuild > 1
]
<> [ fillSep
[ flow "Other executables with the same name might be overwritten:"
, exesText otherLocals <> "."
]
| not (null otherLocals)
]
where
-- Cases of several project packages having executables with the same name.
-- The Map entries have the following form:
--
-- executable name: ( package names for executables that are being built
-- , package names for other project packages that have an
-- executable with the same name
-- )
warnings :: Map StackUnqualCompName ([PackageName],[PackageName])
warnings =
Map.mapMaybe
(\(pkgsToBuild, localPkgs) ->
case (pkgsToBuild, NE.toList localPkgs \\ NE.toList pkgsToBuild) of
(_ :| [], []) ->
-- We want to build the executable of single project package
-- and there are no other project packages with an executable of
-- the same name. Nothing to warn about, ignore.
Nothing
(_, otherLocals) ->
-- We could be here for two reasons (or their combination):
-- 1) We are building two or more executables with the same
-- name that will end up overwriting each other.
-- 2) In addition to the executable(s) that we want to build
-- there are other project packages with an executable of the
-- same name that might get overwritten.
-- Both cases warrant a warning.
Just (NE.toList pkgsToBuild, otherLocals))
(Map.intersectionWith (,) exesToBuild localExes)
exesToBuild :: Map StackUnqualCompName (NonEmpty PackageName)
exesToBuild =
collect
[ (exe, pkgName')
| (pkgName', task) <- Map.toList plan.tasks
, TTLocalMutable lp <- [task.taskType]
, exe <- (Set.toList . exeComponents . (.components)) lp
]
localExes :: Map StackUnqualCompName (NonEmpty PackageName)
localExes =
collect
[ (exe, pkg.name)
| pkg <- map (.package) locals
, exe <- Set.toList (buildableExes pkg)
]
collect :: Ord k => [(k, v)] -> Map k (NonEmpty v)
collect = Map.mapMaybe nonEmpty . Map.fromDistinctAscList . groupSort
warnAboutHaddockComps ::
(HasCompiler env, HasTerm env)
=> BuildOpts
-> RIO env Bool
warnAboutHaddockComps bopts = do
let haddockCompsWanted =
bopts.haddockExecutables
|| bopts.haddockTests
|| bopts.haddockBenchmarks
cabalVer <- view cabalVersionL
if haddockCompsWanted && cabalVer < mkVersion [3, 8, 1]
then do
prettyWarnL
[ flow "Stack builds Haddock documentation with the version of the \
\Cabal package that comes with the specified version of GHC. \
\Version"
, fromString $ versionString cabalVer
, flow "was found, which does not support the building of \
\documentation for executables, test suites or benchmarks. \
\Options to build such documentation will be ignored. To use \
\the options, use a snapshot that specifies a version of GHC \
\that is 9.4 or later. Stackage LTS Haskell 21.0"
, parens (style Shell "lts-21.0")
, flow "or later or Nightly 2022-11-19"
, parens (style Shell "nightly-2022-11-19")
, flow "or later specify such GHC versions."
]
pure False
else pure haddockCompsWanted
warnAboutSplitObjs :: HasTerm env => BuildOpts -> RIO env ()
warnAboutSplitObjs bopts | bopts.splitObjs =
prettyWarnL
[ flow "Building with"
, style Shell "--split-objs"
, flow "is enabled."
, flow splitObjsWarning
]
warnAboutSplitObjs _ = pure ()
splitObjsWarning :: String
splitObjsWarning =
"Note that this feature is EXPERIMENTAL, and its behavior may be changed and \
\improved. You will need to clean your workdirs before use. If you want to \
\compile all dependencies with split-objs, you will need to delete the \
\snapshot (and all snapshots that could reference that snapshot)."
-- | Get the @BaseConfigOpts@ necessary for constructing configure options
mkBaseConfigOpts :: (HasEnvConfig env)
=> BuildOptsCLI -> RIO env BaseConfigOpts
mkBaseConfigOpts buildOptsCLI = do
buildOpts <- view buildOptsL
snapDB <- packageDatabaseDeps
localDB <- packageDatabaseLocal
snapInstallRoot <- installationRootDeps
localInstallRoot <- installationRootLocal
extraDBs <- packageDatabaseExtra
pure BaseConfigOpts
{ snapDB
, localDB
, snapInstallRoot
, localInstallRoot
, buildOpts
, buildOptsCLI
, extraDBs
}
-- | Provide a function for loading package information from the package index
loadPackage ::
(HasBuildConfig env, HasSourceMap env)
=> PackageLocationImmutable
-> Map FlagName Bool
-> [Text] -- ^ GHC options
-> [Text] -- ^ Cabal configure options
-> RIO env Package
loadPackage loc flags ghcOptions cabalConfigOpts = do
compilerVersion <- view actualCompilerVersionL
platform <- view platformL
let pkgConfig = PackageConfig
{ enableTests = False
, enableBenchmarks = False
, flags
, ghcOptions
, cabalConfigOpts
, compilerVersion
, platform
}
resolvePackage pkgConfig <$> loadCabalFileImmutable loc
checkComponentsBuildable :: MonadThrow m => [LocalPackage] -> m ()
checkComponentsBuildable lps =
unless (null unbuildable) $
prettyThrowM $ SomeTargetsNotBuildable unbuildable
where
unbuildable =
[ (lp.package.name, c)
| lp <- lps
, c <- Set.toList lp.unbuildable
]