[project @ 2001-05-16 12:49:59 by simonmar]
[ghc-hetmet.git] / ghc / compiler / main / Finder.lhs
index 70aa69c..176a244 100644 (file)
@@ -9,7 +9,8 @@ module Finder (
     findModule,                -- :: ModuleName -> IO (Maybe (Module, ModuleLocation))
     mkHomeModuleLocn,  -- :: ModuleName -> String -> FilePath 
                        --      -> IO ModuleLocation
-    emptyHomeDirCache  -- :: IO ()
+    emptyHomeDirCache, -- :: IO ()
+    flushPackageCache   -- :: [PackageConfig] -> IO ()
   ) where
 
 #include "HsVersions.h"
@@ -26,11 +27,10 @@ import Panic                ( panic )
 import Config
 
 import IOExts
-import Directory
 import List
 import IO
 import Monad
-import Outputable      ( showSDoc, ppr )       -- debugging only
+import Outputable
 \end{code}
 
 The Finder provides a thin filesystem abstraction to the rest of the
@@ -51,15 +51,16 @@ GLOBAL_VAR(v_HomeDirCache, Nothing, Maybe (FiniteMap String FilePath))
 
 initFinder :: [PackageConfig] -> IO ()
 initFinder pkgs 
-  = do {       -- expunge our home cache
-       ; writeIORef v_HomeDirCache Nothing
-               -- lazilly fill in the package cache
-       ; writeIORef v_PkgDirCache (unsafePerformIO (newPkgCache pkgs))
-       }
+   = do emptyHomeDirCache
+       flushPackageCache pkgs
+
+-- empty, and lazilly fill in the package cache
+flushPackageCache :: [PackageConfig] -> IO ()
+flushPackageCache pkgs = writeIORef v_PkgDirCache 
+                           (unsafePerformIO (newPkgCache pkgs))
 
 emptyHomeDirCache :: IO ()
-emptyHomeDirCache
-   = writeIORef v_HomeDirCache Nothing
+emptyHomeDirCache = writeIORef v_HomeDirCache Nothing
 
 findModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
 findModule name
@@ -81,10 +82,10 @@ maybeHomeModule mod_name = do
           -- to ["."]).
           home_imports <- readIORef v_Import_paths
           let extendFM fm path = do
-                  contents <- getDirectoryContents' path
+                  contents <- softGetDirectoryContents path
                    let clean_contents = filter isUsefulFile contents
                   return (addListToFM fm (zip clean_contents (repeat path)))
-          home_map <- foldM extendFM emptyFM home_imports
+          home_map <- foldM extendFM emptyFM (reverse home_imports)
           writeIORef v_HomeDirCache (Just home_map)
           return home_map
 
@@ -136,15 +137,9 @@ maybeHomeModule mod_name = do
 
 mkHomeModuleLocn mod_name basename source_fn = do
 
-   -- figure out the .hi file name: it lives in the same dir as the
-   -- source, unless there's a -ohi flag on the command line.
-   ohi    <- readIORef v_Output_hi
    hisuf  <- readIORef v_Hi_suf
-   let hifile = case ohi of
-                  Nothing -> getdir basename 
-                               ++ '/':moduleNameUserString mod_name 
+   let hifile = getdir basename ++ '/':moduleNameUserString mod_name 
                                ++ '.':hisuf
-                  Just fn -> fn
 
    -- figure out the .o file name.  It also lives in the same dir
    -- as the source, but can be overriden by a -odir flag.
@@ -166,7 +161,7 @@ newPkgCache pkgs = do
            let dirs = import_dirs pkg
                pkg_name = _PK_ (name pkg)
            let addDir fm dir = do
-                   contents <- getDirectoryContents' dir
+                   contents <- softGetDirectoryContents dir
                    return (addListToFM fm (zip contents 
                                               (repeat (pkg_name,dir))))
            foldM addDir fm dirs
@@ -204,12 +199,4 @@ maybePackageModule mod_name = do
 isUsefulFile fn
    = let suffix = (reverse . takeWhile (/= '.') . reverse) fn
      in  suffix `elem` ["hi", "hs", "lhs", "hi-boot", "hi-boot-5"]
-
-getDirectoryContents' d
-   = IO.catch (getDirectoryContents d)
-         (\_ -> do hPutStr stderr 
-                         ("WARNING: error while reading directory " ++ d)
-                   return []
-         )
-        
 \end{code}