2 % (c) The University of Glasgow, 2000
4 \section[Finder]{Module Finder}
8 flushFinderCache, -- :: IO ()
10 findModule, -- :: ModuleName -> IO (Maybe (Module, ModLocation))
11 findPackageModule, -- :: ModuleName -> IO (Maybe (Module, ModLocation))
13 mkHomeModLocation, -- :: ModuleName -> String -> FilePath
16 findLinkable, -- :: ModuleName -> ModLocation -> IO (Maybe Linkable)
18 hiBootExt, -- :: String
19 hiBootVerExt, -- :: String
23 #include "HsVersions.h"
26 import UniqFM ( filterUFM )
27 import Packages ( PackageConfig(..) )
28 import HscTypes ( Linkable(..), Unlinked(..) )
30 import DriverUtil ( split_longest_prefix, splitFilename3 )
35 import DATA_IOREF ( IORef, writeIORef, readIORef )
42 -- -----------------------------------------------------------------------------
45 -- The Finder provides a thin filesystem abstraction to the rest of the
46 -- compiler. For a given module, it knows (a) whether the module lives
47 -- in the home package or in another package, so it can make a Module
48 -- from a ModuleName, and (b) where the source, interface, and object
49 -- files for a module live.
51 -- It does *not* know which particular package a module lives in, because
52 -- that information is only contained in the interface file.
54 -- -----------------------------------------------------------------------------
57 GLOBAL_VAR(finder_cache, emptyModuleEnv, ModuleEnv (Module,ModLocation))
59 -- remove all the home modules from the cache; package modules are
60 -- assumed to not move around during a session.
61 flushFinderCache :: IO ()
63 fm <- readIORef finder_cache
64 writeIORef finder_cache (filterUFM (not . isHomeModule . fst) fm)
66 addToFinderCache :: ModuleName -> (Module,ModLocation) -> IO ()
67 addToFinderCache mod_name stuff = do
68 fm <- readIORef finder_cache
69 writeIORef finder_cache (extendModuleEnvByName fm mod_name stuff)
71 lookupFinderCache :: ModuleName -> IO (Maybe (Module,ModLocation))
72 lookupFinderCache mod_name = do
73 fm <- readIORef finder_cache
74 return $! lookupModuleEnvByName fm mod_name
76 -- -----------------------------------------------------------------------------
79 -- This is the main interface to the finder, which maps ModuleNames to
80 -- Modules and ModLocations.
82 -- The Module contains one crucial bit of information about a module:
83 -- whether it lives in the current ("home") package or not (see Module
86 -- The ModLocation contains the names of all the files associated with
87 -- that module: its source file, .hi file, object file, etc.
89 findModule :: ModuleName -> IO (Maybe (Module, ModLocation))
91 r <- lookupFinderCache name
93 Just result -> return (Just result)
95 j <- maybeHomeModule name
97 Just home_module -> return (Just home_module)
98 Nothing -> findPackageMod name
100 findPackageModule :: ModuleName -> IO (Maybe (Module, ModLocation))
101 findPackageModule name = do
102 r <- lookupFinderCache name
104 Just result -> return (Just result)
105 Nothing -> findPackageMod name
107 hiBootExt = "hi-boot"
108 hiBootVerExt = "hi-boot-" ++ cHscIfaceFileVersion
110 maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModLocation))
111 maybeHomeModule mod_name = do
112 home_path <- readIORef v_Import_paths
113 hisuf <- readIORef v_Hi_suf
114 mode <- readIORef v_GhcMode
118 [ ("hs", mkHomeModLocation mod_name False)
119 , ("lhs", mkHomeModLocation mod_name False)
122 hi_exts = [ (hisuf, mkHiOnlyModLocation hisuf mod_name) ]
125 [ (hiBootVerExt, mkHiOnlyModLocation hisuf mod_name)
126 , (hiBootExt, mkHiOnlyModLocation hisuf mod_name)
129 -- In compilation manager modes, we look for source files in the home
130 -- package because we can compile these automatically. In one-shot
131 -- compilation mode we look for .hi and .hi-boot files only.
133 -- When generating dependencies, we're interested in either category.
136 | mode == DoMkDependHS = hi_exts ++ source_exts ++ boot_exts
137 | isCompManagerMode mode = source_exts
138 | otherwise {-one-shot-} = hi_exts ++ boot_exts
140 searchPathExts home_path mod_name exts
142 -- -----------------------------------------------------------------------------
143 -- Looking for a package module
145 findPackageMod :: ModuleName -> IO (Maybe (Module, ModLocation))
146 findPackageMod mod_name = do
147 mode <- readIORef v_GhcMode
148 imp_dirs <- getPackageImportPath -- including the 'auto' ones
150 -- hi-suffix for packages depends on the build tag.
152 do tag <- readIORef v_Build_tag
155 else return (tag ++ "_hi")
159 [ (package_hisuf, mkPackageModLocation package_hisuf mod_name) ]
162 [ ("hs", mkPackageModLocation package_hisuf mod_name)
163 , ("lhs", mkPackageModLocation package_hisuf mod_name)
166 -- mkdependHS needs to look for source files in packages too, so
167 -- that we can make dependencies between package before they have
170 | mode == DoMkDependHS = hi_exts ++ source_exts
171 | otherwise = hi_exts
173 -- we never look for a .hi-boot file in an external package;
174 -- .hi-boot files only make sense for the home package.
175 searchPathExts imp_dirs mod_name exts
177 -- -----------------------------------------------------------------------------
178 -- General path searching
181 :: [FilePath] -- paths to search
182 -> ModuleName -- module name
185 String -> String -> String -> IO (Module, ModLocation) -- action
188 -> IO (Maybe (Module, ModLocation))
190 searchPathExts path mod_name exts = search path
192 mod_str = moduleNameUserString mod_name
193 basename = map (\c -> if c == '.' then '/' else c) mod_str
195 search [] = return Nothing
196 search (p:ps) = loop exts
198 base | p == "." = basename
199 | otherwise = p ++ '/':basename
202 loop ((ext,fn):exts) = do
203 let file = base ++ '.':ext
204 b <- doesFileExist file
205 if b then Just `liftM` fn p basename ext
208 -- -----------------------------------------------------------------------------
209 -- Building ModLocations
211 mkHiOnlyModLocation hisuf mod_name path basename extension = do
212 loc <- hiOnlyModLocation path basename hisuf
213 let result = (mkHomeModule mod_name, loc)
214 addToFinderCache mod_name result
217 mkPackageModLocation hisuf mod_name path basename _extension = do
218 loc <- hiOnlyModLocation path basename hisuf
219 let result = (mkPackageModule mod_name, loc)
220 addToFinderCache mod_name result
223 hiOnlyModLocation path basename hisuf
224 = do { obj_fn <- mkObjPath path basename ;
225 return (ModLocation{ ml_hspp_file = Nothing,
226 ml_hs_file = Nothing,
227 ml_hi_file = path ++ '/':basename ++ '.':hisuf,
228 -- Remove the .hi-boot suffix from hi_file, if it
229 -- had one. We always want the name of the real
230 -- .hi file in the ml_hi_file field.
234 -- -----------------------------------------------------------------------------
235 -- Constructing a home module location
237 -- The .hi file always follows the module name, whereas the object
238 -- file may follow the name of the source file in the case where the
239 -- two differ (see summariseFile in compMan/CompManager.lhs).
241 -- The source filename is specified in three components. For example,
242 -- if we have a module "A.B.C" which was found along the patch "/P/Q/R"
243 -- with extension ".hs", then the full filename is "/P/Q/R/A/B/C.hs". The
244 -- components passed to mkHomeModLocation are
250 -- the object file and interface file are constructed by possibly
251 -- replacing the path component with the values of the -odir or the
252 -- -hidr options respectively, and the extension with the values of
253 -- the -osuf and -hisuf options respectively. That is, the basename
254 -- always remains intact.
256 -- mkHomeModLocation is called directly by the compilation manager to
257 -- construct the information for a root module. For a "root" module,
258 -- the rules are slightly different. The filename is allowed to
259 -- diverge from the module name, but we have to name the interface
260 -- file after the module name. For example, a root module
261 -- "/P/Q/R/foo.hs" will have components
267 -- and we set the flag is_root to True, to indicate that the basename
268 -- portion for the .hi file should be replaced by the last component
269 -- of the module name. eg. if the module name is "A.B.C" then basename
270 -- will be replaced by "C" for the .hi file only, resulting in an
271 -- .hi file like "/P/Q/R/C.hi" (subject to -hidir and -hisuf as usual).
273 mkHomeModLocation mod_name is_root path basename extension = do
275 hisuf <- readIORef v_Hi_suf
276 hidir <- readIORef v_Hi_dir
278 obj_fn <- mkObjPath path basename
281 mod_str = moduleNameUserString mod_name
282 (_,mod_suf) = split_longest_prefix mod_str (=='.')
286 | otherwise = basename
288 hi_path | Just d <- hidir = d
290 hi_fn = hi_path ++ '/':hi_basename ++ '.':hisuf
292 -- source filename (extension is always .hs or .lhs)
294 | path == "." = basename ++ '.':extension
295 | otherwise = path ++ '/':basename ++ '.':extension
297 result = ( mkHomeModule mod_name,
298 ModLocation{ ml_hspp_file = Nothing,
299 ml_hs_file = Just source_fn,
301 ml_obj_file = obj_fn,
304 addToFinderCache mod_name result
307 mkObjPath :: String -> FilePath -> IO FilePath
308 -- Construct the filename of a .o file from the path/basename
309 -- derived either from a .hs file or a .hi file.
311 -- Does *not* check whether the .o file exists
312 mkObjPath path basename
313 = do odir <- readIORef v_Output_dir
314 osuf <- readIORef v_Object_suf
315 let obj_path | Just d <- odir = d
317 return (obj_path ++ '/':basename ++ '.':osuf)
321 -- -----------------------------------------------------------------------------
322 -- findLinkable isn't related to the other stuff in here,
323 -- but there' no other obvious place for it
325 findLinkable :: ModuleName -> ModLocation -> IO (Maybe Linkable)
326 findLinkable mod locn
327 = do let obj_fn = ml_obj_file locn
328 obj_exist <- doesFileExist obj_fn
332 do let stub_fn = case splitFilename3 obj_fn of
333 (dir, base, ext) -> dir ++ "/" ++ base ++ "_stub.o"
334 stub_exist <- doesFileExist stub_fn
335 obj_time <- getModificationTime obj_fn
337 then return (Just (LM obj_time mod [DotO obj_fn, DotO stub_fn]))
338 else return (Just (LM obj_time mod [DotO obj_fn]))