-
Notifications
You must be signed in to change notification settings - Fork 704
/
Copy pathDependency.hs
1238 lines (1137 loc) · 42.1 KB
/
Dependency.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- |
-- Module : Distribution.Client.Dependency
-- Copyright : (c) David Himmelstrup 2005,
-- Bjorn Bringert 2007
-- Duncan Coutts 2008
-- License : BSD-like
--
-- Maintainer : cabal-devel@gmail.com
-- Stability : provisional
-- Portability : portable
--
-- Top level interface to dependency resolution.
module Distribution.Client.Dependency
( -- * The main package dependency resolver
DepResolverParams
, resolveDependencies
, Progress (..)
, foldProgress
-- * Alternate, simple resolver that does not do dependencies recursively
, resolveWithoutDependencies
-- * Constructing resolver policies
, PackageProperty (..)
, PackageConstraint (..)
, scopeToplevel
, PackagesPreferenceDefault (..)
, PackagePreference (..)
-- ** Standard policy
, basicInstallPolicy
, standardInstallPolicy
, PackageSpecifier (..)
-- ** Extra policy options
, upgradeDependencies
, reinstallTargets
-- ** Policy utils
, addConstraints
, addPreferences
, setPreferenceDefault
, setReorderGoals
, setCountConflicts
, setFineGrainedConflicts
, setMinimizeConflictSet
, setIndependentGoals
, setAvoidReinstalls
, setShadowPkgs
, setStrongFlags
, setAllowBootLibInstalls
, setOnlyConstrained
, setMaxBackjumps
, setEnableBackjumping
, setSolveExecutables
, setGoalOrder
, setSolverVerbosity
, removeLowerBounds
, removeUpperBounds
, addDefaultSetupDependencies
, addSetupCabalMinVersionConstraint
, addSetupCabalMaxVersionConstraint
, addSetupCabalProfiledDynamic
) where
import Distribution.Client.Compat.Prelude
import Distribution.Client.Dependency.Types
( PackagesPreferenceDefault (..)
)
import Distribution.Client.SolverInstallPlan (SolverInstallPlan)
import qualified Distribution.Client.SolverInstallPlan as SolverInstallPlan
import Distribution.Client.Types
( AllowNewer (..)
, AllowOlder (..)
, PackageSpecifier (..)
, RelaxDepMod (..)
, RelaxDepScope (..)
, RelaxDepSubject (..)
, RelaxDeps (..)
, RelaxedDep (..)
, SourcePackageDb (SourcePackageDb)
, UnresolvedPkgLoc
, UnresolvedSourcePackage
, isRelaxDeps
, pkgSpecifierConstraints
, pkgSpecifierTarget
)
import Distribution.Client.Utils
( MergeResult (..)
, duplicatesBy
, mergeBy
)
import qualified Distribution.Compat.Graph as Graph
import Distribution.Compiler
( CompilerInfo (..)
)
import Distribution.Package
( Package (..)
, PackageId
, PackageIdentifier (PackageIdentifier)
, PackageName
, mkPackageName
, packageName
, packageVersion
)
import qualified Distribution.PackageDescription as PD
import Distribution.PackageDescription.Configuration
( finalizePD
)
import qualified Distribution.PackageDescription.Configuration as PD
import Distribution.Simple.PackageIndex (InstalledPackageIndex)
import qualified Distribution.Simple.PackageIndex as InstalledPackageIndex
import Distribution.Simple.Setup
( asBool
)
import Distribution.Solver.Modular
( PruneAfterFirstSuccess (..)
, SolverConfig (..)
, modularResolver
)
import Distribution.System
( Platform
)
import Distribution.Types.Dependency
import Distribution.Types.DependencySatisfaction
( DependencySatisfaction (..)
)
import Distribution.Verbosity
( normal
)
import Distribution.Version
import Distribution.Solver.Types.ComponentDeps (ComponentDeps)
import qualified Distribution.Solver.Types.ComponentDeps as CD
import Distribution.Solver.Types.ConstraintSource
import Distribution.Solver.Types.DependencyResolver
import Distribution.Solver.Types.InstalledPreference as Preference
import Distribution.Solver.Types.LabeledPackageConstraint
import Distribution.Solver.Types.OptionalStanza
import Distribution.Solver.Types.PackageConstraint
import qualified Distribution.Solver.Types.PackageIndex as PackageIndex
import Distribution.Solver.Types.PackagePath
import Distribution.Solver.Types.PackagePreferences
import Distribution.Solver.Types.PkgConfigDb (PkgConfigDb)
import Distribution.Solver.Types.Progress
import Distribution.Solver.Types.ResolverPackage
import Distribution.Solver.Types.Settings
import Distribution.Solver.Types.SolverId
import Distribution.Solver.Types.SolverPackage
import Distribution.Solver.Types.SourcePackage
import Distribution.Solver.Types.Variable
import Control.Exception
( assert
)
import Data.List
( maximumBy
)
import qualified Data.Map as Map
import qualified Data.Set as Set
-- ------------------------------------------------------------
-- * High level planner policy
-- ------------------------------------------------------------
-- | The set of parameters to the dependency resolver. These parameters are
-- relatively low level but many kinds of high level policies can be
-- implemented in terms of adjustments to the parameters.
data DepResolverParams = DepResolverParams
{ depResolverTargets :: Set PackageName
, depResolverConstraints :: [LabeledPackageConstraint]
, depResolverPreferences :: [PackagePreference]
, depResolverPreferenceDefault :: PackagesPreferenceDefault
, depResolverInstalledPkgIndex :: InstalledPackageIndex
, depResolverSourcePkgIndex :: PackageIndex.PackageIndex UnresolvedSourcePackage
, depResolverReorderGoals :: ReorderGoals
, depResolverCountConflicts :: CountConflicts
, depResolverFineGrainedConflicts :: FineGrainedConflicts
, depResolverMinimizeConflictSet :: MinimizeConflictSet
, depResolverIndependentGoals :: IndependentGoals
, depResolverAvoidReinstalls :: AvoidReinstalls
, depResolverShadowPkgs :: ShadowPkgs
, depResolverStrongFlags :: StrongFlags
, depResolverAllowBootLibInstalls :: AllowBootLibInstalls
-- ^ Whether to allow base and its dependencies to be installed.
, depResolverOnlyConstrained :: OnlyConstrained
-- ^ Whether to only allow explicitly constrained packages plus
-- goals or to allow any package.
, depResolverMaxBackjumps :: Maybe Int
, depResolverEnableBackjumping :: EnableBackjumping
, depResolverSolveExecutables :: SolveExecutables
-- ^ Whether or not to solve for dependencies on executables.
-- This should be true, except in the legacy code path where
-- we can't tell if an executable has been installed or not,
-- so we shouldn't solve for them. See #3875.
, depResolverGoalOrder :: Maybe (Variable QPN -> Variable QPN -> Ordering)
-- ^ Function to override the solver's goal-ordering heuristics.
, depResolverVerbosity :: Verbosity
}
showDepResolverParams :: DepResolverParams -> String
showDepResolverParams p =
"targets: "
++ intercalate ", " (map prettyShow $ Set.toList (depResolverTargets p))
++ "\nconstraints: "
++ concatMap
(("\n " ++) . showLabeledConstraint)
(depResolverConstraints p)
++ "\npreferences: "
++ concatMap
(("\n " ++) . showPackagePreference)
(depResolverPreferences p)
++ "\nstrategy: "
++ show (depResolverPreferenceDefault p)
++ "\nreorder goals: "
++ show (asBool (depResolverReorderGoals p))
++ "\ncount conflicts: "
++ show (asBool (depResolverCountConflicts p))
++ "\nfine grained conflicts: "
++ show (asBool (depResolverFineGrainedConflicts p))
++ "\nminimize conflict set: "
++ show (asBool (depResolverMinimizeConflictSet p))
++ "\nindependent goals: "
++ show (asBool (depResolverIndependentGoals p))
++ "\navoid reinstalls: "
++ show (asBool (depResolverAvoidReinstalls p))
++ "\nshadow packages: "
++ show (asBool (depResolverShadowPkgs p))
++ "\nstrong flags: "
++ show (asBool (depResolverStrongFlags p))
++ "\nallow boot library installs: "
++ show (asBool (depResolverAllowBootLibInstalls p))
++ "\nonly constrained packages: "
++ show (depResolverOnlyConstrained p)
++ "\nmax backjumps: "
++ maybe
"infinite"
show
(depResolverMaxBackjumps p)
where
showLabeledConstraint :: LabeledPackageConstraint -> String
showLabeledConstraint (LabeledPackageConstraint pc src) =
showPackageConstraint pc ++ " (" ++ showConstraintSource src ++ ")"
-- | A package selection preference for a particular package.
--
-- Preferences are soft constraints that the dependency resolver should try to
-- respect where possible. It is not specified if preferences on some packages
-- are more important than others.
data PackagePreference
= -- | A suggested constraint on the version number.
PackageVersionPreference PackageName VersionRange
| -- | 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.
showPackagePreference :: PackagePreference -> String
showPackagePreference (PackageVersionPreference pn vr) =
prettyShow pn ++ " " ++ prettyShow (simplifyVersionRange vr)
showPackagePreference (PackageInstalledPreference pn ip) =
prettyShow pn ++ " " ++ show ip
showPackagePreference (PackageStanzasPreference pn st) =
prettyShow pn ++ " " ++ show st
basicDepResolverParams
:: InstalledPackageIndex
-> PackageIndex.PackageIndex UnresolvedSourcePackage
-> DepResolverParams
basicDepResolverParams installedPkgIndex sourcePkgIndex =
DepResolverParams
{ depResolverTargets = Set.empty
, depResolverConstraints = []
, depResolverPreferences = []
, depResolverPreferenceDefault = PreferLatestForSelected
, depResolverInstalledPkgIndex = installedPkgIndex
, depResolverSourcePkgIndex = sourcePkgIndex
, depResolverReorderGoals = ReorderGoals False
, depResolverCountConflicts = CountConflicts True
, depResolverFineGrainedConflicts = FineGrainedConflicts True
, depResolverMinimizeConflictSet = MinimizeConflictSet False
, depResolverIndependentGoals = IndependentGoals False
, depResolverAvoidReinstalls = AvoidReinstalls False
, depResolverShadowPkgs = ShadowPkgs False
, depResolverStrongFlags = StrongFlags False
, depResolverAllowBootLibInstalls = AllowBootLibInstalls False
, depResolverOnlyConstrained = OnlyConstrainedNone
, depResolverMaxBackjumps = Nothing
, depResolverEnableBackjumping = EnableBackjumping True
, depResolverSolveExecutables = SolveExecutables True
, depResolverGoalOrder = Nothing
, depResolverVerbosity = normal
}
addTargets
:: [PackageName]
-> DepResolverParams
-> DepResolverParams
addTargets extraTargets params =
params
{ depResolverTargets = Set.fromList extraTargets `Set.union` depResolverTargets params
}
addConstraints
:: [LabeledPackageConstraint]
-> DepResolverParams
-> DepResolverParams
addConstraints extraConstraints params =
params
{ depResolverConstraints =
extraConstraints
++ depResolverConstraints params
}
addPreferences
:: [PackagePreference]
-> DepResolverParams
-> DepResolverParams
addPreferences extraPreferences params =
params
{ depResolverPreferences =
extraPreferences
++ depResolverPreferences params
}
setPreferenceDefault
:: PackagesPreferenceDefault
-> DepResolverParams
-> DepResolverParams
setPreferenceDefault preferenceDefault params =
params
{ depResolverPreferenceDefault = preferenceDefault
}
setReorderGoals :: ReorderGoals -> DepResolverParams -> DepResolverParams
setReorderGoals reorder params =
params
{ depResolverReorderGoals = reorder
}
setCountConflicts :: CountConflicts -> DepResolverParams -> DepResolverParams
setCountConflicts count params =
params
{ depResolverCountConflicts = count
}
setFineGrainedConflicts :: FineGrainedConflicts -> DepResolverParams -> DepResolverParams
setFineGrainedConflicts fineGrained params =
params
{ depResolverFineGrainedConflicts = fineGrained
}
setMinimizeConflictSet :: MinimizeConflictSet -> DepResolverParams -> DepResolverParams
setMinimizeConflictSet minimize params =
params
{ depResolverMinimizeConflictSet = minimize
}
setIndependentGoals :: IndependentGoals -> DepResolverParams -> DepResolverParams
setIndependentGoals indep params =
params
{ depResolverIndependentGoals = indep
}
setAvoidReinstalls :: AvoidReinstalls -> DepResolverParams -> DepResolverParams
setAvoidReinstalls avoid params =
params
{ depResolverAvoidReinstalls = avoid
}
setShadowPkgs :: ShadowPkgs -> DepResolverParams -> DepResolverParams
setShadowPkgs shadow params =
params
{ depResolverShadowPkgs = shadow
}
setStrongFlags :: StrongFlags -> DepResolverParams -> DepResolverParams
setStrongFlags sf params =
params
{ depResolverStrongFlags = sf
}
setAllowBootLibInstalls :: AllowBootLibInstalls -> DepResolverParams -> DepResolverParams
setAllowBootLibInstalls i params =
params
{ depResolverAllowBootLibInstalls = i
}
setOnlyConstrained :: OnlyConstrained -> DepResolverParams -> DepResolverParams
setOnlyConstrained i params =
params
{ depResolverOnlyConstrained = i
}
setMaxBackjumps :: Maybe Int -> DepResolverParams -> DepResolverParams
setMaxBackjumps n params =
params
{ depResolverMaxBackjumps = n
}
setEnableBackjumping :: EnableBackjumping -> DepResolverParams -> DepResolverParams
setEnableBackjumping b params =
params
{ depResolverEnableBackjumping = b
}
setSolveExecutables :: SolveExecutables -> DepResolverParams -> DepResolverParams
setSolveExecutables b params =
params
{ depResolverSolveExecutables = b
}
setGoalOrder
:: Maybe (Variable QPN -> Variable QPN -> Ordering)
-> DepResolverParams
-> DepResolverParams
setGoalOrder order params =
params
{ depResolverGoalOrder = order
}
setSolverVerbosity :: Verbosity -> DepResolverParams -> DepResolverParams
setSolverVerbosity verbosity params =
params
{ depResolverVerbosity = verbosity
}
-- | Some packages are specific to a given compiler version and should never be
-- reinstalled.
dontInstallNonReinstallablePackages :: DepResolverParams -> DepResolverParams
dontInstallNonReinstallablePackages params =
addConstraints extraConstraints params
where
extraConstraints =
[ LabeledPackageConstraint
(PackageConstraint (ScopeAnyQualifier pkgname) PackagePropertyInstalled)
ConstraintSourceNonReinstallablePackage
| pkgname <- nonReinstallablePackages
]
-- | The set of non-reinstallable packages includes those which cannot be
-- rebuilt using a GHC installation and Hackage-published source distribution.
-- There are a few reasons why this might be true:
--
-- * the package overrides its unit ID (e.g. with ghc's @-this-unit-id@ flag),
-- which can result in multiple indistinguishable packages (having potentially
-- different ABIs) with the same unit ID.
--
-- * the package contains definitions of wired-in declarations which tie
-- it to a particular compiler (e.g. we can't build link against
-- @base-4.18.0.0@ using GHC 9.6.1).
--
-- * the package does not have a complete (that is, buildable) source distribution.
-- For instance, some packages provided by GHC rely on files outside of the
-- source tree generated by GHC's build system.
nonReinstallablePackages :: [PackageName]
nonReinstallablePackages =
[ mkPackageName "base"
, mkPackageName "ghc-bignum"
, mkPackageName "ghc-internal"
, mkPackageName "ghc-prim"
, mkPackageName "ghc"
, mkPackageName "integer-gmp"
, mkPackageName "integer-simple"
, mkPackageName "template-haskell"
]
addSourcePackages
:: [UnresolvedSourcePackage]
-> DepResolverParams
-> DepResolverParams
addSourcePackages pkgs params =
params
{ depResolverSourcePkgIndex =
foldl
(flip PackageIndex.insert)
(depResolverSourcePkgIndex params)
pkgs
}
hideInstalledPackagesSpecificBySourcePackageId
:: [PackageId]
-> DepResolverParams
-> DepResolverParams
hideInstalledPackagesSpecificBySourcePackageId pkgids params =
-- TODO: this should work using exclude constraints instead
params
{ depResolverInstalledPkgIndex =
foldl'
(flip InstalledPackageIndex.deleteSourcePackageId)
(depResolverInstalledPkgIndex params)
pkgids
}
hideInstalledPackagesAllVersions
:: [PackageName]
-> DepResolverParams
-> DepResolverParams
hideInstalledPackagesAllVersions pkgnames params =
-- TODO: this should work using exclude constraints instead
params
{ depResolverInstalledPkgIndex =
foldl'
(flip InstalledPackageIndex.deletePackageName)
(depResolverInstalledPkgIndex params)
pkgnames
}
-- | Remove upper bounds in dependencies using the policy specified by the
-- 'AllowNewer' argument (all/some/none).
--
-- Note: It's important to apply 'removeUpperBounds' after
-- 'addSourcePackages'. Otherwise, the packages inserted by
-- 'addSourcePackages' won't have upper bounds in dependencies relaxed.
removeUpperBounds :: AllowNewer -> DepResolverParams -> DepResolverParams
removeUpperBounds (AllowNewer relDeps) = removeBounds RelaxUpper relDeps
-- | Dual of 'removeUpperBounds'
removeLowerBounds :: AllowOlder -> DepResolverParams -> DepResolverParams
removeLowerBounds (AllowOlder relDeps) = removeBounds RelaxLower relDeps
data RelaxKind = RelaxLower | RelaxUpper
-- | Common internal implementation of 'removeLowerBounds'/'removeUpperBounds'
removeBounds :: RelaxKind -> RelaxDeps -> DepResolverParams -> DepResolverParams
removeBounds _ rd params | not (isRelaxDeps rd) = params -- no-op optimisation
removeBounds relKind relDeps params =
params
{ depResolverSourcePkgIndex = sourcePkgIndex'
}
where
sourcePkgIndex' :: PackageIndex.PackageIndex UnresolvedSourcePackage
sourcePkgIndex' = relaxDeps <$> depResolverSourcePkgIndex params
relaxDeps :: UnresolvedSourcePackage -> UnresolvedSourcePackage
relaxDeps srcPkg =
srcPkg
{ srcpkgDescription = relaxPackageDeps relKind relDeps (srcpkgDescription srcPkg)
}
-- | Relax the dependencies of this package if needed.
--
-- Helper function used by 'removeBounds'
relaxPackageDeps
:: RelaxKind
-> RelaxDeps
-> PD.GenericPackageDescription
-> PD.GenericPackageDescription
relaxPackageDeps _ rd gpd | not (isRelaxDeps rd) = gpd -- subsumed by no-op case in 'removeBounds'
relaxPackageDeps relKind RelaxDepsAll gpd = PD.transformAllBuildDepends relaxAll gpd
where
relaxAll :: Dependency -> Dependency
relaxAll (Dependency pkgName verRange cs) =
Dependency pkgName (removeBound relKind RelaxDepModNone verRange) cs
relaxPackageDeps relKind (RelaxDepsSome depsToRelax0) gpd =
PD.transformAllBuildDepends relaxSome gpd
where
thisPkgName = packageName gpd
thisPkgId = packageId gpd
depsToRelax = Map.fromList $ mapMaybe f depsToRelax0
f :: RelaxedDep -> Maybe (RelaxDepSubject, RelaxDepMod)
f (RelaxedDep scope rdm p) = case scope of
RelaxDepScopeAll -> Just (p, rdm)
RelaxDepScopePackage p0
| p0 == thisPkgName -> Just (p, rdm)
| otherwise -> Nothing
RelaxDepScopePackageId p0
| p0 == thisPkgId -> Just (p, rdm)
| otherwise -> Nothing
relaxSome :: Dependency -> Dependency
relaxSome d@(Dependency depName verRange cs)
| Just relMod <- Map.lookup RelaxDepSubjectAll depsToRelax =
-- a '*'-subject acts absorbing, for consistency with
-- the 'Semigroup RelaxDeps' instance
Dependency depName (removeBound relKind relMod verRange) cs
| Just relMod <- Map.lookup (RelaxDepSubjectPkg depName) depsToRelax =
Dependency depName (removeBound relKind relMod verRange) cs
| otherwise = d -- no-op
-- | Internal helper for 'relaxPackageDeps'
removeBound :: RelaxKind -> RelaxDepMod -> VersionRange -> VersionRange
removeBound RelaxLower RelaxDepModNone = removeLowerBound
removeBound RelaxUpper RelaxDepModNone = removeUpperBound
removeBound RelaxLower RelaxDepModCaret = transformCaretLower
removeBound RelaxUpper RelaxDepModCaret = transformCaretUpper
-- | Supply defaults for packages without explicit Setup dependencies
--
-- Note: It's important to apply 'addDefaultSetupDepends' after
-- 'addSourcePackages'. Otherwise, the packages inserted by
-- 'addSourcePackages' won't have upper bounds in dependencies relaxed.
addDefaultSetupDependencies
:: (UnresolvedSourcePackage -> Maybe [Dependency])
-> DepResolverParams
-> DepResolverParams
addDefaultSetupDependencies defaultSetupDeps params =
params
{ depResolverSourcePkgIndex =
fmap applyDefaultSetupDeps (depResolverSourcePkgIndex params)
}
where
applyDefaultSetupDeps :: UnresolvedSourcePackage -> UnresolvedSourcePackage
applyDefaultSetupDeps srcpkg =
srcpkg
{ srcpkgDescription =
gpkgdesc
{ PD.packageDescription =
pkgdesc
{ PD.setupBuildInfo =
case PD.setupBuildInfo pkgdesc of
Just sbi -> Just sbi
Nothing -> case defaultSetupDeps srcpkg of
Nothing -> Nothing
Just deps
| isCustom ->
Just
PD.SetupBuildInfo
{ PD.defaultSetupDepends = True
, PD.setupDepends = deps
}
| otherwise -> Nothing
}
}
}
where
isCustom = PD.buildType pkgdesc == PD.Custom || PD.buildType pkgdesc == PD.Hooks
gpkgdesc = srcpkgDescription srcpkg
pkgdesc = PD.packageDescription gpkgdesc
-- | If a package has a custom setup then we need to add a setup-depends
-- on Cabal.
addSetupCabalMinVersionConstraint
:: Version
-> DepResolverParams
-> DepResolverParams
addSetupCabalMinVersionConstraint minVersion =
addConstraints
[ LabeledPackageConstraint
( PackageConstraint
(ScopeAnySetupQualifier cabalPkgname)
(PackagePropertyVersion $ orLaterVersion minVersion)
)
ConstraintSetupCabalMinVersion
]
where
cabalPkgname = mkPackageName "Cabal"
-- | Variant of 'addSetupCabalMinVersionConstraint' which sets an
-- upper bound on @setup.Cabal@ labeled with 'ConstraintSetupCabalMaxVersion'.
addSetupCabalMaxVersionConstraint
:: Version
-> DepResolverParams
-> DepResolverParams
addSetupCabalMaxVersionConstraint maxVersion =
addConstraints
[ LabeledPackageConstraint
( PackageConstraint
(ScopeAnySetupQualifier cabalPkgname)
(PackagePropertyVersion $ earlierVersion maxVersion)
)
ConstraintSetupCabalMaxVersion
]
where
cabalPkgname = mkPackageName "Cabal"
-- | Add an a lower bound @setup.Cabal >= 3.13@ labeled with 'ConstraintSourceProfiledDynamic'
addSetupCabalProfiledDynamic
:: DepResolverParams
-> DepResolverParams
addSetupCabalProfiledDynamic =
addConstraints
[ LabeledPackageConstraint
( PackageConstraint
(ScopeAnySetupQualifier cabalPkgname)
(PackagePropertyVersion $ orLaterVersion (mkVersion [3, 13, 0]))
)
ConstraintSourceProfiledDynamic
]
where
cabalPkgname = mkPackageName "Cabal"
upgradeDependencies :: DepResolverParams -> DepResolverParams
upgradeDependencies = setPreferenceDefault PreferAllLatest
reinstallTargets :: DepResolverParams -> DepResolverParams
reinstallTargets params =
hideInstalledPackagesAllVersions (Set.toList $ depResolverTargets params) params
-- | A basic solver policy on which all others are built.
basicInstallPolicy
:: InstalledPackageIndex
-> SourcePackageDb
-> [PackageSpecifier UnresolvedSourcePackage]
-> DepResolverParams
basicInstallPolicy
installedPkgIndex
(SourcePackageDb sourcePkgIndex sourcePkgPrefs)
pkgSpecifiers =
addPreferences
[ PackageVersionPreference name ver
| (name, ver) <- Map.toList sourcePkgPrefs
]
. addConstraints
(concatMap pkgSpecifierConstraints pkgSpecifiers)
. addTargets
(map pkgSpecifierTarget pkgSpecifiers)
. hideInstalledPackagesSpecificBySourcePackageId
[packageId pkg | SpecificSourcePackage pkg <- pkgSpecifiers]
. addSourcePackages
[pkg | SpecificSourcePackage pkg <- pkgSpecifiers]
$ basicDepResolverParams
installedPkgIndex
sourcePkgIndex
-- | The policy used by all the standard commands, install, fetch, freeze etc
-- (but not the v2-build and related commands).
--
-- It extends the 'basicInstallPolicy' with a policy on setup deps.
standardInstallPolicy
:: InstalledPackageIndex
-> SourcePackageDb
-> [PackageSpecifier UnresolvedSourcePackage]
-> DepResolverParams
standardInstallPolicy installedPkgIndex sourcePkgDb pkgSpecifiers =
addDefaultSetupDependencies mkDefaultSetupDeps $
basicInstallPolicy
installedPkgIndex
sourcePkgDb
pkgSpecifiers
where
-- Force Cabal >= 1.24 dep when the package is affected by #3199.
mkDefaultSetupDeps :: UnresolvedSourcePackage -> Maybe [Dependency]
mkDefaultSetupDeps srcpkg
| affected =
Just [Dependency (mkPackageName "Cabal") (orLaterVersion $ mkVersion [1, 24]) mainLibSet]
| otherwise = Nothing
where
gpkgdesc = srcpkgDescription srcpkg
pkgdesc = PD.packageDescription gpkgdesc
bt = PD.buildType pkgdesc
affected = (bt == PD.Custom || bt == PD.Hooks) && hasBuildableFalse gpkgdesc
-- Does this package contain any components with non-empty 'build-depends'
-- and a 'buildable' field that could potentially be set to 'False'? False
-- positives are possible.
hasBuildableFalse :: PD.GenericPackageDescription -> Bool
hasBuildableFalse gpkg =
not (all alwaysTrue (zipWith PD.cOr buildableConditions noDepConditions))
where
buildableConditions = PD.extractConditions PD.buildable gpkg
noDepConditions =
PD.extractConditions
(null . PD.targetBuildDepends)
gpkg
alwaysTrue (PD.Lit True) = True
alwaysTrue _ = False
-- ------------------------------------------------------------
-- * Interface to the standard resolver
-- ------------------------------------------------------------
runSolver :: SolverConfig -> DependencyResolver UnresolvedPkgLoc
runSolver = modularResolver
-- | Run the dependency solver.
--
-- Since this is potentially an expensive operation, the result is wrapped in a
-- a 'Progress' structure that can be unfolded to provide progress information,
-- logging messages and the final result or an error.
resolveDependencies
:: Platform
-> CompilerInfo
-> Maybe PkgConfigDb
-> DepResolverParams
-> Progress String String SolverInstallPlan
resolveDependencies platform comp pkgConfigDB params =
Step (showDepResolverParams finalparams) $
fmap (validateSolverResult platform comp indGoals) $
runSolver
( SolverConfig
reordGoals
cntConflicts
fineGrained
minimize
indGoals
noReinstalls
shadowing
strFlags
onlyConstrained_
maxBkjumps
enableBj
solveExes
order
verbosity
(PruneAfterFirstSuccess False)
)
platform
comp
installedPkgIndex
sourcePkgIndex
pkgConfigDB
preferences
constraints
targets
where
finalparams@( DepResolverParams
targets
constraints
prefs
defpref
installedPkgIndex
sourcePkgIndex
reordGoals
cntConflicts
fineGrained
minimize
indGoals
noReinstalls
shadowing
strFlags
_allowBootLibs
onlyConstrained_
maxBkjumps
enableBj
solveExes
order
verbosity
) =
if asBool (depResolverAllowBootLibInstalls params)
then params
else dontInstallNonReinstallablePackages params
preferences :: PackageName -> PackagePreferences
preferences = interpretPackagesPreference targets defpref prefs
-- | Give an interpretation to the global 'PackagesPreference' as
-- specific per-package 'PackageVersionPreference'.
interpretPackagesPreference
:: Set PackageName
-> PackagesPreferenceDefault
-> [PackagePreference]
-> (PackageName -> PackagePreferences)
interpretPackagesPreference selected defaultPref prefs =
\pkgname ->
PackagePreferences
(versionPref pkgname)
(installPref pkgname)
(stanzasPref pkgname)
where
versionPref :: PackageName -> [VersionRange]
versionPref pkgname =
fromMaybe [anyVersion] (Map.lookup pkgname versionPrefs)
versionPrefs =
Map.fromListWith
(++)
[ (pkgname, [pref])
| PackageVersionPreference pkgname pref <- prefs
]
installPref :: PackageName -> InstalledPreference
installPref pkgname =
fromMaybe (installPrefDefault pkgname) (Map.lookup pkgname installPrefs)
installPrefs =
Map.fromList
[ (pkgname, pref)
| PackageInstalledPreference pkgname pref <- prefs
]
installPrefDefault = case defaultPref of
PreferAllLatest -> const Preference.PreferLatest
PreferAllOldest -> const Preference.PreferOldest
PreferAllInstalled -> const Preference.PreferInstalled
PreferLatestForSelected -> \pkgname ->
-- When you say cabal install foo, what you really mean is, prefer the
-- latest version of foo, but the installed version of everything else
if pkgname `Set.member` selected
then Preference.PreferLatest
else Preference.PreferInstalled
stanzasPref :: PackageName -> [OptionalStanza]
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
-- ------------------------------------------------------------
-- | Make an install plan from the output of the dep resolver.
-- It checks that the plan is valid, or it's an error in the dep resolver.
validateSolverResult
:: Platform
-> CompilerInfo
-> IndependentGoals
-> [ResolverPackage UnresolvedPkgLoc]
-> SolverInstallPlan
validateSolverResult platform comp indepGoals pkgs =
case planPackagesProblems platform comp pkgs of
[] -> case SolverInstallPlan.new indepGoals graph of
Right plan -> plan
Left problems -> error (formatPlanProblems problems)
problems -> error (formatPkgProblems problems)
where
graph :: Graph.Graph (ResolverPackage UnresolvedPkgLoc)
graph = Graph.fromDistinctList pkgs
formatPkgProblems :: [PlanPackageProblem] -> String
formatPkgProblems = formatProblemMessage . map showPlanPackageProblem
formatPlanProblems :: [SolverInstallPlan.SolverPlanProblem] -> String
formatPlanProblems = formatProblemMessage . map SolverInstallPlan.showPlanProblem
formatProblemMessage problems =
unlines $
"internal error: could not construct a valid install plan."
: "The proposed (invalid) plan contained the following problems:"
: problems
++ "Proposed plan:"
: [SolverInstallPlan.showPlanIndex pkgs]
data PlanPackageProblem
= InvalidConfiguredPackage
(SolverPackage UnresolvedPkgLoc)
[PackageProblem]
| DuplicatePackageSolverId SolverId [ResolverPackage UnresolvedPkgLoc]
showPlanPackageProblem :: PlanPackageProblem -> String
showPlanPackageProblem (InvalidConfiguredPackage pkg packageProblems) =
"Package "
++ prettyShow (packageId pkg)
++ " has an invalid configuration, in particular:\n"
++ unlines
[ " " ++ showPackageProblem problem
| problem <- packageProblems
]
showPlanPackageProblem (DuplicatePackageSolverId pid dups) =
"Package "
++ prettyShow (packageId pid)
++ " has "
++ show (length dups)
++ " duplicate instances."
planPackagesProblems
:: Platform
-> CompilerInfo
-> [ResolverPackage UnresolvedPkgLoc]
-> [PlanPackageProblem]
planPackagesProblems platform cinfo pkgs =
[ InvalidConfiguredPackage pkg packageProblems
| Configured pkg <- pkgs
, let packageProblems = configuredPackageProblems platform cinfo pkg
, not (null packageProblems)
]
++ [ DuplicatePackageSolverId (Graph.nodeKey aDup) dups
| dups <- duplicatesBy (comparing Graph.nodeKey) pkgs
, aDup <- case dups of
[] -> []
(ad : _) -> [ad]
]
data PackageProblem
= DuplicateFlag PD.FlagName
| MissingFlag PD.FlagName
| ExtraFlag PD.FlagName
| DuplicateDeps [PackageId]
| MissingDep Dependency
| ExtraDep PackageId
| InvalidDep Dependency PackageId
showPackageProblem :: PackageProblem -> String
showPackageProblem (DuplicateFlag flag) =
"duplicate flag in the flag assignment: " ++ PD.unFlagName flag
showPackageProblem (MissingFlag flag) =
"missing an assignment for the flag: " ++ PD.unFlagName flag
showPackageProblem (ExtraFlag flag) =
"extra flag given that is not used by the package: " ++ PD.unFlagName flag
showPackageProblem (DuplicateDeps pkgids) =
"duplicate packages specified as selected dependencies: "
++ intercalate ", " (map prettyShow pkgids)
showPackageProblem (MissingDep dep) =
"the package has a dependency "