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"
16 import HscTypes ( Finder, ModuleLocation(..) )
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.
38 -- caches contents of package directories, never expunged
39 GLOBAL_VAR(v_PkgDirCache, Nothing, Maybe (FiniteMap String (PackageName, FilePath)))
41 -- caches contents of home directories, expunged whenever we
42 -- create a new finder.
43 GLOBAL_VAR(v_HomeDirCache, Nothing, Maybe (FiniteMap String FilePath))
46 newFinder :: PackageConfigInfo -> IO Finder
47 newFinder (PackageConfigInfo pkgs) = do
48 -- expunge our home cache
49 writeIORef v_HomeDirCache Nothing
51 -- and return the finder
55 finder :: [Package] -> ModuleName -> IO (Maybe (Module, ModuleLocation))
57 j <- maybeHomeModule name
59 Just home_module -> return (Just home_module)
60 Nothing -> maybePackageModule pkgs name
62 maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
63 maybeHomeModule mod_name = do
64 home_cache <- readIORef v_HomeDirCache
69 -- populate the home dir cache, using the import path (the import
70 -- path is changed by -i flags on the command line, and defaults
72 home_imports <- readIORef v_Import_paths
73 let extendFM fm path = do
74 contents <- getDirectoryContents' path
75 return (addListToFM fm (zip contents (repeat path)))
76 home_map <- foldM extendFM emptyFM home_imports
77 writeIORef v_HomeDirCache (Just home_map)
80 Just home_map -> return home_map
82 let basename = moduleNameString mod_name
83 hs = basename ++ ".hs"
84 lhs = basename ++ ".lhs"
86 case lookupFM home_map hs of {
87 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename) hs;
90 case lookupFM home_map lhs of {
91 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename) lhs;
92 Nothing -> return Nothing
96 mkHomeModuleLocn mod_name basename source_fn = do
98 -- figure out the .hi file name: it lives in the same dir as the
99 -- source, unless there's a -ohi flag on the command line.
100 ohi <- readIORef v_Output_hi
101 hisuf <- readIORef v_Hi_suf
102 let hifile = case ohi of
103 Nothing -> basename ++ '.':hisuf
106 -- figure out the .o file name. It also lives in the same dir
107 -- as the source, but can be overriden by a -odir flag.
108 o_file <- odir_ify (basename ++ '.':phaseInputExt Ln) >>= osuf_ify
110 return (Just (mkHomeModule mod_name,
118 maybePackageModule :: [Package] -> ModuleName
119 -> IO (Maybe (Module, ModuleLocation))
121 maybePackageModule pkgs mod_name = do
122 maybe_pkg_cache <- readIORef v_PkgDirCache
124 -- populate the package cache, if necessary
126 case maybe_pkg_cache of
129 let extendFM fm pkg = do
130 let dirs = import_dirs pkg
131 pkg_name = _PK_ (name pkg)
132 let addDir fm dir = do
133 contents <- getDirectoryContents' dir
134 return (addListToFM fm (zip contents
135 (repeat (pkg_name,dir))))
138 pkg_map <- foldM extendFM emptyFM pkgs
139 writeIORef v_PkgDirCache (Just pkg_map)
145 -- hi-suffix for packages depends on the build tag.
147 do tag <- readIORef v_Build_tag
150 else return (tag ++ "_hi")
152 let basename = moduleNameString mod_name
153 hi = basename ++ '.':package_hisuf
155 case lookupFM pkg_cache hi of
156 Nothing -> return Nothing
157 Just (pkg_name,path) ->
158 return (Just (mkModule mod_name pkg_name,
160 hs_file = error "package module; no source",
162 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)