Configure.hs 7.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Client.Configure
-- Copyright   :  (c) David Himmelstrup 2005,
--                    Duncan Coutts 2005
-- License     :  BSD-like
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- High level interface to configuring a package.
-----------------------------------------------------------------------------
module Distribution.Client.Configure (
    configure,
  ) where

import Distribution.Client.Dependency
import qualified Distribution.Client.InstallPlan as InstallPlan
import Distribution.Client.InstallPlan (InstallPlan)
import Distribution.Client.IndexUtils as IndexUtils
21
         ( getAvailablePackages, getInstalledPackages )
22
import Distribution.Client.Setup
23
         ( ConfigExFlags(..), configureCommand, filterConfigureFlags )
24 25 26 27 28
import Distribution.Client.Types as Available
import Distribution.Client.SetupWrapper
         ( setupWrapper, SetupScriptOptions(..), defaultSetupScriptOptions )

import Distribution.Simple.Compiler
29 30
         ( CompilerId(..), Compiler(compilerId)
         , PackageDB(..), PackageDBStack )
31
import Distribution.Simple.Program (ProgramConfiguration )
32 33
import Distribution.Simple.Setup
         ( ConfigFlags(..), toFlag, flagToMaybe, fromFlagOrDefault )
34
import Distribution.Client.PackageIndex (PackageIndex)
35 36 37
import Distribution.Simple.Utils
         ( defaultPackageDesc )
import Distribution.Package
38
         ( Package(..), packageName, Dependency(..), thisPackageVersion )
39 40 41 42 43
import Distribution.PackageDescription.Parse
         ( readPackageDescription )
import Distribution.PackageDescription.Configuration
         ( finalizePackageDescription )
import Distribution.Version
44
         ( anyVersion, thisVersion )
45
import Distribution.Simple.Utils as Utils
46
         ( notice, info, debug, die )
47
import Distribution.System
48
         ( Platform, buildPlatform )
49 50 51 52 53
import Distribution.Verbosity as Verbosity
         ( Verbosity )

-- | Configure the package found in the local directory
configure :: Verbosity
54
          -> PackageDBStack
55 56 57
          -> [Repo]
          -> Compiler
          -> ProgramConfiguration
58
          -> ConfigFlags
59
          -> ConfigExFlags
60 61
          -> [String]
          -> IO ()
62
configure verbosity packageDBs repos comp conf
63 64
  configFlags configExFlags extraArgs = do

65
  installed <- getInstalledPackages verbosity comp packageDBs conf
66 67
  available <- getAvailablePackages verbosity repos

68
  progress <- planLocalPackage verbosity comp configFlags configExFlags
69 70 71
                               installed available

  notice verbosity "Resolving dependencies..."
72 73
  maybePlan <- foldProgress logMsg (return . Left) (return . Right)
                            progress
74 75 76 77 78 79 80
  case maybePlan of
    Left message -> do
      info verbosity message
      setupWrapper verbosity (setupScriptOptions installed) Nothing
        configureCommand (const configFlags) extraArgs

    Right installPlan -> case InstallPlan.ready installPlan of
81
      [pkg@(ConfiguredPackage (AvailablePackage _ _ (LocalUnpackedPackage _)) _ _)] ->
82 83 84 85 86 87 88 89 90 91 92
        configurePackage verbosity
          (InstallPlan.planPlatform installPlan)
          (InstallPlan.planCompiler installPlan)
          (setupScriptOptions installed)
          configFlags pkg extraArgs

      _ -> die $ "internal error: configure install plan should have exactly "
              ++ "one local ready package."

  where
    setupScriptOptions index = SetupScriptOptions {
Duncan Coutts's avatar
Duncan Coutts committed
93
      useCabalVersion  = maybe anyVersion thisVersion
94
                         (flagToMaybe (configCabalVersion configExFlags)),
95 96 97 98
      useCompiler      = Just comp,
      -- Hack: we typically want to allow the UserPackageDB for finding the
      -- Cabal lib when compiling any Setup.hs even if we're doing a global
      -- install. However we also allow looking in a specific package db.
99 100 101 102
      usePackageDB     = if UserPackageDB `elem` packageDBs
                           then packageDBs
                           else packageDBs ++ [UserPackageDB],
      usePackageIndex  = if UserPackageDB `elem` packageDBs
103
                           then Just index
104
                           else Nothing,
105
      useProgramConfig = conf,
106
      useDistPref      = fromFlagOrDefault
107
                           (useDistPref defaultSetupScriptOptions)
108
                           (configDistPref configFlags),
109 110 111 112
      useLoggingHandle = Nothing,
      useWorkingDir    = Nothing
    }

113 114
    logMsg message rest = debug verbosity message >> rest

115 116 117 118
-- | Make an 'InstallPlan' for the unpacked package in the current directory,
-- and all its dependencies.
--
planLocalPackage :: Verbosity -> Compiler
119
                 -> ConfigFlags -> ConfigExFlags
120
                 -> PackageIndex InstalledPackage
121 122
                 -> AvailablePackageDb
                 -> IO (Progress String String InstallPlan)
123
planLocalPackage verbosity comp configFlags configExFlags installed
124
  availabledb = do
125
  pkg <- readPackageDescription verbosity =<< defaultPackageDesc verbosity
126 127

  let -- We create a local package and ask to resolve a dependency on it
128 129 130
      localPkg = AvailablePackage {
        packageInfoId                = packageId pkg,
        Available.packageDescription = pkg,
131
        packageSource                = LocalUnpackedPackage "."
132
      }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

      resolverParams =

          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
            [ PackageVersionConstraint name ver
            | Dependency name ver <- configConstraints configFlags ]

        . addConstraints
            -- package flags from the config file or command line
            [ PackageFlagsConstraint (packageName pkg)
                                     (configConfigurationsFlags configFlags) ]

        $ standardInstallPolicy installed availabledb
                                [SpecificSourcePackage localPkg]

  return (resolveDependencies buildPlatform (compilerId comp) resolverParams)

156 157 158 159 160 161 162 163 164 165

-- | Call an installer for an 'AvailablePackage' but override the configure
-- flags with the ones given by the 'ConfiguredPackage'. In particular the
-- 'ConfiguredPackage' specifies an exact 'FlagAssignment' and exactly
-- versioned package dependencies. So we ignore any previous partial flag
-- assignment or dependency constraints and use the new ones.
--
configurePackage :: Verbosity
                 -> Platform -> CompilerId
                 -> SetupScriptOptions
166
                 -> ConfigFlags
167 168 169
                 -> ConfiguredPackage
                 -> [String]
                 -> IO ()
170
configurePackage verbosity platform comp scriptOptions configFlags
171 172 173 174 175 176 177
  (ConfiguredPackage (AvailablePackage _ gpkg _) flags deps) extraArgs =

  setupWrapper verbosity
    scriptOptions (Just pkg) configureCommand configureFlags extraArgs

  where
    configureFlags   = filterConfigureFlags configFlags {
178 179 180
      configConfigurationsFlags = flags,
      configConstraints         = map thisPackageVersion deps,
      configVerbosity           = toFlag verbosity
181 182 183
    }

    pkg = case finalizePackageDescription flags
184 185
           (const True)
           platform comp [] gpkg of
186 187
      Left _ -> error "finalizePackageDescription ConfiguredPackage failed"
      Right (desc, _) -> desc