2 % (c) The University of Glasgow, 2000
4 \section[Finder]{Module Finder}
8 flushFinderCache, -- :: IO ()
10 findModule, -- :: ModuleName -> Bool -> IO FindResult
11 findPackageModule, -- :: ModuleName -> Bool -> IO FindResult
12 mkHomeModLocation, -- :: ModuleName -> FilePath -> IO ModLocation
13 findLinkable, -- :: ModuleName -> ModLocation -> IO (Maybe Linkable)
15 hiBootExt, -- :: String
16 hiBootVerExt, -- :: String
20 #include "HsVersions.h"
23 import UniqFM ( filterUFM )
24 import HscTypes ( Linkable(..), Unlinked(..), IfacePackage(..) )
31 import CmdLineOpts ( DynFlags(..) )
33 import DATA_IOREF ( IORef, writeIORef, readIORef )
36 import System.Directory
39 import Data.Maybe ( isNothing )
41 -- -----------------------------------------------------------------------------
44 -- The Finder provides a thin filesystem abstraction to the rest of
45 -- the compiler. For a given module, it can tell you where the
46 -- source, interface, and object files for that module live.
48 -- It does *not* know which particular package a module lives in. Use
49 -- Packages.moduleToPackageConfig for that.
51 -- -----------------------------------------------------------------------------
54 GLOBAL_VAR(finder_cache, emptyModuleEnv, ModuleEnv FinderCacheEntry)
56 type FinderCacheEntry = (ModLocation,Maybe (PackageConfig,Bool))
58 -- remove all the home modules from the cache; package modules are
59 -- assumed to not move around during a session.
60 flushFinderCache :: IO ()
62 fm <- readIORef finder_cache
63 writeIORef finder_cache (filterUFM (\(loc,m) -> isNothing m) fm)
65 addToFinderCache :: Module -> FinderCacheEntry -> IO ()
66 addToFinderCache mod_name entry = do
67 fm <- readIORef finder_cache
68 writeIORef finder_cache (extendModuleEnv fm mod_name entry)
70 lookupFinderCache :: Module -> IO (Maybe FinderCacheEntry)
71 lookupFinderCache mod_name = do
72 fm <- readIORef finder_cache
73 return $! lookupModuleEnv fm mod_name
75 -- -----------------------------------------------------------------------------
78 -- This is the main interface to the finder, which maps ModuleNames to
79 -- Modules and ModLocations.
81 -- The Module contains one crucial bit of information about a module:
82 -- whether it lives in the current ("home") package or not (see Module
85 -- The ModLocation contains the names of all the files associated with
86 -- that module: its source file, .hi file, object file, etc.
89 = Found ModLocation IfacePackage
90 -- the module was found
91 | PackageHidden PackageId
92 -- for an explicit source import: the package containing the module is
94 | ModuleHidden PackageId
95 -- for an explicit source import: the package containing the module is
96 -- exposed, but the module itself is hidden.
98 -- the module was not found, the specified places were searched.
100 findModule :: DynFlags -> Module -> Bool -> IO FindResult
101 findModule = cached findModule'
103 findModule' :: DynFlags -> Module -> Bool -> IO FindResult
104 findModule' dflags name explicit = do
105 j <- maybeHomeModule dflags name
107 NotFound home_files -> do
108 r <- findPackageModule' dflags name explicit
111 -> return (NotFound (home_files ++ pkg_files))
113 -> return other_result
114 other_result -> return other_result
116 cached fn dflags name explicit = do
117 m <- lookupFinderCache name
119 Nothing -> fn dflags name explicit
121 | Just err <- visible explicit maybe_pkg -> return err
122 | otherwise -> return (Found loc (pkgInfoToId maybe_pkg))
124 pkgInfoToId :: Maybe (PackageConfig,Bool) -> IfacePackage
125 pkgInfoToId (Just (pkg,_)) = ExternalPackage (mkPackageId (package pkg))
126 pkgInfoToId Nothing = ThisPackage
128 -- Is a module visible or not? Returns Nothing if the import is ok,
129 -- or Just err if there's a visibility error.
130 visible :: Bool -> Maybe (PackageConfig,Bool) -> Maybe FindResult
131 visible explicit maybe_pkg
132 | Nothing <- maybe_pkg = Nothing -- home module ==> YES
133 | not explicit = Nothing -- implicit import ==> YES
134 | Just (pkg, exposed_module) <- maybe_pkg
136 _ | not exposed_module -> Just (ModuleHidden pkgname)
137 | not (exposed pkg) -> Just (PackageHidden pkgname)
138 | otherwise -> Nothing
140 pkgname = packageConfigId pkg
143 hiBootExt = "hi-boot"
144 hiBootVerExt = "hi-boot-" ++ cHscIfaceFileVersion
146 maybeHomeModule :: DynFlags -> Module -> IO FindResult
147 maybeHomeModule dflags mod = do
148 let home_path = importPaths dflags
149 hisuf <- readIORef v_Hi_suf
150 mode <- readIORef v_GhcMode
154 [ ("hs", mkHomeModLocationSearched mod)
155 , ("lhs", mkHomeModLocationSearched mod)
158 hi_exts = [ (hisuf, mkHiOnlyModLocation hisuf mod) ]
161 [ (hiBootVerExt, mkHiOnlyModLocation hisuf mod)
162 , (hiBootExt, mkHiOnlyModLocation hisuf mod)
165 -- In compilation manager modes, we look for source files in the home
166 -- package because we can compile these automatically. In one-shot
167 -- compilation mode we look for .hi and .hi-boot files only.
169 -- When generating dependencies, we're interested in either category.
172 | mode == DoMkDependHS = hi_exts ++ source_exts ++ boot_exts
173 | isCompManagerMode mode = source_exts
174 | otherwise {-one-shot-} = hi_exts ++ boot_exts
176 searchPathExts home_path mod exts
178 -- -----------------------------------------------------------------------------
179 -- Looking for a package module
181 findPackageModule :: DynFlags -> Module -> Bool -> IO FindResult
182 findPackageModule = cached findPackageModule'
184 findPackageModule' :: DynFlags -> Module -> Bool -> IO FindResult
185 findPackageModule' dflags mod explicit = do
186 mode <- readIORef v_GhcMode
188 case moduleToPackageConfig dflags mod of
189 Nothing -> return (NotFound [])
190 pkg_info@(Just (pkg_conf, module_exposed))
191 | Just err <- visible explicit pkg_info -> return err
192 | otherwise -> findPackageIface mode mod paths pkg_info
194 paths = importDirs pkg_conf
200 -> Maybe (PackageConfig,Bool)
202 findPackageIface mode mod imp_dirs pkg_info = do
203 -- hi-suffix for packages depends on the build tag.
205 do tag <- readIORef v_Build_tag
208 else return (tag ++ "_hi")
213 mkPackageModLocation pkg_info package_hisuf mod) ]
216 [ ("hs", mkPackageModLocation pkg_info package_hisuf mod)
217 , ("lhs", mkPackageModLocation pkg_info package_hisuf mod)
220 -- mkdependHS needs to look for source files in packages too, so
221 -- that we can make dependencies between package before they have
224 | mode == DoMkDependHS = hi_exts ++ source_exts
225 | otherwise = hi_exts
227 -- we never look for a .hi-boot file in an external package;
228 -- .hi-boot files only make sense for the home package.
229 searchPathExts imp_dirs mod exts
231 -- -----------------------------------------------------------------------------
232 -- General path searching
235 :: [FilePath] -- paths to search
236 -> Module -- module name
239 String -> String -> String -> IO FindResult -- action
244 searchPathExts path mod exts = search to_search
246 basename = dots_to_slashes (moduleUserString mod)
248 to_search :: [(FilePath, IO FindResult)]
249 to_search = [ (file, fn p basename ext)
252 let base | p == "." = basename
253 | otherwise = p ++ '/':basename
254 file = base ++ '.':ext
257 search [] = return (NotFound (map fst to_search))
258 search ((file, result) : rest) = do
259 b <- doesFileExist file
264 -- -----------------------------------------------------------------------------
265 -- Building ModLocations
267 mkHiOnlyModLocation hisuf mod path basename _ext = do
268 -- basename == dots_to_slashes (moduleNameUserString mod)
269 loc <- hiOnlyModLocation path basename hisuf
270 addToFinderCache mod (loc, Nothing)
271 return (Found loc ThisPackage)
273 mkPackageModLocation pkg_info hisuf mod path basename _ext = do
274 -- basename == dots_to_slashes (moduleNameUserString mod)
275 loc <- hiOnlyModLocation path basename hisuf
276 addToFinderCache mod (loc, pkg_info)
277 return (Found loc (pkgInfoToId pkg_info))
279 hiOnlyModLocation path basename hisuf
280 = do let full_basename = path++'/':basename
281 obj_fn <- mkObjPath full_basename basename
282 return ModLocation{ ml_hspp_file = Nothing,
283 ml_hs_file = Nothing,
284 ml_hi_file = full_basename ++ '.':hisuf,
285 -- Remove the .hi-boot suffix from
286 -- hi_file, if it had one. We always
287 -- want the name of the real .hi file
288 -- in the ml_hi_file field.
292 -- -----------------------------------------------------------------------------
293 -- Constructing a home module location
295 -- This is where we construct the ModLocation for a module in the home
296 -- package, for which we have a source file. It is called from three
299 -- (a) Here in the finder, when we are searching for a module to import,
300 -- using the search path (-i option).
302 -- (b) The compilation manager, when constructing the ModLocation for
303 -- a "root" module (a source file named explicitly on the command line
304 -- or in a :load command in GHCi).
306 -- (c) The driver in one-shot mode, when we need to construct a
307 -- ModLocation for a source file named on the command-line.
312 -- The name of the module
315 -- (a): The search path component where the source file was found.
319 -- (a): dots_to_slashes (moduleNameUserString mod)
320 -- (b) and (c): The filename of the source file, minus its extension
323 -- The filename extension of the source file (usually "hs" or "lhs").
325 mkHomeModLocation mod src_filename = do
326 let (basename,extension) = splitFilename src_filename
327 mkHomeModLocation' mod basename extension
329 mkHomeModLocationSearched mod path basename ext = do
330 loc <- mkHomeModLocation' mod (path ++ '/':basename) ext
331 return (Found loc ThisPackage)
333 mkHomeModLocation' mod src_basename ext = do
334 let mod_basename = dots_to_slashes (moduleUserString mod)
336 obj_fn <- mkObjPath src_basename mod_basename
337 hi_fn <- mkHiPath src_basename mod_basename
339 let loc = ModLocation{ ml_hspp_file = Nothing,
340 ml_hs_file = Just (src_basename ++ '.':ext),
342 ml_obj_file = obj_fn }
344 addToFinderCache mod (loc, Nothing)
347 -- | Constructs the filename of a .o file for a given source file.
348 -- Does /not/ check whether the .o file exists
350 :: FilePath -- the filename of the source file, minus the extension
351 -> String -- the module name with dots replaced by slashes
353 mkObjPath basename mod_basename
354 = do odir <- readIORef v_Output_dir
355 osuf <- readIORef v_Object_suf
357 let obj_basename | Just dir <- odir = dir ++ '/':mod_basename
358 | otherwise = basename
360 return (obj_basename ++ '.':osuf)
362 -- | Constructs the filename of a .hi file for a given source file.
363 -- Does /not/ check whether the .hi file exists
365 :: FilePath -- the filename of the source file, minus the extension
366 -> String -- the module name with dots replaced by slashes
368 mkHiPath basename mod_basename
369 = do hidir <- readIORef v_Hi_dir
370 hisuf <- readIORef v_Hi_suf
372 let hi_basename | Just dir <- hidir = dir ++ '/':mod_basename
373 | otherwise = basename
375 return (hi_basename ++ '.':hisuf)
377 -- -----------------------------------------------------------------------------
378 -- findLinkable isn't related to the other stuff in here,
379 -- but there's no other obvious place for it
381 findLinkable :: Module -> ModLocation -> IO (Maybe Linkable)
382 findLinkable mod locn
383 = do let obj_fn = ml_obj_file locn
384 obj_exist <- doesFileExist obj_fn
388 do let stub_fn = case splitFilename3 obj_fn of
389 (dir, base, ext) -> dir ++ "/" ++ base ++ "_stub.o"
390 stub_exist <- doesFileExist stub_fn
391 obj_time <- getModificationTime obj_fn
393 then return (Just (LM obj_time mod [DotO obj_fn, DotO stub_fn]))
394 else return (Just (LM obj_time mod [DotO obj_fn]))
396 -- -----------------------------------------------------------------------------
399 dots_to_slashes = map (\c -> if c == '.' then '/' else c)