Add some Binary & Structured instances
[cabal.git] / Cabal-syntax / src / Distribution / Types / Component.hs
blobfee1201fba9aecfbcecd93eb332f0a7bf626ad9f
1 {-# LANGUAGE DeriveGeneric #-}
3 module Distribution.Types.Component
4 ( Component (..)
5 , foldComponent
6 , componentBuildInfo
7 , componentBuildable
8 , componentName
9 , partitionComponents
10 ) where
12 import Distribution.Compat.Prelude
13 import Prelude ()
15 import Distribution.Types.Benchmark
16 import Distribution.Types.Executable
17 import Distribution.Types.ForeignLib
18 import Distribution.Types.Library
19 import Distribution.Types.TestSuite
21 import Distribution.Types.BuildInfo
22 import Distribution.Types.ComponentName
24 import qualified Distribution.Types.BuildInfo.Lens as L
26 data Component
27 = CLib Library
28 | CFLib ForeignLib
29 | CExe Executable
30 | CTest TestSuite
31 | CBench Benchmark
32 deriving (Generic, Show, Eq, Read)
34 instance Binary Component
35 instance Structured Component
37 instance Semigroup Component where
38 CLib l <> CLib l' = CLib (l <> l')
39 CFLib l <> CFLib l' = CFLib (l <> l')
40 CExe e <> CExe e' = CExe (e <> e')
41 CTest t <> CTest t' = CTest (t <> t')
42 CBench b <> CBench b' = CBench (b <> b')
43 _ <> _ = error "Cannot merge Component"
45 instance L.HasBuildInfo Component where
46 buildInfo f (CLib l) = CLib <$> L.buildInfo f l
47 buildInfo f (CFLib l) = CFLib <$> L.buildInfo f l
48 buildInfo f (CExe e) = CExe <$> L.buildInfo f e
49 buildInfo f (CTest t) = CTest <$> L.buildInfo f t
50 buildInfo f (CBench b) = CBench <$> L.buildInfo f b
52 foldComponent
53 :: (Library -> a)
54 -> (ForeignLib -> a)
55 -> (Executable -> a)
56 -> (TestSuite -> a)
57 -> (Benchmark -> a)
58 -> Component
59 -> a
60 foldComponent f _ _ _ _ (CLib lib) = f lib
61 foldComponent _ f _ _ _ (CFLib flib) = f flib
62 foldComponent _ _ f _ _ (CExe exe) = f exe
63 foldComponent _ _ _ f _ (CTest tst) = f tst
64 foldComponent _ _ _ _ f (CBench bch) = f bch
66 componentBuildInfo :: Component -> BuildInfo
67 componentBuildInfo =
68 foldComponent libBuildInfo foreignLibBuildInfo buildInfo testBuildInfo benchmarkBuildInfo
70 -- | Is a component buildable (i.e., not marked with @buildable: False@)?
71 -- See also this note in
72 -- "Distribution.Types.ComponentRequestedSpec#buildable_vs_enabled_components".
74 -- @since 2.0.0.2
75 componentBuildable :: Component -> Bool
76 componentBuildable = buildable . componentBuildInfo
78 componentName :: Component -> ComponentName
79 componentName =
80 foldComponent
81 (CLibName . libName)
82 (CFLibName . foreignLibName)
83 (CExeName . exeName)
84 (CTestName . testName)
85 (CBenchName . benchmarkName)
87 partitionComponents
88 :: [Component]
89 -> ([Library], [ForeignLib], [Executable], [TestSuite], [Benchmark])
90 partitionComponents = foldr (foldComponent fa fb fc fd fe) ([], [], [], [], [])
91 where
92 fa x ~(a, b, c, d, e) = (x : a, b, c, d, e)
93 fb x ~(a, b, c, d, e) = (a, x : b, c, d, e)
94 fc x ~(a, b, c, d, e) = (a, b, x : c, d, e)
95 fd x ~(a, b, c, d, e) = (a, b, c, x : d, e)
96 fe x ~(a, b, c, d, e) = (a, b, c, d, x : e)