Configure.hs 9.65 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
import Distribution.Client.Dependency.Types (AllowNewer(..), isAllowNewer)
19 20 21
import qualified Distribution.Client.InstallPlan as InstallPlan
import Distribution.Client.InstallPlan (InstallPlan)
import Distribution.Client.IndexUtils as IndexUtils
22
         ( getSourcePackages, getInstalledPackages )
23
import Distribution.Client.Setup
24
         ( ConfigExFlags(..), configureCommand, filterConfigureFlags )
25
import Distribution.Client.Types as Source
26 27
import Distribution.Client.SetupWrapper
         ( setupWrapper, SetupScriptOptions(..), defaultSetupScriptOptions )
28 29
import Distribution.Client.Targets
         ( userToPackageConstraint )
30 31

import Distribution.Simple.Compiler
32 33
         ( CompilerId(..), Compiler(compilerId)
         , PackageDB(..), PackageDBStack )
34
import Distribution.Simple.Program (ProgramConfiguration )
35
import Distribution.Simple.Setup
36
         ( ConfigFlags(..), fromFlag, toFlag, flagToMaybe, fromFlagOrDefault )
37
import Distribution.Simple.PackageIndex (PackageIndex)
38 39
import Distribution.Simple.Utils
         ( defaultPackageDesc )
40
import qualified Distribution.InstalledPackageInfo as Installed
41
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, debug, die )
51
import Distribution.System
52
         ( Platform )
53 54
import Distribution.Verbosity as Verbosity
         ( Verbosity )
55 56
import Distribution.Version
         ( Version(..), orLaterVersion )
57

58 59
import Data.Monoid (Monoid(..))

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

74 75
  installedPkgIndex <- getInstalledPackages verbosity comp packageDBs conf
  sourcePkgDb       <- getSourcePackages    verbosity repos
76

77
  progress <- planLocalPackage verbosity comp platform configFlags configExFlags
78
                               installedPkgIndex sourcePkgDb
79 80

  notice verbosity "Resolving dependencies..."
81 82
  maybePlan <- foldProgress logMsg (return . Left) (return . Right)
                            progress
83
  case maybePlan of
84
    Left message -> die message
85 86

    Right installPlan -> case InstallPlan.ready installPlan of
87
      [pkg@(ReadyPackage (SourcePackage _ _ (LocalUnpackedPackage _) _) _ _ _)] ->
88 89 90
        configurePackage verbosity
          (InstallPlan.planPlatform installPlan)
          (InstallPlan.planCompiler installPlan)
91
          (setupScriptOptions installedPkgIndex)
92 93 94 95 96 97
          configFlags pkg extraArgs

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

  where
98 99
    allowNewer = fromFlagOrDefault False $
                 fmap isAllowNewer (configAllowNewer configExFlags)
100
    setupScriptOptions index = SetupScriptOptions {
101 102 103 104
      useCabalVersion  = maybe (if allowNewer
                                then orLaterVersion (Version [1,19,2] [])
                                else anyVersion)
                         thisVersion
105
                         (flagToMaybe (configCabalVersion configExFlags)),
106
      useCompiler      = Just comp,
107
      usePlatform      = Just platform,
Duncan Coutts's avatar
Duncan Coutts committed
108 109
      usePackageDB     = packageDBs',
      usePackageIndex  = index',
110
      useProgramConfig = conf,
111
      useDistPref      = fromFlagOrDefault
112
                           (useDistPref defaultSetupScriptOptions)
113
                           (configDistPref configFlags),
114
      useLoggingHandle = Nothing,
115
      useWorkingDir    = Nothing,
refold's avatar
refold committed
116
      forceExternalSetupMethod = False,
refold's avatar
refold committed
117
      setupCacheLock   = Nothing
118
    }
Duncan Coutts's avatar
Duncan Coutts committed
119 120 121 122 123 124 125 126 127 128
      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)
129

130 131
    logMsg message rest = debug verbosity message >> rest

132 133 134 135
-- | Make an 'InstallPlan' for the unpacked package in the current directory,
-- and all its dependencies.
--
planLocalPackage :: Verbosity -> Compiler
136
                 -> Platform
137
                 -> ConfigFlags -> ConfigExFlags
138
                 -> PackageIndex
139
                 -> SourcePackageDb
140
                 -> IO (Progress String String InstallPlan)
141
planLocalPackage verbosity comp platform configFlags configExFlags installedPkgIndex
142
  (SourcePackageDb _ packagePrefs) = do
143
  pkg <- readPackageDescription verbosity =<< defaultPackageDesc verbosity
144
  solver <- chooseSolver verbosity (fromFlag $ configSolver configExFlags) (compilerId comp)
145 146

  let -- We create a local package and ask to resolve a dependency on it
147 148 149
      localPkg = SourcePackage {
        packageInfoId             = packageId pkg,
        Source.packageDescription = pkg,
150 151
        packageSource             = LocalUnpackedPackage ".",
        packageDescrOverride      = Nothing
152
      }
153

154 155 156 157
      testsEnabled = fromFlagOrDefault False $ configTests configFlags
      benchmarksEnabled =
        fromFlagOrDefault False $ configBenchmarks configFlags

158
      resolverParams =
159 160
          removeUpperBounds (fromFlagOrDefault AllowNewerNone $
                             configAllowNewer configExFlags)
161

162
        . addPreferences
163 164 165 166 167 168
            -- 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
169 170
            -- TODO: should warn or error on constraints that are not on direct
            -- deps or flag constraints not on the package in question.
171
            (map userToPackageConstraint (configExConstraints configExFlags))
172 173 174

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

178 179 180
        . addConstraints
            -- '--enable-tests' and '--enable-benchmarks' constraints from
            -- command line
EyalLotem's avatar
EyalLotem committed
181 182 183
            [ PackageConstraintStanzas (packageName pkg) $
                [ TestStanzas  | testsEnabled ] ++
                [ BenchStanzas | benchmarksEnabled ]
184 185
            ]

186
        $ standardInstallPolicy
187 188
            installedPkgIndex
            (SourcePackageDb mempty packagePrefs)
189
            [SpecificSourcePackage localPkg]
190

191
  return (resolveDependencies platform (compilerId comp) solver resolverParams)
192

193

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

  setupWrapper verbosity
    scriptOptions (Just pkg) configureCommand configureFlags extraArgs

  where
    configureFlags   = filterConfigureFlags configFlags {
217
      configConfigurationsFlags = flags,
218 219 220 221 222 223 224 225
      -- 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 ],
226 227
      -- Use '--exact-configuration' if supported.
      configExactConfiguration = toFlag True,
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
228 229 230
      configVerbosity          = toFlag verbosity,
      configBenchmarks         = toFlag (BenchStanzas `elem` stanzas),
      configTests              = toFlag (TestStanzas `elem` stanzas)
231 232 233
    }

    pkg = case finalizePackageDescription flags
234
           (const True)
235
           platform comp [] (enableStanzas stanzas gpkg) of
236
      Left _ -> error "finalizePackageDescription ReadyPackage failed"
237
      Right (desc, _) -> desc