X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Fmain%2FDriverMkDepend.hs;h=410f5b1abcda15a5466c544a553183306e22869c;hb=069370a53a92a68a6df163f07cec47b3d62632e7;hp=7d13a708f740eec0665e547e7d8b699adda7a482;hpb=508a505e9853984bfdaa3ad855ae3fcbc6d31787;p=ghc-hetmet.git diff --git a/ghc/compiler/main/DriverMkDepend.hs b/ghc/compiler/main/DriverMkDepend.hs index 7d13a70..410f5b1 100644 --- a/ghc/compiler/main/DriverMkDepend.hs +++ b/ghc/compiler/main/DriverMkDepend.hs @@ -1,9 +1,8 @@ ----------------------------------------------------------------------------- --- $Id: DriverMkDepend.hs,v 1.37 2005/01/27 10:44:27 simonpj Exp $ -- --- GHC Driver +-- Makefile Dependency Generation -- --- (c) Simon Marlow 2000 +-- (c) The University of Glasgow 2005 -- ----------------------------------------------------------------------------- @@ -13,22 +12,22 @@ module DriverMkDepend ( #include "HsVersions.h" -import CompManager ( cmInit, cmDepAnal, cmTopSort, cyclicModuleErr ) -import CmdLineOpts ( DynFlags( verbosity ) ) -import DriverState ( getStaticOpts, v_Opt_dep ) -import DriverUtil ( escapeSpaces, splitFilename, add ) -import DriverFlags ( processArgs, OptKind(..) ) -import HscTypes ( IsBootInterface, ModSummary(..), GhciMode(..), - msObjFilePath, msHsFilePath ) +import qualified GHC +import GHC ( Session, ModSummary(..) ) +import DynFlags ( DynFlags( verbosity, opt_dep ), getOpts ) +import Util ( escapeSpaces, splitFilename ) +import HscTypes ( HscEnv, IsBootInterface, msObjFilePath, msHsFilePath ) import Packages ( PackageIdH(..) ) import SysTools ( newTempName ) import qualified SysTools -import Module ( Module, ModLocation(..), moduleUserString, addBootSuffix_maybe ) +import Module ( Module, ModLocation(..), mkModule, moduleUserString, + addBootSuffix_maybe ) import Digraph ( SCC(..) ) import Finder ( findModule, FindResult(..) ) -import Util ( global ) +import Util ( global, consIORef ) import Outputable import Panic +import CmdLineParser import DATA_IOREF ( IORef, readIORef, writeIORef ) import EXCEPTION @@ -48,27 +47,31 @@ import Panic ( catchJust, ioErrors ) -- ----------------------------------------------------------------- -doMkDependHS :: DynFlags -> [FilePath] -> IO () -doMkDependHS dflags srcs +doMkDependHS :: Session -> [FilePath] -> IO () +doMkDependHS session srcs = do { -- Initialisation - cm_state <- cmInit Batch dflags - ; files <- beginMkDependHS + dflags <- GHC.getSessionDynFlags session + ; files <- beginMkDependHS dflags -- Do the downsweep to find all the modules - ; mod_summaries <- cmDepAnal cm_state srcs + ; targets <- mapM GHC.guessTarget srcs + ; GHC.setTargets session targets + ; excl_mods <- readIORef v_Dep_exclude_mods + ; GHC.depanal session excl_mods + ; mod_summaries <- GHC.getModuleGraph session -- Sort into dependency order -- There should be no cycles - ; let sorted = cmTopSort False mod_summaries + ; let sorted = GHC.topSortModuleGraph False mod_summaries -- Print out the dependencies if wanted - ; if verbosity dflags >= 3 then + ; if verbosity dflags >= 2 then hPutStrLn stderr (showSDoc (text "Module dependencies" $$ ppr sorted)) else return () -- Prcess them one by one, dumping results into makefile -- and complaining about cycles - ; mapM (processDeps dflags (mkd_tmp_hdl files)) sorted + ; mapM (processDeps session (mkd_tmp_hdl files)) sorted -- Tidy up ; endMkDependHS dflags files } @@ -88,16 +91,16 @@ data MkDepFiles mkd_tmp_file :: FilePath, -- Name of the temporary file mkd_tmp_hdl :: Handle } -- Handle of the open temporary file -beginMkDependHS :: IO MkDepFiles +beginMkDependHS :: DynFlags -> IO MkDepFiles -beginMkDependHS = do +beginMkDependHS dflags = do -- slurp in the mkdependHS-style options - flags <- getStaticOpts v_Opt_dep - _ <- processArgs dep_opts flags [] + 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 "dep" + tmp_file <- newTempName dflags "dep" tmp_hdl <- openFile tmp_file WriteMode -- open the makefile @@ -146,7 +149,7 @@ beginMkDependHS = do -- ----------------------------------------------------------------- -processDeps :: DynFlags +processDeps :: Session -> Handle -- Write dependencies to here -> SCC ModSummary -> IO () @@ -165,18 +168,21 @@ processDeps :: DynFlags -- -- For {-# SOURCE #-} imports the "hi" will be "hi-boot". -processDeps dflags hdl (CyclicSCC nodes) +processDeps session hdl (CyclicSCC nodes) = -- There shouldn't be any cycles; report them - throwDyn (ProgramError (showSDoc $ cyclicModuleErr nodes)) + throwDyn (ProgramError (showSDoc $ GHC.cyclicModuleErr nodes)) -processDeps dflags hdl (AcyclicSCC node) - = do { extra_suffixes <- readIORef v_Dep_suffixes +processDeps session 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 obj_file = msObjFilePath node obj_files = insertSuffixes obj_file extra_suffixes do_imp is_boot imp_mod - = do { mb_hi <- findDependency dflags src_file imp_mod is_boot + = do { mb_hi <- findDependency hsc_env src_file imp_mod + is_boot include_pkg_deps ; case mb_hi of { Nothing -> return () ; Just hi_file -> do @@ -199,37 +205,28 @@ processDeps dflags hdl (AcyclicSCC node) } -findDependency :: DynFlags +findDependency :: HscEnv -> FilePath -- Importing module: used only for error msg -> Module -- Imported module -> IsBootInterface -- Source import + -> Bool -- Record dependency on package modules -> IO (Maybe FilePath) -- Interface file file -findDependency dflags src imp is_boot - = do { excl_mods <- readIORef v_Dep_exclude_mods - ; include_prelude <- readIORef v_Dep_include_prelude - - -- Deal with the excluded modules - ; let imp_mod = moduleUserString imp - ; if imp_mod `elem` excl_mods - then return Nothing - else do - { -- Find the module; this will be fast because +findDependency hsc_env src imp is_boot include_pkg_deps + = do { -- Find the module; this will be fast because -- we've done it once during downsweep - r <- findModule dflags imp True {-explicit-} + r <- findModule hsc_env imp True {-explicit-} ; case r of Found loc pkg -- Not in this package: we don't need a dependency - | ExtPackage _ <- pkg, not include_prelude + | ExtPackage _ <- pkg, not include_pkg_deps -> return Nothing -- Home package: just depend on the .hi or hi-boot file | otherwise -> return (Just (addBootSuffix_maybe is_boot (ml_hi_file loc))) - _ -> throwDyn (ProgramError - (src ++ ": " ++ "can't locate import `" ++ imp_mod ++ "'" - ++ if is_boot then " (SOURCE import)" else "")) - }} + _ -> panic "findDependency" + } ----------------------------- writeDependency :: Handle -> [FilePath] -> FilePath -> IO () @@ -273,36 +270,37 @@ insertSuffixes file_name extras endMkDependHS :: DynFlags -> MkDepFiles -> IO () -endMkDependHS dflags (MkDep { mkd_make_file = make_file, mkd_make_hdl = makefile_hdl, - mkd_tmp_file = tmp_file, mkd_tmp_hdl = tmp_hdl }) - = do { -- write the magic marker into the tmp file - hPutStrLn tmp_hdl depEndMarker +endMkDependHS dflags + (MkDep { mkd_make_file = makefile, mkd_make_hdl = makefile_hdl, + mkd_tmp_file = tmp_file, mkd_tmp_hdl = tmp_hdl }) + = do + -- write the magic marker into the tmp file + hPutStrLn tmp_hdl depEndMarker + + case makefile_hdl of + Nothing -> return () + Just hdl -> do - ; case makefile_hdl of - Nothing -> return () - Just hdl -> do - { -- slurp the rest of the original makefile and copy it into the output - let slurp = do + let slurp = do l <- hGetLine hdl hPutStrLn tmp_hdl l slurp - ; catchJust ioErrors slurp + catchJust ioErrors slurp (\e -> if isEOFError e then return () else ioError e) - ; hClose hdl + hClose hdl - ; hClose tmp_hdl -- make sure it's flushed + hClose tmp_hdl -- make sure it's flushed - -- Create a backup of the original makefile - ; when (isJust makefile_hdl) - (SysTools.copy dflags ("Backing up " ++ make_file) - make_file (make_file++".bak")) + -- Create a backup of the original makefile + when (isJust makefile_hdl) + (SysTools.copy dflags ("Backing up " ++ makefile) + makefile (makefile++".bak")) - -- Copy the new makefile in place - ; SysTools.copy dflags "Installing new makefile" tmp_file make_file - }} + -- Copy the new makefile in place + SysTools.copy dflags "Installing new makefile" tmp_file makefile ----------------------------------------------------------------- @@ -313,8 +311,8 @@ endMkDependHS dflags (MkDep { mkd_make_file = make_file, mkd_make_hdl = makefil -- Flags GLOBAL_VAR(v_Dep_makefile, "Makefile", String); -GLOBAL_VAR(v_Dep_include_prelude, False, Bool); -GLOBAL_VAR(v_Dep_exclude_mods, ["GHC.Prim"], [String]); +GLOBAL_VAR(v_Dep_include_pkg_deps, False, Bool); +GLOBAL_VAR(v_Dep_exclude_mods, [], [Module]); GLOBAL_VAR(v_Dep_suffixes, [], [String]); GLOBAL_VAR(v_Dep_warnings, True, Bool); @@ -324,10 +322,11 @@ 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 = - [ ( "s", SepArg (add v_Dep_suffixes) ) + [ ( "s", SepArg (consIORef v_Dep_suffixes) ) , ( "f", SepArg (writeIORef v_Dep_makefile) ) , ( "w", NoArg (writeIORef v_Dep_warnings False) ) - , ( "-include-prelude", NoArg (writeIORef v_Dep_include_prelude True) ) - , ( "-exclude-module=", Prefix (add v_Dep_exclude_mods) ) - , ( "x", Prefix (add v_Dep_exclude_mods) ) + , ( "-include-prelude", NoArg (writeIORef v_Dep_include_pkg_deps True) ) + , ( "-include-pkg-deps", NoArg (writeIORef v_Dep_include_pkg_deps True) ) + , ( "-exclude-module=", Prefix (consIORef v_Dep_exclude_mods . mkModule) ) + , ( "x", Prefix (consIORef v_Dep_exclude_mods . mkModule) ) ]