2 % (c) The University of Glasgow, 2000
4 \section[Finder]{Module Finder}
8 Finder, -- = ModuleName -> IO (Maybe (Module, ModuleLocation))
9 newFinder, -- :: PackageConfigInfo -> IO Finder,
13 #include "HsVersions.h"
31 type Finder = ModuleName -> IO (Maybe (Module, ModuleLocation))
40 -- caches contents of package directories, never expunged
41 GLOBAL_VAR(pkgDirCache, Nothing, Maybe (FiniteMap String (PackageName, FilePath)))
43 -- caches contents of home directories, expunged whenever we
44 -- create a new finder.
45 GLOBAL_VAR(homeDirCache, emptyFM, FiniteMap String FilePath)
47 -- caches finder mapping, expunged whenever we create a new finder.
48 GLOBAL_VAR(finderMapCache, emptyFM, FiniteMap ModuleName Module)
51 newFinder :: PackageConfigInfo -> IO Finder
52 newFinder (PackageConfigInfo pkgs) = do
54 writeIORef homeDirCache emptyFM
55 writeIORef finderMapCache emptyFM
57 -- populate the home dir cache, using the import path (the import path
58 -- is changed by -i flags on the command line, and defaults to ["."]).
59 home_imports <- readIORef import_paths
60 let extendFM fm path = do
61 contents <- getDirectoryContents' path
62 return (addListToFM fm (zip contents (repeat path)))
63 home_map <- foldM extendFM emptyFM home_imports
64 writeIORef homeDirCache home_map
66 -- populate the package cache, if necessary
67 pkg_cache <- readIORef pkgDirCache
71 let extendFM fm pkg = do
72 let dirs = import_dirs pkg
73 pkg_name = _PK_ (name pkg)
74 let addDir fm dir = do
75 contents <- getDirectoryContents' dir
76 return (addListToFM fm (zip contents
77 (repeat (pkg_name,dir))))
80 pkg_map <- foldM extendFM emptyFM pkgs
81 writeIORef pkgDirCache (Just pkg_map)
86 -- and return the finder
90 finder :: ModuleName -> IO (Maybe (Module, ModuleLocation))
92 j <- maybeHomeModule name
94 Just home_module -> return (Just home_module)
95 Nothing -> maybePackageModule name
97 maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
98 maybeHomeModule mod_name = do
99 home_cache <- readIORef homeDirCache
101 let basename = moduleNameString mod_name
102 hs = basename ++ ".hs"
103 lhs = basename ++ ".lhs"
105 case lookupFM home_cache hs of {
106 Just path -> mkHomeModuleLocn mod_name basename path hs;
109 case lookupFM home_cache lhs of {
110 Just path -> mkHomeModuleLocn mod_name basename path lhs;
111 Nothing -> return Nothing
115 mkHomeModuleLocn mod_name basename path source_fn = do
117 -- figure out the .hi file name: it lives in the same dir as the
118 -- source, unless there's a -ohi flag on the command line.
119 ohi <- readIORef output_hi
120 hisuf <- readIORef hi_suf
121 let hifile = case ohi of
122 Nothing -> path ++ '/':basename ++ hisuf
125 -- figure out the .o file name. It also lives in the same dir
126 -- as the source, but can be overriden by a -odir flag.
127 o_file <- odir_ify (path ++ '/':basename ++ '.':phaseInputExt Ln)
129 return (Just (mkHomeModule mod_name,
137 maybePackageModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
138 maybePackageModule mod_name = do
139 maybe_pkg_cache <- readIORef pkgDirCache
140 case maybe_pkg_cache of {
141 Nothing -> panic "maybePackageModule: no pkg_cache";
144 -- hi-suffix for packages depends on the build tag.
146 do tag <- readIORef build_tag
149 else return (tag ++ "_hi")
151 let basename = moduleNameString mod_name
152 hi = basename ++ '.':package_hisuf
154 case lookupFM pkg_cache hi of
155 Nothing -> return Nothing
156 Just (pkg_name,path) ->
157 return (Just (mkModule mod_name pkg_name,
159 hs_file = error "package module; no source",
161 obj_file = error "package module; no object"
167 getDirectoryContents' d
168 = IO.catch (getDirectoryContents d)
169 (\_ -> do hPutStr stderr
170 ("WARNING: error while reading directory " ++ d)