Configure.hs 7.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
-----------------------------------------------------------------------------
-- |
-- 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
18 19
import Distribution.Client.Dependency.Types
         ( Solver(..) )
20 21 22
import qualified Distribution.Client.InstallPlan as InstallPlan
import Distribution.Client.InstallPlan (InstallPlan)
import Distribution.Client.IndexUtils as IndexUtils
23
         ( getSourcePackages, getInstalledPackages )
24
import Distribution.Client.Setup
25
         ( ConfigExFlags(..), configureCommand, filterConfigureFlags )
26
import Distribution.Client.Types as Source
27 28
import Distribution.Client.SetupWrapper
         ( setupWrapper, SetupScriptOptions(..), defaultSetupScriptOptions )
29 30
import Distribution.Client.Targets
         ( userToPackageConstraint )
31 32

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

56 57
import Data.Monoid (Monoid(..))

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

71 72
  installedPkgIndex <- getInstalledPackages verbosity comp packageDBs conf
  sourcePkgDb       <- getSourcePackages    verbosity repos
73

74
  progress <- planLocalPackage verbosity comp configFlags configExFlags
75
                               installedPkgIndex sourcePkgDb
76 77

  notice verbosity "Resolving dependencies..."
78 79
  maybePlan <- foldProgress logMsg (return . Left) (return . Right)
                            progress
80 81 82
  case maybePlan of
    Left message -> do
      info verbosity message
83
      setupWrapper verbosity (setupScriptOptions installedPkgIndex) Nothing
84 85 86
        configureCommand (const configFlags) extraArgs

    Right installPlan -> case InstallPlan.ready installPlan of
87
      [pkg@(ConfiguredPackage (SourcePackage _ _ (LocalUnpackedPackage _)) _ _)] ->
88 89 90
        configurePackage verbosity
          (InstallPlan.planPlatform installPlan)
          (InstallPlan.planCompiler installPlan)
91
          (setupScriptOptions installedPkgIndex)
92 93 94 95 96 97 98
          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
99
      useCabalVersion  = maybe anyVersion thisVersion
100
                         (flagToMaybe (configCabalVersion configExFlags)),
101 102 103 104
      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.
105 106 107 108
      usePackageDB     = if UserPackageDB `elem` packageDBs
                           then packageDBs
                           else packageDBs ++ [UserPackageDB],
      usePackageIndex  = if UserPackageDB `elem` packageDBs
109
                           then Just index
110
                           else Nothing,
111
      useProgramConfig = conf,
112
      useDistPref      = fromFlagOrDefault
113
                           (useDistPref defaultSetupScriptOptions)
114
                           (configDistPref configFlags),
115 116 117 118
      useLoggingHandle = Nothing,
      useWorkingDir    = Nothing
    }

119 120
    logMsg message rest = debug verbosity message >> rest

121 122 123 124
-- | Make an 'InstallPlan' for the unpacked package in the current directory,
-- and all its dependencies.
--
planLocalPackage :: Verbosity -> Compiler
125
                 -> ConfigFlags -> ConfigExFlags
126
                 -> PackageIndex
127
                 -> SourcePackageDb
128
                 -> IO (Progress String String InstallPlan)
129 130
planLocalPackage verbosity comp configFlags configExFlags installedPkgIndex
  (SourcePackageDb _ packagePrefs) = do
131
  pkg <- readPackageDescription verbosity =<< defaultPackageDesc verbosity
132 133

  let -- We create a local package and ask to resolve a dependency on it
134 135 136 137
      localPkg = SourcePackage {
        packageInfoId             = packageId pkg,
        Source.packageDescription = pkg,
        packageSource             = LocalUnpackedPackage "."
138
      }
139 140 141 142 143 144 145 146 147 148

      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
149 150 151
            -- TODO: should warn or error on constraints that are not on direct deps
            -- or flag constraints not on the package in question.
            (map userToPackageConstraint (configExConstraints configExFlags))
152 153 154

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

158
        $ standardInstallPolicy
159 160
            installedPkgIndex
            (SourcePackageDb mempty packagePrefs)
161
            [SpecificSourcePackage localPkg]
162

163
  return (resolveDependencies buildPlatform (compilerId comp) Modular resolverParams)
164

165

166
-- | Call an installer for an 'SourcePackage' but override the configure
167 168 169 170 171 172 173 174
-- 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
175
                 -> ConfigFlags
176 177 178
                 -> ConfiguredPackage
                 -> [String]
                 -> IO ()
179
configurePackage verbosity platform comp scriptOptions configFlags
180
  (ConfiguredPackage (SourcePackage _ gpkg _) flags deps) extraArgs =
181 182 183 184 185 186

  setupWrapper verbosity
    scriptOptions (Just pkg) configureCommand configureFlags extraArgs

  where
    configureFlags   = filterConfigureFlags configFlags {
187 188 189
      configConfigurationsFlags = flags,
      configConstraints         = map thisPackageVersion deps,
      configVerbosity           = toFlag verbosity
190 191 192
    }

    pkg = case finalizePackageDescription flags
193 194
           (const True)
           platform comp [] gpkg of
195 196
      Left _ -> error "finalizePackageDescription ConfiguredPackage failed"
      Right (desc, _) -> desc