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 addToFinderCache mod_name result
215 result = ( mkHomeModule mod_name, hiOnlyModLocation path basename hisuf )
217 mkPackageModLocation hisuf mod_name path basename _extension = do
218 addToFinderCache mod_name result
221 result = ( mkPackageModule mod_name, hiOnlyModLocation path basename hisuf )
223 hiOnlyModLocation path basename hisuf =
224 ModLocation{ ml_hspp_file = Nothing,
225 ml_hs_file = Nothing,
226 -- remove the .hi-boot suffix from hi_file, if it
227 -- had one. We always want the name of the real
228 -- .hi file in the ml_hi_file field.
229 ml_hi_file = path ++ '/':basename ++ '.':hisuf,
230 ml_obj_file = Nothing
233 -- -----------------------------------------------------------------------------
234 -- Constructing a home module location
236 -- The .hi file always follows the module name, whereas the object
237 -- file may follow the name of the source file in the case where the
238 -- two differ (see summariseFile in compMan/CompManager.lhs).
240 -- The source filename is specified in three components. For example,
241 -- if we have a module "A.B.C" which was found along the patch "/P/Q/R"
242 -- with extension ".hs", then the full filename is "/P/Q/R/A/B/C.hs". The
243 -- components passed to mkHomeModLocation are
249 -- the object file and interface file are constructed by possibly
250 -- replacing the path component with the values of the -odir or the
251 -- -hidr options respectively, and the extension with the values of
252 -- the -osuf and -hisuf options respectively. That is, the basename
253 -- always remains intact.
255 -- mkHomeModLocation is called directly by the compilation manager to
256 -- construct the information for a root module. For a "root" module,
257 -- the rules are slightly different. The filename is allowed to
258 -- diverge from the module name, but we have to name the interface
259 -- file after the module name. For example, a root module
260 -- "/P/Q/R/foo.hs" will have components
266 -- and we set the flag is_root to True, to indicate that the basename
267 -- portion for the .hi file should be replaced by the last component
268 -- of the module name. eg. if the module name is "A.B.C" then basename
269 -- will be replaced by "C" for the .hi file only, resulting in an
270 -- .hi file like "/P/Q/R/C.hi" (subject to -hidir and -hisuf as usual).
272 mkHomeModLocation mod_name is_root path basename extension = do
274 hisuf <- readIORef v_Hi_suf
275 hidir <- readIORef v_Hi_dir
276 odir <- readIORef v_Output_dir
277 osuf <- readIORef v_Object_suf
280 mod_str = moduleNameUserString mod_name
281 (_,mod_suf) = split_longest_prefix mod_str (=='.')
285 | otherwise = basename
287 hi_path | Just d <- hidir = d
289 hi_fn = hi_path ++ '/':hi_basename ++ '.':hisuf
291 -- source filename (extension is always .hs or .lhs)
293 | path == "." = basename ++ '.':extension
294 | otherwise = path ++ '/':basename ++ '.':extension
296 -- the object filename
297 obj_path | Just d <- odir = d
299 obj_fn = obj_path ++ '/':basename ++ '.':osuf
302 result = ( mkHomeModule mod_name,
303 ModLocation{ ml_hspp_file = Nothing,
304 ml_hs_file = Just source_fn,
306 ml_obj_file = Just obj_fn,
309 addToFinderCache mod_name result
312 -- -----------------------------------------------------------------------------
313 -- findLinkable isn't related to the other stuff in here,
314 -- but there' no other obvious place for it
316 findLinkable :: ModuleName -> ModLocation -> IO (Maybe Linkable)
317 findLinkable mod locn
318 | Just obj_fn <- ml_obj_file locn
319 = do obj_exist <- doesFileExist obj_fn
323 do let stub_fn = case splitFilename3 obj_fn of
324 (dir, base, ext) -> dir ++ "/" ++ base ++ "_stub.o"
325 stub_exist <- doesFileExist stub_fn
326 obj_time <- getModificationTime obj_fn
328 then return (Just (LM obj_time mod [DotO obj_fn, DotO stub_fn]))
329 else return (Just (LM obj_time mod [DotO obj_fn]))