2 % (c) The University of Glasgow, 2000
4 \section[Finder]{Module Finder}
8 initFinder, -- :: PackageConfigInfo -> IO (),
9 findModule, -- :: ModuleName -> IO (Maybe (Module, ModuleLocation))
10 emptyHomeDirCache -- :: IO ()
13 #include "HsVersions.h"
15 import HscTypes ( ModuleLocation(..) )
22 import Panic ( panic )
30 import Outputable ( showSDoc, ppr ) -- debugging only
33 The Finder provides a thin filesystem abstraction to the rest of the
34 compiler. For a given module, it knows (a) which package the module
35 lives in, so it can make a Module from a ModuleName, and (b) where the
36 source, interface, and object files for a module live.
40 -- v_PkgDirCache caches contents of package directories, never expunged
41 GLOBAL_VAR(v_PkgDirCache, panic "no pkg cache!",
42 FiniteMap String (PackageName, FilePath))
44 -- v_HomeDirCache caches contents of home directories,
45 -- expunged whenever we create a new finder.
46 GLOBAL_VAR(v_HomeDirCache, Nothing, Maybe (FiniteMap String FilePath))
49 initFinder :: PackageConfigInfo -> IO ()
51 = do { -- expunge our home cache
52 ; writeIORef v_HomeDirCache Nothing
53 -- lazilly fill in the package cache
54 ; writeIORef v_PkgDirCache (unsafePerformIO (newPkgCache pkgs))
57 emptyHomeDirCache :: IO ()
59 = writeIORef v_HomeDirCache Nothing
61 findModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
63 = do { j <- maybeHomeModule name
65 Just home_module -> return (Just home_module)
66 Nothing -> maybePackageModule name
69 maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
70 maybeHomeModule mod_name = do
71 home_cache <- readIORef v_HomeDirCache
76 -- populate the home dir cache, using the import path (the import
77 -- path is changed by -i flags on the command line, and defaults
79 home_imports <- readIORef v_Import_paths
80 let extendFM fm path = do
81 contents <- getDirectoryContents' path
82 let clean_contents = filter isUsefulFile contents
83 return (addListToFM fm (zip clean_contents (repeat path)))
84 home_map <- foldM extendFM emptyFM home_imports
85 writeIORef v_HomeDirCache (Just home_map)
88 Just home_map -> return home_map
90 let basename = moduleNameUserString mod_name
91 hs = basename ++ ".hs"
92 lhs = basename ++ ".lhs"
94 case lookupFM home_map hs of {
95 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename)
99 case lookupFM home_map lhs of {
100 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename)
104 -- can't find a source file anywhere, check for a lone .hi file.
105 hisuf <- readIORef v_Hi_suf
106 let hi = basename ++ '.':hisuf
107 case lookupFM home_map hi of {
108 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename)
112 -- last chance: .hi-boot-<ver> and .hi-boot
113 let hi_boot = basename ++ ".hi-boot"
114 let hi_boot_ver = basename ++ ".hi-boot-" ++ cHscIfaceFileVersion
115 case lookupFM home_map hi_boot_ver of {
116 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename)
119 case lookupFM home_map hi_boot of {
120 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename)
122 Nothing -> return Nothing
125 mkHomeModuleLocn mod_name basename source_fn = do
127 -- figure out the .hi file name: it lives in the same dir as the
128 -- source, unless there's a -ohi flag on the command line.
129 ohi <- readIORef v_Output_hi
130 hisuf <- readIORef v_Hi_suf
131 let hifile = case ohi of
132 Nothing -> basename ++ '.':hisuf
135 -- figure out the .o file name. It also lives in the same dir
136 -- as the source, but can be overriden by a -odir flag.
137 o_file <- odir_ify (basename ++ '.':phaseInputExt Ln) >>= osuf_ify
139 return (Just (mkHomeModule mod_name,
141 ml_hspp_file = Nothing,
142 ml_hs_file = Just source_fn,
143 ml_hi_file = Just hifile,
144 ml_obj_file = Just o_file
149 newPkgCache :: [Package] -> IO (FiniteMap String (PackageName, FilePath))
150 newPkgCache pkgs = do
151 let extendFM fm pkg = do
152 let dirs = import_dirs pkg
153 pkg_name = _PK_ (name pkg)
154 let addDir fm dir = do
155 contents <- getDirectoryContents' dir
156 let clean_contents = filter isUsefulFile contents
157 return (addListToFM fm (zip clean_contents
158 (repeat (pkg_name,dir))))
161 pkg_map <- foldM extendFM emptyFM pkgs
165 maybePackageModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
166 maybePackageModule mod_name = do
167 pkg_cache <- readIORef v_PkgDirCache
169 -- hi-suffix for packages depends on the build tag.
171 do tag <- readIORef v_Build_tag
174 else return (tag ++ "_hi")
176 let basename = moduleNameString mod_name
177 hi = basename ++ '.':package_hisuf
179 case lookupFM pkg_cache hi of
180 Nothing -> return Nothing
181 Just (pkg_name,path) ->
182 return (Just (mkModule mod_name pkg_name,
184 ml_hspp_file = Nothing,
185 ml_hs_file = Nothing,
186 ml_hi_file = Just (path ++ '/':hi),
187 ml_obj_file = Nothing
192 = let suffix = (reverse . takeWhile (/= '.') . reverse) fn
193 in suffix `elem` ["hi", "hs", "lhs", "hi-boot", "hi-boot-5"]
195 getDirectoryContents' d
196 = IO.catch (getDirectoryContents d)
197 (\_ -> do hPutStr stderr
198 ("WARNING: error while reading directory " ++ d)