Configure.hs 10.1 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, info, 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 99 100 101
    Left message -> do
      info verbosity message
      setupWrapper verbosity (setupScriptOptions installedPkgIndex) Nothing
        configureCommand (const configFlags) extraArgs
102 103

    Right installPlan -> case InstallPlan.ready installPlan of
104
      [pkg@(ReadyPackage (SourcePackage _ _ (LocalUnpackedPackage _) _) _ _ _)] ->
105 106 107
        configurePackage verbosity
          (InstallPlan.planPlatform installPlan)
          (InstallPlan.planCompiler installPlan)
108
          (setupScriptOptions installedPkgIndex)
109 110 111 112 113 114 115
          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
116
      useCabalVersion  = chooseCabalVersion configExFlags
117
                         (flagToMaybe (configCabalVersion configExFlags)),
118
      useCompiler      = Just comp,
119
      usePlatform      = Just platform,
Duncan Coutts's avatar
Duncan Coutts committed
120 121
      usePackageDB     = packageDBs',
      usePackageIndex  = index',
122
      useProgramConfig = conf,
123
      useDistPref      = fromFlagOrDefault
124
                           (useDistPref defaultSetupScriptOptions)
125
                           (configDistPref configFlags),
126
      useLoggingHandle = Nothing,
127
      useWorkingDir    = Nothing,
refold's avatar
refold committed
128
      forceExternalSetupMethod = False,
refold's avatar
refold committed
129
      setupCacheLock   = Nothing
130
    }
Duncan Coutts's avatar
Duncan Coutts committed
131 132 133 134 135 136 137 138 139 140
      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)
141

142 143
    logMsg message rest = debug verbosity message >> rest

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

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

166 167 168 169
      testsEnabled = fromFlagOrDefault False $ configTests configFlags
      benchmarksEnabled =
        fromFlagOrDefault False $ configBenchmarks configFlags

170
      resolverParams =
171 172
          removeUpperBounds (fromFlagOrDefault AllowNewerNone $
                             configAllowNewer configExFlags)
173

174
        . addPreferences
175 176 177 178 179 180
            -- 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
181 182
            -- TODO: should warn or error on constraints that are not on direct
            -- deps or flag constraints not on the package in question.
183
            (map userToPackageConstraint (configExConstraints configExFlags))
184 185 186

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

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

198
        $ standardInstallPolicy
199 200
            installedPkgIndex
            (SourcePackageDb mempty packagePrefs)
201
            [SpecificSourcePackage localPkg]
202

203
  return (resolveDependencies platform (compilerId comp) solver resolverParams)
204

205

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

  setupWrapper verbosity
    scriptOptions (Just pkg) configureCommand configureFlags extraArgs

  where
    configureFlags   = filterConfigureFlags configFlags {
229
      configConfigurationsFlags = flags,
230 231 232 233 234 235 236 237
      -- 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 ],
238 239
      -- Use '--exact-configuration' if supported.
      configExactConfiguration = toFlag True,
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
240 241 242
      configVerbosity          = toFlag verbosity,
      configBenchmarks         = toFlag (BenchStanzas `elem` stanzas),
      configTests              = toFlag (TestStanzas `elem` stanzas)
243 244 245
    }

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