Merge pull request #10771 from alt-romes/wip/romes/10686
[cabal.git] / Cabal-syntax / src / Distribution / CabalSpecVersion.hs
bloba81e8921a949e1af6226f7d8b1a27033865c6e26
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE DeriveGeneric #-}
4 module Distribution.CabalSpecVersion where
6 import Distribution.Compat.Prelude
7 import Prelude ()
9 -- | Different Cabal-the-spec versions.
11 -- We branch based on this at least in the parser.
12 data CabalSpecVersion
13 = -- | this is older than 'CabalSpecV1_2'
14 CabalSpecV1_0
15 | -- | new syntax (sections)
16 CabalSpecV1_2
17 | CabalSpecV1_4
18 | CabalSpecV1_6
19 | CabalSpecV1_8
20 | CabalSpecV1_10
21 | CabalSpecV1_12
22 | -- 1.16 -- 1.14: no changes
23 CabalSpecV1_18
24 | CabalSpecV1_20
25 | CabalSpecV1_22
26 | CabalSpecV1_24
27 | CabalSpecV2_0
28 | CabalSpecV2_2
29 | CabalSpecV2_4
30 | CabalSpecV3_0
31 | -- 3.2: no changes
32 CabalSpecV3_4
33 | CabalSpecV3_6
34 | CabalSpecV3_8
35 | -- 3.10: no changes
36 CabalSpecV3_12
37 | CabalSpecV3_14
38 deriving (Eq, Ord, Show, Read, Enum, Bounded, Data, Generic)
40 instance Binary CabalSpecVersion
41 instance Structured CabalSpecVersion
42 instance NFData CabalSpecVersion where rnf = genericRnf
44 -- | Show cabal spec version, but not the way in the .cabal files
46 -- @since 3.0.0.0
47 showCabalSpecVersion :: CabalSpecVersion -> String
48 showCabalSpecVersion CabalSpecV3_14 = "3.14"
49 showCabalSpecVersion CabalSpecV3_12 = "3.12"
50 showCabalSpecVersion CabalSpecV3_8 = "3.8"
51 showCabalSpecVersion CabalSpecV3_6 = "3.6"
52 showCabalSpecVersion CabalSpecV3_4 = "3.4"
53 showCabalSpecVersion CabalSpecV3_0 = "3.0"
54 showCabalSpecVersion CabalSpecV2_4 = "2.4"
55 showCabalSpecVersion CabalSpecV2_2 = "2.2"
56 showCabalSpecVersion CabalSpecV2_0 = "2.0"
57 showCabalSpecVersion CabalSpecV1_24 = "1.24"
58 showCabalSpecVersion CabalSpecV1_22 = "1.22"
59 showCabalSpecVersion CabalSpecV1_20 = "1.20"
60 showCabalSpecVersion CabalSpecV1_18 = "1.18"
61 showCabalSpecVersion CabalSpecV1_12 = "1.12"
62 showCabalSpecVersion CabalSpecV1_10 = "1.10"
63 showCabalSpecVersion CabalSpecV1_8 = "1.8"
64 showCabalSpecVersion CabalSpecV1_6 = "1.6"
65 showCabalSpecVersion CabalSpecV1_4 = "1.4"
66 showCabalSpecVersion CabalSpecV1_2 = "1.2"
67 showCabalSpecVersion CabalSpecV1_0 = "1.0"
69 cabalSpecLatest :: CabalSpecVersion
70 cabalSpecLatest = CabalSpecV3_14
72 -- | Parse 'CabalSpecVersion' from version digits.
74 -- It may fail if for recent versions the version is not exact.
75 cabalSpecFromVersionDigits :: [Int] -> Maybe CabalSpecVersion
76 cabalSpecFromVersionDigits v
77 | v == [3, 14] = Just CabalSpecV3_14
78 | v == [3, 12] = Just CabalSpecV3_12
79 | v == [3, 8] = Just CabalSpecV3_8
80 | v == [3, 6] = Just CabalSpecV3_6
81 | v == [3, 4] = Just CabalSpecV3_4
82 | v == [3, 0] = Just CabalSpecV3_0
83 | v == [2, 4] = Just CabalSpecV2_4
84 | v == [2, 2] = Just CabalSpecV2_2
85 | v == [2, 0] = Just CabalSpecV2_0
86 | v >= [1, 25] = Nothing
87 | v >= [1, 23] = Just CabalSpecV1_24
88 | v >= [1, 21] = Just CabalSpecV1_22
89 | v >= [1, 19] = Just CabalSpecV1_20
90 | v >= [1, 17] = Just CabalSpecV1_18
91 | v >= [1, 11] = Just CabalSpecV1_12
92 | v >= [1, 9] = Just CabalSpecV1_10
93 | v >= [1, 7] = Just CabalSpecV1_8
94 | v >= [1, 5] = Just CabalSpecV1_6
95 | v >= [1, 3] = Just CabalSpecV1_4
96 | v >= [1, 1] = Just CabalSpecV1_2
97 | otherwise = Just CabalSpecV1_0
99 -- | @since 3.4.0.0
100 cabalSpecToVersionDigits :: CabalSpecVersion -> [Int]
101 cabalSpecToVersionDigits CabalSpecV3_14 = [3, 14]
102 cabalSpecToVersionDigits CabalSpecV3_12 = [3, 12]
103 cabalSpecToVersionDigits CabalSpecV3_8 = [3, 8]
104 cabalSpecToVersionDigits CabalSpecV3_6 = [3, 6]
105 cabalSpecToVersionDigits CabalSpecV3_4 = [3, 4]
106 cabalSpecToVersionDigits CabalSpecV3_0 = [3, 0]
107 cabalSpecToVersionDigits CabalSpecV2_4 = [2, 4]
108 cabalSpecToVersionDigits CabalSpecV2_2 = [2, 2]
109 cabalSpecToVersionDigits CabalSpecV2_0 = [2, 0]
110 cabalSpecToVersionDigits CabalSpecV1_24 = [1, 24]
111 cabalSpecToVersionDigits CabalSpecV1_22 = [1, 22]
112 cabalSpecToVersionDigits CabalSpecV1_20 = [1, 20]
113 cabalSpecToVersionDigits CabalSpecV1_18 = [1, 18]
114 cabalSpecToVersionDigits CabalSpecV1_12 = [1, 12]
115 cabalSpecToVersionDigits CabalSpecV1_10 = [1, 10]
116 cabalSpecToVersionDigits CabalSpecV1_8 = [1, 8]
117 cabalSpecToVersionDigits CabalSpecV1_6 = [1, 6]
118 cabalSpecToVersionDigits CabalSpecV1_4 = [1, 4]
119 cabalSpecToVersionDigits CabalSpecV1_2 = [1, 2]
120 cabalSpecToVersionDigits CabalSpecV1_0 = [1, 0]
122 -- | What is the minimum Cabal library version which knows how handle
123 -- this spec version.
125 -- /Note:/ this is a point where we could decouple cabal-spec and Cabal
126 -- versions, if we ever want that.
128 -- >>> cabalSpecMinimumLibraryVersion CabalSpecV3_0
129 -- [2,5]
131 -- >>> cabalSpecMinimumLibraryVersion CabalSpecV2_4
132 -- [2,3]
134 -- @since 3.4.0.0
135 cabalSpecMinimumLibraryVersion :: CabalSpecVersion -> [Int]
136 cabalSpecMinimumLibraryVersion CabalSpecV1_0 = [1, 0]
137 cabalSpecMinimumLibraryVersion csv = case cabalSpecToVersionDigits (pred csv) of
138 [x, y] -> [x, y + 1]
139 xs -> xs
141 specHasCommonStanzas :: CabalSpecVersion -> HasCommonStanzas
142 specHasCommonStanzas v =
143 if v >= CabalSpecV2_2
144 then HasCommonStanzas
145 else NoCommonStanzas
147 specHasElif :: CabalSpecVersion -> HasElif
148 specHasElif v =
149 if v >= CabalSpecV2_2
150 then HasElif
151 else NoElif
153 -------------------------------------------------------------------------------
154 -- Booleans
155 -------------------------------------------------------------------------------
157 -- IDEA: make some kind of tagged booleans?
158 data HasElif = HasElif | NoElif
159 deriving (Eq, Show)
161 data HasCommonStanzas = HasCommonStanzas | NoCommonStanzas
162 deriving (Eq, Show)
164 data HasGlobstar = HasGlobstar | NoGlobstar