2014-01-08 13:16:10 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings, CPP #-}
|
2013-03-03 06:47:03 +00:00
|
|
|
|
2013-05-17 01:00:01 +00:00
|
|
|
module Language.Haskell.GhcMod.CabalApi (
|
2013-09-19 07:21:48 +00:00
|
|
|
getCompilerOptions
|
2013-05-30 01:50:19 +00:00
|
|
|
, parseCabalFile
|
|
|
|
, cabalAllBuildInfo
|
2013-09-16 00:56:08 +00:00
|
|
|
, cabalDependPackages
|
|
|
|
, cabalSourceDirs
|
2013-09-20 08:25:28 +00:00
|
|
|
, cabalAllTargets
|
2012-10-24 01:06:24 +00:00
|
|
|
) where
|
2012-10-24 00:11:09 +00:00
|
|
|
|
2013-05-29 08:47:52 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2013-03-04 04:55:03 +00:00
|
|
|
import Control.Exception (throwIO)
|
2013-09-21 14:08:45 +00:00
|
|
|
import Control.Monad (filterM)
|
|
|
|
import CoreMonad (liftIO)
|
2013-05-29 08:47:52 +00:00
|
|
|
import Data.Maybe (maybeToList)
|
2012-10-24 00:11:09 +00:00
|
|
|
import Data.Set (fromList, toList)
|
2013-09-21 12:01:43 +00:00
|
|
|
import Distribution.ModuleName (ModuleName,toFilePath)
|
2013-05-30 01:50:19 +00:00
|
|
|
import Distribution.Package (Dependency(Dependency)
|
|
|
|
, PackageName(PackageName)
|
|
|
|
, PackageIdentifier(pkgName))
|
2012-10-24 00:11:09 +00:00
|
|
|
import Distribution.PackageDescription
|
2013-05-29 08:47:52 +00:00
|
|
|
import Distribution.PackageDescription.Configuration (finalizePackageDescription)
|
2012-10-24 00:11:09 +00:00
|
|
|
import Distribution.PackageDescription.Parse (readPackageDescription)
|
2013-05-29 08:47:52 +00:00
|
|
|
import Distribution.Simple.Compiler (CompilerId(..), CompilerFlavor(..))
|
2013-03-04 04:55:03 +00:00
|
|
|
import Distribution.Simple.Program (ghcProgram)
|
|
|
|
import Distribution.Simple.Program.Types (programName, programFindVersion)
|
2013-05-29 08:47:52 +00:00
|
|
|
import Distribution.System (buildPlatform)
|
2013-03-01 06:25:43 +00:00
|
|
|
import Distribution.Text (display)
|
2013-03-01 05:40:34 +00:00
|
|
|
import Distribution.Verbosity (silent)
|
2013-09-20 08:21:10 +00:00
|
|
|
import Distribution.Version (Version)
|
2013-05-17 01:00:01 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
2013-09-16 03:04:34 +00:00
|
|
|
import System.Directory (doesFileExist)
|
2013-03-03 06:47:03 +00:00
|
|
|
import System.FilePath
|
2012-10-24 00:11:09 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-09-20 01:30:51 +00:00
|
|
|
-- | Getting necessary 'CompilerOptions' from three information sources.
|
2013-09-19 07:21:48 +00:00
|
|
|
getCompilerOptions :: [GHCOption] -> Cradle -> PackageDescription -> IO CompilerOptions
|
|
|
|
getCompilerOptions ghcopts cradle pkgDesc = do
|
2013-09-20 08:15:41 +00:00
|
|
|
gopts <- getGHCOptions ghcopts cradle cdir $ head buildInfos
|
2013-09-19 06:58:50 +00:00
|
|
|
return $ CompilerOptions gopts idirs depPkgs
|
2013-03-03 06:47:03 +00:00
|
|
|
where
|
2013-05-13 03:57:58 +00:00
|
|
|
wdir = cradleCurrentDir cradle
|
2013-03-04 01:39:39 +00:00
|
|
|
Just cdir = cradleCabalDir cradle
|
|
|
|
Just cfile = cradleCabalFile cradle
|
2013-09-19 07:21:48 +00:00
|
|
|
buildInfos = cabalAllBuildInfo pkgDesc
|
2013-09-16 00:56:08 +00:00
|
|
|
idirs = includeDirectories cdir wdir $ cabalSourceDirs buildInfos
|
|
|
|
depPkgs = removeThem problematicPackages $ removeMe cfile $ cabalDependPackages buildInfos
|
2013-03-03 06:47:03 +00:00
|
|
|
|
2013-09-16 02:15:34 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
-- Dependent packages
|
|
|
|
|
2013-09-16 02:00:39 +00:00
|
|
|
removeMe :: FilePath -> [Package] -> [Package]
|
2013-03-03 06:47:03 +00:00
|
|
|
removeMe cabalfile = filter (/= me)
|
|
|
|
where
|
|
|
|
me = dropExtension $ takeFileName cabalfile
|
|
|
|
|
2013-09-16 02:00:39 +00:00
|
|
|
removeThem :: [Package] -> [Package] -> [Package]
|
2013-09-11 05:09:18 +00:00
|
|
|
removeThem badpkgs = filter (`notElem` badpkgs)
|
|
|
|
|
2013-09-16 02:00:39 +00:00
|
|
|
problematicPackages :: [Package]
|
2013-09-11 05:09:18 +00:00
|
|
|
problematicPackages = [
|
|
|
|
"base-compat" -- providing "Prelude"
|
|
|
|
]
|
|
|
|
|
2013-09-16 02:15:34 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
-- Include directories for modules
|
|
|
|
|
2013-09-16 01:55:26 +00:00
|
|
|
cabalBuildDirs :: [FilePath]
|
2013-09-20 07:23:58 +00:00
|
|
|
cabalBuildDirs = ["dist/build", "dist/build/autogen"]
|
2013-09-16 01:55:26 +00:00
|
|
|
|
2013-09-16 02:00:39 +00:00
|
|
|
includeDirectories :: FilePath -> FilePath -> [FilePath] -> [FilePath]
|
2013-09-16 01:55:26 +00:00
|
|
|
includeDirectories cdir wdir dirs = uniqueAndSort (extdirs ++ [cdir,wdir])
|
|
|
|
where
|
2013-09-20 02:22:11 +00:00
|
|
|
extdirs = map expand $ dirs ++ cabalBuildDirs
|
|
|
|
expand "." = cdir
|
|
|
|
expand subdir = cdir </> subdir
|
2013-05-29 08:47:52 +00:00
|
|
|
|
2013-03-03 06:47:03 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-09-16 00:56:08 +00:00
|
|
|
-- | Parsing a cabal file and returns 'PackageDescription'.
|
|
|
|
-- 'IOException' is thrown if parsing fails.
|
2013-05-30 01:50:19 +00:00
|
|
|
parseCabalFile :: FilePath -> IO PackageDescription
|
|
|
|
parseCabalFile file = do
|
2013-05-29 08:47:52 +00:00
|
|
|
cid <- getGHCId
|
|
|
|
epgd <- readPackageDescription silent file
|
|
|
|
case toPkgDesc cid epgd of
|
2013-05-30 01:50:19 +00:00
|
|
|
Left deps -> throwIO $ userError $ show deps ++ " are not installed"
|
|
|
|
Right (pd,_) -> if nullPkg pd
|
|
|
|
then throwIO $ userError $ file ++ " is broken"
|
|
|
|
else return pd
|
2013-05-29 08:47:52 +00:00
|
|
|
where
|
|
|
|
toPkgDesc cid = finalizePackageDescription [] (const True) buildPlatform cid []
|
2013-05-30 01:50:19 +00:00
|
|
|
nullPkg pd = name == ""
|
|
|
|
where
|
|
|
|
PackageName name = pkgName (package pd)
|
2013-03-01 05:40:34 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
2012-10-24 01:48:13 +00:00
|
|
|
|
2013-09-20 08:15:41 +00:00
|
|
|
getGHCOptions :: [GHCOption] -> Cradle -> FilePath -> BuildInfo -> IO [GHCOption]
|
|
|
|
getGHCOptions ghcopts cradle cdir binfo = do
|
2013-09-16 03:04:34 +00:00
|
|
|
cabalCpp <- cabalCppOptions cdir
|
|
|
|
let cpps = map ("-optP" ++) $ cppOptions binfo ++ cabalCpp
|
2013-09-20 08:15:41 +00:00
|
|
|
return $ ghcopts ++ pkgDb ++ exts ++ [lang] ++ libs ++ libDirs ++ cpps
|
2013-03-05 01:21:55 +00:00
|
|
|
where
|
2013-09-20 08:15:41 +00:00
|
|
|
pkgDb = cradlePackageDbOpts cradle
|
2013-03-05 01:21:55 +00:00
|
|
|
lang = maybe "-XHaskell98" (("-X" ++) . display) $ defaultLanguage binfo
|
|
|
|
libDirs = map ("-L" ++) $ extraLibDirs binfo
|
2013-09-16 02:15:34 +00:00
|
|
|
exts = map (("-X" ++) . display) $ usedExtensions binfo
|
|
|
|
libs = map ("-l" ++) $ extraLibs binfo
|
|
|
|
|
2013-09-16 03:04:34 +00:00
|
|
|
cabalCppOptions :: FilePath -> IO [String]
|
|
|
|
cabalCppOptions dir = do
|
|
|
|
exist <- doesFileExist cabalMacro
|
|
|
|
if exist then
|
|
|
|
return ["-include", cabalMacro]
|
|
|
|
else
|
|
|
|
return []
|
|
|
|
where
|
|
|
|
cabalMacro = dir </> "dist/build/autogen/cabal_macros.h"
|
2013-03-05 01:21:55 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-09-16 00:56:08 +00:00
|
|
|
-- | Extracting all 'BuildInfo' for libraries, executables, tests and benchmarks.
|
2013-05-29 08:47:52 +00:00
|
|
|
cabalAllBuildInfo :: PackageDescription -> [BuildInfo]
|
|
|
|
cabalAllBuildInfo pd = libBI ++ execBI ++ testBI ++ benchBI
|
2012-10-24 01:06:24 +00:00
|
|
|
where
|
2013-05-29 08:47:52 +00:00
|
|
|
libBI = map libBuildInfo $ maybeToList $ library pd
|
|
|
|
execBI = map buildInfo $ executables pd
|
|
|
|
testBI = map testBuildInfo $ testSuites pd
|
2014-01-08 13:16:10 +00:00
|
|
|
#if __GLASGOW_HASKELL__ >= 704
|
2013-05-29 08:47:52 +00:00
|
|
|
benchBI = map benchmarkBuildInfo $ benchmarks pd
|
2014-01-08 13:16:10 +00:00
|
|
|
#else
|
|
|
|
benchBI = []
|
|
|
|
#endif
|
2012-10-24 01:06:24 +00:00
|
|
|
|
2013-03-01 05:40:34 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-09-16 00:56:08 +00:00
|
|
|
-- | Extracting package names of dependency.
|
|
|
|
cabalDependPackages :: [BuildInfo] -> [Package]
|
|
|
|
cabalDependPackages bis = uniqueAndSort $ pkgs
|
2013-03-01 05:40:34 +00:00
|
|
|
where
|
2013-05-29 08:47:52 +00:00
|
|
|
pkgs = map getDependencyPackageName $ concatMap targetBuildDepends bis
|
|
|
|
getDependencyPackageName (Dependency (PackageName nm) _) = nm
|
2013-03-05 01:21:55 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-09-16 00:56:08 +00:00
|
|
|
-- | Extracting include directories for modules.
|
|
|
|
cabalSourceDirs :: [BuildInfo] -> [IncludeDir]
|
|
|
|
cabalSourceDirs bis = uniqueAndSort $ concatMap hsSourceDirs bis
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-03-01 06:25:43 +00:00
|
|
|
uniqueAndSort :: [String] -> [String]
|
|
|
|
uniqueAndSort = toList . fromList
|
2013-03-04 04:55:03 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-05-29 08:47:52 +00:00
|
|
|
getGHCId :: IO CompilerId
|
|
|
|
getGHCId = CompilerId GHC <$> getGHC
|
|
|
|
|
|
|
|
getGHC :: IO Version
|
|
|
|
getGHC = do
|
|
|
|
mv <- programFindVersion ghcProgram silent (programName ghcProgram)
|
|
|
|
case mv of
|
|
|
|
Nothing -> throwIO $ userError "ghc not found"
|
|
|
|
Just v -> return $ v
|
2013-09-20 08:25:28 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Extracting all 'Module' 'FilePath's for libraries, executables,
|
|
|
|
-- tests and benchmarks.
|
2013-09-21 14:08:45 +00:00
|
|
|
cabalAllTargets :: PackageDescription -> IO ([String],[String],[String],[String])
|
|
|
|
cabalAllTargets pd = do
|
2013-09-21 15:26:55 +00:00
|
|
|
exeTargets <- mapM getExecutableTarget $ executables pd
|
|
|
|
testTargets <- mapM getTestTarget $ testSuites pd
|
|
|
|
return (libTargets,concat exeTargets,concat testTargets,benchTargets)
|
2013-09-20 08:25:28 +00:00
|
|
|
where
|
|
|
|
lib = case library pd of
|
|
|
|
Nothing -> []
|
|
|
|
Just l -> libModules l
|
|
|
|
|
2013-09-21 14:08:45 +00:00
|
|
|
libTargets = map toModuleString $ lib
|
2014-01-08 13:16:10 +00:00
|
|
|
#if __GLASGOW_HASKELL__ >= 704
|
2013-09-21 14:08:45 +00:00
|
|
|
benchTargets = map toModuleString $ concatMap benchmarkModules $ benchmarks pd
|
2014-01-08 13:16:10 +00:00
|
|
|
#else
|
|
|
|
benchTargets = []
|
|
|
|
#endif
|
2013-09-21 12:01:43 +00:00
|
|
|
toModuleString :: ModuleName -> String
|
|
|
|
toModuleString mn = fromFilePath $ toFilePath mn
|
|
|
|
|
|
|
|
fromFilePath :: FilePath -> String
|
|
|
|
fromFilePath fp = map (\c -> if c=='/' then '.' else c) fp
|
|
|
|
|
2013-09-21 15:26:55 +00:00
|
|
|
getTestTarget :: TestSuite -> IO [String]
|
|
|
|
getTestTarget ts =
|
|
|
|
case testInterface ts of
|
|
|
|
(TestSuiteExeV10 _ filePath) -> do
|
|
|
|
let maybeTests = [p </> e | p <- hsSourceDirs $ testBuildInfo ts, e <- [filePath]]
|
|
|
|
liftIO $ filterM doesFileExist maybeTests
|
|
|
|
(TestSuiteLibV09 _ moduleName) -> return [toModuleString moduleName]
|
|
|
|
(TestSuiteUnsupported _) -> return []
|
2013-09-21 14:08:45 +00:00
|
|
|
|
|
|
|
getExecutableTarget :: Executable -> IO [String]
|
|
|
|
getExecutableTarget exe = do
|
|
|
|
let maybeExes = [p </> e | p <- hsSourceDirs $ buildInfo exe, e <- [modulePath exe]]
|
|
|
|
liftIO $ filterM doesFileExist maybeExes
|
|
|
|
|