Configure.hs 10.3 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 (InstalledPackageIndex)
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
-- | 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
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
67 68
    -- Cabal < 1.19.2 doesn't support '--exact-configuration' which is needed
    -- for '--allow-newer' to work.
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
69 70 71 72 73 74 75
    allowNewer = fromFlagOrDefault False $
                 fmap isAllowNewer (configAllowNewer configExFlags)

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

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

90 91
  installedPkgIndex <- getInstalledPackages verbosity comp packageDBs conf
  sourcePkgDb       <- getSourcePackages    verbosity repos
92

93
  progress <- planLocalPackage verbosity comp platform configFlags configExFlags
94
                               installedPkgIndex sourcePkgDb
95 96

  notice verbosity "Resolving dependencies..."
97 98
  maybePlan <- foldProgress logMsg (return . Left) (return . Right)
                            progress
99
  case maybePlan of
100 101 102 103
    Left message -> do
      info verbosity message
      setupWrapper verbosity (setupScriptOptions installedPkgIndex) Nothing
        configureCommand (const configFlags) extraArgs
104 105

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

145 146
    logMsg message rest = debug verbosity message >> rest

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

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

169 170 171 172
      testsEnabled = fromFlagOrDefault False $ configTests configFlags
      benchmarksEnabled =
        fromFlagOrDefault False $ configBenchmarks configFlags

173
      resolverParams =
174 175
          removeUpperBounds (fromFlagOrDefault AllowNewerNone $
                             configAllowNewer configExFlags)
176

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

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

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

201
        $ standardInstallPolicy
202 203
            installedPkgIndex
            (SourcePackageDb mempty packagePrefs)
204
            [SpecificSourcePackage localPkg]
205

206
  return (resolveDependencies platform (compilerId comp) solver resolverParams)
207

208

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

  setupWrapper verbosity
    scriptOptions (Just pkg) configureCommand configureFlags extraArgs

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

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