Skip to content

Commit 4cad7b0

Browse files
committed
Make more dependency types, and PkgconfigName
Plain `Dependency` should strictly refer to a Haskell Package, usually the library component of that package.
1 parent ad1b3e4 commit 4cad7b0

File tree

25 files changed

+216
-119
lines changed

25 files changed

+216
-119
lines changed

Cabal/Cabal.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ library
143143
Distribution.Make
144144
Distribution.ModuleName
145145
Distribution.Package
146+
Distribution.Package.TextClass
146147
Distribution.PackageDescription
147148
Distribution.PackageDescription.Check
148149
Distribution.PackageDescription.Configuration

Cabal/Distribution/Backpack/ComponentsGraph.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ toComponentsGraph enabled pkg_descr =
5252
-- The dependencies for the given component
5353
componentDeps component =
5454
[ CExeName toolname
55-
| Dependency pkgname _ <- buildTools bi
56-
, let toolname = packageNameToUnqualComponentName pkgname
55+
| LegacyExeDependency name _ <- buildTools bi
56+
, let toolname = mkUnqualComponentName name
5757
, toolname `elem` map exeName (executables pkg_descr) ]
5858

5959
++ [ if pkgname == packageName pkg_descr

Cabal/Distribution/Backpack/ConfiguredComponent.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,8 @@ toConfiguredComponent pkg_descr this_cid
140140
| otherwise
141141
= Map.toList external_lib_map
142142
exe_deps = [ cid
143-
| Dependency pkgname _ <- buildTools bi
144-
, let name = packageNameToUnqualComponentName pkgname
145-
, Just cid <- [ Map.lookup name exe_map ] ]
143+
| LegacyExeDependency name _ <- buildTools bi
144+
, Just cid <- [ Map.lookup (mkUnqualComponentName name) exe_map ] ]
146145

147146
-- | Also computes the 'ComponentId', and sets cc_public if necessary.
148147
-- This is Cabal-only; cabal-install won't use this.

Cabal/Distribution/InstalledPackageInfo.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import Distribution.Compat.Prelude
4949
import Distribution.ParseUtils
5050
import Distribution.License
5151
import Distribution.Package hiding (installedUnitId, installedPackageId)
52+
import Distribution.Package.TextClass ()
5253
import Distribution.Backpack
5354
import qualified Distribution.Package as Package
5455
import Distribution.ModuleName

Cabal/Distribution/Package.hs

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Distribution.Package (
2323
packageNameToUnqualComponentName, unqualComponentNameToPackageName,
2424
PackageIdentifier(..),
2525
PackageId,
26+
PkgconfigName, unPkgconfigName, mkPkgconfigName,
2627

2728
-- * Package keys/installed package IDs (used for linker symbols)
2829
ComponentId, unComponentId, mkComponentId,
@@ -43,6 +44,8 @@ module Distribution.Package (
4344

4445
-- * Package source dependencies
4546
Dependency(..),
47+
LegacyExeDependency(..),
48+
PkgconfigDependency(..),
4649
thisPackageVersion,
4750
notThisPackageVersion,
4851
simplifyDependency,
@@ -59,7 +62,7 @@ import Distribution.Compat.Prelude
5962
import Distribution.Utils.ShortText
6063

6164
import Distribution.Version
62-
( Version, VersionRange, anyVersion, thisVersion
65+
( Version, VersionRange, thisVersion
6366
, notThisVersion, simplifyVersionRange
6467
, nullVersion )
6568

@@ -69,7 +72,7 @@ import Distribution.Compat.ReadP
6972
import Distribution.Text
7073
import Distribution.ModuleName
7174

72-
import Text.PrettyPrint ((<+>), text)
75+
import Text.PrettyPrint (text)
7376

7477
-- | An unqualified component name, for any kind of component.
7578
--
@@ -175,6 +178,44 @@ instance Text PackageName where
175178
instance NFData PackageName where
176179
rnf (PackageName pkg) = rnf pkg
177180

181+
-- | A pkg-config library name
182+
--
183+
-- This is parsed as any valid argument to the pkg-config utility.
184+
--
185+
-- @since 2.0
186+
newtype PkgconfigName = PkgconfigName ShortText
187+
deriving (Generic, Read, Show, Eq, Ord, Typeable, Data)
188+
189+
-- | Convert 'PkgconfigName' to 'String'
190+
--
191+
-- @since 2.0
192+
unPkgconfigName :: PkgconfigName -> String
193+
unPkgconfigName (PkgconfigName s) = fromShortText s
194+
195+
-- | Construct a 'PkgconfigName' from a 'String'
196+
--
197+
-- 'mkPkgconfigName' is the inverse to 'unPkgconfigName'
198+
--
199+
-- Note: No validations are performed to ensure that the resulting
200+
-- 'PkgconfigName' is valid
201+
--
202+
-- @since 2.0
203+
mkPkgconfigName :: String -> PkgconfigName
204+
mkPkgconfigName = PkgconfigName . toShortText
205+
206+
instance Binary PkgconfigName
207+
208+
-- pkg-config allows versions and other letters in package names, eg
209+
-- "gtk+-2.0" is a valid pkg-config package _name_. It then has a package
210+
-- version number like 2.10.13
211+
instance Text PkgconfigName where
212+
disp = Disp.text . unPkgconfigName
213+
parse = mkPkgconfigName
214+
<$> munch1 (\c -> isAlphaNum c || c `elem` "+-._")
215+
216+
instance NFData PkgconfigName where
217+
rnf (PkgconfigName pkg) = rnf pkg
218+
178219
-- | Type alias so we can use the shorter name PackageId.
179220
type PackageId = PackageIdentifier
180221

@@ -354,19 +395,33 @@ mkLegacyUnitId = newSimpleUnitId . mkComponentId . display
354395
data Dependency = Dependency PackageName VersionRange
355396
deriving (Generic, Read, Show, Eq, Typeable, Data)
356397

357-
instance Binary Dependency
398+
-- | Describes a legacy `build-tools`-style dependency on an executable
399+
--
400+
-- It is "legacy" because we do not know what the build-tool referred to. It
401+
-- could refer to a pkg-config executable (PkgconfigName), or an internal
402+
-- executable (UnqualComponentName). Thus it is stringly typed.
403+
--
404+
-- @since 2.0
405+
data LegacyExeDependency = LegacyExeDependency
406+
String -- ^ Internal exe component or extern tool
407+
VersionRange
408+
deriving (Generic, Read, Show, Eq, Typeable, Data)
358409

359-
instance Text Dependency where
360-
disp (Dependency name ver) =
361-
disp name <+> disp ver
410+
-- | Describes a dependency on a pkg-config library
411+
--
412+
-- @since 2.0
413+
data PkgconfigDependency = PkgconfigDependency
414+
PkgconfigName
415+
VersionRange
416+
deriving (Generic, Read, Show, Eq, Typeable, Data)
362417

363-
parse = do name <- parse
364-
Parse.skipSpaces
365-
ver <- parse <++ return anyVersion
366-
Parse.skipSpaces
367-
return (Dependency name ver)
418+
instance Binary Dependency
419+
instance Binary LegacyExeDependency
420+
instance Binary PkgconfigDependency
368421

369422
instance NFData Dependency where rnf = genericRnf
423+
instance NFData LegacyExeDependency where rnf = genericRnf
424+
instance NFData PkgconfigDependency where rnf = genericRnf
370425

371426
thisPackageVersion :: PackageIdentifier -> Dependency
372427
thisPackageVersion (PackageIdentifier n v) =
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
-- | *Dependency Text instances moved from Distribution.Package
2+
{-# OPTIONS_GHC -fno-warn-orphans #-}
3+
module Distribution.Package.TextClass () where
4+
5+
import Prelude ()
6+
import Distribution.Compat.Prelude
7+
8+
import Distribution.Package
9+
import Distribution.ParseUtils
10+
import Distribution.Version (anyVersion)
11+
12+
import qualified Distribution.Compat.ReadP as Parse
13+
import qualified Text.PrettyPrint as Disp
14+
import Distribution.Compat.ReadP
15+
import Distribution.Text
16+
17+
import Text.PrettyPrint ((<+>))
18+
19+
20+
instance Text Dependency where
21+
disp (Dependency name ver) =
22+
disp name <+> disp ver
23+
24+
parse = do name <- parse
25+
Parse.skipSpaces
26+
ver <- parse <++ return anyVersion
27+
Parse.skipSpaces
28+
return (Dependency name ver)
29+
30+
instance Text LegacyExeDependency where
31+
disp (LegacyExeDependency name ver) =
32+
Disp.text name <+> disp ver
33+
34+
parse = do name <- parseMaybeQuoted parseBuildToolName
35+
Parse.skipSpaces
36+
ver <- parse <++ return anyVersion
37+
Parse.skipSpaces
38+
return $ LegacyExeDependency name ver
39+
where
40+
-- like parsePackageName but accepts symbols in components
41+
parseBuildToolName :: Parse.ReadP r String
42+
parseBuildToolName = do ns <- sepBy1 component (Parse.char '-')
43+
return (intercalate "-" ns)
44+
where component = do
45+
cs <- munch1 (\c -> isAlphaNum c || c == '+' || c == '_')
46+
if all isDigit cs then pfail else return cs
47+
48+
instance Text PkgconfigDependency where
49+
disp (PkgconfigDependency name ver) =
50+
disp name <+> disp ver
51+
52+
parse = do name <- parse
53+
Parse.skipSpaces
54+
ver <- parse <++ return anyVersion
55+
Parse.skipSpaces
56+
return $ PkgconfigDependency name ver

Cabal/Distribution/PackageDescription/Parse.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import Distribution.ParseUtils hiding (parseFields)
5555
import Distribution.PackageDescription
5656
import Distribution.PackageDescription.Utils
5757
import Distribution.Package
58+
import Distribution.Package.TextClass ()
5859
import Distribution.ModuleName
5960
import Distribution.Version
6061
import Distribution.Verbosity
@@ -407,7 +408,7 @@ binfoFieldDescrs =
407408
[ boolField "buildable"
408409
buildable (\val binfo -> binfo{buildable=val})
409410
, commaListField "build-tools"
410-
disp parseBuildTool
411+
disp parse
411412
buildTools (\xs binfo -> binfo{buildTools=xs})
412413
, commaListFieldWithSep vcat "build-depends"
413414
disp parse
@@ -425,7 +426,7 @@ binfoFieldDescrs =
425426
showToken parseTokenQ'
426427
ldOptions (\val binfo -> binfo{ldOptions=val})
427428
, commaListField "pkgconfig-depends"
428-
disp parsePkgconfigDependency
429+
disp parse
429430
pkgconfigDepends (\xs binfo -> binfo{pkgconfigDepends=xs})
430431
, listField "frameworks"
431432
showToken parseTokenQ

Cabal/Distribution/PackageDescription/Parsec/FieldDescr.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import qualified Distribution.Compat.Parsec as Parsec
4141
import Distribution.Compiler (CompilerFlavor (..))
4242
import Distribution.ModuleName (ModuleName)
4343
import Distribution.Package
44+
import Distribution.Package.TextClass ()
4445
import Distribution.PackageDescription
4546
import Distribution.Types.ForeignLib
4647
import Distribution.Parsec.Class
@@ -415,7 +416,7 @@ binfoFieldDescrs =
415416
[ boolField "buildable"
416417
buildable (\val binfo -> binfo{buildable=val})
417418
, commaListField "build-tools"
418-
disp parsecBuildTool
419+
disp parsec
419420
buildTools (\xs binfo -> binfo{buildTools=xs})
420421
, commaListFieldWithSep vcat "build-depends"
421422
disp parsec
@@ -433,7 +434,7 @@ binfoFieldDescrs =
433434
showToken parsecToken'
434435
ldOptions (\val binfo -> binfo{ldOptions=val})
435436
, commaListField "pkgconfig-depends"
436-
disp parsecPkgconfigDependency
437+
disp parsec
437438
pkgconfigDepends (\xs binfo -> binfo{pkgconfigDepends=xs})
438439
, listField "frameworks"
439440
showToken parsecToken

Cabal/Distribution/ParseUtils.hs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ module Distribution.ParseUtils (
2828
showFields, showSingleNamedField, showSimpleSingleNamedField,
2929
parseFields, parseFieldsFlat,
3030
parseFilePathQ, parseTokenQ, parseTokenQ',
31-
parseModuleNameQ, parseBuildTool, parsePkgconfigDependency,
31+
parseModuleNameQ,
3232
parseOptVersion, parsePackageNameQ,
3333
parseTestedWithQ, parseLicenseQ, parseLanguageQ, parseExtensionQ,
3434
parseSepList, parseCommaList, parseOptCommaList,
3535
showFilePath, showToken, showTestedWith, showFreeText, parseFreeText,
3636
field, simpleField, listField, listFieldWithSep, spaceListField,
3737
commaListField, commaListFieldWithSep, commaNewLineListField,
38-
optsField, liftField, boolField, parseQuoted, indentWith,
38+
optsField, liftField, boolField, parseQuoted, parseMaybeQuoted, indentWith,
3939

4040
UnrecFieldParser, warnUnrec, ignoreUnrec,
4141
) where
@@ -625,33 +625,6 @@ betweenSpaces act = do skipSpaces
625625
skipSpaces
626626
return res
627627

628-
parseBuildTool :: ReadP r Dependency
629-
parseBuildTool = do name <- parseBuildToolNameQ
630-
ver <- betweenSpaces $
631-
parse <++ return anyVersion
632-
return $ Dependency name ver
633-
634-
parseBuildToolNameQ :: ReadP r PackageName
635-
parseBuildToolNameQ = parseMaybeQuoted parseBuildToolName
636-
637-
-- like parsePackageName but accepts symbols in components
638-
parseBuildToolName :: ReadP r PackageName
639-
parseBuildToolName = do ns <- sepBy1 component (ReadP.char '-')
640-
return (mkPackageName (intercalate "-" ns))
641-
where component = do
642-
cs <- munch1 (\c -> isAlphaNum c || c == '+' || c == '_')
643-
if all isDigit cs then pfail else return cs
644-
645-
-- pkg-config allows versions and other letters in package names,
646-
-- eg "gtk+-2.0" is a valid pkg-config package _name_.
647-
-- It then has a package version number like 2.10.13
648-
parsePkgconfigDependency :: ReadP r Dependency
649-
parsePkgconfigDependency = do name <- munch1
650-
(\c -> isAlphaNum c || c `elem` "+-._")
651-
ver <- betweenSpaces $
652-
parse <++ return anyVersion
653-
return $ Dependency (mkPackageName name) ver
654-
655628
parsePackageNameQ :: ReadP r PackageName
656629
parsePackageNameQ = parseMaybeQuoted parse
657630

0 commit comments

Comments
 (0)