|
| 1 | +{-# LANGUAGE DeriveDataTypeable #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +module Distribution.Types.PkgconfigVersionRange ( |
| 4 | + PkgconfigVersionRange (..), |
| 5 | + anyPkgconfigVersion, |
| 6 | + isAnyPkgconfigVersion, |
| 7 | + withinPkgconfigVersionRange, |
| 8 | + ) where |
| 9 | + |
| 10 | +import Distribution.Compat.Prelude |
| 11 | +import Prelude () |
| 12 | + |
| 13 | +import Distribution.CabalSpecVersion |
| 14 | +import Distribution.Parsec |
| 15 | +import Distribution.Pretty |
| 16 | +import Distribution.Types.PkgconfigVersion |
| 17 | +import Distribution.Types.Version |
| 18 | +import Distribution.Types.VersionRange |
| 19 | + |
| 20 | +import qualified Data.ByteString.Char8 as BS8 |
| 21 | +import qualified Distribution.Compat.CharParsing as P |
| 22 | +import qualified Text.PrettyPrint as PP |
| 23 | + |
| 24 | +-- | @since 3.0 |
| 25 | +data PkgconfigVersionRange |
| 26 | + = PcAnyVersion |
| 27 | + | PcThisVersion PkgconfigVersion -- = version |
| 28 | + | PcLaterVersion PkgconfigVersion -- > version (NB. not >=) |
| 29 | + | PcEarlierVersion PkgconfigVersion -- < version |
| 30 | + | PcOrLaterVersion PkgconfigVersion -- >= version |
| 31 | + | PcOrEarlierVersion PkgconfigVersion -- =< version |
| 32 | + | PcUnionVersionRanges PkgconfigVersionRange PkgconfigVersionRange |
| 33 | + | PcIntersectVersionRanges PkgconfigVersionRange PkgconfigVersionRange |
| 34 | + deriving (Generic, Read, Show, Eq, Typeable, Data) |
| 35 | + |
| 36 | +instance Binary PkgconfigVersionRange |
| 37 | +instance NFData PkgconfigVersionRange where rnf = genericRnf |
| 38 | + |
| 39 | +instance Pretty PkgconfigVersionRange where |
| 40 | + pretty = pp 0 where |
| 41 | + pp :: Int -> PkgconfigVersionRange -> PP.Doc |
| 42 | + pp _ PcAnyVersion = PP.text "-any" |
| 43 | + pp _ (PcThisVersion v) = PP.text "==" <<>> pretty v |
| 44 | + pp _ (PcLaterVersion v) = PP.text ">" <<>> pretty v |
| 45 | + pp _ (PcEarlierVersion v) = PP.text "<" <<>> pretty v |
| 46 | + pp _ (PcOrLaterVersion v) = PP.text ">=" <<>> pretty v |
| 47 | + pp _ (PcOrEarlierVersion v) = PP.text "<=" <<>> pretty v |
| 48 | + |
| 49 | + pp d (PcUnionVersionRanges v u) = parens (d >= 1) $ |
| 50 | + pp 1 v PP.<+> PP.text "||" PP.<+> pp 0 u |
| 51 | + pp d (PcIntersectVersionRanges v u) = parens (d >= 2) $ |
| 52 | + pp 2 v PP.<+> PP.text "&&" PP.<+> pp 1 u |
| 53 | + |
| 54 | + parens True = PP.parens |
| 55 | + parens False = id |
| 56 | + |
| 57 | +instance Parsec PkgconfigVersionRange where |
| 58 | + -- note: the wildcard is used in some places, e.g |
| 59 | + -- http://hackage.haskell.org/package/bindings-libzip-0.10.1/bindings-libzip.cabal |
| 60 | + -- |
| 61 | + -- however, in the presense of alphanumerics etc. lax version parser, |
| 62 | + -- wildcard is ill-specified |
| 63 | + |
| 64 | + parsec = do |
| 65 | + csv <- askCabalSpecVersion |
| 66 | + if csv >= CabalSpecV3_0 |
| 67 | + then pkgconfigParser |
| 68 | + else versionRangeToPkgconfigVersionRange <$> versionRangeParser P.integral |
| 69 | + |
| 70 | +-- "modern" parser of @pkg-config@ package versions. |
| 71 | +pkgconfigParser :: CabalParsing m => m PkgconfigVersionRange |
| 72 | +pkgconfigParser = P.spaces >> expr where |
| 73 | + -- every parser here eats trailing space |
| 74 | + expr = do |
| 75 | + ts <- term `P.sepBy` (P.string "||" >> P.spaces) |
| 76 | + return $ foldr1 PcUnionVersionRanges ts |
| 77 | + |
| 78 | + term = do |
| 79 | + fs <- factor `P.sepBy` (P.string "&&" >> P.spaces) |
| 80 | + return $ foldr1 PcIntersectVersionRanges fs |
| 81 | + |
| 82 | + factor = parens expr <|> prim |
| 83 | + |
| 84 | + prim = do |
| 85 | + op <- P.munch1 (`elem` "<>=^-") P.<?> "operator" |
| 86 | + case op of |
| 87 | + "-" -> anyPkgconfigVersion <$ (P.string "any" *> P.spaces) |
| 88 | + |
| 89 | + "==" -> afterOp PcThisVersion |
| 90 | + ">" -> afterOp PcLaterVersion |
| 91 | + "<" -> afterOp PcEarlierVersion |
| 92 | + ">=" -> afterOp PcOrLaterVersion |
| 93 | + "<=" -> afterOp PcOrEarlierVersion |
| 94 | + |
| 95 | + _ -> P.unexpected $ "Unknown version operator " ++ show op |
| 96 | + |
| 97 | + afterOp f = do |
| 98 | + P.spaces |
| 99 | + v <- parsec |
| 100 | + P.spaces |
| 101 | + return (f v) |
| 102 | + |
| 103 | + parens = P.between |
| 104 | + ((P.char '(' P.<?> "opening paren") >> P.spaces) |
| 105 | + (P.char ')' >> P.spaces) |
| 106 | + |
| 107 | +anyPkgconfigVersion :: PkgconfigVersionRange |
| 108 | +anyPkgconfigVersion = PcAnyVersion |
| 109 | + |
| 110 | +-- | TODO: this is not precise, but used only to prettify output. |
| 111 | +isAnyPkgconfigVersion :: PkgconfigVersionRange -> Bool |
| 112 | +isAnyPkgconfigVersion = (== PcAnyVersion) |
| 113 | + |
| 114 | +withinPkgconfigVersionRange :: PkgconfigVersion -> PkgconfigVersionRange -> Bool |
| 115 | +withinPkgconfigVersionRange v = go where |
| 116 | + go PcAnyVersion = True |
| 117 | + go (PcThisVersion u) = v == u |
| 118 | + go (PcLaterVersion u) = v > u |
| 119 | + go (PcEarlierVersion u) = v < u |
| 120 | + go (PcOrLaterVersion u) = v >= u |
| 121 | + go (PcOrEarlierVersion u) = v <= u |
| 122 | + go (PcUnionVersionRanges a b) = go a || go b |
| 123 | + go (PcIntersectVersionRanges a b) = go a && go b |
| 124 | + |
| 125 | +------------------------------------------------------------------------------- |
| 126 | +-- Conversion |
| 127 | +------------------------------------------------------------------------------- |
| 128 | + |
| 129 | +versionToPkgconfigVersion :: Version -> PkgconfigVersion |
| 130 | +versionToPkgconfigVersion = PkgconfigVersion . BS8.pack . prettyShow |
| 131 | + |
| 132 | +versionRangeToPkgconfigVersionRange :: VersionRange -> PkgconfigVersionRange |
| 133 | +versionRangeToPkgconfigVersionRange = foldVersionRange |
| 134 | + anyPkgconfigVersion |
| 135 | + (PcThisVersion . versionToPkgconfigVersion) |
| 136 | + (PcLaterVersion . versionToPkgconfigVersion) |
| 137 | + (PcEarlierVersion . versionToPkgconfigVersion) |
| 138 | + PcUnionVersionRanges |
| 139 | + PcIntersectVersionRanges |
0 commit comments