2015-03-03 20:12:43 +00:00
|
|
|
-- ghc-mod: Making Haskell development *more* fun
|
|
|
|
-- Copyright (C) 2015 Daniel Gröber <dxld ÄT darkboxed DOT org>
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-03-28 01:33:42 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2015-08-07 04:47:34 +00:00
|
|
|
module Language.Haskell.GhcMod.CabalHelper
|
|
|
|
#ifndef SPEC
|
|
|
|
( getComponents
|
2015-06-05 20:42:46 +00:00
|
|
|
, getGhcMergedPkgOptions
|
2015-08-14 08:28:32 +00:00
|
|
|
, getCabalPackageDbStack
|
|
|
|
, getCustomPkgDbStack
|
2015-08-14 01:48:29 +00:00
|
|
|
, prepareCabalHelper
|
2015-08-07 04:47:34 +00:00
|
|
|
)
|
|
|
|
#endif
|
|
|
|
where
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
import Control.Applicative
|
|
|
|
import Control.Monad
|
2015-08-11 04:35:14 +00:00
|
|
|
import Control.Category ((.))
|
2015-08-07 04:47:34 +00:00
|
|
|
import Data.Maybe
|
2015-03-03 20:12:43 +00:00
|
|
|
import Data.Monoid
|
2015-06-07 01:36:50 +00:00
|
|
|
import Data.Serialize (Serialize)
|
2015-08-07 04:47:34 +00:00
|
|
|
import Data.Traversable
|
2015-03-15 19:48:55 +00:00
|
|
|
import Distribution.Helper
|
|
|
|
import qualified Language.Haskell.GhcMod.Types as T
|
|
|
|
import Language.Haskell.GhcMod.Types hiding (ghcProgram, ghcPkgProgram,
|
|
|
|
cabalProgram)
|
2015-03-03 20:12:43 +00:00
|
|
|
import Language.Haskell.GhcMod.Monad.Types
|
|
|
|
import Language.Haskell.GhcMod.Utils
|
|
|
|
import Language.Haskell.GhcMod.PathsAndFiles
|
2015-08-03 03:20:14 +00:00
|
|
|
import Language.Haskell.GhcMod.Logging
|
2015-08-13 07:01:58 +00:00
|
|
|
import Language.Haskell.GhcMod.Output
|
2015-03-03 20:12:43 +00:00
|
|
|
import System.FilePath
|
2015-08-11 04:35:14 +00:00
|
|
|
import Prelude hiding ((.))
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2015-03-28 01:33:42 +00:00
|
|
|
import Paths_ghc_mod as GhcMod
|
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
-- | Only package related GHC options, sufficient for things that don't need to
|
|
|
|
-- access home modules
|
2015-08-11 04:35:14 +00:00
|
|
|
getGhcMergedPkgOptions :: (Applicative m, IOish m, GmEnv m, GmState m, GmLog m)
|
|
|
|
=> m [GHCOption]
|
2015-06-05 20:42:46 +00:00
|
|
|
getGhcMergedPkgOptions = chCached Cached {
|
2015-08-11 04:35:14 +00:00
|
|
|
cacheLens = Just (lGmcMergedPkgOptions . lGmCaches),
|
2015-06-05 20:42:46 +00:00
|
|
|
cacheFile = mergedPkgOptsCacheFile,
|
2015-08-10 07:07:41 +00:00
|
|
|
cachedAction = \ _tcf (progs, rootdir, distdir, _) _ma -> do
|
2015-08-13 07:01:58 +00:00
|
|
|
readProc <- gmReadProcess
|
|
|
|
opts <- withCabal $ runQuery'' readProc progs rootdir distdir $
|
|
|
|
ghcMergedPkgOptions
|
2015-06-05 20:42:46 +00:00
|
|
|
return ([setupConfigPath], opts)
|
|
|
|
}
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2015-08-14 08:28:32 +00:00
|
|
|
getCabalPackageDbStack :: (IOish m, GmEnv m, GmState m, GmLog m) => m [GhcPkgDb]
|
|
|
|
getCabalPackageDbStack = chCached Cached {
|
2015-08-11 04:35:14 +00:00
|
|
|
cacheLens = Just (lGmcPackageDbStack . lGmCaches),
|
2015-08-07 04:47:34 +00:00
|
|
|
cacheFile = pkgDbStackCacheFile,
|
2015-08-10 07:07:41 +00:00
|
|
|
cachedAction = \ _tcf (progs, rootdir, distdir, _) _ma -> do
|
2015-08-13 07:01:58 +00:00
|
|
|
readProc <- gmReadProcess
|
|
|
|
dbs <- withCabal $ map chPkgToGhcPkg <$> runQuery'' readProc progs rootdir distdir packageDbStack
|
2015-08-11 04:35:07 +00:00
|
|
|
return ([setupConfigPath, sandboxConfigFile], dbs)
|
2015-08-07 04:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chPkgToGhcPkg :: ChPkgDb -> GhcPkgDb
|
|
|
|
chPkgToGhcPkg ChPkgGlobal = GlobalDb
|
|
|
|
chPkgToGhcPkg ChPkgUser = UserDb
|
|
|
|
chPkgToGhcPkg (ChPkgSpecific f) = PackageDb f
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
-- | Primary interface to cabal-helper and intended single entrypoint to
|
|
|
|
-- constructing 'GmComponent's
|
|
|
|
--
|
|
|
|
-- The Component\'s 'gmcHomeModuleGraph' will be empty and has to be resolved by
|
|
|
|
-- 'resolveGmComponents'.
|
2015-08-11 04:35:14 +00:00
|
|
|
getComponents :: (Applicative m, IOish m, GmEnv m, GmState m, GmLog m)
|
2015-08-03 01:09:56 +00:00
|
|
|
=> m [GmComponent 'GMCRaw ChEntrypoint]
|
2015-08-11 04:35:14 +00:00
|
|
|
getComponents = chCached Cached {
|
|
|
|
cacheLens = Just (lGmcComponents . lGmCaches),
|
2015-03-28 01:30:51 +00:00
|
|
|
cacheFile = cabalHelperCacheFile,
|
2015-08-13 07:01:58 +00:00
|
|
|
cachedAction = \ _tcf (progs, rootdir, distdir, _vers) _ma -> do
|
|
|
|
readProc <- gmReadProcess
|
|
|
|
runQuery'' readProc progs rootdir distdir $ do
|
2015-05-17 20:22:56 +00:00
|
|
|
q <- join7
|
|
|
|
<$> ghcOptions
|
|
|
|
<*> ghcPkgOptions
|
|
|
|
<*> ghcSrcOptions
|
|
|
|
<*> ghcLangOptions
|
|
|
|
<*> entrypoints
|
|
|
|
<*> entrypoints
|
|
|
|
<*> sourceDirs
|
2015-05-05 12:44:42 +00:00
|
|
|
let cs = flip map q $ curry8 (GmComponent mempty)
|
2015-03-28 01:30:51 +00:00
|
|
|
return ([setupConfigPath], cs)
|
|
|
|
}
|
2015-03-15 19:48:55 +00:00
|
|
|
where
|
2015-05-05 12:44:42 +00:00
|
|
|
curry8 fn (a, (b, (c, (d, (e, (f, (g, h))))))) = fn a b c d e f g h
|
|
|
|
|
|
|
|
join7 a b c d e f = join' a . join' b . join' c . join' d . join' e . join' f
|
2015-03-15 19:48:55 +00:00
|
|
|
join' :: Eq a => [(a,b)] -> [(a,c)] -> [(a,(b,c))]
|
|
|
|
join' lb lc = [ (a, (b, c))
|
2015-05-17 20:22:56 +00:00
|
|
|
| (a, b) <- lb
|
2015-05-05 12:44:42 +00:00
|
|
|
, (a', c) <- lc
|
|
|
|
, a == a'
|
|
|
|
]
|
2015-03-28 01:33:42 +00:00
|
|
|
|
2015-08-14 01:48:29 +00:00
|
|
|
prepareCabalHelper :: (IOish m, GmEnv m, GmLog m) => m ()
|
|
|
|
prepareCabalHelper = do
|
|
|
|
crdl <- cradle
|
|
|
|
let projdir = cradleRootDir crdl
|
|
|
|
distdir = projdir </> "dist"
|
|
|
|
readProc <- gmReadProcess
|
2015-08-14 04:32:20 +00:00
|
|
|
when (cradleProjectType crdl == CabalProject) $
|
|
|
|
withCabal $ liftIO $ prepare readProc projdir distdir
|
2015-08-14 01:48:29 +00:00
|
|
|
|
2015-08-14 08:28:32 +00:00
|
|
|
parseCustomPackageDb :: String -> [GhcPkgDb]
|
|
|
|
parseCustomPackageDb src = map parsePkgDb $ filter (not . null) $ lines src
|
|
|
|
where
|
|
|
|
parsePkgDb "global" = GlobalDb
|
|
|
|
parsePkgDb "user" = UserDb
|
|
|
|
parsePkgDb s = PackageDb s
|
|
|
|
|
|
|
|
getCustomPkgDbStack :: (IOish m, GmEnv m) => m (Maybe [GhcPkgDb])
|
|
|
|
getCustomPkgDbStack = do
|
|
|
|
mCusPkgDbFile <- liftIO . (traverse readFile <=< findCustomPackageDbFile) . cradleRootDir =<< cradle
|
|
|
|
return $ parseCustomPackageDb <$> mCusPkgDbFile
|
|
|
|
|
2015-08-03 03:20:14 +00:00
|
|
|
withCabal :: (IOish m, GmEnv m, GmLog m) => m a -> m a
|
2015-03-03 20:12:43 +00:00
|
|
|
withCabal action = do
|
|
|
|
crdl <- cradle
|
2015-03-07 18:23:55 +00:00
|
|
|
opts <- options
|
2015-08-13 07:01:58 +00:00
|
|
|
readProc <- gmReadProcess
|
2015-08-12 07:04:09 +00:00
|
|
|
|
|
|
|
let projdir = cradleRootDir crdl
|
|
|
|
distdir = projdir </> "dist"
|
|
|
|
|
2015-08-07 04:47:34 +00:00
|
|
|
mCabalFile <- liftIO $ timeFile `traverse` cradleCabalFile crdl
|
|
|
|
mCabalConfig <- liftIO $ timeMaybe (setupConfigFile crdl)
|
|
|
|
|
|
|
|
mCusPkgDbStack <- getCustomPkgDbStack
|
|
|
|
|
|
|
|
pkgDbStackOutOfSync <-
|
|
|
|
case mCusPkgDbStack of
|
|
|
|
Just cusPkgDbStack -> do
|
2015-08-13 07:01:58 +00:00
|
|
|
pkgDb <- runQuery'' readProc (helperProgs opts) projdir distdir $
|
2015-08-07 04:47:34 +00:00
|
|
|
map chPkgToGhcPkg <$> packageDbStack
|
|
|
|
return $ pkgDb /= cusPkgDbStack
|
|
|
|
|
|
|
|
Nothing -> return False
|
|
|
|
|
|
|
|
cusPkgStack <- maybe [] ((PackageDb "clear"):) <$> getCustomPkgDbStack
|
|
|
|
|
2015-08-11 04:35:14 +00:00
|
|
|
--TODO: also invalidate when sandboxConfig file changed
|
|
|
|
|
2015-08-07 04:47:34 +00:00
|
|
|
when (isSetupConfigOutOfDate mCabalFile mCabalConfig) $
|
|
|
|
gmLog GmDebug "" $ strDoc $ "setup configuration is out of date, reconfiguring Cabal project."
|
|
|
|
when pkgDbStackOutOfSync $
|
|
|
|
gmLog GmDebug "" $ strDoc $ "package-db stack out of sync with ghc-mod.package-db-stack, reconfiguring Cabal project."
|
|
|
|
|
|
|
|
when (isSetupConfigOutOfDate mCabalFile mCabalConfig || pkgDbStackOutOfSync) $
|
2015-03-07 18:23:55 +00:00
|
|
|
withDirectory_ (cradleRootDir crdl) $ do
|
2015-08-07 04:47:34 +00:00
|
|
|
let progOpts =
|
2015-03-15 19:48:55 +00:00
|
|
|
[ "--with-ghc=" ++ T.ghcProgram opts ]
|
2015-03-07 18:23:55 +00:00
|
|
|
-- Only pass ghc-pkg if it was actually set otherwise we
|
|
|
|
-- might break cabal's guessing logic
|
2015-03-15 19:48:55 +00:00
|
|
|
++ if T.ghcPkgProgram opts /= T.ghcPkgProgram defaultOptions
|
|
|
|
then [ "--with-ghc-pkg=" ++ T.ghcPkgProgram opts ]
|
2015-03-07 18:23:55 +00:00
|
|
|
else []
|
2015-08-07 04:47:34 +00:00
|
|
|
++ map pkgDbArg cusPkgStack
|
2015-08-13 07:01:58 +00:00
|
|
|
liftIO $ void $ readProc (T.cabalProgram opts) ("configure":progOpts) ""
|
2015-08-03 03:20:14 +00:00
|
|
|
gmLog GmDebug "" $ strDoc $ "writing Cabal autogen files"
|
2015-08-13 07:01:58 +00:00
|
|
|
liftIO $ writeAutogenFiles readProc projdir distdir
|
2015-03-03 20:12:43 +00:00
|
|
|
action
|
2015-03-03 11:18:54 +00:00
|
|
|
|
|
|
|
pkgDbArg :: GhcPkgDb -> String
|
|
|
|
pkgDbArg GlobalDb = "--package-db=global"
|
|
|
|
pkgDbArg UserDb = "--package-db=user"
|
|
|
|
pkgDbArg (PackageDb p) = "--package-db=" ++ p
|
2015-08-07 04:47:34 +00:00
|
|
|
|
|
|
|
-- * Neither file exists -> should return False:
|
|
|
|
-- @Nothing < Nothing = False@
|
|
|
|
-- (since we don't need to @cabal configure@ when no cabal file exists.)
|
|
|
|
--
|
|
|
|
-- * Cabal file doesn't exist (unlikely case) -> should return False
|
|
|
|
-- @Just cc < Nothing = False@
|
|
|
|
-- TODO: should we delete dist/setup-config?
|
|
|
|
--
|
|
|
|
-- * dist/setup-config doesn't exist yet -> should return True:
|
|
|
|
-- @Nothing < Just cf = True@
|
|
|
|
--
|
|
|
|
-- * Both files exist
|
|
|
|
-- @Just cc < Just cf = cc < cf = cc `olderThan` cf@
|
|
|
|
isSetupConfigOutOfDate :: Maybe TimedFile -> Maybe TimedFile -> Bool
|
|
|
|
isSetupConfigOutOfDate worldCabalFile worldCabalConfig = do
|
|
|
|
worldCabalConfig < worldCabalFile
|
|
|
|
|
|
|
|
|
|
|
|
helperProgs :: Options -> Programs
|
|
|
|
helperProgs opts = Programs {
|
|
|
|
cabalProgram = T.cabalProgram opts,
|
|
|
|
ghcProgram = T.ghcProgram opts,
|
|
|
|
ghcPkgProgram = T.ghcPkgProgram opts
|
|
|
|
}
|
|
|
|
|
2015-08-11 04:35:14 +00:00
|
|
|
chCached :: (Applicative m, IOish m, GmEnv m, GmState m, GmLog m, Serialize a)
|
|
|
|
=> Cached m GhcModState ChCacheData a -> m a
|
2015-08-07 04:47:34 +00:00
|
|
|
chCached c = do
|
|
|
|
root <- cradleRootDir <$> cradle
|
|
|
|
d <- cacheInputData root
|
|
|
|
withCabal $ cached root c d
|
|
|
|
where
|
|
|
|
cacheInputData root = do
|
|
|
|
opt <- options
|
|
|
|
return $ ( helperProgs opt
|
2015-08-10 07:07:41 +00:00
|
|
|
, root
|
2015-08-07 04:47:34 +00:00
|
|
|
, root </> "dist"
|
|
|
|
, (gmVer, chVer)
|
|
|
|
)
|
|
|
|
|
|
|
|
gmVer = GhcMod.version
|
|
|
|
chVer = VERSION_cabal_helper
|