Skip to content

Commit 80fe5e1

Browse files
committed
Upgrade to Cabal 2.0
1 parent 4fd1284 commit 80fe5e1

20 files changed

Lines changed: 97 additions & 69 deletions

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Major changes:
1111
details, please see
1212
[the writeup on these changes](https://www.fpcomplete.com/blog/2017/07/stacks-new-extensible-snapshots). [PR #3249](https://github.com/commercialhaskell/stack/pull/3249),
1313
see the PR description for a number of related issues.
14+
* Upgraded to version 2.0 of the Cabal library.
1415

1516
Behavior changes:
1617

Setup.hs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ module Main (main) where
33

44
import Data.List ( nub, sortBy )
55
import Data.Ord ( comparing )
6-
import Data.Version ( showVersion )
7-
import Distribution.Package ( PackageName(PackageName), PackageId, InstalledPackageId, packageVersion, packageName )
6+
import Distribution.Package ( PackageId, InstalledPackageId, packageVersion, packageName )
87
import Distribution.PackageDescription ( PackageDescription(), Executable(..) )
98
import Distribution.InstalledPackageInfo (sourcePackageId, installedPackageId)
109
import Distribution.Simple ( defaultMainWithHooks, UserHooks(..), simpleUserHooks )
@@ -13,7 +12,10 @@ import Distribution.Simple.BuildPaths ( autogenModulesDir )
1312
import Distribution.Simple.PackageIndex (allPackages, dependencyClosure)
1413
import Distribution.Simple.Setup ( BuildFlags(buildVerbosity), fromFlag )
1514
import Distribution.Simple.LocalBuildInfo ( installedPkgs, withLibLBI, withExeLBI, LocalBuildInfo(), ComponentLocalBuildInfo(componentPackageDeps) )
15+
import Distribution.Types.PackageName (PackageName, unPackageName)
16+
import Distribution.Types.UnqualComponentName (unUnqualComponentName)
1617
import Distribution.Verbosity ( Verbosity )
18+
import Distribution.Version ( showVersion )
1719
import System.FilePath ( (</>) )
1820

1921
main :: IO ()
@@ -29,27 +31,28 @@ generateBuildModule verbosity pkg lbi = do
2931
createDirectoryIfMissingVerbose verbosity True dir
3032
withLibLBI pkg lbi $ \_ libcfg -> do
3133
withExeLBI pkg lbi $ \exe clbi ->
32-
rewriteFile (dir </> "Build_" ++ exeName exe ++ ".hs") $ unlines
33-
[ "module Build_" ++ exeName exe ++ " where"
34+
rewriteFile (dir </> "Build_" ++ exeName' exe ++ ".hs") $ unlines
35+
[ "module Build_" ++ exeName' exe ++ " where"
3436
, ""
3537
, "deps :: [String]"
3638
, "deps = " ++ (show $ formatdeps (transDeps libcfg clbi))
3739
]
3840
where
41+
exeName' = unUnqualComponentName . exeName
3942
formatdeps = map formatone . sortBy (comparing unPackageName')
4043
formatone p = unPackageName' p ++ "-" ++ showVersion (packageVersion p)
41-
unPackageName' p = case packageName p of PackageName n -> n
44+
unPackageName' = unPackageName . packageName
4245
transDeps xs ys =
4346
either (map sourcePackageId . allPackages) handleDepClosureFailure $ dependencyClosure allInstPkgsIdx availInstPkgIds
4447
where
4548
allInstPkgsIdx = installedPkgs lbi
4649
allInstPkgIds = map installedPackageId $ allPackages allInstPkgsIdx
4750
-- instPkgIds includes `stack-X.X.X`, which is not a depedency hence is missing from allInstPkgsIdx. Filter that out.
48-
availInstPkgIds = filter (`elem` allInstPkgIds) . map fst $ testDeps xs ys
51+
availInstPkgIds = filter (`elem` allInstPkgIds) $ testDeps xs ys
4952
handleDepClosureFailure unsatisfied =
5053
error $
5154
"Computation of transitive dependencies failed." ++
5255
if null unsatisfied then "" else " Unresolved dependencies: " ++ show unsatisfied
5356

54-
testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo -> [(InstalledPackageId, PackageId)]
55-
testDeps xs ys = nub $ componentPackageDeps xs ++ componentPackageDeps ys
57+
testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo -> [InstalledPackageId]
58+
testDeps xs ys = map fst $ nub $ componentPackageDeps xs ++ componentPackageDeps ys

src/Stack/Build/ConstructPlan.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ packageDepsWithTools p = do
795795
ctx <- ask
796796
-- TODO: it would be cool to defer these warnings until there's an
797797
-- actual issue building the package.
798-
let toEither (Cabal.Dependency (Cabal.PackageName name) _) mp =
798+
let toEither (Cabal.Dependency (Cabal.unPackageName -> name) _) mp =
799799
case Map.toList mp of
800800
[] -> Left (NoToolFound name (packageName p))
801801
[_] -> Right mp

src/Stack/BuildPlan.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Distribution.PackageDescription (GenericPackageDescription,
4444
flagName, genPackageFlags,
4545
condExecutables)
4646
import qualified Distribution.PackageDescription as C
47+
import qualified Distribution.Types.UnqualComponentName as C
4748
import Distribution.System (Platform)
4849
import Distribution.Text (display)
4950
import qualified Distribution.Version as C
@@ -186,7 +187,7 @@ getToolMap ls locals =
186187
-- worse case scenario is we build an extra package that wasn't
187188
-- strictly needed.
188189
gpdExes :: GenericPackageDescription -> [Text]
189-
gpdExes = map (T.pack . fst) . condExecutables
190+
gpdExes = map (T.pack . C.unUnqualComponentName . fst) . condExecutables
190191

191192
gpdPackages :: [GenericPackageDescription] -> Map PackageName Version
192193
gpdPackages gpds = Map.fromList $

src/Stack/Config.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ import qualified Data.Text as T
5959
import Data.Text.Encoding (encodeUtf8)
6060
import qualified Data.Yaml as Yaml
6161
import qualified Distribution.PackageDescription as C
62+
import qualified Distribution.Types.UnqualComponentName as C
6263
import Distribution.System (OS (..), Platform (..), buildPlatform, Arch(OtherArch))
6364
import qualified Distribution.Text
64-
import Distribution.Version (simplifyVersionRange)
65+
import Distribution.Version (simplifyVersionRange, mkVersion')
6566
import GHC.Conc (getNumProcessors)
6667
import Lens.Micro (lens)
6768
import Network.HTTP.Client (parseUrlThrow)
@@ -473,7 +474,7 @@ loadConfigMaybeProject configArgs mresolver mproject = do
473474
LCSNoConfig _ -> configNoLocalConfig stackRoot mresolver configArgs
474475
LCSProject project -> loadHelper $ Just project
475476
LCSNoProject -> loadHelper Nothing
476-
unless (fromCabalVersion Meta.version `withinRange` configRequireStackVersion config)
477+
unless (fromCabalVersion (mkVersion' Meta.version) `withinRange` configRequireStackVersion config)
477478
(throwM (BadStackVersionException (configRequireStackVersion config)))
478479

479480
let mprojectRoot = fmap (\(_, fp, _) -> parent fp) mproject
@@ -708,9 +709,9 @@ getNamedComponents gpkg = Set.fromList $ concat
708709
]
709710
where
710711
go :: (T.Text -> NamedComponent)
711-
-> (C.GenericPackageDescription -> [String])
712+
-> (C.GenericPackageDescription -> [C.UnqualComponentName])
712713
-> [NamedComponent]
713-
go wrapper f = map (wrapper . T.pack) $ f gpkg
714+
go wrapper f = map (wrapper . T.pack . C.unUnqualComponentName) $ f gpkg
714715

715716
-- | Check if there are any duplicate package names and, if so, throw an
716717
-- exception.

src/Stack/Init.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ renderStackYaml p ignoredPackages dupPackages =
293293

294294
footerHelp =
295295
let major = toCabalVersion
296-
$ toMajorVersion $ fromCabalVersion Meta.version
296+
$ toMajorVersion $ fromCabalVersion $ C.mkVersion' Meta.version
297297
in commentHelp
298298
[ "Control whether we use the GHC we find on the path"
299299
, "system-ghc: true"

src/Stack/Options/Completion.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Data.Maybe
1818
import qualified Data.Set as Set
1919
import qualified Data.Text as T
2020
import qualified Distribution.PackageDescription as C
21+
import qualified Distribution.Types.UnqualComponentName as C
2122
import Options.Applicative
2223
import Options.Applicative.Builder.Extra
2324
import Stack.Config (getLocalPackages)
@@ -89,8 +90,8 @@ flagCompleter = buildConfigCompleter $ \input -> do
8990
(C.genPackageFlags (lpvGPD lpv)))
9091
$ Map.toList lpvs
9192
flagString name fl =
92-
case C.flagName fl of
93-
C.FlagName flname -> (if flagEnabled name fl then "-" else "") ++ flname
93+
let flname = C.unFlagName $ C.flagName fl
94+
in (if flagEnabled name fl then "-" else "") ++ flname
9495
flagEnabled name fl =
9596
fromMaybe (C.flagDefault fl) $
9697
Map.lookup (fromCabalFlagName (C.flagName fl)) $
@@ -107,5 +108,5 @@ projectExeCompleter = buildConfigCompleter $ \input -> do
107108
return $
108109
filter (input `isPrefixOf`) $
109110
nubOrd $
110-
concatMap (\(_, lpv) -> map fst (C.condExecutables (lpvGPD lpv))) $
111+
concatMap (\(_, lpv) -> map (C.unUnqualComponentName . fst) (C.condExecutables (lpvGPD lpv))) $
111112
Map.toList lpvs

src/Stack/Package.hs

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import qualified Data.Set as S
5151
import qualified Data.Text as T
5252
import Data.Text.Encoding (decodeUtf8, decodeUtf8With)
5353
import Data.Text.Encoding.Error (lenientDecode)
54-
import Data.Version (showVersion)
5554
import Distribution.Compiler
5655
import Distribution.ModuleName (ModuleName)
5756
import qualified Distribution.ModuleName as Cabal
@@ -65,7 +64,13 @@ import Distribution.ParseUtils
6564
import Distribution.Simple.Utils
6665
import Distribution.System (OS (..), Arch, Platform (..))
6766
import qualified Distribution.Text as D
67+
import qualified Distribution.Types.CondTree as Cabal
68+
import qualified Distribution.Types.Dependency as Cabal
69+
import qualified Distribution.Types.ExeDependency as Cabal
70+
import qualified Distribution.Types.LegacyExeDependency as Cabal
71+
import qualified Distribution.Types.UnqualComponentName as Cabal
6872
import qualified Distribution.Verbosity as D
73+
import Distribution.Version (showVersion)
6974
import qualified Hpack
7075
import qualified Hpack.Config as Hpack
7176
import Path as FL
@@ -116,7 +121,7 @@ readPackageUnresolvedBS source bs =
116121
rawParseGPD :: BS.ByteString
117122
-> Either PError ([PWarning], GenericPackageDescription)
118123
rawParseGPD bs =
119-
case parsePackageDescription chars of
124+
case parseGenericPackageDescription chars of
120125
ParseFailed per -> Left per
121126
ParseOk warnings gpkg -> Right (warnings,gpkg)
122127
where
@@ -217,21 +222,24 @@ packageFromPackageDescription packageConfig pkgFlags pkg =
217222
, packageLicense = license pkg
218223
, packageDeps = deps
219224
, packageFiles = pkgFiles
220-
, packageTools = packageDescTools pkg
225+
, packageTools = map
226+
(\(Cabal.ExeDependency name' _ range) -> Cabal.Dependency name' range)
227+
(packageDescTools pkg)
221228
, packageGhcOptions = packageConfigGhcOptions packageConfig
222229
, packageFlags = packageConfigFlags packageConfig
223230
, packageDefaultFlags = M.fromList
224231
[(fromCabalFlagName (flagName flag), flagDefault flag) | flag <- pkgFlags]
225232
, packageAllDeps = S.fromList (M.keys deps)
226233
, packageHasLibrary = maybe False (buildable . libBuildInfo) (library pkg)
227234
, packageTests = M.fromList
228-
[(T.pack (testName t), testInterface t) | t <- testSuites pkg
235+
[(T.pack (Cabal.unUnqualComponentName $ testName t), testInterface t) | t <- testSuites pkg
229236
, buildable (testBuildInfo t)]
230237
, packageBenchmarks = S.fromList
231-
[T.pack (benchmarkName biBuildInfo) | biBuildInfo <- benchmarks pkg
238+
[T.pack (Cabal.unUnqualComponentName $ benchmarkName biBuildInfo) | biBuildInfo <- benchmarks pkg
232239
, buildable (benchmarkBuildInfo biBuildInfo)]
233240
, packageExes = S.fromList
234-
[T.pack (exeName biBuildInfo) | biBuildInfo <- executables pkg
241+
[T.pack (Cabal.unUnqualComponentName $ exeName biBuildInfo)
242+
| biBuildInfo <- executables pkg
235243
, buildable (buildInfo biBuildInfo)]
236244
-- This is an action used to collect info needed for "stack ghci".
237245
-- This info isn't usually needed, so computation of it is deferred.
@@ -338,19 +346,19 @@ generatePkgDescOpts sourceMap installedMap omitPkgs addPkgs cabalfp pkg componen
338346
, fmap
339347
(\exe ->
340348
generate
341-
(CExe (T.pack (exeName exe)))
349+
(CExe (T.pack (Cabal.unUnqualComponentName (exeName exe))))
342350
(buildInfo exe))
343351
(executables pkg)
344352
, fmap
345353
(\bench ->
346354
generate
347-
(CBench (T.pack (benchmarkName bench)))
355+
(CBench (T.pack (Cabal.unUnqualComponentName (benchmarkName bench))))
348356
(benchmarkBuildInfo bench))
349357
(benchmarks pkg)
350358
, fmap
351359
(\test ->
352360
generate
353-
(CTest (T.pack (testName test)))
361+
(CTest (T.pack (Cabal.unUnqualComponentName (testName test))))
354362
(testBuildInfo test))
355363
(testSuites pkg)]))
356364
where
@@ -531,13 +539,13 @@ packageDependencies pkg =
531539
packageToolDependencies :: PackageDescription -> Map Text VersionRange
532540
packageToolDependencies =
533541
M.fromList .
534-
concatMap (fmap (packageNameText . depName &&& depRange) .
542+
concatMap (fmap (\(Cabal.LegacyExeDependency name range) -> (T.pack name, range)) .
535543
buildTools) .
536544
allBuildInfo'
537545

538546
-- | Get all dependencies of the package (buildable targets only).
539-
packageDescTools :: PackageDescription -> [Dependency]
540-
packageDescTools = concatMap buildTools . allBuildInfo'
547+
packageDescTools :: PackageDescription -> [Cabal.ExeDependency]
548+
packageDescTools = concatMap buildToolDepends . allBuildInfo'
541549

542550
-- | This is a copy-paste from Cabal's @allBuildInfo@ function, but with the
543551
-- @buildable@ test removed. The implementation is broken.
@@ -551,12 +559,10 @@ allBuildInfo' pkg_descr = [ bi | Just lib <- [library pkg_descr]
551559
, True || buildable bi ]
552560
++ [ bi | tst <- testSuites pkg_descr
553561
, let bi = testBuildInfo tst
554-
, True || buildable bi
555-
, testEnabled tst ]
562+
, True || buildable bi ]
556563
++ [ bi | tst <- benchmarks pkg_descr
557564
, let bi = benchmarkBuildInfo tst
558-
, True || buildable bi
559-
, benchmarkEnabled tst ]
565+
, True || buildable bi ]
560566

561567
-- | Get all files referenced by the package.
562568
packageDescModulesAndFiles
@@ -596,9 +602,9 @@ packageDescModulesAndFiles pkg = do
596602
return (modules, files, dfiles, warnings)
597603
where
598604
libComponent = const CLib
599-
exeComponent = CExe . T.pack . exeName
600-
testComponent = CTest . T.pack . testName
601-
benchComponent = CBench . T.pack . benchmarkName
605+
exeComponent = CExe . T.pack . Cabal.unUnqualComponentName . exeName
606+
testComponent = CTest . T.pack . Cabal.unUnqualComponentName . testName
607+
benchComponent = CBench . T.pack . Cabal.unUnqualComponentName . benchmarkName
602608
asModuleAndFileMap label f lib = do
603609
(a,b,c) <- f lib
604610
return (M.singleton (label lib) a, M.singleton (label lib) b, c)
@@ -651,7 +657,7 @@ resolveGlobFiles =
651657
--
652658
matchDirFileGlob_ :: (MonadLogger m, MonadIO m) => String -> String -> m [String]
653659
matchDirFileGlob_ dir filepath = case parseFileGlob filepath of
654-
Nothing -> liftIO $ die $
660+
Nothing -> liftIO $ throwString $
655661
"invalid file glob '" ++ filepath
656662
++ "'. Wildcards '*' are only allowed in place of the file"
657663
++ " name, not in the directory name or file extension."
@@ -681,7 +687,7 @@ benchmarkFiles bench = do
681687
dir <- asks (parent . fst)
682688
(modules,files,warnings) <-
683689
resolveFilesAndDeps
684-
(Just $ benchmarkName bench)
690+
(Just $ Cabal.unUnqualComponentName $ benchmarkName bench)
685691
(dirs ++ [dir])
686692
(bnames <> exposed)
687693
haskellModuleExts
@@ -705,7 +711,7 @@ testFiles test = do
705711
dir <- asks (parent . fst)
706712
(modules,files,warnings) <-
707713
resolveFilesAndDeps
708-
(Just $ testName test)
714+
(Just $ Cabal.unUnqualComponentName $ testName test)
709715
(dirs ++ [dir])
710716
(bnames <> exposed)
711717
haskellModuleExts
@@ -730,7 +736,7 @@ executableFiles exe = do
730736
dir <- asks (parent . fst)
731737
(modules,files,warnings) <-
732738
resolveFilesAndDeps
733-
(Just $ exeName exe)
739+
(Just $ Cabal.unUnqualComponentName $ exeName exe)
734740
(dirs ++ [dir])
735741
(map DotCabalModule (otherModules build) ++
736742
[DotCabalMain (modulePath exe)])
@@ -782,7 +788,7 @@ targetJsSources = jsSources
782788
resolvePackageDescription :: PackageConfig
783789
-> GenericPackageDescription
784790
-> PackageDescription
785-
resolvePackageDescription packageConfig (GenericPackageDescription desc defaultFlags mlib exes tests benches) =
791+
resolvePackageDescription packageConfig (GenericPackageDescription desc defaultFlags mlib _subLibs _foreignLibs exes tests benches) =
786792
desc {library =
787793
fmap (resolveConditions rc updateLibDeps) mlib
788794
,executables =
@@ -811,12 +817,18 @@ resolvePackageDescription packageConfig (GenericPackageDescription desc defaultF
811817
(buildInfo exe) {targetBuildDepends = deps}}
812818
updateTestDeps test deps =
813819
test {testBuildInfo =
814-
(testBuildInfo test) {targetBuildDepends = deps}
815-
,testEnabled = packageConfigEnableTests packageConfig}
820+
(testBuildInfo test)
821+
{ targetBuildDepends = deps
822+
, buildable = packageConfigEnableTests packageConfig
823+
}
824+
}
816825
updateBenchmarkDeps benchmark deps =
817826
benchmark {benchmarkBuildInfo =
818-
(benchmarkBuildInfo benchmark) {targetBuildDepends = deps}
819-
,benchmarkEnabled = packageConfigEnableBenchmarks packageConfig}
827+
(benchmarkBuildInfo benchmark)
828+
{ targetBuildDepends = deps
829+
, buildable = packageConfigEnableBenchmarks packageConfig
830+
}
831+
}
820832

821833
-- | Make a map from a list of flag specifications.
822834
--
@@ -854,7 +866,7 @@ resolveConditions :: (Monoid target,Show target)
854866
resolveConditions rc addDeps (CondNode lib deps cs) = basic <> children
855867
where basic = addDeps lib deps
856868
children = mconcat (map apply cs)
857-
where apply (cond,node,mcs) =
869+
where apply (Cabal.CondBranch cond node mcs) =
858870
if condSatisfied cond
859871
then resolveConditions rc addDeps node
860872
else maybe mempty (resolveConditions rc addDeps) mcs
@@ -1256,10 +1268,10 @@ cabalFilePackageId
12561268
:: (MonadIO m, MonadThrow m)
12571269
=> Path Abs File -> m PackageIdentifier
12581270
cabalFilePackageId fp = do
1259-
pkgDescr <- liftIO (D.readPackageDescription D.silent $ toFilePath fp)
1271+
pkgDescr <- liftIO (D.readGenericPackageDescription D.silent $ toFilePath fp)
12601272
(toStackPI . D.package . D.packageDescription) pkgDescr
12611273
where
1262-
toStackPI (D.PackageIdentifier (D.PackageName name) ver) = do
1274+
toStackPI (D.PackageIdentifier (D.unPackageName -> name) ver) = do
12631275
name' <- parsePackageNameFromString name
12641276
ver' <- parseVersionFromString (showVersion ver)
12651277
return (PackageIdentifier name' ver')

src/Stack/Setup.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import qualified Data.Yaml as Yaml
6363
import Distribution.System (OS, Arch (..), Platform (..))
6464
import qualified Distribution.System as Cabal
6565
import Distribution.Text (simpleParse)
66+
import Distribution.Version (mkVersion')
6667
import Lens.Micro (set)
6768
import Network.HTTP.Simple (getResponseBody, httpLBS, withResponse, getResponseStatusCode)
6869
import Network.HTTP.Download
@@ -1939,4 +1940,4 @@ getDownloadVersion (StackReleaseInfo val) = do
19391940
parseVersion $ T.drop 1 rawName
19401941

19411942
stackVersion :: Version
1942-
stackVersion = fromCabalVersion Meta.version
1943+
stackVersion = fromCabalVersion (mkVersion' Meta.version)

0 commit comments

Comments
 (0)