Install.hs 59.7 KB
Newer Older
1
{-# LANGUAGE CPP #-}
2
3
-----------------------------------------------------------------------------
-- |
4
-- Module      :  Distribution.Client.Install
5
6
7
-- Copyright   :  (c) 2005 David Himmelstrup
--                    2007 Bjorn Bringert
--                    2007-2010 Duncan Coutts
8
9
-- License     :  BSD-like
--
10
-- Maintainer  :  cabal-devel@haskell.org
11
12
13
14
15
-- Stability   :  provisional
-- Portability :  portable
--
-- High level interface to package installation.
-----------------------------------------------------------------------------
16
module Distribution.Client.Install (
17
18
19
20
21
22
23
24
25
26
27
28
    -- * High-level interface
    install,

    -- * Lower-level interface that allows to manipulate the install plan
    makeInstallContext,
    makeInstallPlan,
    processInstallPlan,
    InstallArgs,
    InstallContext,

    -- * Prune certain packages from the install plan
    pruneInstallPlan
29
  ) where
30

31
import Data.List
32
         ( isPrefixOf, unfoldr, nub, sort, (\\) )
33
import qualified Data.Set as S
Duncan Coutts's avatar
Duncan Coutts committed
34
import Data.Maybe
35
         ( isJust, fromMaybe, maybeToList )
36
import Control.Exception as Exception
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
37
         ( Exception(toException), bracket, catches
38
         , Handler(Handler), handleJust, IOException, SomeException )
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
39
40
41
42
#ifndef mingw32_HOST_OS
import Control.Exception as Exception
         ( Exception(fromException) )
#endif
Duncan Coutts's avatar
Duncan Coutts committed
43
import System.Exit
44
         ( ExitCode(..) )
45
import Distribution.Compat.Exception
46
         ( catchIO, catchExit )
47
import Control.Monad
48
         ( when, unless )
49
import System.Directory
50
51
         ( getTemporaryDirectory, doesDirectoryExist, doesFileExist,
           createDirectoryIfMissing, removeFile, renameDirectory )
Duncan Coutts's avatar
Duncan Coutts committed
52
import System.FilePath
53
         ( (</>), (<.>), equalFilePath, takeDirectory )
Duncan Coutts's avatar
Duncan Coutts committed
54
import System.IO
55
         ( openFile, IOMode(AppendMode), hClose )
56
57
import System.IO.Error
         ( isDoesNotExistError, ioeGetFileName )
bjorn@bringert.net's avatar
bjorn@bringert.net committed
58

59
import Distribution.Client.Targets
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
60
61
import Distribution.Client.Configure
         ( chooseCabalVersion )
62
import Distribution.Client.Dependency
63
import Distribution.Client.Dependency.Types
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
64
         ( Solver(..) )
Duncan Coutts's avatar
Duncan Coutts committed
65
import Distribution.Client.FetchUtils
66
import qualified Distribution.Client.Haddock as Haddock (regenerateHaddockIndex)
67
import Distribution.Client.IndexUtils as IndexUtils
68
         ( getSourcePackages, getInstalledPackages )
69
70
71
import qualified Distribution.Client.InstallPlan as InstallPlan
import Distribution.Client.InstallPlan (InstallPlan)
import Distribution.Client.Setup
72
73
         ( GlobalFlags(..)
         , ConfigFlags(..), configureCommand, filterConfigureFlags
74
         , ConfigExFlags(..), InstallFlags(..) )
Duncan Coutts's avatar
Duncan Coutts committed
75
import Distribution.Client.Config
76
         ( defaultCabalDir, defaultUserInstall )
77
78
79
import Distribution.Client.Sandbox.Timestamp
         ( withUpdateTimestamps )
import Distribution.Client.Sandbox.Types
80
81
         ( SandboxPackageInfo(..), UseSandbox(..), isUseSandbox
         , whenUsingSandbox )
82
import Distribution.Client.Tar (extractTarGzFile)
83
import Distribution.Client.Types as Source
84
85
import Distribution.Client.BuildReports.Types
         ( ReportLevel(..) )
86
import Distribution.Client.SetupWrapper
Duncan Coutts's avatar
Duncan Coutts committed
87
         ( setupWrapper, SetupScriptOptions(..), defaultSetupScriptOptions )
David Himmelstrup's avatar
David Himmelstrup committed
88
import qualified Distribution.Client.BuildReports.Anonymous as BuildReports
Duncan Coutts's avatar
Duncan Coutts committed
89
90
import qualified Distribution.Client.BuildReports.Storage as BuildReports
         ( storeAnonymous, storeLocal, fromInstallPlan )
91
import qualified Distribution.Client.InstallSymlink as InstallSymlink
92
         ( symlinkBinaries )
93
import qualified Distribution.Client.PackageIndex as SourcePackageIndex
94
import qualified Distribution.Client.Win32SelfUpgrade as Win32SelfUpgrade
95
import qualified Distribution.Client.World as World
96
import qualified Distribution.InstalledPackageInfo as Installed
97
import Distribution.Client.Compat.ExecutablePath
98
import Distribution.Client.JobControl
99

100
import Distribution.Simple.Compiler
101
         ( CompilerId(..), Compiler(compilerId), compilerFlavor
102
         , PackageDB(..), PackageDBStack, packageKeySupported )
refold's avatar
refold committed
103
104
import Distribution.Simple.Program (ProgramConfiguration,
                                    defaultProgramConfiguration)
105
import qualified Distribution.Simple.InstallDirs as InstallDirs
106
107
import qualified Distribution.Simple.PackageIndex as PackageIndex
import Distribution.Simple.PackageIndex (PackageIndex)
108
import Distribution.Simple.Setup
109
         ( haddockCommand, HaddockFlags(..)
110
         , buildCommand, BuildFlags(..), emptyBuildFlags
111
         , toFlag, fromFlag, fromFlagOrDefault, flagToMaybe, defaultDistPref )
112
import qualified Distribution.Simple.Setup as Cabal
TillmannRendel's avatar
TillmannRendel committed
113
         ( Flag(..)
114
115
         , copyCommand, CopyFlags(..), emptyCopyFlags
         , registerCommand, RegisterFlags(..), emptyRegisterFlags
TillmannRendel's avatar
TillmannRendel committed
116
         , testCommand, TestFlags(..), emptyTestFlags )
117
import Distribution.Simple.Utils
118
         ( createDirectoryIfMissingVerbose, rawSystemExit, comparing
119
         , writeFileAtomic, withTempFile , withUTF8FileContents )
120
121
import Distribution.Simple.InstallDirs as InstallDirs
         ( PathTemplate, fromPathTemplate, toPathTemplate, substPathTemplate
122
         , initialPathTemplateEnv, installDirsTemplateEnv )
123
import Distribution.Package
124
         ( PackageIdentifier, PackageId, packageName, packageVersion
125
         , Package(..), PackageFixedDeps(..), mkPackageKey
126
         , Dependency(..), thisPackageVersion, InstalledPackageId )
127
128
import qualified Distribution.PackageDescription as PackageDescription
import Distribution.PackageDescription
129
130
         ( PackageDescription, GenericPackageDescription(..), Flag(..)
         , FlagName(..), FlagAssignment )
131
import Distribution.PackageDescription.Configuration
132
         ( finalizePackageDescription )
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
133
134
import Distribution.ParseUtils
         ( showPWarning )
135
import Distribution.Version
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
136
         ( Version )
137
import Distribution.Simple.Utils as Utils
138
139
         ( notice, info, warn, debug, debugNoWrap, die
         , intercalate, withTempDirectory )
140
import Distribution.Client.Utils
141
         ( determineNumJobs, inDir, mergeBy, MergeResult(..)
142
         , tryCanonicalizePath )
143
import Distribution.System
144
         ( Platform, OS(Windows), buildOS )
145
146
import Distribution.Text
         ( display )
Duncan Coutts's avatar
Duncan Coutts committed
147
import Distribution.Verbosity as Verbosity
148
         ( Verbosity, showForCabal, normal, verbose )
149
import Distribution.Simple.BuildPaths ( exeExtension )
bjorn@bringert.net's avatar
bjorn@bringert.net committed
150

151
152
153
154
155
156
157
158
159
160
161
--TODO:
-- * assign flags to packages individually
--   * complain about flags that do not apply to any package given as target
--     so flags do not apply to dependencies, only listed, can use flag
--     constraints for dependencies
--   * only record applicable flags in world file
-- * allow flag constraints
-- * allow installed constraints
-- * allow flag and installed preferences
-- * change world file to use cabal section syntax
--   * allow persistent configure flags for each package individually
162

163
164
165
166
167
168
-- ------------------------------------------------------------
-- * Top level user actions
-- ------------------------------------------------------------

-- | Installs the packages needed to satisfy a list of dependencies.
--
169
install
170
  :: Verbosity
171
  -> PackageDBStack
172
173
  -> [Repo]
  -> Compiler
174
  -> Platform
175
  -> ProgramConfiguration
176
  -> UseSandbox
177
  -> Maybe SandboxPackageInfo
178
  -> GlobalFlags
179
  -> ConfigFlags
180
  -> ConfigExFlags
181
  -> InstallFlags
182
  -> HaddockFlags
183
  -> [UserTarget]
184
  -> IO ()
185
install verbosity packageDBs repos comp platform conf useSandbox mSandboxPkgInfo
refold's avatar
refold committed
186
187
  globalFlags configFlags configExFlags installFlags haddockFlags
  userTargets0 = do
188

189
    installContext <- makeInstallContext verbosity args (Just userTargets0)
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
190
    installPlan    <- foldProgress logMsg die' return =<<
191
192
193
194
195
                      makeInstallPlan verbosity args installContext

    processInstallPlan verbosity args installContext installPlan
  where
    args :: InstallArgs
196
    args = (packageDBs, repos, comp, platform, conf, useSandbox, mSandboxPkgInfo,
197
198
199
            globalFlags, configFlags, configExFlags, installFlags,
            haddockFlags)

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
200
201
202
203
    die' message = die (message ++ if isUseSandbox useSandbox
                                   then installFailedInSandbox else [])
    -- TODO: use a better error message, remove duplication.
    installFailedInSandbox =
204
205
206
207
      "\nNote: when using a sandbox, all packages are required to have "
      ++ "consistent dependencies. "
      ++ "Try reinstalling/unregistering the offending packages or "
      ++ "recreating the sandbox."
208
209
    logMsg message rest = debugNoWrap verbosity message >> rest

Ian D. Bollinger's avatar
Ian D. Bollinger committed
210
-- TODO: Make InstallContext a proper data type with documented fields.
211
212
213
214
-- | Common context for makeInstallPlan and processInstallPlan.
type InstallContext = ( PackageIndex, SourcePackageDb
                      , [UserTarget], [PackageSpecifier SourcePackage] )

Ian D. Bollinger's avatar
Ian D. Bollinger committed
215
-- TODO: Make InstallArgs a proper data type with documented fields or just get
refold's avatar
refold committed
216
-- rid of it completely.
217
218
219
220
-- | Initial arguments given to 'install' or 'makeInstallContext'.
type InstallArgs = ( PackageDBStack
                   , [Repo]
                   , Compiler
221
                   , Platform
222
                   , ProgramConfiguration
223
                   , UseSandbox
224
                   , Maybe SandboxPackageInfo
225
226
227
228
229
230
231
                   , GlobalFlags
                   , ConfigFlags
                   , ConfigExFlags
                   , InstallFlags
                   , HaddockFlags )

-- | Make an install context given install arguments.
232
makeInstallContext :: Verbosity -> InstallArgs -> Maybe [UserTarget]
233
234
                      -> IO InstallContext
makeInstallContext verbosity
235
  (packageDBs, repos, comp, _, conf,_,_,
236
   globalFlags, _, _, _, _) mUserTargets = do
237

238
239
    installedPkgIndex <- getInstalledPackages verbosity comp packageDBs conf
    sourcePkgDb       <- getSourcePackages    verbosity repos
240

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    (userTargets, pkgSpecifiers) <- case mUserTargets of
      Nothing           ->
        -- We want to distinguish between the case where the user has given an
        -- empty list of targets on the command-line and the case where we
        -- specifically want to have an empty list of targets.
        return ([], [])
      Just userTargets0 -> do
        -- For install, if no target is given it means we use the current
        -- directory as the single target.
        let userTargets | null userTargets0 = [UserTargetLocalDir "."]
                        | otherwise         = userTargets0

        pkgSpecifiers <- resolveUserTargets verbosity
                         (fromFlag $ globalWorldFile globalFlags)
                         (packageIndex sourcePkgDb)
                         userTargets
        return (userTargets, pkgSpecifiers)
258
259

    return (installedPkgIndex, sourcePkgDb, userTargets, pkgSpecifiers)
260

261
262
263
264
-- | Make an install plan given install context and install arguments.
makeInstallPlan :: Verbosity -> InstallArgs -> InstallContext
                -> IO (Progress String String InstallPlan)
makeInstallPlan verbosity
265
  (_, _, comp, platform, _, _, mSandboxPkgInfo,
266
267
268
269
270
271
272
   _, configFlags, configExFlags, installFlags,
   _)
  (installedPkgIndex, sourcePkgDb,
   _, pkgSpecifiers) = do

    solver <- chooseSolver verbosity (fromFlag (configSolver configExFlags))
              (compilerId comp)
273
    notice verbosity "Resolving dependencies..."
274
275
276
    return $ planPackages comp platform mSandboxPkgInfo solver
      configFlags configExFlags installFlags
      installedPkgIndex sourcePkgDb pkgSpecifiers
277

278
279
280
281
282
-- | Given an install plan, perform the actual installations.
processInstallPlan :: Verbosity -> InstallArgs -> InstallContext
                   -> InstallPlan
                   -> IO ()
processInstallPlan verbosity
283
  args@(_,_, comp, _, _, _, _, _, _, _, installFlags, _)
284
285
  (installedPkgIndex, sourcePkgDb,
   userTargets, pkgSpecifiers) installPlan = do
286
    checkPrintPlan verbosity comp installedPkgIndex installPlan sourcePkgDb
287
      installFlags pkgSpecifiers
288

289
    unless (dryRun || nothingToInstall) $ do
290
      installPlan' <- performInstallations verbosity
291
292
                      args installedPkgIndex installPlan
      postInstallActions verbosity args userTargets installPlan'
293
  where
294
    dryRun = fromFlag (installDryRun installFlags)
295
    nothingToInstall = null (InstallPlan.ready installPlan)
296

297
298
299
-- ------------------------------------------------------------
-- * Installation planning
-- ------------------------------------------------------------
300

301
planPackages :: Compiler
302
             -> Platform
303
             -> Maybe SandboxPackageInfo
304
             -> Solver
305
306
307
             -> ConfigFlags
             -> ConfigExFlags
             -> InstallFlags
308
             -> PackageIndex
309
310
             -> SourcePackageDb
             -> [PackageSpecifier SourcePackage]
311
             -> Progress String String InstallPlan
312
313
planPackages comp platform mSandboxPkgInfo solver
             configFlags configExFlags installFlags
314
             installedPkgIndex sourcePkgDb pkgSpecifiers =
315

316
        resolveDependencies
317
          platform (compilerId comp)
318
          solver
319
          resolverParams
320

321
    >>= if onlyDeps then pruneInstallPlan pkgSpecifiers else return
322

323
324
325
  where
    resolverParams =

Andres Löh's avatar
Andres Löh committed
326
327
328
        setMaxBackjumps (if maxBackjumps < 0 then Nothing
                                             else Just maxBackjumps)

329
330
      . setIndependentGoals independentGoals

Andres Löh's avatar
Andres Löh committed
331
      . setReorderGoals reorderGoals
Andres Löh's avatar
Andres Löh committed
332
333

      . setAvoidReinstalls avoidReinstalls
334

335
336
      . setShadowPkgs shadowPkgs

Andres Löh's avatar
Andres Löh committed
337
338
      . setStrongFlags strongFlags

339
      . setPreferenceDefault (if upgradeDeps then PreferAllLatest
340
341
                                             else PreferLatestForSelected)

342
      . removeUpperBounds allowNewer
343

344
345
346
347
348
349
350
      . addPreferences
          -- preferences from the config file or command line
          [ PackageVersionPreference name ver
          | Dependency name ver <- configPreferences configExFlags ]

      . addConstraints
          -- version constraints from the config file or command line
351
            (map userToPackageConstraint (configExConstraints configExFlags))
352
353
354
355

      . addConstraints
          --FIXME: this just applies all flags to all targets which
          -- is silly. We should check if the flags are appropriate
356
          [ PackageConstraintFlags (pkgSpecifierTarget pkgSpecifier) flags
357
358
          | let flags = configConfigurationsFlags configFlags
          , not (null flags)
359
360
361
362
363
          , pkgSpecifier <- pkgSpecifiers ]

      . addConstraints
          [ PackageConstraintStanzas (pkgSpecifierTarget pkgSpecifier) stanzas
          | pkgSpecifier <- pkgSpecifiers ]
364

365
366
      . maybe id applySandboxInstallPolicy mSandboxPkgInfo

367
368
      . (if reinstall then reinstallTargets else id)

369
      $ standardInstallPolicy
370
        installedPkgIndex sourcePkgDb pkgSpecifiers
371

372
373
374
375
    stanzas = concat
        [ if testsEnabled then [TestStanzas] else []
        , if benchmarksEnabled then [BenchStanzas] else []
        ]
376
    testsEnabled = fromFlagOrDefault False $ configTests configFlags
377
    benchmarksEnabled = fromFlagOrDefault False $ configBenchmarks configFlags
378

379
380
381
382
    reinstall        = fromFlag (installReinstall        installFlags)
    reorderGoals     = fromFlag (installReorderGoals     installFlags)
    independentGoals = fromFlag (installIndependentGoals installFlags)
    avoidReinstalls  = fromFlag (installAvoidReinstalls  installFlags)
383
    shadowPkgs       = fromFlag (installShadowPkgs       installFlags)
Andres Löh's avatar
Andres Löh committed
384
    strongFlags      = fromFlag (installStrongFlags      installFlags)
385
386
387
    maxBackjumps     = fromFlag (installMaxBackjumps     installFlags)
    upgradeDeps      = fromFlag (installUpgradeDeps      installFlags)
    onlyDeps         = fromFlag (installOnlyDeps         installFlags)
388
    allowNewer       = fromFlag (configAllowNewer        configExFlags)
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
-- | Remove the provided targets from the install plan.
pruneInstallPlan :: Package pkg => [PackageSpecifier pkg] -> InstallPlan
                    -> Progress String String InstallPlan
pruneInstallPlan pkgSpecifiers =
  -- TODO: this is a general feature and should be moved to D.C.Dependency
  -- Also, the InstallPlan.remove should return info more precise to the
  -- problem, rather than the very general PlanProblem type.
  either (Fail . explain) Done
  . InstallPlan.remove (\pkg -> packageName pkg `elem` targetnames)
  where
    explain :: [InstallPlan.PlanProblem] -> String
    explain problems =
      "Cannot select only the dependencies (as requested by the "
      ++ "'--only-dependencies' flag), "
      ++ (case pkgids of
             [pkgid] -> "the package " ++ display pkgid ++ " is "
             _       -> "the packages "
                        ++ intercalate ", " (map display pkgids) ++ " are ")
      ++ "required by a dependency of one of the other targets."
      where
        pkgids =
          nub [ depid
              | InstallPlan.PackageMissingDeps _ depids <- problems
              , depid <- depids
              , packageName depid `elem` targetnames ]

    targetnames  = map pkgSpecifierTarget pkgSpecifiers

418
419
420
421
-- ------------------------------------------------------------
-- * Informational messages
-- ------------------------------------------------------------

422
423
-- | Perform post-solver checks of the install plan and print it if
-- either requested or needed.
424
checkPrintPlan :: Verbosity
425
               -> Compiler
426
427
               -> PackageIndex
               -> InstallPlan
428
               -> SourcePackageDb
429
               -> InstallFlags
430
               -> [PackageSpecifier SourcePackage]
431
               -> IO ()
432
checkPrintPlan verbosity comp installed installPlan sourcePkgDb
433
  installFlags pkgSpecifiers = do
434

435
436
437
438
439
440
441
442
  -- User targets that are already installed.
  let preExistingTargets =
        [ p | let tgts = map pkgSpecifierTarget pkgSpecifiers,
              InstallPlan.PreExisting p <- InstallPlan.toList installPlan,
              packageName p `elem` tgts ]

  -- If there's nothing to install, we print the already existing
  -- target packages as an explanation.
443
  when nothingToInstall $
444
445
446
447
    notice verbosity $ unlines $
         "All the requested packages are already installed:"
       : map (display . packageId) preExistingTargets
      ++ ["Use --reinstall if you want to reinstall anyway."]
448

449
  let lPlan = linearizeInstallPlan comp installed installPlan
450
451
  -- Are any packages classified as reinstalls?
  let reinstalledPkgs = concatMap (extractReinstalls . snd) lPlan
452
453
454
455
456
457
458
459
  -- Packages that are already broken.
  let oldBrokenPkgs =
          map Installed.installedPackageId
        . PackageIndex.reverseDependencyClosure installed
        . map (Installed.installedPackageId . fst)
        . PackageIndex.brokenPackages
        $ installed
  let excluded = reinstalledPkgs ++ oldBrokenPkgs
460
  -- Packages that are reverse dependencies of replaced packages are very
461
462
463
  -- likely to be broken. We exclude packages that are already broken.
  let newBrokenPkgs =
        filter (\ p -> not (Installed.installedPackageId p `elem` excluded))
464
465
               (PackageIndex.reverseDependencyClosure installed reinstalledPkgs)
  let containsReinstalls = not (null reinstalledPkgs)
466
  let breaksPkgs         = not (null newBrokenPkgs)
467
468
469
470
471
472
473
474

  let adaptedVerbosity
        | containsReinstalls && not overrideReinstall = verbosity `max` verbose
        | otherwise                                   = verbosity

  -- We print the install plan if we are in a dry-run or if we are confronted
  -- with a dangerous install plan.
  when (dryRun || containsReinstalls && not overrideReinstall) $
475
476
    printPlan (dryRun || breaksPkgs && not overrideReinstall)
      adaptedVerbosity lPlan sourcePkgDb
477

478
479
480
481
482
483
484
485
  -- If the install plan is dangerous, we print various warning messages. In
  -- particular, if we can see that packages are likely to be broken, we even
  -- bail out (unless installation has been forced with --force-reinstalls).
  when containsReinstalls $ do
    if breaksPkgs
      then do
        (if dryRun || overrideReinstall then warn verbosity else die) $ unlines $
            "The following packages are likely to be broken by the reinstalls:"
486
          : map (display . Installed.sourcePackageId) newBrokenPkgs
487
488
489
490
491
492
493
          ++ if overrideReinstall
               then if dryRun then [] else
                 ["Continuing even though the plan contains dangerous reinstalls."]
               else
                 ["Use --force-reinstalls if you want to install anyway."]
      else unless dryRun $ warn verbosity
             "Note that reinstalls are always dangerous. Continuing anyway..."
494
495
496

  where
    nothingToInstall = null (InstallPlan.ready installPlan)
497
498

    dryRun            = fromFlag (installDryRun            installFlags)
499
    overrideReinstall = fromFlag (installOverrideReinstall installFlags)
500

501
502
linearizeInstallPlan :: Compiler
                     -> PackageIndex
503
                     -> InstallPlan
504
                     -> [(ReadyPackage, PackageStatus)]
505
linearizeInstallPlan comp installedPkgIndex plan =
506
    unfoldr next plan
507
508
509
  where
    next plan' = case InstallPlan.ready plan' of
      []      -> Nothing
510
      (pkg:_) -> Just ((pkg, status), plan'')
511
512
        where
          pkgid  = packageId pkg
513
          status = packageStatus comp installedPkgIndex pkg
514
          plan'' = InstallPlan.completed pkgid
515
                     (BuildOk DocsNotTried TestsNotTried
516
517
                              (Just $ Installed.emptyInstalledPackageInfo
                              { Installed.sourcePackageId = pkgid }))
518
519
520
                     (InstallPlan.processing [pkg] plan')
          --FIXME: This is a bit of a hack,
          -- pretending that each package is installed
521

522
523
data PackageStatus = NewPackage
                   | NewVersion [Version]
524
                   | Reinstall  [InstalledPackageId] [PackageChange]
525
526
527

type PackageChange = MergeResult PackageIdentifier PackageIdentifier

528
529
530
extractReinstalls :: PackageStatus -> [InstalledPackageId]
extractReinstalls (Reinstall ipids _) = ipids
extractReinstalls _                   = []
531

532
533
packageStatus :: Compiler -> PackageIndex -> ReadyPackage -> PackageStatus
packageStatus comp installedPkgIndex cpkg@(ReadyPackage pid flags _ deps) =
534
535
536
  case PackageIndex.lookupPackageName installedPkgIndex
                                      (packageName cpkg) of
    [] -> NewPackage
537
538
    ps ->  case filter ((==pkg_key)
                        . Installed.packageKey) (concatMap snd ps) of
539
540
541
      []           -> NewVersion (map fst ps)
      pkgs@(pkg:_) -> Reinstall (map Installed.installedPackageId pkgs)
                                (changes pkg cpkg)
542
543
544

  where

545
546
547
    pkg_key = mkPackageKey (packageKeySupported comp)
                           (packageId pid) (map Installed.packageKey deps) flags

548
    changes :: Installed.InstalledPackageInfo
549
            -> ReadyPackage
550
            -> [MergeResult PackageIdentifier PackageIdentifier]
refold's avatar
refold committed
551
552
553
554
555
556
557
558
559
560
561
    changes pkg pkg' =
      filter changed
      $ mergeBy (comparing packageName)
        -- get dependencies of installed package (convert to source pkg ids via
        -- index)
        (nub . sort . concatMap
         (maybeToList . fmap Installed.sourcePackageId .
          PackageIndex.lookupInstalledPackageId installedPkgIndex) .
         Installed.depends $ pkg)
        -- get dependencies of configured package
        (nub . sort . depends $ pkg')
562
563
564
565

    changed (InBoth    pkgid pkgid') = pkgid /= pkgid'
    changed _                        = True

566
567
printPlan :: Bool -- is dry run
          -> Verbosity
568
          -> [(ReadyPackage, PackageStatus)]
569
          -> SourcePackageDb
570
          -> IO ()
571
printPlan dryRun verbosity plan sourcePkgDb = case plan of
572
573
574
  []   -> return ()
  pkgs
    | verbosity >= Verbosity.verbose -> notice verbosity $ unlines $
575
        ("In order, the following " ++ wouldWill ++ " be installed:")
576
577
      : map showPkgAndReason pkgs
    | otherwise -> notice verbosity $ unlines $
refold's avatar
refold committed
578
579
        ("In order, the following " ++ wouldWill
         ++ " be installed (use -v for more details):")
580
      : map showPkg pkgs
581
  where
582
583
584
    wouldWill | dryRun    = "would"
              | otherwise = "will"

585
586
587
    showPkg (pkg, _) = display (packageId pkg) ++
                       showLatest (pkg)

588
    showPkgAndReason (pkg', pr) = display (packageId pkg') ++
589
          showLatest pkg' ++
590
591
          showFlagAssignment (nonDefaultFlags pkg') ++
          showStanzas (stanzas pkg') ++ " " ++
592
          case pr of
593
594
595
            NewPackage     -> "(new package)"
            NewVersion _   -> "(new version)"
            Reinstall _ cs -> "(reinstall)" ++ case cs of
596
597
598
                []   -> ""
                diff -> " changes: "  ++ intercalate ", " (map change diff)

599
    showLatest :: ReadyPackage -> String
600
601
    showLatest pkg = case mLatestVersion of
        Just latestVersion ->
602
            if pkgVersion < latestVersion
603
604
605
            then (" (latest: " ++ display latestVersion ++ ")")
            else ""
        Nothing -> ""
606
607
      where
        pkgVersion    = packageVersion pkg
608
609
610
611
612
613
        mLatestVersion :: Maybe Version
        mLatestVersion = case SourcePackageIndex.lookupPackageName
                                (packageIndex sourcePkgDb)
                                (packageName pkg) of
            [] -> Nothing
            x -> Just $ packageVersion $ last x
614

615
616
617
    toFlagAssignment :: [Flag] -> FlagAssignment
    toFlagAssignment = map (\ f -> (flagName f, flagDefault f))

618
619
    nonDefaultFlags :: ReadyPackage -> FlagAssignment
    nonDefaultFlags (ReadyPackage spkg fa _ _) =
620
621
622
623
624
      let defaultAssignment =
            toFlagAssignment
             (genPackageFlags (Source.packageDescription spkg))
      in  fa \\ defaultAssignment

625
626
    stanzas :: ReadyPackage -> [OptionalStanza]
    stanzas (ReadyPackage _ _ sts _) = sts
627
628
629
630
631
632

    showStanzas :: [OptionalStanza] -> String
    showStanzas = concatMap ((' ' :) . showStanza)
    showStanza TestStanzas  = "*test"
    showStanza BenchStanzas = "*bench"

633
    -- FIXME: this should be a proper function in a proper place
634
    showFlagAssignment :: FlagAssignment -> String
635
636
637
638
639
    showFlagAssignment = concatMap ((' ' :) . showFlagValue)
    showFlagValue (f, True)   = '+' : showFlagName f
    showFlagValue (f, False)  = '-' : showFlagName f
    showFlagName (FlagName f) = f

640
641
642
643
644
    change (OnlyInLeft pkgid)        = display pkgid ++ " removed"
    change (InBoth     pkgid pkgid') = display pkgid ++ " -> "
                                    ++ display (packageVersion pkgid')
    change (OnlyInRight      pkgid') = display pkgid' ++ " added"

645
646
647
648
649
650
651
652
653
654
-- ------------------------------------------------------------
-- * Post installation stuff
-- ------------------------------------------------------------

-- | Various stuff we do after successful or unsuccessfully installing a bunch
-- of packages. This includes:
--
--  * build reporting, local and remote
--  * symlinking binaries
--  * updating indexes
655
--  * updating world file
656
657
658
--  * error reporting
--
postInstallActions :: Verbosity
659
                   -> InstallArgs
660
                   -> [UserTarget]
661
662
663
                   -> InstallPlan
                   -> IO ()
postInstallActions verbosity
664
665
  (packageDBs, _, comp, platform, conf, useSandbox, mSandboxPkgInfo
  ,globalFlags, configFlags, _, installFlags, _)
666
  targets installPlan = do
667

668
  unless oneShot $
669
    World.insert verbosity worldFile
670
671
672
      --FIXME: does not handle flags
      [ World.WorldPkgInfo dep []
      | UserTargetNamed dep <- targets ]
673

674
  let buildReports = BuildReports.fromInstallPlan installPlan
refold's avatar
refold committed
675
676
  BuildReports.storeLocal (installSummaryFile installFlags) buildReports
    (InstallPlan.planPlatform installPlan)
677
678
679
680
681
  when (reportingLevel >= AnonymousReports) $
    BuildReports.storeAnonymous buildReports
  when (reportingLevel == DetailedReports) $
    storeDetailedBuildReports verbosity logsDir buildReports

682
  regenerateHaddockIndex verbosity packageDBs comp platform conf useSandbox
683
684
685
686
687
688
                         configFlags installFlags installPlan

  symlinkBinaries verbosity configFlags installFlags installPlan

  printBuildFailures installPlan

689
690
691
  updateSandboxTimestampsFile useSandbox mSandboxPkgInfo
                              comp platform installPlan

692
693
  where
    reportingLevel = fromFlag (installBuildReports installFlags)
694
    logsDir        = fromFlag (globalLogsDir globalFlags)
695
696
    oneShot        = fromFlag (installOneShot installFlags)
    worldFile      = fromFlag $ globalWorldFile globalFlags
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

storeDetailedBuildReports :: Verbosity -> FilePath
                          -> [(BuildReports.BuildReport, Repo)] -> IO ()
storeDetailedBuildReports verbosity logsDir reports = sequence_
  [ do dotCabal <- defaultCabalDir
       let logFileName = display (BuildReports.package report) <.> "log"
           logFile     = logsDir </> logFileName
           reportsDir  = dotCabal </> "reports" </> remoteRepoName remoteRepo
           reportFile  = reportsDir </> logFileName

       handleMissingLogFile $ do
         buildLog <- readFile logFile
         createDirectoryIfMissing True reportsDir -- FIXME
         writeFile reportFile (show (BuildReports.show report, buildLog))

  | (report, Repo { repoKind = Left remoteRepo }) <- reports
  , isLikelyToHaveLogFile (BuildReports.installOutcome report) ]

  where
    isLikelyToHaveLogFile BuildReports.ConfigureFailed {} = True
    isLikelyToHaveLogFile BuildReports.BuildFailed     {} = True
    isLikelyToHaveLogFile BuildReports.InstallFailed   {} = True
    isLikelyToHaveLogFile BuildReports.InstallOk       {} = True
    isLikelyToHaveLogFile _                               = False

    handleMissingLogFile = Exception.handleJust missingFile $ \ioe ->
      warn verbosity $ "Missing log file for build report: "
                    ++ fromMaybe ""  (ioeGetFileName ioe)

    missingFile ioe
      | isDoesNotExistError ioe  = Just ioe
    missingFile _                = Nothing


regenerateHaddockIndex :: Verbosity
                       -> [PackageDB]
                       -> Compiler
734
                       -> Platform
735
                       -> ProgramConfiguration
736
                       -> UseSandbox
737
738
739
740
                       -> ConfigFlags
                       -> InstallFlags
                       -> InstallPlan
                       -> IO ()
741
regenerateHaddockIndex verbosity packageDBs comp platform conf useSandbox
742
743
744
745
746
747
748
749
750
751
752
753
754
755
                       configFlags installFlags installPlan
  | haddockIndexFileIsRequested && shouldRegenerateHaddockIndex = do

  defaultDirs <- InstallDirs.defaultInstallDirs
                   (compilerFlavor comp)
                   (fromFlag (configUserInstall configFlags))
                   True
  let indexFileTemplate = fromFlag (installHaddockIndex installFlags)
      indexFile = substHaddockIndexFileName defaultDirs indexFileTemplate

  notice verbosity $
     "Updating documentation index " ++ indexFile

  --TODO: might be nice if the install plan gave us the new InstalledPackageInfo
756
757
  installedPkgIndex <- getInstalledPackages verbosity comp packageDBs conf
  Haddock.regenerateHaddockIndex verbosity installedPkgIndex conf indexFile
758
759
760
761
762
763
764
765

  | otherwise = return ()
  where
    haddockIndexFileIsRequested =
         fromFlag (installDocumentation installFlags)
      && isJust (flagToMaybe (installHaddockIndex installFlags))

    -- We want to regenerate the index if some new documentation was actually
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
766
767
768
769
    -- installed. Since the index can be only per-user or per-sandbox (see
    -- #1337), we don't do it for global installs or special cases where we're
    -- installing into a specific db.
    shouldRegenerateHaddockIndex = (isUseSandbox useSandbox || normalUserInstall)
770
771
772
773
774
775
                                && someDocsWereInstalled installPlan
      where
        someDocsWereInstalled = any installedDocs . InstallPlan.toList
        normalUserInstall     = (UserPackageDB `elem` packageDBs)
                             && all (not . isSpecificPackageDB) packageDBs

776
        installedDocs (InstallPlan.Installed _ (BuildOk DocsOk _ _)) = True
777
778
779
780
781
782
783
784
785
        installedDocs _                                            = False
        isSpecificPackageDB (SpecificPackageDB _) = True
        isSpecificPackageDB _                     = False

    substHaddockIndexFileName defaultDirs = fromPathTemplate
                                          . substPathTemplate env
      where
        env  = env0 ++ installDirsTemplateEnv absoluteDirs
        env0 = InstallDirs.compilerTemplateEnv (compilerId comp)
786
            ++ InstallDirs.platformTemplateEnv platform
787
788
789
790
791
792
        absoluteDirs = InstallDirs.substituteInstallDirTemplates
                         env0 templateDirs
        templateDirs = InstallDirs.combineInstallDirs fromFlagOrDefault
                         defaultDirs (configInstallDirs configFlags)


793
symlinkBinaries :: Verbosity
794
                -> ConfigFlags
795
                -> InstallFlags
796
                -> InstallPlan -> IO ()
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
symlinkBinaries verbosity configFlags installFlags plan = do
  failed <- InstallSymlink.symlinkBinaries configFlags installFlags plan
  case failed of
    [] -> return ()
    [(_, exe, path)] ->
      warn verbosity $
           "could not create a symlink in " ++ bindir ++ " for "
        ++ exe ++ " because the file exists there already but is not "
        ++ "managed by cabal. You can create a symlink for this executable "
        ++ "manually if you wish. The executable file has been installed at "
        ++ path
    exes ->
      warn verbosity $
           "could not create symlinks in " ++ bindir ++ " for "
        ++ intercalate ", " [ exe | (_, exe, _) <- exes ]
        ++ " because the files exist there already and are not "
        ++ "managed by cabal. You can create symlinks for these executables "
        ++ "manually if you wish. The executable files have been installed at "
        ++ intercalate ", " [ path | (_, _, path) <- exes ]
  where
817
    bindir = fromFlag (installSymlinkBinDir installFlags)
818

819

820
printBuildFailures :: InstallPlan -> IO ()
821
822
823
824
825
826
827
828
829
830
831
832
printBuildFailures plan =
  case [ (pkg, reason)
       | InstallPlan.Failed pkg reason <- InstallPlan.toList plan ] of
    []     -> return ()
    failed -> die . unlines
            $ "Error: some packages failed to install:"
            : [ display (packageId pkg) ++ printFailureReason reason
              | (pkg, reason) <- failed ]
  where
    printFailureReason reason = case reason of
      DependentFailed pkgid -> " depends on " ++ display pkgid
                            ++ " which failed to install."
833
      DownloadFailed  e -> " failed while downloading the package."
834
                        ++ showException e
835
      UnpackFailed    e -> " failed while unpacking the package."
836
                        ++ showException e
837
      ConfigureFailed e -> " failed during the configure step."
838
                        ++ showException e
839
      BuildFailed     e -> " failed during the building phase."
840
                        ++ showException e
841
      TestsFailed     e -> " failed during the tests phase."
842
                        ++ showException e
843
      InstallFailed   e -> " failed during the final install step."
844
845
846
847
848
849
850
                        ++ showException e

    showException e   =  " The exception was:\n  " ++ show e ++ maybeOOM e
#ifdef mingw32_HOST_OS
    maybeOOM _        = ""
#else
    maybeOOM e                    = maybe "" onExitFailure (fromException e)
851
852
    onExitFailure (ExitFailure n)
      | n == 9 || n == -9         =
853
854
855
856
      "\nThis may be due to an out-of-memory condition."
    onExitFailure _               = ""
#endif

857

858
859
860
-- | If we're working inside a sandbox and some add-source deps were installed,
-- update the timestamps of those deps.
updateSandboxTimestampsFile :: UseSandbox -> Maybe SandboxPackageInfo
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
861
862
                            -> Compiler -> Platform -> InstallPlan
                            -> IO ()
863
864
865
866
867
868
updateSandboxTimestampsFile (UseSandbox sandboxDir)
                            (Just (SandboxPackageInfo _ _ _ allAddSourceDeps))
                            comp platform installPlan =
  withUpdateTimestamps sandboxDir (compilerId comp) platform $ \_ -> do
    let allInstalled = [ pkg | InstallPlan.Installed pkg _
                            <- InstallPlan.toList installPlan ]
869
        allSrcPkgs   = [ pkg | ReadyPackage pkg _ _ _ <- allInstalled ]
870
871
872
873
874
875
        allPaths     = [ pth | LocalUnpackedPackage pth
                            <- map packageSource allSrcPkgs]
    allPathsCanonical <- mapM tryCanonicalizePath allPaths
    return $! filter (`S.member` allAddSourceDeps) allPathsCanonical

updateSandboxTimestampsFile _ _ _ _ _ = return ()
876
877
878
879
880
881
882
883
884
885

-- ------------------------------------------------------------
-- * Actually do the installations
-- ------------------------------------------------------------

data InstallMisc = InstallMisc {
    rootCmd    :: Maybe FilePath,
    libVersion :: Maybe Version
  }

refold's avatar
refold committed
886
887
888
889
-- | If logging is enabled, contains location of the log file and the verbosity
-- level for logging.
type UseLogFile = Maybe (PackageIdentifier -> FilePath, Verbosity)

890
performInstallations :: Verbosity
891
                     -> InstallArgs
892
                     -> PackageIndex
893
894
895
                     -> InstallPlan
                     -> IO InstallPlan
performInstallations verbosity
896
  (packageDBs, _, comp, _, conf, useSandbox, _,
897
   globalFlags, configFlags, configExFlags, installFlags, haddockFlags)
898
  installedPkgIndex installPlan = do
899

900
901
  -- With 'install -j' it can be a bit hard to tell whether a sandbox is used.
  whenUsingSandbox useSandbox $ \sandboxDir ->
902
    when parallelInstall $
903
904
905
      notice verbosity $ "Notice: installing into a sandbox located at "
                         ++ sandboxDir

906
907
  jobControl   <- if parallelInstall then newParallelJobControl
                                     else newSerialJobControl
908
909
  buildLimit   <- newJobLimit numJobs
  fetchLimit   <- newJobLimit (min numJobs numFetchJobs)
refold's avatar
refold committed
910
911
  installLock  <- newLock -- serialise installation
  cacheLock    <- newLock -- serialise access to setup exe cache
912

913
914
915
  executeInstallPlan verbosity jobControl useLogFile installPlan $ \rpkg ->
    installReadyPackage platform compid configFlags
                        rpkg $ \configFlags' src pkg pkgoverride ->
916
      fetchSourcePackage verbosity fetchLimit src $ \src' ->
917
918
        installLocalPackage verbosity buildLimit
                            (packageId pkg) src' distPref $ \mpath ->
refold's avatar
refold committed
919
          installUnpackedPackage verbosity buildLimit installLock numJobs
refold's avatar
refold committed
920
                                 (setupScriptOptions installedPkgIndex cacheLock)
921
                                 miscOptions configFlags' installFlags haddockFlags
922
                                 compid platform pkg pkgoverride mpath useLogFile
923
924
925
926
927

  where
    platform = InstallPlan.planPlatform installPlan
    compid   = InstallPlan.planCompiler installPlan

928
929
    numJobs         = determineNumJobs (installNumJobs installFlags)
    numFetchJobs    = 2
930
    parallelInstall = numJobs >= 2
931
932
    distPref        = fromFlagOrDefault (useDistPref defaultSetupScriptOptions)
                      (configDistPref configFlags)
933

refold's avatar
refold committed
934
    setupScriptOptions index lock = SetupScriptOptions {
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed