cabal-ghc-dynflags (empty) → 0.1.0.0
raw patch · 4 files changed
+157/−0 lines, 4 filesdep +Cabaldep +basedep +ghcsetup-changed
Dependencies added: Cabal, base, ghc, transformers
Files
- GHC/Cabal.hs +100/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- cabal-ghc-dynflags.cabal +25/−0
+ GHC/Cabal.hs view
@@ -0,0 +1,100 @@+-- | One of the challenges of using the GHC API for external tooling+-- is handling integration with Cabal. This library provides a simple+-- interface for configuring GHC's 'DynFlags' as Cabal would have,+-- allowing seamless tooling use on Cabal projects.+--+-- A typical usage might look like,+--+-- > import GHC+-- > import qualified GHC.Paths+-- > import qualified Distribution.Verbosity as Verbosity+-- > import GHC.Cabal+-- >+-- > main = runGhc (Just GHC.Paths.libdir) $ do+-- > dflags <- GHC.getSessionDynFlags+-- > -- Use default DynFlags if we aren't in a Cabal project+-- > dflags' <- fromMaybe dflags <$> liftIO (initCabalDynFlags Verbosity.normal dflags)+-- > GHC.setSessionDynFlags dflags'+-- >+-- > -- Standard GHC API usage goes here+--++module GHC.Cabal (+ -- * Initializing GHC DynFlags for Cabal packages+ initCabalDynFlags+ ) where++import Control.Monad (guard, msum, mzero)+import Control.Monad.Trans.Maybe (MaybeT, runMaybeT)+import Control.Monad.Trans.Class++import Control.Applicative ((<|>))+import Distribution.Verbosity+import Distribution.Simple.Utils (defaultPackageDesc, warn, debug, findPackageDesc)+import Distribution.Simple.Program (defaultProgramConfiguration)+import qualified Distribution.Simple.Setup as Setup+import Distribution.PackageDescription as PD+import Distribution.PackageDescription.Parse as PD+import Distribution.PackageDescription.Configuration (finalizePackageDescription)+import Distribution.Simple.LocalBuildInfo as LBI+import qualified Distribution.Simple.Configure as Configure+import qualified Distribution.Simple.Compiler as Compiler+import qualified Distribution.Simple.GHC as CGHC+import qualified Distribution.Simple.Program.GHC as CGHC+import DynFlags (DynFlags, parseDynamicFlagsCmdLine)+import qualified SrcLoc++data CabalDetails = CabalDetails { cdLocalBuildInfo :: LocalBuildInfo+ }++-- | Modify a set of 'DynFlags' to match what Cabal would produce.+initCabalDynFlags :: Verbosity -> DynFlags -> IO (Maybe DynFlags)+initCabalDynFlags verbosity dflags0 = runMaybeT $ do+ let warnNoCabal _err = lift (warn verbosity "Couldn't find cabal file") >> mzero+ pdfile <- either warnNoCabal pure =<< lift (findPackageDesc ".")+ gpkg_descr <- lift $ PD.readPackageDescription verbosity pdfile+ lbi <- lift $ Configure.getPersistBuildConfig Setup.defaultDistPref++ let programsConfig = defaultProgramConfiguration+ (comp, compPlatform, programsConfig') <- lift $+ Configure.configCompilerEx (Just Compiler.GHC) Nothing Nothing+ (withPrograms lbi) (lessVerbose verbosity)++ -- TODO: is any of this correct?+ let pkg_descr = case finalizePackageDescription+ [] (const True) compPlatform (Compiler.compilerInfo comp)+ [] gpkg_descr of+ Right (pd,_) -> pd+ -- This shouldn't happen since we claim dependencies can always be satisfied+ Left err -> error "missing dependencies"+ let comp :: Maybe (PD.BuildInfo, LBI.ComponentLocalBuildInfo)+ comp = msum [libraryComp, executableComp]++ libraryComp = do+ lib <- PD.library pkg_descr+ let bi = PD.libBuildInfo lib+ guard $ PD.buildable bi+ return (bi, getComponentLocalBuildInfo lbi CLibName)++ executableComp = msum $ flip map (PD.executables pkg_descr) $ \exec->do+ let bi = PD.buildInfo exec+ guard $ PD.buildable bi+ return (bi, getComponentLocalBuildInfo lbi (LBI.CExeName $ PD.exeName exec))++ case comp of+ Just (bi, clbi) -> lift $ initCabalDynFlags' verbosity lbi bi clbi dflags0+ Nothing -> do+ lift $ warn verbosity $ "Found no buildable components in "++pdfile+ mzero++initCabalDynFlags' :: Verbosity -> LocalBuildInfo+ -> BuildInfo -> ComponentLocalBuildInfo+ -> DynFlags -> IO DynFlags+initCabalDynFlags' verbosity lbi bi clbi dflags0 = do+ debug verbosity $ "initCabalDynFlags': Flags = "++show rendered+ (dflags, leftovers, warnings) <- DynFlags.parseDynamicFlagsCmdLine dflags0 (map SrcLoc.noLoc rendered)+ putStrLn $ unlines $ map SrcLoc.unLoc warnings+ return dflags+ where+ baseOpts = CGHC.componentGhcOptions verbosity lbi bi clbi (buildDir lbi)+ rendered = CGHC.renderGhcOptions (LBI.compiler lbi) baseOpts
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Ben Gamari++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Ben Gamari nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ cabal-ghc-dynflags.cabal view
@@ -0,0 +1,25 @@+name: cabal-ghc-dynflags+version: 0.1.0.0+synopsis: Conveniently configure GHC's dynamic flags for use with Cabal projects+description: See Haddocks in 'GHC.Cabal' for details.+homepage: http://github.com/bgamari/cabal-ghc-dynflags+license: BSD3+license-file: LICENSE+author: Ben Gamari+maintainer: ben@smart-cactus.org+copyright: (c) 2015 Ben Gamari+category: Development+build-type: Simple+cabal-version: >=1.10++source-repository head+ type: git+ location: git://github.com/bgamari/cabal-ghc-dynflags++library+ exposed-modules: GHC.Cabal+ build-depends: base >=4.8 && <4.9,+ transformers >=0.4 && <0.5,+ Cabal >=1.22 && <1.23,+ ghc >= 7.10 && <7.12+ default-language: Haskell2010