forked from haskell/cabal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnpackedPackage.hs
More file actions
749 lines (668 loc) · 26.3 KB
/
Copy pathUnpackedPackage.hs
File metadata and controls
749 lines (668 loc) · 26.3 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | This module exposes functions to build and register packages.
module Distribution.Client.ProjectBuilding.UnpackedPackage
( buildAndInstallUnpackedPackage
, PackageBuildingPhase(..)
-- ** Utilities
, annotateFailure
, annotateFailureNoLog
) where
import Distribution.Client.Compat.Prelude
import Prelude ()
import Distribution.Client.ProjectBuilding.Types
import Distribution.Client.ProjectConfig
import Distribution.Client.ProjectConfig.Types
import Distribution.Client.ProjectPlanning
import Distribution.Client.ProjectPlanning.Types
import Distribution.Client.DistDirLayout
import Distribution.Client.JobControl
import Distribution.Client.Setup
( CommonSetupFlags
, filterBenchmarkFlags
, filterBuildFlags
, filterConfigureFlags
, filterCopyFlags
, filterHaddockArgs
, filterHaddockFlags
, filterRegisterFlags
, filterReplFlags
, filterTestFlags
)
import Distribution.Client.SetupWrapper
import Distribution.Client.Types hiding
( BuildFailure (..)
, BuildOutcome
, BuildOutcomes
, BuildResult (..)
)
import Distribution.Client.Utils
( ProgressPhase (..)
, progressMessage
)
import Distribution.Compat.Lens
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
import qualified Distribution.InstalledPackageInfo as Installed
import Distribution.Package
import Distribution.Simple.Command (CommandUI)
import Distribution.Simple.Compiler
( PackageDBStackCWD
, coercePackageDBStack
)
import qualified Distribution.Simple.Configure as Cabal
import Distribution.Simple.LocalBuildInfo
( ComponentName (..)
, LibraryName (..)
)
import qualified Distribution.Simple.LocalBuildInfo as Cabal
import Distribution.Simple.Program
import qualified Distribution.Simple.Register as Cabal
import qualified Distribution.Simple.Setup as Cabal
import Distribution.Types.PackageDescription.Lens (componentModules)
import Distribution.Client.Errors
import Distribution.Simple.Utils
import Distribution.Utils.Path hiding
( (<.>)
, (</>)
)
import Distribution.Verbosity (setVerbosityHandles)
import Distribution.Version
import Distribution.Client.ProjectBuilding.PackageFileMonitor
import qualified Data.ByteString as BS
import qualified Data.List.NonEmpty as NE
import Control.Exception (Handler (..), SomeAsyncException, catches, onException)
import System.Directory (canonicalizePath, createDirectoryIfMissing)
import System.FilePath (takeDirectory, (</>))
import System.IO (Handle, IOMode (AppendMode), withFile)
import System.Semaphore (SemaphoreName (..))
import GHC.Stack
import qualified Distribution.Compat.Graph as Graph
import Distribution.PackageDescription (BuildType(..))
import qualified Distribution.PackageDescription as PD
import Distribution.Client.FileMonitor (MonitorFilePath, monitorFileHashed, beginUpdateFileMonitor)
import Distribution.Client.SourceFiles (needElaboratedConfiguredPackage)
import Distribution.Client.SrcDist (allPackageSourceFiles)
import Distribution.Client.RebuildMonad (execRebuild)
-- | Each unpacked package is processed in the following phases:
--
-- * Configure phase
-- * Build phase
-- * Haddock phase
-- * Install phase (copy + register)
-- * Register phase
-- * Test phase
-- * Bench phase
-- * Repl phase
--
-- Depending on whether we are installing the package or building it inplace,
-- the phases will be carried out differently. For example, when installing,
-- the test, benchmark, and repl phase are ignored.
data PackageBuildingPhase r where
PBConfigurePhase :: {runConfigure :: IO InLibraryLBI} -> PackageBuildingPhase InLibraryLBI
PBBuildPhase :: {runBuild :: IO [MonitorFilePath]} -> PackageBuildingPhase ()
PBHaddockPhase :: {runHaddock :: IO [MonitorFilePath]} -> PackageBuildingPhase ()
PBReplPhase :: {runRepl :: IO [MonitorFilePath]} -> PackageBuildingPhase ()
PBInstallPhase
:: { runCopy :: Cabal.CopyDest -> IO ()
, runRegister
:: PackageDBStackCWD
-> Cabal.RegisterOptions
-> IO InstalledPackageInfo
}
-> PackageBuildingPhase ()
PBTestPhase :: {runTest :: IO ()} -> PackageBuildingPhase ()
PBBenchPhase :: {runBench :: IO ()} -> PackageBuildingPhase ()
-- | Structures the phases of building and registering a package amongst others
-- (see t'PackageBuildingPhase'). Delegates logic specific to a certain
-- building style (notably, inplace vs install) to the delegate function that
-- receives as an argument t'PackageBuildingPhase')
buildAndRegisterUnpackedPackage
:: Verbosity
-> DistDirLayout
-> Maybe SemaphoreName
-- ^ Whether to pass a semaphore to build process
-- this is different to BuildTimeSettings because the
-- name of the semaphore is created freshly each time.
-> BuildTimeSettings
-> Lock
-> Lock
-> ElaboratedSharedConfig
-> ElaboratedReadyPackage
-> SymbolicPath CWD (Dir Pkg)
-> SymbolicPath Pkg (Dir Dist)
-> Maybe FilePath
-- ^ The path to an /initialized/ log file
-> (forall r. PackageBuildingPhase r -> IO r)
-> IO ()
buildAndRegisterUnpackedPackage
verbosity
DistDirLayout{distTempDirectory}
maybe_semaphore
buildTimeSettings@BuildTimeSettings{buildSettingKeepTempFiles}
registerLock
cacheLock
pkgshared
rpkg@(ReadyPackage pkg)
srcdir
builddir
mlogFile
delegate = do
info verbosity $ "\n\nbuildAndRegisterUnpackedPackage: " ++ prettyShow (Graph.nodeKey pkg)
-- Configure phase
mbLBI <-
delegate $
PBConfigurePhase $
annotateFailure mlogFile ConfigureFailed $ do
info verbosity $ "--- Configure phase " ++ prettyShow (Graph.nodeKey pkg)
setup configureCommand Cabal.configCommonFlags configureFlags configureArgs
(InLibraryArgs $ InLibraryConfigureArgs pkgshared rpkg)
-- Build phase
delegate $
PBBuildPhase $
annotateFailure mlogFile BuildFailed $ do
info verbosity $ "--- Build phase " ++ prettyShow (Graph.nodeKey pkg)
setup buildCommand Cabal.buildCommonFlags (return . buildFlags) buildArgs
(InLibraryArgs $ InLibraryPostConfigureArgs SBuildPhase mbLBI)
-- Haddock phase
whenHaddock $
delegate $
PBHaddockPhase $
annotateFailure mlogFile HaddocksFailed $ do
info verbosity $ "--- Haddock phase " ++ prettyShow (Graph.nodeKey pkg)
setup haddockCommand Cabal.haddockCommonFlags (return . haddockFlags) haddockArgs
(InLibraryArgs $ InLibraryPostConfigureArgs SHaddockPhase mbLBI)
-- Install phase
delegate $
PBInstallPhase
{ runCopy = \destdir ->
annotateFailure mlogFile InstallFailed $ do
info verbosity $ "--- Install phase (copy) " ++ prettyShow (Graph.nodeKey pkg)
setup Cabal.copyCommand Cabal.copyCommonFlags (return . copyFlags destdir) copyArgs
(InLibraryArgs $ InLibraryPostConfigureArgs SCopyPhase mbLBI)
, runRegister = \pkgDBStack registerOpts ->
annotateFailure mlogFile InstallFailed $ do
info verbosity $ "--- Install phase (register) " ++ prettyShow (Graph.nodeKey pkg)
-- We register ourselves rather than via Setup.hs. We need to
-- grab and modify the InstalledPackageInfo. We decide what
-- the installed package id is, not the build system.
ipkg0 <- generateInstalledPackageInfo mbLBI
let ipkg = ipkg0{Installed.installedUnitId = uid}
criticalSection registerLock $
Cabal.registerPackage
verbosity
toolchainCompiler
toolchainProgramDb
Nothing
(coercePackageDBStack pkgDBStack)
ipkg
registerOpts
return ipkg
}
-- Test phase
whenTest $
delegate $
PBTestPhase $
annotateFailure mlogFile TestsFailed $ do
info verbosity $ "--- Test phase " ++ prettyShow (Graph.nodeKey pkg)
setup testCommand Cabal.testCommonFlags (return . testFlags) testArgs
(InLibraryArgs $ InLibraryPostConfigureArgs STestPhase mbLBI)
-- Bench phase
whenBench $
delegate $
PBBenchPhase $
annotateFailure mlogFile BenchFailed $ do
info verbosity $ "--- Benchmark phase " ++ prettyShow (Graph.nodeKey pkg)
setup benchCommand Cabal.benchmarkCommonFlags (return . benchFlags) benchArgs
(InLibraryArgs $ InLibraryPostConfigureArgs SBenchPhase mbLBI)
-- Repl phase
whenRepl $
delegate $
PBReplPhase $
annotateFailure mlogFile ReplFailed $ do
info verbosity $ "--- Repl phase " ++ prettyShow (Graph.nodeKey pkg)
setupInteractive replCommand Cabal.replCommonFlags (return . replFlags) replArgs
(InLibraryArgs $ InLibraryPostConfigureArgs SReplPhase mbLBI)
return ()
where
uid = installedUnitId rpkg
Toolchain{toolchainCompiler, toolchainProgramDb} = elabToolchain pkg
comp_par_strat = case maybe_semaphore of
Just sem_name -> Cabal.toFlag (getSemaphoreName sem_name)
_ -> Cabal.NoFlag
whenTest action
| null (elabTestTargets pkg) = return ()
| otherwise = action
whenBench action
| null (elabBenchTargets pkg) = return ()
| otherwise = action
whenRepl action
| null (elabReplTarget pkg) = return ()
| otherwise = action
whenHaddock action
| hasValidHaddockTargets pkg = action
| otherwise = return ()
mbWorkDir = useWorkingDir scriptOptions
commonFlags targets =
setupHsCommonFlags verbosity mbWorkDir builddir targets buildSettingKeepTempFiles
configureCommand = Cabal.configureCommand defaultProgramDb
configureFlags v =
flip filterConfigureFlags v
<$> setupHsConfigureFlags
(fmap makeSymbolicPath . canonicalizePath)
rpkg
(commonFlags $ configureArgs v)
configureArgs _ = setupHsConfigureArgs pkg
buildCommand = Cabal.buildCommand defaultProgramDb
buildFlags v =
flip filterBuildFlags v $
setupHsBuildFlags
comp_par_strat
pkg
pkgshared
(commonFlags $ buildArgs v)
buildArgs _ = setupHsBuildArgs pkg
copyFlags destdir v =
flip filterCopyFlags v $
setupHsCopyFlags
pkg
pkgshared
(commonFlags $ buildArgs v)
destdir
-- In theory, we could want to copy less things than those that were
-- built, but instead, we simply copy the targets that were built.
copyArgs = buildArgs
testCommand = Cabal.testCommand -- defaultProgramDb
testFlags v =
flip filterTestFlags v $
setupHsTestFlags
pkg
(commonFlags $ testArgs v)
testArgs _ = setupHsTestArgs pkg
benchCommand = Cabal.benchmarkCommand
benchFlags v =
flip filterBenchmarkFlags v $
setupHsBenchFlags
pkg
pkgshared
(commonFlags $ benchArgs v)
benchArgs _ = setupHsBenchArgs pkg
replCommand = Cabal.replCommand defaultProgramDb
replFlags v =
flip filterReplFlags v $
setupHsReplFlags
pkg
pkgshared
(commonFlags $ replArgs v)
replArgs _ = setupHsReplArgs pkg
haddockCommand = Cabal.haddockCommand
haddockFlags v =
flip filterHaddockFlags v $
setupHsHaddockFlags
pkg
buildTimeSettings
(commonFlags $ haddockArgs v)
haddockArgs v =
flip filterHaddockArgs v $
setupHsHaddockArgs pkg
scriptOptions =
setupHsScriptOptions
rpkg
pkgshared
srcdir
builddir
cacheLock
setup
:: (HasCallStack, RightFlagsForPhase flags setupSpec)
=> CommandUI flags
-> (flags -> CommonSetupFlags)
-> (Version -> IO flags)
-> (Version -> [String])
-> SetupRunnerArgs setupSpec
-> IO (SetupRunnerRes setupSpec)
setup cmd getCommonFlags flags args wrapperArgs =
withLogging $ \mLogFileHandle -> do
let opts = scriptOptions{useLoggingHandle = mLogFileHandle}
setupWrapper
(setVerbosityHandles mLogFileHandle verbosity)
opts
(Just (elabPkgDescription pkg))
cmd
getCommonFlags
flags
args
wrapperArgs
setupInteractive
:: RightFlagsForPhase flags setupSpec
=> CommandUI flags
-> (flags -> CommonSetupFlags)
-> (Version -> IO flags)
-> (Version -> [String])
-> SetupRunnerArgs setupSpec
-> IO (SetupRunnerRes setupSpec)
setupInteractive =
setupWrapper
verbosity
scriptOptions { isInteractive = True }
(Just (elabPkgDescription pkg))
generateInstalledPackageInfo :: InLibraryLBI -> IO InstalledPackageInfo
generateInstalledPackageInfo mbLBI =
withTempInstalledPackageInfoFile
verbosity
distTempDirectory
$ \pkgConfDest -> do
let registerFlags v =
flip filterRegisterFlags v $
setupHsRegisterFlags
pkg
pkgshared
(commonFlags [])
pkgConfDest
setup
(Cabal.registerCommand)
Cabal.registerCommonFlags
(return . registerFlags)
(const [])
(InLibraryArgs $ InLibraryPostConfigureArgs SRegisterPhase mbLBI)
withLogging :: (Maybe Handle -> IO r) -> IO r
withLogging action =
case mlogFile of
Nothing -> action Nothing
Just logFile -> withFile logFile AppendMode (action . Just)
buildAndInstallUnpackedPackage
:: Verbosity
-> DistDirLayout
-> Maybe SemaphoreName
-- ^ Whether to pass a semaphore to build process
-- this is different to BuildTimeSettings because the
-- name of the semaphore is created freshly each time.
-> BuildTimeSettings
-> Lock
-> Lock
-> ElaboratedInstallPlan
-> ElaboratedSharedConfig
-> ElaboratedReadyPackage
-> BuildStatusRebuild
-> SymbolicPath CWD (Dir Pkg)
-> SymbolicPath Pkg (Dir Dist)
-> IO BuildResult
buildAndInstallUnpackedPackage
verbosity
distDirLayout
maybe_semaphore
buildSettings@BuildTimeSettings{buildSettingNumJobs, buildSettingLogFile}
registerLock
cacheLock
plan
pkgshared
rpkg@(ReadyPackage pkg)
buildStatus
srcdir
builddir = do
createDirectoryIfMissingVerbose verbosity True (interpretSymbolicPath (Just srcdir) builddir)
createDirectoryIfMissingVerbose verbosity True (distPackageCacheDirectory distDirLayout dparams)
-- TODO: [code cleanup] deal consistently with talking to older
-- Setup.hs versions, much like we do for ghc, with a proper
-- options type and rendering step which will also let us
-- call directly into the lib, rather than always going via
-- the lib's command line interface, which would also allow
-- passing data like installed packages, compiler, and
-- program db for a quicker configure.
-- TODO: [required feature] docs and tests
-- TODO: [required feature] sudo re-exec
initLogFile
let docsResult = DocsNotTried
testsResult = TestsNotTried
buildResult :: BuildResultMisc
buildResult = (docsResult, testsResult)
buildAndRegisterUnpackedPackage
verbosity
distDirLayout
maybe_semaphore
buildSettings
registerLock
cacheLock
pkgshared
rpkg
srcdir
builddir
mlogFile
$ \case
PBConfigurePhase{runConfigure} ->
whenReconfigure $ do
noticeProgress ProgressStarting
lbi <- runConfigure
invalidatePackageRegFileMonitor packageFileMonitor
updatePackageConfigFileMonitor packageFileMonitor (getSymbolicPath srcdir) pkg
return lbi
PBBuildPhase{runBuild} -> do
whenRebuild $ do
noticeProgress ProgressBuilding
timestamp <- beginUpdateFileMonitor
_ <- runBuild
-- Be sure to invalidate the cache if building throws an exception!
-- If not, we'll abort execution with a stale recompilation cache.
-- See ghc#24926 for an example of how this can go wrong.
`onException` invalidatePackageRegFileMonitor packageFileMonitor
let listSimple =
execRebuild (getSymbolicPath srcdir) (needElaboratedConfiguredPackage pkg)
listSdist =
fmap (map monitorFileHashed) $
allPackageSourceFiles verbosity (getSymbolicPath srcdir)
ifNullThen m m' = do
xs <- m
if null xs then m' else return xs
monitors <- case PD.buildType (elabPkgDescription pkg) of
Simple -> listSimple
-- If a Custom setup was used, AND the Cabal is recent
-- enough to have sdist --list-sources, use that to
-- determine the files that we need to track. This can
-- cause unnecessary rebuilding (for example, if README
-- is edited, we will try to rebuild) but there isn't
-- a more accurate Custom interface we can use to get
-- this info. We prefer not to use listSimple here
-- as it can miss extra source files that are considered
-- by the Custom setup.
_
| elabSetupScriptCliVersion pkg >= mkVersion [1, 17] ->
-- However, sometimes sdist --list-sources will fail
-- and return an empty list. In that case, fall
-- back on the (inaccurate) simple tracking.
listSdist `ifNullThen` listSimple
| otherwise ->
listSimple
let dep_monitors =
map monitorFileHashed $
elabInplaceDependencyBuildCacheFiles
distDirLayout
plan
pkg
updatePackageBuildFileMonitor
packageFileMonitor
(getSymbolicPath srcdir)
timestamp
pkg
buildStatus
(monitors ++ dep_monitors)
buildResult
PBHaddockPhase{runHaddock} -> do
noticeProgress ProgressHaddock
_monitors <- runHaddock
return ()
PBInstallPhase{runCopy, runRegister} -> do
-- NOTE: We re-copy and re-register if we rebuild. It seems to make sense.
whenRebuild $ do
noticeProgress ProgressInstalling
runCopy (Cabal.CopyToDb (elabRegistrationPackageDb pkg))
if elabRequiresRegistration pkg then do
ipi <- runRegister
(elabRegisterPackageDBStack pkg)
Cabal.defaultRegisterOptions
{ Cabal.registerMultiInstance = True
, Cabal.registerSuppressFilesCheck = True
}
updatePackageRegFileMonitor packageFileMonitor (getSymbolicPath srcdir) (Just ipi)
else
info verbosity $ "registerPkg: elab does NOT require registration for " ++ prettyShow uid
PBTestPhase{runTest} -> runTest
PBBenchPhase{runBench} -> runBench
PBReplPhase{runRepl} -> runRepl >> return ()
-- TODO: [nice to have] we currently rely on Setup.hs copy to do the right
-- thing. Although we do copy into an image dir and do the move into the
-- final location ourselves, perhaps we ought to do some sanity checks on
-- the image dir first.
-- TODO: [required eventually] note that for nix-style
-- installations it is not necessary to do the
-- 'withWin32SelfUpgrade' dance, but it would be necessary for a
-- shared bin dir.
noticeProgress ProgressCompleted
return
BuildResult
{ buildResultDocs = docsResult
, buildResultTests = testsResult
, buildResultLogFile = mlogFile
}
where
uid = installedUnitId rpkg
pkgid = packageId rpkg
dparams = elabDistDirParams pkg
packageFileMonitor = newPackageFileMonitor distDirLayout dparams
whenReconfigure action = case buildStatus of
BuildStatusConfigure _ -> action
_ -> InLibraryLBI <$> Cabal.getPersistBuildConfig (Just srcdir) builddir
whenRebuild :: IO () -> IO ()
whenRebuild action
| null (elabBuildTargets pkg)
, -- NB: we have to build the test/bench suite!
null (elabTestTargets pkg)
, null (elabBenchTargets pkg) =
return ()
| otherwise = action
dispname :: String
dispname = case elabPkgOrComp pkg of
-- Packages built altogether, instead of per component
ElabPackage ElaboratedPackage{pkgWhyNotPerComponent} ->
prettyShow pkgid
++ " (all, legacy fallback: "
++ unwords (map whyNotPerComponent $ NE.toList pkgWhyNotPerComponent)
++ ")"
-- Packages built per component
ElabComponent comp ->
prettyShow pkgid
++ " ("
++ maybe "custom" prettyShow (compComponentName comp)
++ ")"
noticeProgress :: ProgressPhase -> IO ()
noticeProgress phase =
when (isParallelBuild buildSettingNumJobs) $
progressMessage verbosity phase dispname
mlogFile :: Maybe FilePath
mlogFile =
case buildSettingLogFile of
Nothing -> Nothing
-- TODO: we ignore mkLogFile, because that would require us to fix the templating
-- and add support for '$stage'. Part of the templating is in Cabal (the library),
-- so doing that properly might require some refactoring and careful thought.
--
-- It also means we introduce a regression here and the user can't really overwrite
-- the log destination anymore.
--
-- Last but not least, removing the @Nothing -> Nothing@ case would trigger a bug on
-- FreeBSD, because 'getSetupMethod' would always pick the 'SelfExecMethod', which
-- then on FreeBSD would try to execute @_build/cabal/bin/cabal@ and fail, because
-- it just changed the CWD to a source directory. This needs further investigation.
--
-- An alternative patch was constructed here: https://github.com/stable-haskell/cabal/commit/604cab98c9a599953c7d95e4f37af449d5357577
Just _ -> Just
$ distDirectory distDirLayout
</> "logs"
</> prettyShow (elabStage pkg)
</> betterPlatform (elabToolchain pkg)
</> prettyShow (elabUnitId pkg)
initLogFile :: IO ()
initLogFile =
case mlogFile of
Nothing -> return ()
Just logFile -> do
createDirectoryIfMissing True (takeDirectory logFile)
removeFileForcibly logFile
--------------------------------------------------------------------------------
-- * Exported Utils
--------------------------------------------------------------------------------
{- FOURMOLU_DISABLE -}
annotateFailureNoLog :: (SomeException -> BuildFailureReason)
-> IO a -> IO a
annotateFailureNoLog annotate action =
annotateFailure Nothing annotate action
annotateFailure :: Maybe FilePath
-> (SomeException -> BuildFailureReason)
-> IO a -> IO a
annotateFailure mlogFile annotate action =
action `catches`
-- It's not just IOException and ExitCode we have to deal with, there's
-- lots, including exceptions from the hackage-security and tar packages.
-- So we take the strategy of catching everything except async exceptions.
[
Handler $ \async -> throwIO (async :: SomeAsyncException)
, Handler $ \other -> handler (other :: SomeException)
]
where
handler :: Exception e => e -> IO a
handler = throwIO . BuildFailure mlogFile . annotate . toException
--------------------------------------------------------------------------------
-- * Other Utils
--------------------------------------------------------------------------------
hasValidHaddockTargets :: ElaboratedConfiguredPackage -> Bool
hasValidHaddockTargets ElaboratedConfiguredPackage{..}
| not elabBuildHaddocks = False
| otherwise = any componentHasHaddocks components
where
components :: [ComponentTarget]
components =
elabBuildTargets
++ elabTestTargets
++ elabBenchTargets
++ elabReplTarget
++ elabHaddockTargets
componentHasHaddocks :: ComponentTarget -> Bool
componentHasHaddocks (ComponentTarget name _) =
case name of
CLibName LMainLibName -> hasHaddocks
CLibName (LSubLibName _) -> elabHaddockInternal && hasHaddocks
CFLibName _ -> elabHaddockForeignLibs && hasHaddocks
CExeName _ -> elabHaddockExecutables && hasHaddocks
CTestName _ -> elabHaddockTestSuites && hasHaddocks
CBenchName _ -> elabHaddockBenchmarks && hasHaddocks
where
hasHaddocks = not (null (elabPkgDescription ^. componentModules name))
withTempInstalledPackageInfoFile
:: Verbosity
-> FilePath
-> (FilePath -> IO ())
-> IO InstalledPackageInfo
withTempInstalledPackageInfoFile verbosity tempdir action =
withTempDirectory tempdir "package-registration-" $ \dir -> do
-- make absolute since @action@ will often change directory
abs_dir <- canonicalizePath dir
let pkgConfDest = abs_dir </> "pkgConf"
action pkgConfDest
readPkgConf "." pkgConfDest
where
pkgConfParseFailed :: String -> IO a
pkgConfParseFailed perror =
dieWithException verbosity $ PkgConfParseFailed perror
readPkgConf :: FilePath -> FilePath -> IO InstalledPackageInfo
readPkgConf pkgConfDir pkgConfFile = do
pkgConfStr <- BS.readFile (pkgConfDir </> pkgConfFile)
(warns, ipkg) <- case Installed.parseInstalledPackageInfo pkgConfStr of
Left perrors -> pkgConfParseFailed $ unlines $ NE.toList perrors
Right (warns, ipkg) -> return (warns, ipkg)
unless (null warns) $
warn verbosity $
unlines warns
return ipkg