Configure.hs 8.33 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 39
import Distribution.Simple.Utils
         ( defaultPackageDesc )
import Distribution.Package
40
         ( Package(..), packageName, Dependency(..), thisPackageVersion )
41 42 43 44 45
import Distribution.PackageDescription.Parse
         ( readPackageDescription )
import Distribution.PackageDescription.Configuration
         ( finalizePackageDescription )
import Distribution.Version
46
         ( anyVersion, thisVersion )
47
import Distribution.Simple.Utils as Utils
48
         ( notice, info, debug, die )
49
import Distribution.System
50
         ( Platform, buildPlatform )
51 52 53
import Distribution.Verbosity as Verbosity
         ( Verbosity )

54 55
import Data.Monoid (Monoid(..))

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

69 70
  installedPkgIndex <- getInstalledPackages verbosity comp packageDBs conf
  sourcePkgDb       <- getSourcePackages    verbosity repos
71

72
  progress <- planLocalPackage verbosity comp configFlags configExFlags
73
                               installedPkgIndex sourcePkgDb
74 75

  notice verbosity "Resolving dependencies..."
76 77
  maybePlan <- foldProgress logMsg (return . Left) (return . Right)
                            progress
78 79 80
  case maybePlan of
    Left message -> do
      info verbosity message
81
      setupWrapper verbosity (setupScriptOptions installedPkgIndex) Nothing
82 83 84
        configureCommand (const configFlags) extraArgs

    Right installPlan -> case InstallPlan.ready installPlan of
85
      [pkg@(ConfiguredPackage (SourcePackage _ _ (LocalUnpackedPackage _)) _ _ _)] ->
86 87 88
        configurePackage verbosity
          (InstallPlan.planPlatform installPlan)
          (InstallPlan.planCompiler installPlan)
89
          (setupScriptOptions installedPkgIndex)
90 91 92 93 94 95 96
          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
97
      useCabalVersion  = maybe anyVersion thisVersion
98
                         (flagToMaybe (configCabalVersion configExFlags)),
99 100 101 102
      useCompiler      = Just comp,
      -- 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.
103 104 105 106
      usePackageDB     = if UserPackageDB `elem` packageDBs
                           then packageDBs
                           else packageDBs ++ [UserPackageDB],
      usePackageIndex  = if UserPackageDB `elem` packageDBs
107
                           then Just index
108
                           else Nothing,
109
      useProgramConfig = conf,
110
      useDistPref      = fromFlagOrDefault
111
                           (useDistPref defaultSetupScriptOptions)
112
                           (configDistPref configFlags),
113 114 115 116
      useLoggingHandle = Nothing,
      useWorkingDir    = Nothing
    }

117 118
    logMsg message rest = debug verbosity message >> rest

119 120 121 122
-- | Make an 'InstallPlan' for the unpacked package in the current directory,
-- and all its dependencies.
--
planLocalPackage :: Verbosity -> Compiler
123
                 -> ConfigFlags -> ConfigExFlags
124
                 -> PackageIndex
125
                 -> SourcePackageDb
126
                 -> IO (Progress String String InstallPlan)
127 128
planLocalPackage verbosity comp configFlags configExFlags installedPkgIndex
  (SourcePackageDb _ packagePrefs) = do
129
  pkg <- readPackageDescription verbosity =<< defaultPackageDesc verbosity
130 131

  let -- We create a local package and ask to resolve a dependency on it
132 133 134 135
      localPkg = SourcePackage {
        packageInfoId             = packageId pkg,
        Source.packageDescription = pkg,
        packageSource             = LocalUnpackedPackage "."
136
      }
137

138 139
      solver = fromFlag $ configSolver configExFlags

140 141 142 143
      testsEnabled = fromFlagOrDefault False $ configTests configFlags
      benchmarksEnabled =
        fromFlagOrDefault False $ configBenchmarks configFlags

144 145 146 147 148 149 150 151 152
      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
153 154 155
            -- 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))
156 157 158

        . addConstraints
            -- package flags from the config file or command line
159
            [ PackageConstraintFlags (packageName pkg)
160 161
                                     (configConfigurationsFlags configFlags) ]

162 163 164 165 166 167 168 169 170
        . addConstraints
            -- '--enable-tests' and '--enable-benchmarks' constraints from
            -- command line
            [ PackageConstraintStanzas (packageName pkg) $ concat
                [ if testsEnabled then [TestStanzas] else []
                , if benchmarksEnabled then [BenchStanzas] else []
                ]
            ]

171
        $ standardInstallPolicy
172 173
            installedPkgIndex
            (SourcePackageDb mempty packagePrefs)
174
            [SpecificSourcePackage localPkg]
175

176
  return (resolveDependencies buildPlatform (compilerId comp) solver resolverParams)
177

178

179
-- | Call an installer for an 'SourcePackage' but override the configure
180 181 182 183 184 185 186 187
-- flags with the ones given by the 'ConfiguredPackage'. In particular the
-- 'ConfiguredPackage' specifies an exact 'FlagAssignment' and exactly
-- versioned package dependencies. So we ignore any previous partial flag
-- assignment or dependency constraints and use the new ones.
--
configurePackage :: Verbosity
                 -> Platform -> CompilerId
                 -> SetupScriptOptions
188
                 -> ConfigFlags
189 190 191
                 -> ConfiguredPackage
                 -> [String]
                 -> IO ()
192
configurePackage verbosity platform comp scriptOptions configFlags
193
  (ConfiguredPackage (SourcePackage _ gpkg _) flags stanzas deps) extraArgs =
194 195 196 197 198 199

  setupWrapper verbosity
    scriptOptions (Just pkg) configureCommand configureFlags extraArgs

  where
    configureFlags   = filterConfigureFlags configFlags {
200 201
      configConfigurationsFlags = flags,
      configConstraints         = map thisPackageVersion deps,
202 203 204
      configVerbosity           = toFlag verbosity,
      configBenchmarks          = toFlag (BenchStanzas `elem` stanzas),
      configTests               = toFlag (TestStanzas `elem` stanzas)
205 206 207
    }

    pkg = case finalizePackageDescription flags
208
           (const True)
209
           platform comp [] (enableStanzas stanzas gpkg) of
210 211
      Left _ -> error "finalizePackageDescription ConfiguredPackage failed"
      Right (desc, _) -> desc