Improve bad cabal-version error message (#9754)
[cabal.git] / Cabal-syntax / src / Distribution / CabalSpecVersion.hs
blobeb029b5ffc9a8996208ac56f973b8d85452e8244
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 deriving (Eq, Ord, Show, Read, Enum, Bounded, Typeable, Data, Generic)
39 instance Binary CabalSpecVersion
40 instance Structured CabalSpecVersion
41 instance NFData CabalSpecVersion where rnf = genericRnf
43 -- | Show cabal spec version, but not the way in the .cabal files
45 -- @since 3.0.0.0
46 showCabalSpecVersion :: CabalSpecVersion -> String
47 showCabalSpecVersion CabalSpecV3_12 = "3.12"
48 showCabalSpecVersion CabalSpecV3_8 = "3.8"
49 showCabalSpecVersion CabalSpecV3_6 = "3.6"
50 showCabalSpecVersion CabalSpecV3_4 = "3.4"
51 showCabalSpecVersion CabalSpecV3_0 = "3.0"
52 showCabalSpecVersion CabalSpecV2_4 = "2.4"
53 showCabalSpecVersion CabalSpecV2_2 = "2.2"
54 showCabalSpecVersion CabalSpecV2_0 = "2.0"
55 showCabalSpecVersion CabalSpecV1_24 = "1.24"
56 showCabalSpecVersion CabalSpecV1_22 = "1.22"
57 showCabalSpecVersion CabalSpecV1_20 = "1.20"
58 showCabalSpecVersion CabalSpecV1_18 = "1.18"
59 showCabalSpecVersion CabalSpecV1_12 = "1.12"
60 showCabalSpecVersion CabalSpecV1_10 = "1.10"
61 showCabalSpecVersion CabalSpecV1_8 = "1.8"
62 showCabalSpecVersion CabalSpecV1_6 = "1.6"
63 showCabalSpecVersion CabalSpecV1_4 = "1.4"
64 showCabalSpecVersion CabalSpecV1_2 = "1.2"
65 showCabalSpecVersion CabalSpecV1_0 = "1.0"
67 cabalSpecLatest :: CabalSpecVersion
68 cabalSpecLatest = CabalSpecV3_12
70 -- | Parse 'CabalSpecVersion' from version digits.
72 -- It may fail if for recent versions the version is not exact.
73 cabalSpecFromVersionDigits :: [Int] -> Maybe CabalSpecVersion
74 cabalSpecFromVersionDigits v
75 | v == [3, 12] = Just CabalSpecV3_12
76 | v == [3, 8] = Just CabalSpecV3_8
77 | v == [3, 6] = Just CabalSpecV3_6
78 | v == [3, 4] = Just CabalSpecV3_4
79 | v == [3, 0] = Just CabalSpecV3_0
80 | v == [2, 4] = Just CabalSpecV2_4
81 | v == [2, 2] = Just CabalSpecV2_2
82 | v == [2, 0] = Just CabalSpecV2_0
83 | v >= [1, 25] = Nothing
84 | v >= [1, 23] = Just CabalSpecV1_24
85 | v >= [1, 21] = Just CabalSpecV1_22
86 | v >= [1, 19] = Just CabalSpecV1_20
87 | v >= [1, 17] = Just CabalSpecV1_18
88 | v >= [1, 11] = Just CabalSpecV1_12
89 | v >= [1, 9] = Just CabalSpecV1_10
90 | v >= [1, 7] = Just CabalSpecV1_8
91 | v >= [1, 5] = Just CabalSpecV1_6
92 | v >= [1, 3] = Just CabalSpecV1_4
93 | v >= [1, 1] = Just CabalSpecV1_2
94 | otherwise = Just CabalSpecV1_0
96 -- | @since 3.4.0.0
97 cabalSpecToVersionDigits :: CabalSpecVersion -> [Int]
98 cabalSpecToVersionDigits CabalSpecV3_12 = [3, 12]
99 cabalSpecToVersionDigits CabalSpecV3_8 = [3, 8]
100 cabalSpecToVersionDigits CabalSpecV3_6 = [3, 6]
101 cabalSpecToVersionDigits CabalSpecV3_4 = [3, 4]
102 cabalSpecToVersionDigits CabalSpecV3_0 = [3, 0]
103 cabalSpecToVersionDigits CabalSpecV2_4 = [2, 4]
104 cabalSpecToVersionDigits CabalSpecV2_2 = [2, 2]
105 cabalSpecToVersionDigits CabalSpecV2_0 = [2, 0]
106 cabalSpecToVersionDigits CabalSpecV1_24 = [1, 24]
107 cabalSpecToVersionDigits CabalSpecV1_22 = [1, 22]
108 cabalSpecToVersionDigits CabalSpecV1_20 = [1, 20]
109 cabalSpecToVersionDigits CabalSpecV1_18 = [1, 18]
110 cabalSpecToVersionDigits CabalSpecV1_12 = [1, 12]
111 cabalSpecToVersionDigits CabalSpecV1_10 = [1, 10]
112 cabalSpecToVersionDigits CabalSpecV1_8 = [1, 8]
113 cabalSpecToVersionDigits CabalSpecV1_6 = [1, 6]
114 cabalSpecToVersionDigits CabalSpecV1_4 = [1, 4]
115 cabalSpecToVersionDigits CabalSpecV1_2 = [1, 2]
116 cabalSpecToVersionDigits CabalSpecV1_0 = [1, 0]
118 -- | What is the minimum Cabal library version which knows how handle
119 -- this spec version.
121 -- /Note:/ this is a point where we could decouple cabal-spec and Cabal
122 -- versions, if we ever want that.
124 -- >>> cabalSpecMinimumLibraryVersion CabalSpecV3_0
125 -- [2,5]
127 -- >>> cabalSpecMinimumLibraryVersion CabalSpecV2_4
128 -- [2,3]
130 -- @since 3.4.0.0
131 cabalSpecMinimumLibraryVersion :: CabalSpecVersion -> [Int]
132 cabalSpecMinimumLibraryVersion CabalSpecV1_0 = [1, 0]
133 cabalSpecMinimumLibraryVersion csv = case cabalSpecToVersionDigits (pred csv) of
134 [x, y] -> [x, y + 1]
135 xs -> xs
137 specHasCommonStanzas :: CabalSpecVersion -> HasCommonStanzas
138 specHasCommonStanzas v =
139 if v >= CabalSpecV2_2
140 then HasCommonStanzas
141 else NoCommonStanzas
143 specHasElif :: CabalSpecVersion -> HasElif
144 specHasElif v =
145 if v >= CabalSpecV2_2
146 then HasElif
147 else NoElif
149 -------------------------------------------------------------------------------
150 -- Booleans
151 -------------------------------------------------------------------------------
153 -- IDEA: make some kind of tagged booleans?
154 data HasElif = HasElif | NoElif
155 deriving (Eq, Show)
157 data HasCommonStanzas = HasCommonStanzas | NoCommonStanzas
158 deriving (Eq, Show)
160 data HasGlobstar = HasGlobstar | NoGlobstar