Skip to content

Commit dd1bd2a

Browse files
committed
feat(cabal-install-solver): introduce Stage and Toolchain
add stages list
1 parent 6d741d4 commit dd1bd2a

File tree

6 files changed

+169
-0
lines changed

6 files changed

+169
-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,7 +95,9 @@ library
9595
Distribution.Solver.Types.SolverId
9696
Distribution.Solver.Types.SolverPackage
9797
Distribution.Solver.Types.SourcePackage
98+
Distribution.Solver.Types.Stage
9899
Distribution.Solver.Types.SummarizedMessage
100+
Distribution.Solver.Types.Toolchain
99101
Distribution.Solver.Types.Variable
100102

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

4646
import Data.Coerce (Coercible, coerce)
47+
import Distribution.Solver.Types.Stage (Stage)
4748
import Network.URI (URI (..), URIAuth (..), isUnreserved)
4849
import Test.QuickCheck
4950
( Arbitrary (..)
@@ -324,6 +325,10 @@ instance Arbitrary a => Arbitrary (OptionalStanzaMap a) where
324325
TestStanzas -> x1
325326
BenchStanzas -> x2
326327

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

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
@@ -73,6 +74,7 @@ instance ToExpr ReorderGoals
7374
instance ToExpr RepoIndexState
7475
instance ToExpr RepoName
7576
instance ToExpr ReportLevel
77+
instance ToExpr Stage
7678
instance ToExpr StrongFlags
7779
instance ToExpr Timestamp
7880
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
@@ -43,6 +43,7 @@ import Distribution.Solver.Types.Variable
4343
import Distribution.Verbosity
4444
import Distribution.Version
4545

46+
import Distribution.Solver.Types.Stage (Stage)
4647
import UnitTests.Distribution.Solver.Modular.DSL
4748
import UnitTests.Distribution.Solver.Modular.QuickCheck.Utils
4849
( ArbitraryOrd (..)
@@ -586,6 +587,12 @@ instance Arbitrary OptionalStanza where
586587
shrink BenchStanzas = [TestStanzas]
587588
shrink TestStanzas = []
588589

590+
instance Arbitrary Stage where
591+
arbitrary = elements [minBound .. maxBound]
592+
593+
shrink stage =
594+
[stage' | stage' <- [minBound .. maxBound], stage' /= stage]
595+
589596
instance ArbitraryOrd pn => ArbitraryOrd (Variable pn)
590597
instance ArbitraryOrd a => ArbitraryOrd (P.Qualified a)
591598
instance ArbitraryOrd P.PackagePath
@@ -597,6 +604,7 @@ instance ArbitraryOrd ShortText where
597604
arbitraryCompare = do
598605
strc <- arbitraryCompare
599606
pure $ \l r -> strc (fromShortText l) (fromShortText r)
607+
instance ArbitraryOrd Stage
600608

601609
deriving instance Generic (Variable pn)
602610
deriving instance Generic (P.Qualified a)

0 commit comments

Comments
 (0)