2 % (c) The University of Glasgow, 2000
4 \section[Finder]{Module Finder}
8 Finder, -- = ModuleName -> IO (Maybe (Module, ModuleLocation))
9 newFinder, -- :: PackageConfigInfo -> IO Finder,
14 #include "HsVersions.h"
31 The Finder provides a thin filesystem abstraction to the rest of the
32 compiler. For a given module, it knows (a) which package the module
33 lives in, so it can make a Module from a ModuleName, and (b) where the
34 source, interface, and object files for a module live.
37 type Finder = ModuleName -> IO (Maybe (Module, ModuleLocation))
39 -- For a module in another package, the hs_file and obj_file
40 -- components of ModuleLocation are undefined.
42 -- The locations specified by a ModuleLocation may or may not
43 -- correspond to actual files yet: for example, even if the object
44 -- file doesn't exist, the ModuleLocation still contains the path to
45 -- where the object file will reside if/when it is created.
54 -- caches contents of package directories, never expunged
55 GLOBAL_VAR(pkgDirCache, Nothing, Maybe (FiniteMap String (PackageName, FilePath)))
57 -- caches contents of home directories, expunged whenever we
58 -- create a new finder.
59 GLOBAL_VAR(homeDirCache, emptyFM, FiniteMap String FilePath)
61 -- caches finder mapping, expunged whenever we create a new finder.
62 GLOBAL_VAR(finderMapCache, emptyFM, FiniteMap ModuleName Module)
65 newFinder :: PackageConfigInfo -> IO Finder
66 newFinder (PackageConfigInfo pkgs) = do
68 writeIORef homeDirCache emptyFM
69 writeIORef finderMapCache emptyFM
71 -- populate the home dir cache, using the import path (the import path
72 -- is changed by -i flags on the command line, and defaults to ["."]).
73 home_imports <- readIORef import_paths
74 let extendFM fm path = do
75 contents <- getDirectoryContents' path
76 return (addListToFM fm (zip contents (repeat path)))
77 home_map <- foldM extendFM emptyFM home_imports
78 writeIORef homeDirCache home_map
80 -- populate the package cache, if necessary
81 pkg_cache <- readIORef pkgDirCache
85 let extendFM fm pkg = do
86 let dirs = import_dirs pkg
87 pkg_name = _PK_ (name pkg)
88 let addDir fm dir = do
89 contents <- getDirectoryContents' dir
90 return (addListToFM fm (zip contents
91 (repeat (pkg_name,dir))))
94 pkg_map <- foldM extendFM emptyFM pkgs
95 writeIORef pkgDirCache (Just pkg_map)
100 -- and return the finder
104 finder :: ModuleName -> IO (Maybe (Module, ModuleLocation))
106 j <- maybeHomeModule name
108 Just home_module -> return (Just home_module)
109 Nothing -> maybePackageModule name
111 maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
112 maybeHomeModule mod_name = do
113 home_cache <- readIORef homeDirCache
115 let basename = moduleNameString mod_name
116 hs = basename ++ ".hs"
117 lhs = basename ++ ".lhs"
119 case lookupFM home_cache hs of {
120 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename) hs;
123 case lookupFM home_cache lhs of {
124 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename) lhs;
125 Nothing -> return Nothing
129 mkHomeModuleLocn mod_name basename source_fn = do
131 -- figure out the .hi file name: it lives in the same dir as the
132 -- source, unless there's a -ohi flag on the command line.
133 ohi <- readIORef output_hi
134 hisuf <- readIORef hi_suf
135 let hifile = case ohi of
136 Nothing -> basename ++ hisuf
139 -- figure out the .o file name. It also lives in the same dir
140 -- as the source, but can be overriden by a -odir flag.
141 o_file <- odir_ify (basename ++ '.':phaseInputExt Ln)
143 return (Just (mkHomeModule mod_name,
151 maybePackageModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
152 maybePackageModule mod_name = do
153 maybe_pkg_cache <- readIORef pkgDirCache
154 case maybe_pkg_cache of {
155 Nothing -> panic "maybePackageModule: no pkg_cache";
158 -- hi-suffix for packages depends on the build tag.
160 do tag <- readIORef build_tag
163 else return (tag ++ "_hi")
165 let basename = moduleNameString mod_name
166 hi = basename ++ '.':package_hisuf
168 case lookupFM pkg_cache hi of
169 Nothing -> return Nothing
170 Just (pkg_name,path) ->
171 return (Just (mkModule mod_name pkg_name,
173 hs_file = error "package module; no source",
175 obj_file = error "package module; no object"
181 getDirectoryContents' d
182 = IO.catch (getDirectoryContents d)
183 (\_ -> do hPutStr stderr
184 ("WARNING: error while reading directory " ++ d)