From 3fca557ebd75739efa5b015eb38ebbec380956fe Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Sun, 15 Jan 2017 22:36:32 +0200 Subject: [PATCH 1/4] Add readGenericPackageDescription to ReadP parser --- .../Distribution/PackageDescription/Parse.hs | 22 ++++++++++++++----- Cabal/Distribution/Simple.hs | 4 +--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Cabal/Distribution/PackageDescription/Parse.hs b/Cabal/Distribution/PackageDescription/Parse.hs index ed279656680..1ea7e54a060 100644 --- a/Cabal/Distribution/PackageDescription/Parse.hs +++ b/Cabal/Distribution/PackageDescription/Parse.hs @@ -19,6 +19,10 @@ module Distribution.PackageDescription.Parse ( -- * Package descriptions + readGenericPackageDescription, + parseGenericPackageDescription, + + -- ** Deprecated names readPackageDescription, parsePackageDescription, @@ -592,10 +596,14 @@ readHookedBuildInfo :: Verbosity -> FilePath -> IO HookedBuildInfo readHookedBuildInfo = readAndParseFile withFileContents parseHookedBuildInfo --- |Parse the given package file. readPackageDescription :: Verbosity -> FilePath -> IO GenericPackageDescription -readPackageDescription = - readAndParseFile withUTF8FileContents parsePackageDescription +readPackageDescription = readGenericPackageDescription +{-# DEPRECATED readPackageDescription "Use readGenericPackageDescription, old name is missleading." #-} + +-- | Parse the given package file. +readGenericPackageDescription :: Verbosity -> FilePath -> IO GenericPackageDescription +readGenericPackageDescription = + readAndParseFile withUTF8FileContents parseGenericPackageDescription stanzas :: [Field] -> [[Field]] stanzas [] = [] @@ -713,12 +721,16 @@ skipField = modify tail --FIXME: this should take a ByteString, not a String. We have to be able to -- decode UTF8 and handle the BOM. +parsePackageDescription :: String -> ParseResult GenericPackageDescription +parsePackageDescription = parseGenericPackageDescription +{-# DEPRECATED parsePackageDescription "Use parseGenericPackageDescription, old name is missleading" #-} + -- | Parses the given file into a 'GenericPackageDescription'. -- -- In Cabal 1.2 the syntax for package descriptions was changed to a format -- with sections and possibly indented property descriptions. -parsePackageDescription :: String -> ParseResult GenericPackageDescription -parsePackageDescription file = do +parseGenericPackageDescription :: String -> ParseResult GenericPackageDescription +parseGenericPackageDescription file = do -- This function is quite complex because it needs to be able to parse -- both pre-Cabal-1.2 and post-Cabal-1.2 files. Additionally, it contains diff --git a/Cabal/Distribution/Simple.hs b/Cabal/Distribution/Simple.hs index 91f8c861fee..cf33a6617d0 100644 --- a/Cabal/Distribution/Simple.hs +++ b/Cabal/Distribution/Simple.hs @@ -237,10 +237,8 @@ confPkgDescr hooks verbosity mb_path = do Just path -> return path #ifdef CABAL_PARSEC info verbosity "Using Parsec parser" - descr <- readGenericPackageDescription verbosity pdfile -#else - descr <- readPackageDescription verbosity pdfile #endif + descr <- readGenericPackageDescription verbosity pdfile return (Just pdfile, descr) buildAction :: UserHooks -> BuildFlags -> Args -> IO () From 856b6e1b61880fb2820064eab8831abc1ed50946 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Sun, 15 Jan 2017 23:17:47 +0200 Subject: [PATCH 2/4] Make cabal-install use parsec parser --- .../Distribution/PackageDescription/Parsec.hs | 9 ++++ Cabal/tests/ParserHackageTests.hs | 5 +-- cabal-install/Distribution/Client/Check.hs | 11 +++-- .../Distribution/Client/Configure.hs | 10 ++++- .../Distribution/Client/GenBounds.hs | 10 ++++- .../Distribution/Client/IndexUtils.hs | 44 ++++++++++++++----- .../Distribution/Client/ProjectConfig.hs | 9 +++- cabal-install/Distribution/Client/Sandbox.hs | 11 +++-- .../Distribution/Client/SetupWrapper.hs | 9 +++- cabal-install/Distribution/Client/SrcDist.hs | 12 +++-- cabal-install/Distribution/Client/Targets.hs | 27 +++++++++--- cabal-install/Main.hs | 10 +++-- cabal-install/cabal-install.cabal | 8 ++++ cabal-testsuite/Test/Cabal/Prelude.hs | 2 +- stack.yaml | 4 ++ 15 files changed, 140 insertions(+), 41 deletions(-) diff --git a/Cabal/Distribution/PackageDescription/Parsec.hs b/Cabal/Distribution/PackageDescription/Parsec.hs index ec9bd174285..f01a45b12e0 100644 --- a/Cabal/Distribution/PackageDescription/Parsec.hs +++ b/Cabal/Distribution/PackageDescription/Parsec.hs @@ -19,9 +19,11 @@ module Distribution.PackageDescription.Parsec ( -- * Package descriptions readGenericPackageDescription, parseGenericPackageDescription, + parseGenericPackageDescriptionMaybe, -- ** Parsing ParseResult, + runParseResult, -- ** Supplementary build information -- readHookedBuildInfo, @@ -105,6 +107,13 @@ parseGenericPackageDescription bs = case readFields' bs of -- TODO: better marshalling of errors Left perr -> parseFatalFailure (Position 0 0) (show perr) +-- | 'Maybe' variant of 'parseGenericPackageDescription' +parseGenericPackageDescriptionMaybe :: BS.ByteString -> Maybe GenericPackageDescription +parseGenericPackageDescriptionMaybe = + trdOf3 . runParseResult . parseGenericPackageDescription + where + trdOf3 (_, _, x) = x + runFieldParser :: FieldParser a -> [FieldLine Position] -> ParseResult a runFieldParser p ls = runFieldParser' pos p =<< fieldlinesToString pos ls where diff --git a/Cabal/tests/ParserHackageTests.hs b/Cabal/tests/ParserHackageTests.hs index 12acf497c80..6552c1b5952 100644 --- a/Cabal/tests/ParserHackageTests.hs +++ b/Cabal/tests/ParserHackageTests.hs @@ -30,7 +30,6 @@ import qualified Distribution.PackageDescription.Parse as ReadP import qualified Distribution.PackageDescription.Parsec as Parsec import qualified Distribution.Parsec.Parser as Parsec import qualified Distribution.Parsec.Types.Common as Parsec -import qualified Distribution.Parsec.Types.ParseResult as Parsec import qualified Distribution.ParseUtils as ReadP import qualified Distribution.Compat.DList as DList @@ -97,7 +96,7 @@ compareTest pfx fpath bsl let str = ignoreBOM $ fromUTF8LBS bsl putStrLn $ "::: " ++ fpath - (readp, readpWarnings) <- case ReadP.parsePackageDescription str of + (readp, readpWarnings) <- case ReadP.parseGenericPackageDescription str of ReadP.ParseOk ws x -> return (x, ws) ReadP.ParseFailed err -> print err >> exitFailure traverse_ (putStrLn . ReadP.showPWarning fpath) readpWarnings @@ -155,7 +154,7 @@ compareTest pfx fpath bsl parseReadpTest :: FilePath -> BSL.ByteString -> IO () parseReadpTest fpath bsl = when (not $ any ($ fpath) problematicFiles) $ do let str = fromUTF8LBS bsl - case ReadP.parsePackageDescription str of + case ReadP.parseGenericPackageDescription str of ReadP.ParseOk _ _ -> return () ReadP.ParseFailed err -> print err >> exitFailure diff --git a/cabal-install/Distribution/Client/Check.hs b/cabal-install/Distribution/Client/Check.hs index 2ea875e2537..76e5e5c98d2 100644 --- a/cabal-install/Distribution/Client/Check.hs +++ b/cabal-install/Distribution/Client/Check.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} ----------------------------------------------------------------------------- -- | -- Module : Distribution.Client.Check @@ -17,8 +18,12 @@ module Distribution.Client.Check ( import Control.Monad ( when, unless ) -import Distribution.PackageDescription.Parse - ( readPackageDescription ) +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec ( readGenericPackageDescription ) +#else +import Distribution.PackageDescription.Parse ( readGenericPackageDescription ) +#endif + import Distribution.PackageDescription.Check import Distribution.PackageDescription.Configuration ( flattenPackageDescription ) @@ -30,7 +35,7 @@ import Distribution.Simple.Utils check :: Verbosity -> IO Bool check verbosity = do pdfile <- defaultPackageDesc verbosity - ppd <- readPackageDescription verbosity pdfile + ppd <- readGenericPackageDescription verbosity pdfile -- flatten the generic package description into a regular package -- description -- TODO: this may give more warnings than it should give; diff --git a/cabal-install/Distribution/Client/Configure.hs b/cabal-install/Distribution/Client/Configure.hs index 8d1d442aebd..1eb31234ca6 100644 --- a/cabal-install/Distribution/Client/Configure.hs +++ b/cabal-install/Distribution/Client/Configure.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} ----------------------------------------------------------------------------- -- | -- Module : Distribution.Client.Configure @@ -67,8 +68,13 @@ import Distribution.Package import Distribution.Types.Dependency ( Dependency(..), thisPackageVersion ) import qualified Distribution.PackageDescription as PkgDesc +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec + ( readGenericPackageDescription ) +#else import Distribution.PackageDescription.Parse - ( readPackageDescription ) + ( readGenericPackageDescription ) +#endif import Distribution.PackageDescription.Configuration ( finalizePD ) import Distribution.Version @@ -296,7 +302,7 @@ planLocalPackage :: Verbosity -> Compiler -> IO (Progress String String SolverInstallPlan) planLocalPackage verbosity comp platform configFlags configExFlags installedPkgIndex (SourcePackageDb _ packagePrefs) pkgConfigDb = do - pkg <- readPackageDescription verbosity =<< + pkg <- readGenericPackageDescription verbosity =<< case flagToMaybe (configCabalFilePath configFlags) of Nothing -> defaultPackageDesc verbosity Just fp -> return fp diff --git a/cabal-install/Distribution/Client/GenBounds.hs b/cabal-install/Distribution/Client/GenBounds.hs index b946452d3e3..b6658c48684 100644 --- a/cabal-install/Distribution/Client/GenBounds.hs +++ b/cabal-install/Distribution/Client/GenBounds.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} ----------------------------------------------------------------------------- -- | -- Module : Distribution.Client.GenBounds @@ -28,8 +29,13 @@ import Distribution.PackageDescription ( buildDepends ) import Distribution.PackageDescription.Configuration ( finalizePD ) +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec + ( readGenericPackageDescription ) +#else import Distribution.PackageDescription.Parse - ( readPackageDescription ) + ( readGenericPackageDescription ) +#endif import Distribution.Types.ComponentRequestedSpec ( defaultComponentRequestedSpec ) import Distribution.Types.Dependency @@ -109,7 +115,7 @@ genBounds verbosity packageDBs repoCtxt comp platform progdb mSandboxPkgInfo cwd <- getCurrentDirectory path <- tryFindPackageDesc cwd - gpd <- readPackageDescription verbosity path + gpd <- readGenericPackageDescription verbosity path -- NB: We don't enable tests or benchmarks, since often they -- don't really have useful bounds. let epd = finalizePD [] defaultComponentRequestedSpec diff --git a/cabal-install/Distribution/Client/IndexUtils.hs b/cabal-install/Distribution/Client/IndexUtils.hs index d34b8b8f3ec..f78d8b2fa4f 100644 --- a/cabal-install/Distribution/Client/IndexUtils.hs +++ b/cabal-install/Distribution/Client/IndexUtils.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE BangPatterns #-} @@ -51,19 +52,14 @@ import Distribution.Package , Package(..), packageVersion, packageName ) import Distribution.Types.Dependency import Distribution.Simple.PackageIndex (InstalledPackageIndex) -import qualified Distribution.PackageDescription.Parse as PackageDesc.Parse import Distribution.PackageDescription ( GenericPackageDescription ) -import Distribution.PackageDescription.Parse - ( parsePackageDescription ) import Distribution.Simple.Compiler ( Compiler, PackageDBStack ) import Distribution.Simple.Program ( ProgramDb ) import qualified Distribution.Simple.Configure as Configure ( getInstalledPackages, getInstalledPackagesMonitorFiles ) -import Distribution.ParseUtils - ( ParseResult(..) ) import Distribution.Version ( mkVersion, intersectVersionRanges ) import Distribution.Text @@ -71,10 +67,24 @@ import Distribution.Text import Distribution.Verbosity ( Verbosity, normal, lessVerbose ) import Distribution.Simple.Utils - ( die, warn, info, fromUTF8, ignoreBOM ) + ( die, warn, info ) import Distribution.Client.Setup ( RepoContext(..) ) +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec + ( parseGenericPackageDescriptionMaybe ) +import qualified Distribution.PackageDescription.Parsec as PackageDesc.Parse +#else +import Distribution.ParseUtils + ( ParseResult(..) ) +import Distribution.PackageDescription.Parse + ( parseGenericPackageDescription ) +import Distribution.Simple.Utils + ( fromUTF8, ignoreBOM ) +import qualified Distribution.PackageDescription.Parse as PackageDesc.Parse +#endif + import Distribution.Solver.Types.PackageIndex (PackageIndex) import qualified Distribution.Solver.Types.PackageIndex as PackageIndex import Distribution.Solver.Types.SourcePackage @@ -434,12 +444,20 @@ extractPkg entry blockNo = case Tar.entryContent entry of Just ver -> Just . return $ Just (NormalPackage pkgid descr content blockNo) where pkgid = PackageIdentifier (mkPackageName pkgname) ver - parsed = parsePackageDescription . ignoreBOM . fromUTF8 . BS.Char8.unpack +#ifdef CABAL_PARSEC + parsed = parseGenericPackageDescriptionMaybe (BS.toStrict content) + descr = case parsed of + Just d -> d + Nothing -> error $ "Couldn't read cabal file " + ++ show fileName +#else + parsed = parseGenericPackageDescription . ignoreBOM . fromUTF8 . BS.Char8.unpack $ content descr = case parsed of ParseOk _ d -> d _ -> error $ "Couldn't read cabal file " ++ show fileName +#endif _ -> Nothing _ -> Nothing @@ -451,7 +469,7 @@ extractPkg entry blockNo = case Tar.entryContent entry of result <- if not dirExists then return Nothing else do cabalFile <- tryFindAddSourcePackageDesc path "Error reading package index." - descr <- PackageDesc.Parse.readPackageDescription normal cabalFile + descr <- PackageDesc.Parse.readGenericPackageDescription normal cabalFile return . Just $ BuildTreeRef (refTypeFromTypeCode typeCode) (packageId descr) descr path blockNo return result @@ -674,7 +692,7 @@ packageListFromCache mkPkg hnd Cache{..} mode = accum mempty [] mempty cacheEntr path <- liftM byteStringToFilePath . getEntryContent $ blockno pkg <- do let err = "Error reading package index from cache." file <- tryFindAddSourcePackageDesc path err - PackageDesc.Parse.readPackageDescription normal file + PackageDesc.Parse.readGenericPackageDescription normal file let srcpkg = mkPkg (BuildTreeRef refType (packageId pkg) pkg path blockno) accum srcpkgs (srcpkg:btrs) prefs entries @@ -693,9 +711,15 @@ packageListFromCache mkPkg hnd Cache{..} mode = accum mempty [] mempty cacheEntr readPackageDescription :: ByteString -> IO GenericPackageDescription readPackageDescription content = - case parsePackageDescription . ignoreBOM . fromUTF8 . BS.Char8.unpack $ content of +#ifdef CABAL_PARSEC + case parseGenericPackageDescriptionMaybe (BS.toStrict content) of + Just gpd -> return gpd + Nothing -> interror "failed to parse .cabal file" +#else + case parseGenericPackageDescription . ignoreBOM . fromUTF8 . BS.Char8.unpack $ content of ParseOk _ d -> return d _ -> interror "failed to parse .cabal file" +#endif interror msg = die $ "internal error when reading package index: " ++ msg ++ "The package index or index cache is probably " diff --git a/cabal-install/Distribution/Client/ProjectConfig.hs b/cabal-install/Distribution/Client/ProjectConfig.hs index d04b4e9d11a..3b26670e58a 100644 --- a/cabal-install/Distribution/Client/ProjectConfig.hs +++ b/cabal-install/Distribution/Client/ProjectConfig.hs @@ -76,8 +76,13 @@ import Distribution.System ( Platform ) import Distribution.PackageDescription ( SourceRepo(..) ) +#if CABAL_PARSEC +import Distribution.PackageDescription.Parsec + ( readGenericPackageDescription ) +#else import Distribution.PackageDescription.Parse - ( readPackageDescription ) + ( readGenericPackageDescription ) +#endif import Distribution.Simple.Compiler ( Compiler, compilerInfo ) import Distribution.Simple.Program @@ -867,7 +872,7 @@ readSourcePackage verbosity (ProjectPackageLocalCabalFile cabalFile) = readSourcePackage verbosity (ProjectPackageLocalDirectory dir cabalFile) = do monitorFiles [monitorFileHashed cabalFile] root <- askRoot - pkgdesc <- liftIO $ readPackageDescription verbosity (root cabalFile) + pkgdesc <- liftIO $ readGenericPackageDescription verbosity (root cabalFile) return SourcePackage { packageInfoId = packageId pkgdesc, packageDescription = pkgdesc, diff --git a/cabal-install/Distribution/Client/Sandbox.hs b/cabal-install/Distribution/Client/Sandbox.hs index e8e37745ec5..50fbf6e5a3f 100644 --- a/cabal-install/Distribution/Client/Sandbox.hs +++ b/cabal-install/Distribution/Client/Sandbox.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} ----------------------------------------------------------------------------- -- | @@ -79,7 +80,11 @@ import Distribution.Client.Utils ( inDir, tryCanonicalizePath , tryFindAddSourcePackageDesc) import Distribution.PackageDescription.Configuration ( flattenPackageDescription ) -import Distribution.PackageDescription.Parse ( readPackageDescription ) +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec ( readGenericPackageDescription ) +#else +import Distribution.PackageDescription.Parse ( readGenericPackageDescription ) +#endif import Distribution.Simple.Compiler ( Compiler(..), PackageDB(..) ) import Distribution.Simple.Configure ( configCompilerAuxEx , getPackageDBContents @@ -436,7 +441,7 @@ sandboxAddSourceSnapshot verbosity buildTreeRefs sandboxDir pkgEnv = do pkgs <- forM buildTreeRefs $ \buildTreeRef -> inDir (Just buildTreeRef) $ return . flattenPackageDescription - =<< readPackageDescription verbosity + =<< readGenericPackageDescription verbosity =<< defaultPackageDesc verbosity -- Copy the package sources to "snapshots/$PKGNAME-$VERSION-tmp". If @@ -735,7 +740,7 @@ withSandboxPackageInfo verbosity configFlags globalFlags let err = "Error reading sandbox package information." -- Get the package descriptions for all add-source deps. depsCabalFiles <- mapM (flip tryFindAddSourcePackageDesc err) buildTreeRefs - depsPkgDescs <- mapM (readPackageDescription verbosity) depsCabalFiles + depsPkgDescs <- mapM (readGenericPackageDescription verbosity) depsCabalFiles let depsMap = M.fromList (zip buildTreeRefs depsPkgDescs) isInstalled pkgid = not . null . InstalledPackageIndex.lookupSourcePackageId installedPkgIndex $ pkgid diff --git a/cabal-install/Distribution/Client/SetupWrapper.hs b/cabal-install/Distribution/Client/SetupWrapper.hs index e79bdabf17c..69fe07d4932 100644 --- a/cabal-install/Distribution/Client/SetupWrapper.hs +++ b/cabal-install/Distribution/Client/SetupWrapper.hs @@ -39,8 +39,13 @@ import Distribution.PackageDescription ( GenericPackageDescription(packageDescription) , PackageDescription(..), specVersion , BuildType(..), knownBuildTypes, defaultRenaming ) +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec + ( readGenericPackageDescription ) +#else import Distribution.PackageDescription.Parse - ( readPackageDescription ) + ( readGenericPackageDescription ) +#endif import Distribution.Simple.Configure ( configCompilerEx ) import Distribution.Compiler @@ -302,7 +307,7 @@ getSetup verbosity options mpkg = do } where getPkg = tryFindPackageDesc (fromMaybe "." (useWorkingDir options)) - >>= readPackageDescription verbosity + >>= readGenericPackageDescription verbosity >>= return . packageDescription checkBuildType (UnknownBuildType name) = diff --git a/cabal-install/Distribution/Client/SrcDist.hs b/cabal-install/Distribution/Client/SrcDist.hs index 381608ea96b..e656c7fb1a5 100644 --- a/cabal-install/Distribution/Client/SrcDist.hs +++ b/cabal-install/Distribution/Client/SrcDist.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} {-# LANGUAGE FlexibleContexts #-} -- Implements the \"@.\/cabal sdist@\" command, which creates a source @@ -19,8 +20,13 @@ import Distribution.PackageDescription ( PackageDescription ) import Distribution.PackageDescription.Configuration ( flattenPackageDescription ) +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec + ( readGenericPackageDescription ) +#else import Distribution.PackageDescription.Parse - ( readPackageDescription ) + ( readGenericPackageDescription ) +#endif import Distribution.Simple.Utils ( createDirectoryIfMissingVerbose, defaultPackageDesc , warn, die, notice, withTempDirectory ) @@ -51,7 +57,7 @@ import Control.Exception (IOException, evaluate) sdist :: SDistFlags -> SDistExFlags -> IO () sdist flags exflags = do pkg <- liftM flattenPackageDescription - (readPackageDescription verbosity =<< defaultPackageDesc verbosity) + (readGenericPackageDescription verbosity =<< defaultPackageDesc verbosity) let withDir :: (FilePath -> IO a) -> IO a withDir = if not needMakeArchive then \f -> f tmpTargetDir else withTempDirectory verbosity tmpTargetDir "sdist." @@ -156,7 +162,7 @@ allPackageSourceFiles verbosity setupOpts0 packageDir = do pkg <- do let err = "Error reading source files of package." desc <- tryFindAddSourcePackageDesc packageDir err - flattenPackageDescription `fmap` readPackageDescription verbosity desc + flattenPackageDescription `fmap` readGenericPackageDescription verbosity desc globalTmp <- getTemporaryDirectory withTempDirectory verbosity globalTmp "cabal-list-sources." $ \tempDir -> do let file = tempDir "cabal-sdist-list-sources" diff --git a/cabal-install/Distribution/Client/Targets.hs b/cabal-install/Distribution/Client/Targets.hs index 758e28a3e4c..5ee776e0a19 100644 --- a/cabal-install/Distribution/Client/Targets.hs +++ b/cabal-install/Distribution/Client/Targets.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE BangPatterns #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveFunctor, DeriveFoldable, DeriveTraversable #-} @@ -81,22 +82,30 @@ import Distribution.Client.GlobalFlags import Distribution.PackageDescription ( GenericPackageDescription, parseFlagAssignment ) -import Distribution.PackageDescription.Parse - ( readPackageDescription, parsePackageDescription, ParseResult(..) ) import Distribution.Version ( nullVersion, thisVersion, anyVersion, isAnyVersion ) import Distribution.Text ( Text(..), display ) import Distribution.Verbosity (Verbosity) import Distribution.Simple.Utils - ( die, warn, fromUTF8, lowercase, ignoreBOM ) + ( die, warn, lowercase ) + +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec + ( readGenericPackageDescription, parseGenericPackageDescriptionMaybe ) +#else +import Distribution.PackageDescription.Parse + ( readGenericPackageDescription, parseGenericPackageDescription, ParseResult(..) ) +import Distribution.Simple.Utils + ( fromUTF8, ignoreBOM ) +import qualified Data.ByteString.Lazy.Char8 as BS.Char8 +#endif -- import Data.List ( find, nub ) import Data.Either ( partitionEithers ) import qualified Data.Map as Map import qualified Data.ByteString.Lazy as BS -import qualified Data.ByteString.Lazy.Char8 as BS.Char8 import qualified Distribution.Client.GZipUtils as GZipUtils import Control.Monad (mapM) import qualified Distribution.Compat.ReadP as Parse @@ -483,7 +492,7 @@ readPackageTarget verbosity = traverse modifyLocation LocalUnpackedPackage dir -> do pkg <- tryFindPackageDesc dir (localPackageError dir) >>= - readPackageDescription verbosity + readGenericPackageDescription verbosity return $ SourcePackage { packageInfoId = packageId pkg, packageDescription = pkg, @@ -549,11 +558,15 @@ readPackageTarget verbosity = traverse modifyLocation _ -> False parsePackageDescription' :: BS.ByteString -> Maybe GenericPackageDescription +#ifdef CABAL_PARSEC + parsePackageDescription' bs = + parseGenericPackageDescriptionMaybe (BS.toStrict bs) +#else parsePackageDescription' content = - case parsePackageDescription . ignoreBOM . fromUTF8 . BS.Char8.unpack $ content of + case parseGenericPackageDescription . ignoreBOM . fromUTF8 . BS.Char8.unpack $ content of ParseOk _ pkg -> Just pkg _ -> Nothing - +#endif -- ------------------------------------------------------------ -- * Checking package targets diff --git a/cabal-install/Main.hs b/cabal-install/Main.hs index 67080d003fa..012a451c448 100644 --- a/cabal-install/Main.hs +++ b/cabal-install/Main.hs @@ -134,8 +134,12 @@ import Distribution.Client.Utils (determineNumJobs import Distribution.Package (packageId) import Distribution.PackageDescription ( BuildType(..), Executable(..), buildable ) -import Distribution.PackageDescription.Parse - ( readPackageDescription ) +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec ( readGenericPackageDescription ) +#else +import Distribution.PackageDescription.Parse ( readGenericPackageDescription ) +#endif + import Distribution.PackageDescription.PrettyPrint ( writeGenericPackageDescription ) import qualified Distribution.Simple as Simple @@ -983,7 +987,7 @@ formatAction verbosityFlag extraArgs _globalFlags = do [] -> do cwd <- getCurrentDirectory tryFindPackageDesc cwd (p:_) -> return p - pkgDesc <- readPackageDescription verbosity path + pkgDesc <- readGenericPackageDescription verbosity path -- Uses 'writeFileAtomic' under the hood. writeGenericPackageDescription path pkgDesc diff --git a/cabal-install/cabal-install.cabal b/cabal-install/cabal-install.cabal index e3ddc05ceec..239e53e1103 100644 --- a/cabal-install/cabal-install.cabal +++ b/cabal-install/cabal-install.cabal @@ -194,6 +194,11 @@ Flag debug-tracetree description: Compile in support for tracetree (used to debug the solver) default: False +flag parsec + description: Use parsec parser + default: False + manual: True + executable cabal main-is: Main.hs ghc-options: -Wall -fwarn-tabs -rtsopts @@ -404,6 +409,9 @@ executable cabal cpp-options: -DDEBUG_TRACETREE build-depends: tracetree >= 0.1 && < 0.2 + if flag(parsec) + cpp-options: -DCABAL_PARSEC + default-language: Haskell2010 -- Small, fast running tests. diff --git a/cabal-testsuite/Test/Cabal/Prelude.hs b/cabal-testsuite/Test/Cabal/Prelude.hs index 1f5b55290f4..7bf38cefa97 100644 --- a/cabal-testsuite/Test/Cabal/Prelude.hs +++ b/cabal-testsuite/Test/Cabal/Prelude.hs @@ -139,7 +139,7 @@ setup' cmd args = do then runProgramM cabalProgram full_args else do pdfile <- liftIO $ tryFindPackageDesc (testCurrentDir env) - pdesc <- liftIO $ readPackageDescription (testVerbosity env) pdfile + pdesc <- liftIO $ readGenericPackageDescription (testVerbosity env) pdfile if buildType (packageDescription pdesc) == Just Simple then runM (testSetupPath env) full_args -- Run the Custom script! diff --git a/stack.yaml b/stack.yaml index b3d801a9640..7607d5e079c 100644 --- a/stack.yaml +++ b/stack.yaml @@ -53,6 +53,10 @@ extra-deps: - vector-0.12.0.0 - zlib-0.6.1.1 flags: + Cabal: + parsec: true + cabal-install: + parsec: true time-locale-compat: old-locale: false nix: From c97bd1a9c887b9e1b1a608812c206f5955ddea91 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Thu, 19 Jan 2017 20:15:29 +0200 Subject: [PATCH 3/4] Add a note about Cabal's parsec flag --- cabal-install/cabal-install.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cabal-install/cabal-install.cabal b/cabal-install/cabal-install.cabal index 239e53e1103..5e12350a0c9 100644 --- a/cabal-install/cabal-install.cabal +++ b/cabal-install/cabal-install.cabal @@ -195,7 +195,7 @@ Flag debug-tracetree default: False flag parsec - description: Use parsec parser + description: Use parsec parser. This requires 'Cabal' library built with its parsec flag enabled. default: False manual: True From b27aa253e40f835df49b3020fcde9c45a26288d0 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Thu, 26 Jan 2017 14:43:51 +0200 Subject: [PATCH 4/4] Update outdated --- cabal-install/Distribution/Client/Outdated.hs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cabal-install/Distribution/Client/Outdated.hs b/cabal-install/Distribution/Client/Outdated.hs index e85d5e2278a..a5a1bf84385 100644 --- a/cabal-install/Distribution/Client/Outdated.hs +++ b/cabal-install/Distribution/Client/Outdated.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} ----------------------------------------------------------------------------- -- | -- Module : Distribution.Client.Outdated @@ -28,8 +29,6 @@ import Distribution.Client.Sandbox.PackageEnvironment import Distribution.Package (PackageName, packageVersion) import Distribution.PackageDescription (buildDepends) import Distribution.PackageDescription.Configuration (finalizePD) -import Distribution.PackageDescription.Parse - (readPackageDescription) import Distribution.Simple.Compiler (Compiler, compilerInfo) import Distribution.Simple.Setup (fromFlagOrDefault) import Distribution.Simple.Utils @@ -43,6 +42,13 @@ import Distribution.Verbosity (Verbosity, silent) import Distribution.Version (Version, LowerBound(..), UpperBound(..) ,asVersionIntervals, majorBoundVersion) +#ifdef CABAL_PARSEC +import Distribution.PackageDescription.Parsec + (readGenericPackageDescription) +#else +import Distribution.PackageDescription.Parse + (readGenericPackageDescription) +#endif import qualified Data.Set as S import System.Directory (getCurrentDirectory) @@ -134,7 +140,7 @@ depsFromPkgDesc :: Verbosity -> Compiler -> Platform -> IO [Dependency] depsFromPkgDesc verbosity comp platform = do cwd <- getCurrentDirectory path <- tryFindPackageDesc cwd - gpd <- readPackageDescription verbosity path + gpd <- readGenericPackageDescription verbosity path let cinfo = compilerInfo comp epd = finalizePD [] (ComponentRequestedSpec True True) (const True) platform cinfo [] gpd