2 % (c) The University of Glasgow, 2000
4 \section[Finder]{Module Finder}
8 initFinder, -- :: [PackageConfig] -> IO (),
9 findModule, -- :: ModuleName -> IO (Maybe (Module, ModuleLocation))
10 mkHomeModuleLocn, -- :: ModuleName -> String -> FilePath
11 -- -> IO ModuleLocation
12 emptyHomeDirCache, -- :: IO ()
13 flushPackageCache -- :: [PackageConfig] -> IO ()
16 #include "HsVersions.h"
18 import HscTypes ( ModuleLocation(..) )
26 import Panic ( panic )
36 The Finder provides a thin filesystem abstraction to the rest of the
37 compiler. For a given module, it knows (a) which package the module
38 lives in, so it can make a Module from a ModuleName, and (b) where the
39 source, interface, and object files for a module live.
43 -- v_PkgDirCache caches contents of package directories, never expunged
44 GLOBAL_VAR(v_PkgDirCache, panic "no pkg cache!",
45 FiniteMap String (PackageName, FilePath))
47 -- v_HomeDirCache caches contents of home directories,
48 -- expunged whenever we create a new finder.
49 GLOBAL_VAR(v_HomeDirCache, Nothing, Maybe (FiniteMap String FilePath))
52 initFinder :: [PackageConfig] -> IO ()
54 = do emptyHomeDirCache
55 flushPackageCache pkgs
57 -- empty, and lazilly fill in the package cache
58 flushPackageCache :: [PackageConfig] -> IO ()
59 flushPackageCache pkgs = writeIORef v_PkgDirCache
60 (unsafePerformIO (newPkgCache pkgs))
62 emptyHomeDirCache :: IO ()
63 emptyHomeDirCache = writeIORef v_HomeDirCache Nothing
65 findModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
67 = do { j <- maybeHomeModule name
69 Just home_module -> return (Just home_module)
70 Nothing -> maybePackageModule name
73 maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
74 maybeHomeModule mod_name = do
75 home_cache <- readIORef v_HomeDirCache
80 -- populate the home dir cache, using the import path (the import
81 -- path is changed by -i flags on the command line, and defaults
83 home_imports <- readIORef v_Import_paths
84 let extendFM fm path = do
85 contents <- softGetDirectoryContents path
86 let clean_contents = filter isUsefulFile contents
87 return (addListToFM fm (zip clean_contents (repeat path)))
88 home_map <- foldM extendFM emptyFM (reverse home_imports)
89 writeIORef v_HomeDirCache (Just home_map)
92 Just home_map -> return home_map
94 let basename = moduleNameUserString mod_name
95 hs = basename ++ ".hs"
96 lhs = basename ++ ".lhs"
98 case lookupFM home_map hs of {
99 -- special case to avoid getting "./foo.hs" all the time
100 Just "." -> mkHomeModuleLocn mod_name basename hs;
101 Just path -> mkHomeModuleLocn mod_name
102 (path ++ '/':basename) (path ++ '/':hs);
105 case lookupFM home_map lhs of {
106 -- special case to avoid getting "./foo.hs" all the time
107 Just "." -> mkHomeModuleLocn mod_name basename lhs;
108 Just path -> mkHomeModuleLocn mod_name
109 (path ++ '/':basename) (path ++ '/':lhs);
112 -- can't find a source file anywhere, check for a lone .hi file.
113 hisuf <- readIORef v_Hi_suf
114 let hi = basename ++ '.':hisuf
115 case lookupFM home_map hi of {
116 Just path -> mkHomeModuleLocn mod_name
117 (path ++ '/':basename) (path ++ '/':hs);
120 -- last chance: .hi-boot-<ver> and .hi-boot
121 let hi_boot = basename ++ ".hi-boot"
122 let hi_boot_ver = basename ++ ".hi-boot-" ++ cHscIfaceFileVersion
123 case lookupFM home_map hi_boot_ver of {
124 Just path -> mkHomeModuleLocn mod_name
125 (path ++ '/':basename) (path ++ '/':hs);
127 case lookupFM home_map hi_boot of {
128 Just path -> mkHomeModuleLocn mod_name
129 (path ++ '/':basename) (path ++ '/':hs);
130 Nothing -> return Nothing
134 -- The .hi file always follows the module name, whereas the object
135 -- file may follow the name of the source file in the case where the
136 -- two differ (see summariseFile in compMan/CompManager.lhs).
138 mkHomeModuleLocn mod_name basename source_fn = do
140 hisuf <- readIORef v_Hi_suf
141 let hifile = getdir basename ++ '/':moduleNameUserString mod_name
144 -- figure out the .o file name. It also lives in the same dir
145 -- as the source, but can be overriden by a -odir flag.
146 o_file <- odir_ify (basename ++ '.':phaseInputExt Ln) >>= osuf_ify
148 return (Just (mkHomeModule mod_name,
150 ml_hspp_file = Nothing,
151 ml_hs_file = Just source_fn,
152 ml_hi_file = Just hifile,
153 ml_obj_file = Just o_file
158 newPkgCache :: [PackageConfig] -> IO (FiniteMap String (PackageName, FilePath))
159 newPkgCache pkgs = do
160 let extendFM fm pkg = do
161 let dirs = import_dirs pkg
162 pkg_name = _PK_ (name pkg)
163 let addDir fm dir = do
164 contents <- softGetDirectoryContents dir
165 return (addListToFM fm (zip contents
166 (repeat (pkg_name,dir))))
169 pkg_map <- foldM extendFM emptyFM pkgs
173 maybePackageModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
174 maybePackageModule mod_name = do
175 pkg_cache <- readIORef v_PkgDirCache
177 -- hi-suffix for packages depends on the build tag.
179 do tag <- readIORef v_Build_tag
182 else return (tag ++ "_hi")
184 let basename = moduleNameUserString mod_name
185 hi = basename ++ '.':package_hisuf
187 case lookupFM pkg_cache hi of
188 Nothing -> return Nothing
189 Just (pkg_name,path) ->
190 return (Just (mkModule mod_name pkg_name,
192 ml_hspp_file = Nothing,
193 ml_hs_file = Nothing,
194 ml_hi_file = Just (path ++ '/':hi),
195 ml_obj_file = Nothing
200 = let suffix = (reverse . takeWhile (/= '.') . reverse) fn
201 in suffix `elem` ["hi", "hs", "lhs", "hi-boot", "hi-boot-5"]