Configure.hs 15.8 KB
Newer Older
1
{-# LANGUAGE CPP #-}
2 3 4 5 6 7 8 9 10 11 12 13 14 15
-----------------------------------------------------------------------------
-- |
-- 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,
16
    configureSetupScript,
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
17
    chooseCabalVersion,
18
    checkConfigExFlags
19 20 21
  ) where

import Distribution.Client.Dependency
22
import Distribution.Client.Dependency.Types
23
         ( ConstraintSource(..)
24
         , LabeledPackageConstraint(..), showConstraintSource )
25
import qualified Distribution.Client.InstallPlan as InstallPlan
26
import Distribution.Client.InstallPlan (SolverInstallPlan)
27
import Distribution.Client.IndexUtils as IndexUtils
28
         ( getSourcePackages, getInstalledPackages )
29
import Distribution.Client.PackageIndex ( PackageIndex, elemByPackageName )
30
import Distribution.Client.PkgConfigDb (PkgConfigDb, readPkgConfigDb)
31
import Distribution.Client.Setup
Edsko de Vries's avatar
Edsko de Vries committed
32 33
         ( ConfigExFlags(..), configureCommand, filterConfigureFlags
         , RepoContext(..) )
34
import Distribution.Client.Types as Source
35 36
import Distribution.Client.SetupWrapper
         ( setupWrapper, SetupScriptOptions(..), defaultSetupScriptOptions )
37
import Distribution.Client.Targets
38
         ( userToPackageConstraint, userConstraintPackageName )
39
import qualified Distribution.Client.ComponentDeps as CD
40 41
import Distribution.Package (PackageId)
import Distribution.Client.JobControl (Lock)
42 43

import Distribution.Simple.Compiler
44
         ( Compiler, CompilerInfo, compilerInfo, PackageDB(..), PackageDBStack )
45
import Distribution.Simple.Program (ProgramConfiguration )
46
import Distribution.Simple.Setup
47 48
         ( ConfigFlags(..), AllowNewer(..)
         , fromFlag, toFlag, flagToMaybe, fromFlagOrDefault )
49 50
import Distribution.Simple.PackageIndex
         ( InstalledPackageIndex, lookupPackageName )
51 52 53
import Distribution.Simple.Utils
         ( defaultPackageDesc )
import Distribution.Package
54
         ( Package(..), UnitId, packageName
55 56 57
         , Dependency(..), thisPackageVersion
         )
import qualified Distribution.PackageDescription as PkgDesc
58 59 60 61 62
import Distribution.PackageDescription.Parse
         ( readPackageDescription )
import Distribution.PackageDescription.Configuration
         ( finalizePackageDescription )
import Distribution.Version
63
         ( anyVersion, thisVersion )
64
import Distribution.Simple.Utils as Utils
65
         ( warn, notice, debug, die )
66
import Distribution.Simple.Setup
67
         ( isAllowNewer )
68
import Distribution.System
69
         ( Platform )
70
import Distribution.Text ( display )
71 72
import Distribution.Verbosity as Verbosity
         ( Verbosity )
73
import Distribution.Version
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
74
         ( Version(..), VersionRange, orLaterVersion )
75

76
import Control.Monad (unless)
77
#if !MIN_VERSION_base(4,8,0)
78
import Data.Monoid (Monoid(..))
79
#endif
80
import Data.Maybe (isJust, fromMaybe)
81

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
82 83
-- | Choose the Cabal version such that the setup scripts compiled against this
-- version will support the given command-line flags.
84 85
chooseCabalVersion :: ConfigFlags -> Maybe Version -> VersionRange
chooseCabalVersion configFlags maybeVersion =
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
86 87
  maybe defaultVersionRange thisVersion maybeVersion
  where
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
88 89
    -- Cabal < 1.19.2 doesn't support '--exact-configuration' which is needed
    -- for '--allow-newer' to work.
90 91
    allowNewer = isAllowNewer
                 (fromMaybe AllowNewerNone $ configAllowNewer configFlags)
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
92 93 94 95 96

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

97 98
-- | Configure the package found in the local directory
configure :: Verbosity
99
          -> PackageDBStack
Edsko de Vries's avatar
Edsko de Vries committed
100
          -> RepoContext
101
          -> Compiler
102
          -> Platform
103
          -> ProgramConfiguration
104
          -> ConfigFlags
105
          -> ConfigExFlags
106 107
          -> [String]
          -> IO ()
Edsko de Vries's avatar
Edsko de Vries committed
108
configure verbosity packageDBs repoCtxt comp platform conf
109 110
  configFlags configExFlags extraArgs = do

111
  installedPkgIndex <- getInstalledPackages verbosity comp packageDBs conf
Edsko de Vries's avatar
Edsko de Vries committed
112
  sourcePkgDb       <- getSourcePackages    verbosity repoCtxt
113 114
  pkgConfigDb       <- readPkgConfigDb      verbosity conf

115 116
  checkConfigExFlags verbosity installedPkgIndex
                     (packageIndex sourcePkgDb) configExFlags
117

118
  progress <- planLocalPackage verbosity comp platform configFlags configExFlags
119
                               installedPkgIndex sourcePkgDb pkgConfigDb
120 121

  notice verbosity "Resolving dependencies..."
122 123
  maybePlan <- foldProgress logMsg (return . Left) (return . Right)
                            progress
124
  case maybePlan of
125
    Left message -> do
126 127
      warn verbosity $
           "solver failed to find a solution:\n"
128 129
        ++ message
        ++ "Trying configure anyway."
130 131
      setupWrapper verbosity (setupScriptOptions installedPkgIndex Nothing)
        Nothing configureCommand (const configFlags) extraArgs
132

133 134 135
    Right installPlan0 ->
     let installPlan = InstallPlan.configureInstallPlan installPlan0
     in case InstallPlan.ready installPlan of
136
      [pkg@(ReadyPackage
Edward Z. Yang's avatar
Edward Z. Yang committed
137
              (ConfiguredPackage _ (SourcePackage _ _ (LocalUnpackedPackage _) _)
138
                                 _ _ _))] -> do
139
        configurePackage verbosity
140
          platform (compilerInfo comp)
141
          (setupScriptOptions installedPkgIndex (Just pkg))
142 143 144 145 146 147
          configFlags pkg extraArgs

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

  where
148
    setupScriptOptions :: InstalledPackageIndex
149
                       -> Maybe ReadyPackage
150
                       -> SetupScriptOptions
151 152 153 154 155 156 157 158 159 160
    setupScriptOptions =
      configureSetupScript
        packageDBs
        comp
        platform
        conf
        (fromFlagOrDefault
           (useDistPref defaultSetupScriptOptions)
           (configDistPref configFlags))
        (chooseCabalVersion
161
           configFlags
162 163 164
           (flagToMaybe (configCabalVersion configExFlags)))
        Nothing
        False
165

166 167
    logMsg message rest = debug verbosity message >> rest

168 169 170 171 172 173 174 175 176
configureSetupScript :: PackageDBStack
                     -> Compiler
                     -> Platform
                     -> ProgramConfiguration
                     -> FilePath
                     -> VersionRange
                     -> Maybe Lock
                     -> Bool
                     -> InstalledPackageIndex
177
                     -> Maybe ReadyPackage
178 179 180 181 182 183 184 185 186 187 188 189
                     -> SetupScriptOptions
configureSetupScript packageDBs
                     comp
                     platform
                     conf
                     distPref
                     cabalVersion
                     lock
                     forceExternal
                     index
                     mpkg
  = SetupScriptOptions {
190 191 192 193 194 195 196 197 198 199 200 201
      useCabalVersion          = cabalVersion
    , useCabalSpecVersion      = Nothing
    , useCompiler              = Just comp
    , usePlatform              = Just platform
    , usePackageDB             = packageDBs'
    , usePackageIndex          = index'
    , useProgramConfig         = conf
    , useDistPref              = distPref
    , useLoggingHandle         = Nothing
    , useWorkingDir            = Nothing
    , setupCacheLock           = lock
    , useWin32CleanHack        = False
202 203 204 205 206 207 208 209
    , forceExternalSetupMethod = forceExternal
      -- If we have explicit setup dependencies, list them; otherwise, we give
      -- the empty list of dependencies; ideally, we would fix the version of
      -- Cabal here, so that we no longer need the special case for that in
      -- `compileSetupExecutable` in `externalSetupMethod`, but we don't yet
      -- know the version of Cabal at this point, but only find this there.
      -- Therefore, for now, we just leave this blank.
    , useDependencies          = fromMaybe [] explicitSetupDeps
210 211
    , useDependenciesExclusive = not defaultSetupDeps && isJust explicitSetupDeps
    , useVersionMacros         = not defaultSetupDeps && isJust explicitSetupDeps
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    }
  where
    -- When we are compiling a legacy setup script without an explicit
    -- setup stanza, 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' :: PackageDBStack
    index'      :: Maybe InstalledPackageIndex
    (packageDBs', index') =
      case packageDBs of
        (GlobalPackageDB:dbs) | UserPackageDB `notElem` dbs
                              , Nothing <- explicitSetupDeps
            -> (GlobalPackageDB:UserPackageDB:dbs, Nothing)
        -- but if the user is using an odd db stack, don't touch it
        _otherwise -> (packageDBs, Just index)

229 230
    maybeSetupBuildInfo :: Maybe PkgDesc.SetupBuildInfo
    maybeSetupBuildInfo = do
231
      ReadyPackage cpkg <- mpkg
232
      let gpkg = packageDescription (confPkgSource cpkg)
233 234 235 236 237 238 239 240 241 242 243 244
      PkgDesc.setupBuildInfo (PkgDesc.packageDescription gpkg)

    -- Was a default 'custom-setup' stanza added by 'cabal-install' itself? If
    -- so, 'setup-depends' must not be exclusive. See #3199.
    defaultSetupDeps :: Bool
    defaultSetupDeps = maybe False PkgDesc.defaultSetupDepends
                       maybeSetupBuildInfo

    explicitSetupDeps :: Maybe [(UnitId, PackageId)]
    explicitSetupDeps = do
      -- Check if there is an explicit setup stanza.
      _buildInfo <- maybeSetupBuildInfo
245
      -- Return the setup dependencies computed by the solver
246
      ReadyPackage cpkg <- mpkg
247 248
      return [ ( uid, srcid )
             | ConfiguredId srcid uid <- CD.setupDeps (confPkgDeps cpkg)
249 250
             ]

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
-- | Warn if any constraints or preferences name packages that are not in the
-- source package index or installed package index.
checkConfigExFlags :: Package pkg
                   => Verbosity
                   -> InstalledPackageIndex
                   -> PackageIndex pkg
                   -> ConfigExFlags
                   -> IO ()
checkConfigExFlags verbosity installedPkgIndex sourcePkgIndex flags = do
  unless (null unknownConstraints) $ warn verbosity $
             "Constraint refers to an unknown package: "
          ++ showConstraint (head unknownConstraints)
  unless (null unknownPreferences) $ warn verbosity $
             "Preference refers to an unknown package: "
          ++ display (head unknownPreferences)
  where
    unknownConstraints = filter (unknown . userConstraintPackageName . fst) $
                         configExConstraints flags
    unknownPreferences = filter (unknown . \(Dependency name _) -> name) $
                         configPreferences flags
    unknown pkg = null (lookupPackageName installedPkgIndex pkg)
               && not (elemByPackageName sourcePkgIndex pkg)
    showConstraint (uc, src) =
        display uc ++ " (" ++ showConstraintSource src ++ ")"

276 277 278 279
-- | Make an 'InstallPlan' for the unpacked package in the current directory,
-- and all its dependencies.
--
planLocalPackage :: Verbosity -> Compiler
280
                 -> Platform
281
                 -> ConfigFlags -> ConfigExFlags
282
                 -> InstalledPackageIndex
283
                 -> SourcePackageDb
284
                 -> PkgConfigDb
285
                 -> IO (Progress String String SolverInstallPlan)
286
planLocalPackage verbosity comp platform configFlags configExFlags
287
  installedPkgIndex (SourcePackageDb _ packagePrefs) pkgConfigDb = do
288
  pkg <- readPackageDescription verbosity =<< defaultPackageDesc verbosity
289 290
  solver <- chooseSolver verbosity (fromFlag $ configSolver configExFlags)
            (compilerInfo comp)
291 292

  let -- We create a local package and ask to resolve a dependency on it
293 294 295
      localPkg = SourcePackage {
        packageInfoId             = packageId pkg,
        Source.packageDescription = pkg,
296 297
        packageSource             = LocalUnpackedPackage ".",
        packageDescrOverride      = Nothing
298
      }
299

300 301 302 303
      testsEnabled = fromFlagOrDefault False $ configTests configFlags
      benchmarksEnabled =
        fromFlagOrDefault False $ configBenchmarks configFlags

304
      resolverParams =
305 306
          removeUpperBounds
          (fromMaybe AllowNewerNone $ configAllowNewer configFlags)
307

308
        . addPreferences
309 310 311 312 313 314
            -- 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
315 316
            -- TODO: should warn or error on constraints that are not on direct
            -- deps or flag constraints not on the package in question.
317
            [ LabeledPackageConstraint (userToPackageConstraint uc) src
318
            | (uc, src) <- configExConstraints configExFlags ]
319 320 321

        . addConstraints
            -- package flags from the config file or command line
322 323
            [ let pc = PackageConstraintFlags (packageName pkg)
                       (configConfigurationsFlags configFlags)
324 325
              in LabeledPackageConstraint pc ConstraintSourceConfigFlagOrTarget
            ]
326

327 328
        . addConstraints
            -- '--enable-tests' and '--enable-benchmarks' constraints from
329
            -- the config file or command line
330 331 332
            [ let pc = PackageConstraintStanzas (packageName pkg) $
                       [ TestStanzas  | testsEnabled ] ++
                       [ BenchStanzas | benchmarksEnabled ]
333
              in LabeledPackageConstraint pc ConstraintSourceConfigFlagOrTarget
334 335
            ]

336
        $ standardInstallPolicy
337 338
            installedPkgIndex
            (SourcePackageDb mempty packagePrefs)
339
            [SpecificSourcePackage localPkg]
340

341
  return (resolveDependencies platform (compilerInfo comp) pkgConfigDb solver resolverParams)
342

343

344
-- | Call an installer for an 'SourcePackage' but override the configure
345 346
-- flags with the ones given by the 'ReadyPackage'. In particular the
-- 'ReadyPackage' specifies an exact 'FlagAssignment' and exactly
347 348 349
-- versioned package dependencies. So we ignore any previous partial flag
-- assignment or dependency constraints and use the new ones.
--
350 351
-- NB: when updating this function, don't forget to also update
-- 'installReadyPackage' in D.C.Install.
352
configurePackage :: Verbosity
353
                 -> Platform -> CompilerInfo
354
                 -> SetupScriptOptions
355
                 -> ConfigFlags
356
                 -> ReadyPackage
357 358
                 -> [String]
                 -> IO ()
359
configurePackage verbosity platform comp scriptOptions configFlags
Edward Z. Yang's avatar
Edward Z. Yang committed
360
                 (ReadyPackage (ConfiguredPackage ipid spkg flags stanzas deps))
361
                 extraArgs =
362 363 364 365 366

  setupWrapper verbosity
    scriptOptions (Just pkg) configureCommand configureFlags extraArgs

  where
367
    gpkg = packageDescription spkg
368
    configureFlags   = filterConfigureFlags configFlags {
Edward Z. Yang's avatar
Edward Z. Yang committed
369
      configIPID = toFlag (display ipid),
370
      configConfigurationsFlags = flags,
371 372 373
      -- 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.
374 375 376 377
      configConstraints  = [ thisPackageVersion srcid
                           | ConfiguredId srcid _uid <- CD.nonSetupDeps deps ],
      configDependencies = [ (packageName srcid, uid)
                           | ConfiguredId srcid uid <- CD.nonSetupDeps deps ],
378 379
      -- Use '--exact-configuration' if supported.
      configExactConfiguration = toFlag True,
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
380 381 382
      configVerbosity          = toFlag verbosity,
      configBenchmarks         = toFlag (BenchStanzas `elem` stanzas),
      configTests              = toFlag (TestStanzas `elem` stanzas)
383 384 385
    }

    pkg = case finalizePackageDescription flags
386
           (const True)
387
           platform comp [] (enableStanzas stanzas gpkg) of
388
      Left _ -> error "finalizePackageDescription ReadyPackage failed"
389
      Right (desc, _) -> desc