Configure.hs 9.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
-----------------------------------------------------------------------------
-- |
-- 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,
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
15
    chooseCabalVersion,
16 17 18
  ) where

import Distribution.Client.Dependency
19
import Distribution.Client.Dependency.Types (AllowNewer(..), isAllowNewer)
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
import Distribution.Simple.Setup
37
         ( ConfigFlags(..), fromFlag, toFlag, flagToMaybe, fromFlagOrDefault )
38
import Distribution.Simple.PackageIndex (PackageIndex)
39 40
import Distribution.Simple.Utils
         ( defaultPackageDesc )
41
import qualified Distribution.InstalledPackageInfo as Installed
42
import Distribution.Package
43
         ( Package(..), packageName, Dependency(..), thisPackageVersion )
44 45 46 47 48
import Distribution.PackageDescription.Parse
         ( readPackageDescription )
import Distribution.PackageDescription.Configuration
         ( finalizePackageDescription )
import Distribution.Version
49
         ( anyVersion, thisVersion )
50
import Distribution.Simple.Utils as Utils
51
         ( notice, debug, die )
52
import Distribution.System
53
         ( Platform )
54 55
import Distribution.Verbosity as Verbosity
         ( Verbosity )
56
import Distribution.Version
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
57
         ( Version(..), VersionRange, orLaterVersion )
58

59 60
import Data.Monoid (Monoid(..))

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
61 62 63 64 65 66 67 68 69 70 71 72 73
-- | Choose the Cabal version such that the setup scripts compiled against this
-- version will support the given command-line flags.
chooseCabalVersion :: ConfigExFlags -> Maybe Version -> VersionRange
chooseCabalVersion configExFlags maybeVersion =
  maybe defaultVersionRange thisVersion maybeVersion
  where
    allowNewer = fromFlagOrDefault False $
                 fmap isAllowNewer (configAllowNewer configExFlags)

    defaultVersionRange = if allowNewer
                          then orLaterVersion (Version [1,19,2] [])
                          else anyVersion

74 75
-- | Configure the package found in the local directory
configure :: Verbosity
76
          -> PackageDBStack
77 78
          -> [Repo]
          -> Compiler
79
          -> Platform
80
          -> ProgramConfiguration
81
          -> ConfigFlags
82
          -> ConfigExFlags
83 84
          -> [String]
          -> IO ()
85
configure verbosity packageDBs repos comp platform conf
86 87
  configFlags configExFlags extraArgs = do

88 89
  installedPkgIndex <- getInstalledPackages verbosity comp packageDBs conf
  sourcePkgDb       <- getSourcePackages    verbosity repos
90

91
  progress <- planLocalPackage verbosity comp platform configFlags configExFlags
92
                               installedPkgIndex sourcePkgDb
93 94

  notice verbosity "Resolving dependencies..."
95 96
  maybePlan <- foldProgress logMsg (return . Left) (return . Right)
                            progress
97
  case maybePlan of
98
    Left message -> die message
99 100

    Right installPlan -> case InstallPlan.ready installPlan of
101
      [pkg@(ReadyPackage (SourcePackage _ _ (LocalUnpackedPackage _) _) _ _ _)] ->
102 103 104
        configurePackage verbosity
          (InstallPlan.planPlatform installPlan)
          (InstallPlan.planCompiler installPlan)
105
          (setupScriptOptions installedPkgIndex)
106 107 108 109 110 111 112
          configFlags pkg extraArgs

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

  where
    setupScriptOptions index = SetupScriptOptions {
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
113
      useCabalVersion  = chooseCabalVersion configExFlags
114
                         (flagToMaybe (configCabalVersion configExFlags)),
115
      useCompiler      = Just comp,
116
      usePlatform      = Just platform,
Duncan Coutts's avatar
Duncan Coutts committed
117 118
      usePackageDB     = packageDBs',
      usePackageIndex  = index',
119
      useProgramConfig = conf,
120
      useDistPref      = fromFlagOrDefault
121
                           (useDistPref defaultSetupScriptOptions)
122
                           (configDistPref configFlags),
123
      useLoggingHandle = Nothing,
124
      useWorkingDir    = Nothing,
refold's avatar
refold committed
125
      forceExternalSetupMethod = False,
refold's avatar
refold committed
126
      setupCacheLock   = Nothing
127
    }
Duncan Coutts's avatar
Duncan Coutts committed
128 129 130 131 132 133 134 135 136 137
      where
        -- 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.
        (packageDBs', index') =
          case packageDBs of
            (GlobalPackageDB:dbs) | UserPackageDB `notElem` dbs
                -> (GlobalPackageDB:UserPackageDB:dbs, Nothing)
            -- but if the user is using an odd db stack, don't touch it
            dbs -> (dbs, Just index)
138

139 140
    logMsg message rest = debug verbosity message >> rest

141 142 143 144
-- | Make an 'InstallPlan' for the unpacked package in the current directory,
-- and all its dependencies.
--
planLocalPackage :: Verbosity -> Compiler
145
                 -> Platform
146
                 -> ConfigFlags -> ConfigExFlags
147
                 -> PackageIndex
148
                 -> SourcePackageDb
149
                 -> IO (Progress String String InstallPlan)
150
planLocalPackage verbosity comp platform configFlags configExFlags installedPkgIndex
151
  (SourcePackageDb _ packagePrefs) = do
152
  pkg <- readPackageDescription verbosity =<< defaultPackageDesc verbosity
153
  solver <- chooseSolver verbosity (fromFlag $ configSolver configExFlags) (compilerId comp)
154 155

  let -- We create a local package and ask to resolve a dependency on it
156 157 158
      localPkg = SourcePackage {
        packageInfoId             = packageId pkg,
        Source.packageDescription = pkg,
159 160
        packageSource             = LocalUnpackedPackage ".",
        packageDescrOverride      = Nothing
161
      }
162

163 164 165 166
      testsEnabled = fromFlagOrDefault False $ configTests configFlags
      benchmarksEnabled =
        fromFlagOrDefault False $ configBenchmarks configFlags

167
      resolverParams =
168 169
          removeUpperBounds (fromFlagOrDefault AllowNewerNone $
                             configAllowNewer configExFlags)
170

171
        . addPreferences
172 173 174 175 176 177
            -- 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
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
178 179
            -- TODO: should warn or error on constraints that are not on direct
            -- deps or flag constraints not on the package in question.
180
            (map userToPackageConstraint (configExConstraints configExFlags))
181 182 183

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

187 188 189
        . addConstraints
            -- '--enable-tests' and '--enable-benchmarks' constraints from
            -- command line
EyalLotem's avatar
EyalLotem committed
190 191 192
            [ PackageConstraintStanzas (packageName pkg) $
                [ TestStanzas  | testsEnabled ] ++
                [ BenchStanzas | benchmarksEnabled ]
193 194
            ]

195
        $ standardInstallPolicy
196 197
            installedPkgIndex
            (SourcePackageDb mempty packagePrefs)
198
            [SpecificSourcePackage localPkg]
199

200
  return (resolveDependencies platform (compilerId comp) solver resolverParams)
201

202

203
-- | Call an installer for an 'SourcePackage' but override the configure
204 205
-- flags with the ones given by the 'ReadyPackage'. In particular the
-- 'ReadyPackage' specifies an exact 'FlagAssignment' and exactly
206 207 208
-- versioned package dependencies. So we ignore any previous partial flag
-- assignment or dependency constraints and use the new ones.
--
209 210
-- NB: when updating this function, don't forget to also update
-- 'installReadyPackage' in D.C.Install.
211 212 213
configurePackage :: Verbosity
                 -> Platform -> CompilerId
                 -> SetupScriptOptions
214
                 -> ConfigFlags
215
                 -> ReadyPackage
216 217
                 -> [String]
                 -> IO ()
218
configurePackage verbosity platform comp scriptOptions configFlags
219
  (ReadyPackage (SourcePackage _ gpkg _ _) flags stanzas deps) extraArgs =
220 221 222 223 224 225

  setupWrapper verbosity
    scriptOptions (Just pkg) configureCommand configureFlags extraArgs

  where
    configureFlags   = filterConfigureFlags configFlags {
226
      configConfigurationsFlags = flags,
227 228 229 230 231 232 233 234
      -- We generate the legacy constraints as well as the new style precise
      -- deps.  In the end only one set gets passed to Setup.hs configure,
      -- depending on the Cabal version we are talking to.
      configConstraints  = [ thisPackageVersion (packageId deppkg)
                           | deppkg <- deps ],
      configDependencies = [ (packageName (Installed.sourcePackageId deppkg),
                              Installed.installedPackageId deppkg)
                           | deppkg <- deps ],
235 236
      -- Use '--exact-configuration' if supported.
      configExactConfiguration = toFlag True,
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
237 238 239
      configVerbosity          = toFlag verbosity,
      configBenchmarks         = toFlag (BenchStanzas `elem` stanzas),
      configTests              = toFlag (TestStanzas `elem` stanzas)
240 241 242
    }

    pkg = case finalizePackageDescription flags
243
           (const True)
244
           platform comp [] (enableStanzas stanzas gpkg) of
245
      Left _ -> error "finalizePackageDescription ReadyPackage failed"
246
      Right (desc, _) -> desc