2 % (c) The University of Glasgow, 2000
4 \section[Finder]{Module Finder}
8 initFinder, -- :: PackageConfigInfo -> IO (),
9 findModule -- :: ModuleName -> IO (Maybe (Module, ModuleLocation))
12 #include "HsVersions.h"
14 import HscTypes ( ModuleLocation(..) )
29 The Finder provides a thin filesystem abstraction to the rest of the
30 compiler. For a given module, it knows (a) which package the module
31 lives in, so it can make a Module from a ModuleName, and (b) where the
32 source, interface, and object files for a module live.
36 -- caches contents of package directories, never expunged
37 GLOBAL_VAR(v_PkgDirCache, error "no pkg cache!", FiniteMap String (PackageName, FilePath))
39 -- caches contents of home directories, expunged whenever we
40 -- create a new finder.
41 GLOBAL_VAR(v_HomeDirCache, Nothing, Maybe (FiniteMap String FilePath))
44 initFinder :: PackageConfigInfo -> IO ()
45 initFinder (PackageConfigInfo pkgs) = do
46 -- expunge our home cache
47 writeIORef v_HomeDirCache Nothing
49 -- lazilly fill in the package cache
50 writeIORef v_PkgDirCache (unsafePerformIO (newPkgCache pkgs))
53 findModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
55 j <- maybeHomeModule name
57 Just home_module -> return (Just home_module)
58 Nothing -> maybePackageModule name
60 maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
61 maybeHomeModule mod_name = do
62 home_cache <- readIORef v_HomeDirCache
67 -- populate the home dir cache, using the import path (the import
68 -- path is changed by -i flags on the command line, and defaults
70 home_imports <- readIORef v_Import_paths
71 let extendFM fm path = do
72 contents <- getDirectoryContents' path
73 return (addListToFM fm (zip contents (repeat path)))
74 home_map <- foldM extendFM emptyFM home_imports
75 writeIORef v_HomeDirCache (Just home_map)
78 Just home_map -> return home_map
80 let basename = moduleNameString mod_name
81 hs = basename ++ ".hs"
82 lhs = basename ++ ".lhs"
84 case lookupFM home_map hs of {
85 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename) hs;
88 case lookupFM home_map lhs of {
89 Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename) lhs;
90 Nothing -> return Nothing
94 mkHomeModuleLocn mod_name basename source_fn = do
96 -- figure out the .hi file name: it lives in the same dir as the
97 -- source, unless there's a -ohi flag on the command line.
98 ohi <- readIORef v_Output_hi
99 hisuf <- readIORef v_Hi_suf
100 let hifile = case ohi of
101 Nothing -> basename ++ '.':hisuf
104 -- figure out the .o file name. It also lives in the same dir
105 -- as the source, but can be overriden by a -odir flag.
106 o_file <- odir_ify (basename ++ '.':phaseInputExt Ln) >>= osuf_ify
108 return (Just (mkHomeModule mod_name,
117 newPkgCache :: [Package] -> IO (FiniteMap String (PackageName, FilePath))
118 newPkgCache pkgs = do
119 let extendFM fm pkg = do
120 let dirs = import_dirs pkg
121 pkg_name = _PK_ (name pkg)
122 let addDir fm dir = do
123 contents <- getDirectoryContents' dir
124 return (addListToFM fm (zip contents
125 (repeat (pkg_name,dir))))
128 pkg_map <- foldM extendFM emptyFM pkgs
132 maybePackageModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
133 maybePackageModule mod_name = do
134 pkg_cache <- readIORef v_PkgDirCache
136 -- hi-suffix for packages depends on the build tag.
138 do tag <- readIORef v_Build_tag
141 else return (tag ++ "_hi")
143 let basename = moduleNameString mod_name
144 hi = basename ++ '.':package_hisuf
146 case lookupFM pkg_cache hi of
147 Nothing -> return Nothing
148 Just (pkg_name,path) ->
149 return (Just (mkModule mod_name pkg_name,
151 hs_file = error "package module; no source",
153 obj_file = error "package module; no object"
157 getDirectoryContents' d
158 = IO.catch (getDirectoryContents d)
159 (\_ -> do hPutStr stderr
160 ("WARNING: error while reading directory " ++ d)