Skip to content

Solver feature: soft prefs for enabling testsuites/benchmarks #3092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions cabal-install/Distribution/Client/Dependency.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ import qualified Distribution.Client.InstallPlan as InstallPlan
import Distribution.Client.InstallPlan (InstallPlan)
import Distribution.Client.Types
( SourcePackageDb(SourcePackageDb), SourcePackage(..)
, ConfiguredPackage(..), ConfiguredId(..), enableStanzas )
, ConfiguredPackage(..), ConfiguredId(..)
, OptionalStanza(..), enableStanzas )
import Distribution.Client.Dependency.Types
( PreSolver(..), Solver(..), DependencyResolver, ResolverPackage(..)
, PackageConstraint(..), showPackageConstraint
Expand Down Expand Up @@ -116,7 +117,7 @@ import Distribution.Verbosity
( Verbosity )

import Data.List
( foldl', sort, sortBy, nubBy, maximumBy, intercalate )
( foldl', sort, sortBy, nubBy, maximumBy, intercalate, nub )
import Data.Function (on)
import Data.Maybe (fromMaybe)
import qualified Data.Map as Map
Expand Down Expand Up @@ -178,6 +179,11 @@ data PackagePreference =
-- | If we prefer versions of packages that are already installed.
| PackageInstalledPreference PackageName InstalledPreference

-- | If we would prefer to enable these optional stanzas
-- (i.e. test suites and/or benchmarks)
| PackageStanzasPreference PackageName [OptionalStanza]


-- | Provide a textual representation of a package preference
-- for debugging purposes.
--
Expand All @@ -186,6 +192,8 @@ showPackagePreference (PackageVersionPreference pn vr) =
display pn ++ " " ++ display (simplifyVersionRange vr)
showPackagePreference (PackageInstalledPreference pn ip) =
display pn ++ " " ++ show ip
showPackagePreference (PackageStanzasPreference pn st) =
display pn ++ " " ++ show st

basicDepResolverParams :: InstalledPackageIndex
-> PackageIndex.PackageIndex SourcePackage
Expand Down Expand Up @@ -586,7 +594,9 @@ interpretPackagesPreference :: Set PackageName
-> [PackagePreference]
-> (PackageName -> PackagePreferences)
interpretPackagesPreference selected defaultPref prefs =
\pkgname -> PackagePreferences (versionPref pkgname) (installPref pkgname)
\pkgname -> PackagePreferences (versionPref pkgname)
(installPref pkgname)
(stanzasPref pkgname)
where
versionPref pkgname =
fromMaybe [anyVersion] (Map.lookup pkgname versionPrefs)
Expand All @@ -608,6 +618,13 @@ interpretPackagesPreference selected defaultPref prefs =
if pkgname `Set.member` selected then PreferLatest
else PreferInstalled

stanzasPref pkgname =
fromMaybe [] (Map.lookup pkgname stanzasPrefs)
stanzasPrefs = Map.fromListWith (\a b -> nub (a ++ b))
[ (pkgname, pref)
| PackageStanzasPreference pkgname pref <- prefs ]


-- ------------------------------------------------------------
-- * Checking the result of the solver
-- ------------------------------------------------------------
Expand Down Expand Up @@ -806,7 +823,7 @@ resolveWithoutDependencies (DepResolverParams targets constraints
pkgDependency

-- Preferences
PackagePreferences preferredVersions preferInstalled
PackagePreferences preferredVersions preferInstalled _
= packagePreferences pkgname

bestByPrefs = comparing $ \pkg ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ build = ana go
reorder False = reverse
trivial = L.null t && L.null f

-- For a stanza, we also create only two subtrees. The order is initially
-- False, True. This can be changed later by constraints (force enabling
-- the stanza by replacing the False branch with failure) or preferences
-- (try enabling the stanza if possible by moving the True branch first).

go bs@(BS { next = OneGoal (OpenGoal (Stanza qsn@(SN (PI qpn _) _) t) gr) }) =
SChoiceF qsn gr trivial (P.fromList
[(False, bs { next = Goals }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ import Distribution.Client.ComponentDeps (ComponentDeps)
-- | Converts from the solver specific result @CP QPN@ into
-- a 'ResolverPackage', which can then be converted into
-- the install plan.
convCP :: SI.InstalledPackageIndex -> CI.PackageIndex SourcePackage ->
convCP :: SI.InstalledPackageIndex ->
CI.PackageIndex SourcePackage ->
CP QPN -> ResolverPackage
convCP iidx sidx (CP qpi fa es ds) =
case convPI qpi of
Left pi -> PreExisting
(fromJust $ SI.lookupUnitId iidx pi)
Right pi -> Configured $ ConfiguredPackage
(fromJust $ CI.lookupPackageId sidx pi)
srcpkg
fa
es
ds'
where
Just srcpkg = CI.lookupPackageId sidx pi
where
ds' :: ComponentDeps [ConfiguredId]
ds' = fmap (map convConfId) ds
Expand Down
21 changes: 18 additions & 3 deletions cabal-install/Distribution/Client/Dependency/Modular/Preference.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ preferredVersionsOrdering vrs v1 v2 = compare (check v1) (check v2)
Prelude.map (flip checkVR v) $ vrs

-- | Traversal that tries to establish package preferences (not constraints).
-- Works by reordering choice nodes.
-- Works by reordering choice nodes. Also applies stanza preferences.
preferPackagePreferences :: (PN -> PackagePreferences) -> Tree a -> Tree a
preferPackagePreferences pcs = packageOrderFor (const True) preference
preferPackagePreferences pcs = preferPackageStanzaPreferences pcs
. packageOrderFor (const True) preference
where
preference pn i1@(I v1 _) i2@(I v2 _) =
let PackagePreferences vrs ipref = pcs pn
let PackagePreferences vrs ipref _ = pcs pn
in preferredVersionsOrdering vrs v1 v2 `mappend` -- combines lexically
locationsOrdering ipref i1 i2

Expand All @@ -107,6 +108,20 @@ preferInstalledOrdering _ _ = EQ
preferLatestOrdering :: I -> I -> Ordering
preferLatestOrdering (I v1 _) (I v2 _) = compare v1 v2

-- | Traversal that tries to establish package stanza enable\/disable
-- preferences. Works by reordering the branches of stanza choices.
preferPackageStanzaPreferences :: (PN -> PackagePreferences) -> Tree a -> Tree a
preferPackageStanzaPreferences pcs = trav go
where
go (SChoiceF qsn@(SN (PI (Q pp pn) _) s) gr _tr ts) | primaryPP pp =
let PackagePreferences _ _ spref = pcs pn
enableStanzaPref = s `elem` spref
-- move True case first to try enabling the stanza
ts' | enableStanzaPref = P.sortByKeys (flip compare) ts
| otherwise = ts
in SChoiceF qsn gr True ts' -- True: now weak choice
go x = x

-- | Helper function that tries to enforce a single package constraint on a
-- given instance for a P-node. Translates the constraint into a
-- tree-transformer that either leaves the subtree untouched, or replaces it
Expand Down
4 changes: 2 additions & 2 deletions cabal-install/Distribution/Client/Dependency/TopDown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ explore pref (ChoiceNode _ choices) =
isPreferred p = length . filter (packageVersion p `withinRange`) $
preferredVersions

(PackagePreferences preferredVersions packageInstalledPreference)
(PackagePreferences preferredVersions packageInstalledPreference _)
= pref pkgname

logInfo node = Select selected discarded
Expand Down Expand Up @@ -674,7 +674,7 @@ finaliseSelectedPackages pref selected constraints =
| bounded = boundedRank -- this is a dummy constant
| otherwise = length . filter (packageVersion p `withinRange`) $
preferredVersions
where (PackagePreferences preferredVersions _) = pref (packageName p)
where (PackagePreferences preferredVersions _ _) = pref (packageName p)
boundedRank = 0 -- any value will do

boundedAbove :: VersionRange -> Bool
Expand Down
4 changes: 3 additions & 1 deletion cabal-install/Distribution/Client/Dependency/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ showPackageConstraint (PackageConstraintStanzas pn ss) =
-- It is not specified if preferences on some packages are more important than
-- others.
--
data PackagePreferences = PackagePreferences [VersionRange] InstalledPreference
data PackagePreferences = PackagePreferences [VersionRange]
InstalledPreference
[OptionalStanza]

-- | Whether we prefer an installed version of a package or simply the latest
-- version.
Expand Down