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

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

55 56
import Data.Monoid (Monoid(..))

57 58
-- | Configure the package found in the local directory
configure :: Verbosity
59
          -> PackageDBStack
60 61
          -> [Repo]
          -> Compiler
62
          -> Platform
63
          -> ProgramConfiguration
64
          -> ConfigFlags
65
          -> ConfigExFlags
66 67
          -> [String]
          -> IO ()
68
configure verbosity packageDBs repos comp platform 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 platform configFlags configExFlags
75
                               installedPkgIndex sourcePkgDb
76 77

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

    Right installPlan -> case InstallPlan.ready installPlan of
84
      [pkg@(ReadyPackage (SourcePackage _ _ (LocalUnpackedPackage _) _) _ _ _)] ->
85 86 87
        configurePackage verbosity
          (InstallPlan.planPlatform installPlan)
          (InstallPlan.planCompiler installPlan)
88
          (setupScriptOptions installedPkgIndex)
89 90 91 92 93 94 95
          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
96
      useCabalVersion  = maybe anyVersion thisVersion
97
                         (flagToMaybe (configCabalVersion configExFlags)),
98
      useCompiler      = Just comp,
99
      usePlatform      = Just platform,
Duncan Coutts's avatar
Duncan Coutts committed
100 101
      usePackageDB     = packageDBs',
      usePackageIndex  = index',
102
      useProgramConfig = conf,
103
      useDistPref      = fromFlagOrDefault
104
                           (useDistPref defaultSetupScriptOptions)
105
                           (configDistPref configFlags),
106
      useLoggingHandle = Nothing,
107
      useWorkingDir    = Nothing,
refold's avatar
refold committed
108
      forceExternalSetupMethod = False,
refold's avatar
refold committed
109
      setupCacheLock   = Nothing
110
    }
Duncan Coutts's avatar
Duncan Coutts committed
111 112 113 114 115 116 117 118 119 120
      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)
121

122 123
    logMsg message rest = debug verbosity message >> rest

124 125 126 127
-- | Make an 'InstallPlan' for the unpacked package in the current directory,
-- and all its dependencies.
--
planLocalPackage :: Verbosity -> Compiler
128
                 -> Platform
129
                 -> ConfigFlags -> ConfigExFlags
130
                 -> PackageIndex
131
                 -> SourcePackageDb
132
                 -> IO (Progress String String InstallPlan)
133
planLocalPackage verbosity comp platform configFlags configExFlags installedPkgIndex
134
  (SourcePackageDb _ packagePrefs) = do
135
  pkg <- readPackageDescription verbosity =<< defaultPackageDesc verbosity
136
  solver <- chooseSolver verbosity (fromFlag $ configSolver configExFlags) (compilerId comp)
137 138

  let -- We create a local package and ask to resolve a dependency on it
139 140 141
      localPkg = SourcePackage {
        packageInfoId             = packageId pkg,
        Source.packageDescription = pkg,
142 143
        packageSource             = LocalUnpackedPackage ".",
        packageDescrOverride      = Nothing
144
      }
145

146 147 148 149
      testsEnabled = fromFlagOrDefault False $ configTests configFlags
      benchmarksEnabled =
        fromFlagOrDefault False $ configBenchmarks configFlags

150 151 152 153 154 155 156 157 158
      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
159 160 161
            -- 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))
162 163 164

        . addConstraints
            -- package flags from the config file or command line
165
            [ PackageConstraintFlags (packageName pkg)
166 167
                                     (configConfigurationsFlags configFlags) ]

168 169 170
        . addConstraints
            -- '--enable-tests' and '--enable-benchmarks' constraints from
            -- command line
EyalLotem's avatar
EyalLotem committed
171 172 173
            [ PackageConstraintStanzas (packageName pkg) $
                [ TestStanzas  | testsEnabled ] ++
                [ BenchStanzas | benchmarksEnabled ]
174 175
            ]

176
        $ standardInstallPolicy
177 178
            installedPkgIndex
            (SourcePackageDb mempty packagePrefs)
179
            [SpecificSourcePackage localPkg]
180

181
  return (resolveDependencies platform (compilerId comp) solver resolverParams)
182

183

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

  setupWrapper verbosity
    scriptOptions (Just pkg) configureCommand configureFlags extraArgs

  where
    configureFlags   = filterConfigureFlags configFlags {
207
      configConfigurationsFlags = flags,
208 209 210 211 212 213 214 215
      -- 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 ],
216 217 218
      configVerbosity           = toFlag verbosity,
      configBenchmarks          = toFlag (BenchStanzas `elem` stanzas),
      configTests               = toFlag (TestStanzas `elem` stanzas)
219 220 221
    }

    pkg = case finalizePackageDescription flags
222
           (const True)
223
           platform comp [] (enableStanzas stanzas gpkg) of
224
      Left _ -> error "finalizePackageDescription ReadyPackage failed"
225
      Right (desc, _) -> desc