Skip to content

Commit c9852ac

Browse files
committed
feat(cabal-install-solver): introduce Stage and Toolchain
1 parent c8b468d commit c9852ac

File tree

6 files changed

+158
-0
lines changed

6 files changed

+158
-0
lines changed

cabal-install-solver/cabal-install-solver.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ library
9595
Distribution.Solver.Types.SolverId
9696
Distribution.Solver.Types.SolverPackage
9797
Distribution.Solver.Types.SourcePackage
98+
Distribution.Solver.Types.Stage
99+
Distribution.Solver.Types.Toolchain
98100
Distribution.Solver.Types.Variable
99101

100102
build-depends:
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{-# LANGUAGE DerivingVia #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE ScopedTypeVariables #-}
4+
{-# LANGUAGE DeriveTraversable #-}
5+
6+
module Distribution.Solver.Types.Stage
7+
( Stage (..)
8+
, Staged (..)
9+
, showStage
10+
, tabulate
11+
, foldMapWithKey
12+
, always
13+
, prev
14+
, next
15+
) where
16+
17+
import Prelude (Enum (..))
18+
import Distribution.Compat.Prelude
19+
20+
import Data.Maybe (fromJust)
21+
import GHC.Stack
22+
23+
import Distribution.Pretty (Pretty (..))
24+
import Distribution.Utils.Structured (Structured (..))
25+
import qualified Text.PrettyPrint as Disp
26+
27+
28+
data Stage
29+
= -- | -- The system where the build is running
30+
Build
31+
| -- | -- The system where the built artifacts will run
32+
Host
33+
deriving (Eq, Ord, Read, Show, Enum, Bounded, Generic, Typeable)
34+
35+
instance Binary Stage
36+
instance Structured Stage
37+
38+
instance Pretty Stage where
39+
pretty = Disp.text . showStage
40+
41+
showStage :: Stage -> String
42+
showStage Build = "build"
43+
showStage Host = "host"
44+
45+
-- TOOD: I think there is similar code for stanzas, compare.
46+
47+
newtype Staged a = Staged
48+
{ getStage :: Stage -> a
49+
}
50+
deriving (Functor, Generic, Typeable)
51+
deriving Applicative via ((->) Stage)
52+
53+
instance Eq a => Eq (Staged a) where
54+
lhs == rhs =
55+
all
56+
(\stage -> getStage lhs stage == getStage rhs stage)
57+
[minBound .. maxBound]
58+
59+
instance Show a => Show (Staged a) where
60+
showsPrec _ staged =
61+
showList
62+
[ (stage, getStage staged stage)
63+
| stage <- [minBound .. maxBound]
64+
]
65+
66+
instance Foldable Staged where
67+
foldMap f (Staged gs) = foldMap (f . gs) [minBound..maxBound]
68+
69+
instance Traversable Staged where
70+
traverse f = fmap index . traverse (traverse f) . tabulate
71+
72+
instance Binary a => Binary (Staged a) where
73+
put staged = put (tabulate staged)
74+
-- TODO this could be done better I think
75+
get = index <$> get
76+
77+
-- TODO: I have no idea if this is right
78+
instance (Typeable a, Structured a) => Structured (Staged a) where
79+
structure _ = structure (Proxy :: Proxy [(Stage, a)])
80+
81+
tabulate :: Staged a -> [(Stage, a)]
82+
tabulate staged =
83+
[ (stage, getStage staged stage)
84+
| stage <- [minBound .. maxBound]
85+
]
86+
87+
index :: HasCallStack => [(Stage, a)] -> Staged a
88+
index t = Staged (\s -> fromJust (lookup s t))
89+
90+
foldMapWithKey :: Monoid m => (Stage -> a -> m) -> Staged a -> m
91+
foldMapWithKey f = foldMap (uncurry f) . tabulate
92+
93+
always :: a -> Staged a
94+
always = Staged . const
95+
96+
prev :: Stage -> Stage
97+
prev s | s == minBound = s
98+
| otherwise = Prelude.pred s
99+
100+
next :: Stage -> Stage
101+
next s | s == maxBound = s
102+
| otherwise = Prelude.succ s
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
3+
module Distribution.Solver.Types.Toolchain
4+
( Toolchain (..)
5+
, Toolchains
6+
, Stage (..)
7+
, Staged (..)
8+
) where
9+
10+
import Distribution.Compat.Prelude
11+
import Prelude ()
12+
13+
import Distribution.Simple.Compiler
14+
import Distribution.Simple.Program.Db
15+
import Distribution.Solver.Types.Stage (getStage, Stage (..), Staged (..))
16+
import Distribution.System
17+
18+
---------------------------
19+
-- Toolchain
20+
--
21+
22+
data Toolchain = Toolchain
23+
{ toolchainPlatform :: Platform
24+
, toolchainCompiler :: Compiler
25+
, toolchainProgramDb :: ProgramDb
26+
}
27+
deriving (Show, Generic, Typeable)
28+
29+
-- TODO: review this
30+
instance Eq Toolchain where
31+
lhs == rhs =
32+
(((==) `on` toolchainPlatform) lhs rhs)
33+
&& (((==) `on` toolchainCompiler) lhs rhs)
34+
&& ((((==)) `on` (configuredPrograms . toolchainProgramDb)) lhs rhs)
35+
36+
instance Binary Toolchain
37+
instance Structured Toolchain
38+
39+
type Toolchains = Staged Toolchain

cabal-install/tests/UnitTests/Distribution/Client/ArbitraryInstances.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import Test.QuickCheck
6464
)
6565
import Test.QuickCheck.GenericArbitrary (genericArbitrary)
6666
import Test.QuickCheck.Instances.Cabal ()
67+
import Distribution.Solver.Types.Stage (Stage)
6768

6869
-- note: there are plenty of instances defined in ProjectConfig test file.
6970
-- they should be moved here or into Cabal-quickcheck
@@ -323,6 +324,10 @@ instance Arbitrary a => Arbitrary (OptionalStanzaMap a) where
323324
TestStanzas -> x1
324325
BenchStanzas -> x2
325326

327+
instance Arbitrary Stage where
328+
arbitrary = genericArbitrary
329+
shrink = genericShrink
330+
326331
-------------------------------------------------------------------------------
327332
-- BuildReport
328333
-------------------------------------------------------------------------------

cabal-install/tests/UnitTests/Distribution/Client/TreeDiffInstances.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Distribution.Solver.Types.OptionalStanza
99
import Distribution.Solver.Types.PackageConstraint
1010
import Distribution.Solver.Types.ProjectConfigPath
1111
import Distribution.Solver.Types.Settings
12+
import Distribution.Solver.Types.Stage
1213

1314
import Distribution.Client.BuildReports.Types
1415
import Distribution.Client.CmdInstall.ClientInstallFlags
@@ -76,6 +77,7 @@ instance ToExpr ReorderGoals
7677
instance ToExpr RepoIndexState
7778
instance ToExpr RepoName
7879
instance ToExpr ReportLevel
80+
instance ToExpr Stage
7981
instance ToExpr StrongFlags
8082
instance ToExpr Timestamp
8183
instance ToExpr TotalIndexState

cabal-install/tests/UnitTests/Distribution/Solver/Modular/QuickCheck.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import UnitTests.Distribution.Solver.Modular.QuickCheck.Utils
4848
( ArbitraryOrd (..)
4949
, testPropertyWithSeed
5050
)
51+
import Distribution.Solver.Types.Stage (Stage)
5152

5253
tests :: [TestTree]
5354
tests =
@@ -588,6 +589,12 @@ instance Arbitrary OptionalStanza where
588589
shrink BenchStanzas = [TestStanzas]
589590
shrink TestStanzas = []
590591

592+
instance Arbitrary Stage where
593+
arbitrary = elements [minBound .. maxBound]
594+
595+
shrink stage =
596+
[stage' | stage' <- [minBound .. maxBound], stage' /= stage]
597+
591598
instance ArbitraryOrd pn => ArbitraryOrd (Variable pn)
592599
instance ArbitraryOrd a => ArbitraryOrd (P.Qualified a)
593600
instance ArbitraryOrd P.PackagePath
@@ -599,6 +606,7 @@ instance ArbitraryOrd ShortText where
599606
arbitraryCompare = do
600607
strc <- arbitraryCompare
601608
pure $ \l r -> strc (fromShortText l) (fromShortText r)
609+
instance ArbitraryOrd Stage
602610

603611
deriving instance Generic (Variable pn)
604612
deriving instance Generic (P.Qualified a)

0 commit comments

Comments
 (0)