+{-# OPTIONS -fno-cse #-}
+-- -fno-cse is needed for GLOBAL_VAR's to behave properly
-----------------------------------------------------------------------------
--
#include "HsVersions.h"
import qualified GHC
-import GHC ( Session, ModSummary(..) )
+-- import GHC ( ModSummary(..), GhcMonad )
+import HsSyn ( ImportDecl(..) )
import DynFlags
import Util
-import HscTypes ( HscEnv, IsBootInterface, msObjFilePath, msHsFilePath )
+import HscTypes
import SysTools ( newTempName )
import qualified SysTools
import Module
import Digraph ( SCC(..) )
-import Finder ( findImportedModule, FindResult(..) )
+import Finder
import Outputable
import Panic
import SrcLoc
import Data.List
-import CmdLineParser
import FastString
-import ErrUtils ( debugTraceMsg, putMsg )
+import Exception
+import ErrUtils
+-- import MonadUtils ( liftIO )
-import Data.IORef ( IORef, readIORef, writeIORef )
-import Control.Exception
-import System.Exit ( ExitCode(..), exitWith )
import System.Directory
import System.FilePath
import System.IO
-import SYSTEM_IO_ERROR ( isEOFError )
+import System.IO.Error ( isEOFError )
import Control.Monad ( when )
import Data.Maybe ( isJust )
--
-----------------------------------------------------------------
-doMkDependHS :: Session -> [FilePath] -> IO ()
-doMkDependHS session srcs
- = do { -- Initialisation
- dflags <- GHC.getSessionDynFlags session
- ; files <- beginMkDependHS dflags
-
- -- Do the downsweep to find all the modules
- ; targets <- mapM (\s -> GHC.guessTarget s Nothing) srcs
- ; GHC.setTargets session targets
- ; excl_mods <- readIORef v_Dep_exclude_mods
- ; r <- GHC.depanal session excl_mods True {- Allow dup roots -}
- ; case r of
- Nothing -> exitWith (ExitFailure 1)
- Just mod_summaries -> do {
+doMkDependHS :: GhcMonad m => [FilePath] -> m ()
+doMkDependHS srcs = do
+ -- Initialisation
+ dflags <- GHC.getSessionDynFlags
+ files <- liftIO $ beginMkDependHS dflags
+
+ -- Do the downsweep to find all the modules
+ targets <- mapM (\s -> GHC.guessTarget s Nothing) srcs
+ GHC.setTargets targets
+ let excl_mods = depExcludeMods dflags
+ mod_summaries <- GHC.depanal excl_mods True {- Allow dup roots -}
+
+ -- Sort into dependency order
+ -- There should be no cycles
+ let sorted = GHC.topSortModuleGraph False mod_summaries Nothing
- -- Sort into dependency order
- -- There should be no cycles
- let sorted = GHC.topSortModuleGraph False mod_summaries Nothing
+ -- Print out the dependencies if wanted
+ liftIO $ debugTraceMsg dflags 2 (text "Module dependencies" $$ ppr sorted)
- -- Print out the dependencies if wanted
- ; debugTraceMsg dflags 2 (text "Module dependencies" $$ ppr sorted)
+ -- Prcess them one by one, dumping results into makefile
+ -- and complaining about cycles
+ hsc_env <- getSession
+ root <- liftIO getCurrentDirectory
+ mapM_ (liftIO . processDeps dflags hsc_env excl_mods root (mkd_tmp_hdl files)) sorted
- -- Prcess them one by one, dumping results into makefile
- -- and complaining about cycles
- ; mapM (processDeps session excl_mods (mkd_tmp_hdl files)) sorted
+ -- If -ddump-mod-cycles, show cycles in the module graph
+ liftIO $ dumpModCycles dflags mod_summaries
- -- If -ddump-mod-cycles, show cycles in the module graph
- ; dumpModCycles dflags mod_summaries
+ -- Tidy up
+ liftIO $ endMkDependHS dflags files
- -- Tidy up
- ; endMkDependHS dflags files }}
+ -- Unconditional exiting is a bad idea. If an error occurs we'll get an
+ --exception; if that is not caught it's fine, but at least we have a
+ --chance to find out exactly what went wrong. Uncomment the following
+ --line if you disagree.
+
+ --`GHC.ghcCatch` \_ -> io $ exitWith (ExitFailure 1)
-----------------------------------------------------------------
--
beginMkDependHS :: DynFlags -> IO MkDepFiles
beginMkDependHS dflags = do
- -- slurp in the mkdependHS-style options
- let flags = getOpts dflags opt_dep
- _ <- processArgs dep_opts flags
-
-- open a new temp file in which to stuff the dependency info
-- as we go along.
tmp_file <- newTempName dflags "dep"
tmp_hdl <- openFile tmp_file WriteMode
-- open the makefile
- makefile <- readIORef v_Dep_makefile
+ let makefile = depMakefile dflags
exists <- doesFileExist makefile
mb_make_hdl <-
if not exists
then return ()
else chuck
- catchJust ioErrors slurp
+ catchIO slurp
(\e -> if isEOFError e then return () else ioError e)
- catchJust ioErrors chuck
+ catchIO chuck
(\e -> if isEOFError e then return () else ioError e)
return (Just makefile_hdl)
--
-----------------------------------------------------------------
-processDeps :: Session
+processDeps :: DynFlags
+ -> HscEnv
-> [ModuleName]
+ -> FilePath
-> Handle -- Write dependencies to here
-> SCC ModSummary
-> IO ()
--
-- For {-# SOURCE #-} imports the "hi" will be "hi-boot".
-processDeps _ _ _ (CyclicSCC nodes)
+processDeps _ _ _ _ _ (CyclicSCC nodes)
= -- There shouldn't be any cycles; report them
- throwDyn (ProgramError (showSDoc $ GHC.cyclicModuleErr nodes))
+ ghcError (ProgramError (showSDoc $ GHC.cyclicModuleErr nodes))
-processDeps session excl_mods hdl (AcyclicSCC node)
- = do { extra_suffixes <- readIORef v_Dep_suffixes
- ; hsc_env <- GHC.sessionHscEnv session
- ; include_pkg_deps <- readIORef v_Dep_include_pkg_deps
- ; let src_file = msHsFilePath node
+processDeps dflags hsc_env excl_mods root hdl (AcyclicSCC node)
+ = do { let extra_suffixes = depSuffixes dflags
+ include_pkg_deps = depIncludePkgDeps dflags
+ src_file = msHsFilePath node
obj_file = msObjFilePath node
obj_files = insertSuffixes obj_file extra_suffixes
- do_imp is_boot imp_mod
- = do { mb_hi <- findDependency hsc_env src_file imp_mod
+ do_imp loc is_boot pkg_qual imp_mod
+ = do { mb_hi <- findDependency hsc_env loc pkg_qual imp_mod
is_boot include_pkg_deps
; case mb_hi of {
Nothing -> return () ;
Just hi_file -> do
{ let hi_files = insertSuffixes hi_file extra_suffixes
- write_dep (obj,hi) = writeDependency hdl [obj] hi
+ write_dep (obj,hi) = writeDependency root hdl [obj] hi
-- Add one dependency for each suffix;
-- e.g. A.o : B.hi
-- Emit std dependency of the object(s) on the source file
-- Something like A.o : A.hs
- ; writeDependency hdl obj_files src_file
+ ; writeDependency root hdl obj_files src_file
-- Emit a dependency for each import
- -- SOURCE imports
- ; mapM_ (do_imp True)
- (filter (`notElem` excl_mods) (map unLoc (ms_srcimps node)))
+ ; let do_imps is_boot idecls = sequence_
+ [ do_imp loc is_boot (ideclPkgQual i) mod
+ | L loc i <- idecls,
+ let mod = unLoc (ideclName i),
+ mod `notElem` excl_mods ]
- -- regular imports
- ; mapM_ (do_imp False)
- (filter (`notElem` excl_mods) (map unLoc (ms_imps node)))
+ ; do_imps True (ms_srcimps node)
+ ; do_imps False (ms_imps node)
}
findDependency :: HscEnv
- -> FilePath -- Importing module: used only for error msg
+ -> SrcSpan
+ -> Maybe FastString -- package qualifier, if any
-> ModuleName -- Imported module
-> IsBootInterface -- Source import
-> Bool -- Record dependency on package modules
-> IO (Maybe FilePath) -- Interface file file
-findDependency hsc_env _ imp is_boot _include_pkg_deps
+findDependency hsc_env srcloc pkg imp is_boot include_pkg_deps
= do { -- Find the module; this will be fast because
-- we've done it once during downsweep
- r <- findImportedModule hsc_env imp Nothing
+ r <- findImportedModule hsc_env imp pkg
; case r of
Found loc _
-- Home package: just depend on the .hi or hi-boot file
- | isJust (ml_hs_file loc)
+ | isJust (ml_hs_file loc) || include_pkg_deps
-> return (Just (addBootSuffix_maybe is_boot (ml_hi_file loc)))
-- Not in this package: we don't need a dependency
| otherwise
-> return Nothing
- _ -> panic "findDependency"
+ fail -> throwOneError $ mkPlainErrMsg srcloc $
+ cannotFindModule (hsc_dflags hsc_env) imp fail
}
-----------------------------
-writeDependency :: Handle -> [FilePath] -> FilePath -> IO ()
--- (writeDependency h [t1,t2] dep) writes to handle h the dependency
+writeDependency :: FilePath -> Handle -> [FilePath] -> FilePath -> IO ()
+-- (writeDependency r h [t1,t2] dep) writes to handle h the dependency
-- t1 t2 : dep
-writeDependency hdl targets dep
- = hPutStrLn hdl (unwords (map forOutput targets) ++ " : " ++ forOutput dep)
- where forOutput = escapeSpaces . reslash Forwards . normalise
+writeDependency root hdl targets dep
+ = do let -- We need to avoid making deps on
+ -- c:/foo/...
+ -- on cygwin as make gets confused by the :
+ -- Making relative deps avoids some instances of this.
+ dep' = makeRelative root dep
+ forOutput = escapeSpaces . reslash Forwards . normalise
+ output = unwords (map forOutput targets) ++ " : " ++ forOutput dep'
+ hPutStrLn hdl output
-----------------------------
insertSuffixes
hPutStrLn tmp_hdl l
slurp
- catchJust ioErrors slurp
+ catchIO slurp
(\e -> if isEOFError e then return () else ioError e)
hClose hdl
cycles = [ c | CyclicSCC c <- GHC.topSortModuleGraph True mod_summaries Nothing ]
pp_cycles = vcat [ (ptext (sLit "---------- Cycle") <+> int n <+> ptext (sLit "----------"))
- $$ pprCycle c $$ text ""
+ $$ pprCycle c $$ blankLine
| (n,c) <- [1..] `zip` cycles ]
pprCycle :: [ModSummary] -> SDoc
pp_ms loop_breaker $$ vcat (map pp_group groups)
where
(boot_only, others) = partition is_boot_only mss
- is_boot_only ms = not (any in_group (ms_imps ms))
+ is_boot_only ms = not (any in_group (map (ideclName.unLoc) (ms_imps ms)))
in_group (L _ m) = m `elem` group_mods
group_mods = map (moduleName . ms_mod) mss
groups = GHC.topSortModuleGraph True all_others Nothing
pp_ms summary = text mod_str <> text (take (20 - length mod_str) (repeat ' '))
- <+> (pp_imps empty (ms_imps summary) $$
- pp_imps (ptext (sLit "{-# SOURCE #-}")) (ms_srcimps summary))
+ <+> (pp_imps empty (map (ideclName.unLoc) (ms_imps summary)) $$
+ pp_imps (ptext (sLit "{-# SOURCE #-}")) (map (ideclName.unLoc) (ms_srcimps summary)))
where
mod_str = moduleNameString (moduleName (ms_mod summary))
--
-----------------------------------------------------------------
- -- Flags
-GLOBAL_VAR(v_Dep_makefile, "Makefile", String);
-GLOBAL_VAR(v_Dep_include_pkg_deps, False, Bool);
-GLOBAL_VAR(v_Dep_exclude_mods, [], [ModuleName]);
-GLOBAL_VAR(v_Dep_suffixes, [], [String]);
-GLOBAL_VAR(v_Dep_warnings, True, Bool);
-
depStartMarker, depEndMarker :: String
depStartMarker = "# DO NOT DELETE: Beginning of Haskell dependencies"
depEndMarker = "# DO NOT DELETE: End of Haskell dependencies"
--- for compatibility with the old mkDependHS, we accept options of the form
--- -optdep-f -optdep.depend, etc.
-dep_opts :: [Flag IO]
-dep_opts =
- [ Flag "s" (SepArg (consIORef v_Dep_suffixes))
- , Flag "f" (SepArg (writeIORef v_Dep_makefile))
- , Flag "w" (NoArg (writeIORef v_Dep_warnings False))
-
- , Flag "-include-prelude" (NoArg (writeIORef v_Dep_include_pkg_deps True))
- -- -include-prelude is the old name for -include-pkg-deps, kept around
- -- for backward compatibility, but undocumented
-
- , Flag "-include-pkg-deps" (NoArg (writeIORef v_Dep_include_pkg_deps True))
- , Flag "-exclude-module=" (Prefix (consIORef v_Dep_exclude_mods . mkModuleName))
- , Flag "x" (Prefix (consIORef v_Dep_exclude_mods . mkModuleName))
- ]