[project @ 2000-11-14 15:03:46 by sewardj]
[ghc-hetmet.git] / ghc / compiler / main / Finder.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section[Finder]{Module Finder}
5
6 \begin{code}
7 module Finder (
8     initFinder,         -- :: PackageConfigInfo -> IO (), 
9     findModule,         -- :: ModuleName -> IO (Maybe (Module, ModuleLocation))
10     emptyHomeDirCache   -- :: IO ()
11   ) where
12
13 #include "HsVersions.h"
14
15 import HscTypes         ( ModuleLocation(..) )
16 import CmStaticInfo
17 import DriverPhases
18 import DriverState
19 import Module
20 import FiniteMap
21 import Util
22 import Panic            ( panic )
23 import Config
24
25 import IOExts
26 import Directory
27 import List
28 import IO
29 import Monad
30 import Outputable       ( showSDoc, ppr )       -- debugging only
31 \end{code}
32
33 The Finder provides a thin filesystem abstraction to the rest of the
34 compiler.  For a given module, it knows (a) which package the module
35 lives in, so it can make a Module from a ModuleName, and (b) where the
36 source, interface, and object files for a module live.
37
38 \begin{code}
39
40 -- v_PkgDirCache caches contents of package directories, never expunged
41 GLOBAL_VAR(v_PkgDirCache, panic "no pkg cache!", 
42            FiniteMap String (PackageName, FilePath))
43
44 -- v_HomeDirCache caches contents of home directories, 
45 -- expunged whenever we create a new finder.
46 GLOBAL_VAR(v_HomeDirCache, Nothing, Maybe (FiniteMap String FilePath))
47
48
49 initFinder :: PackageConfigInfo -> IO ()
50 initFinder pkgs 
51   = do  {       -- expunge our home cache
52         ; writeIORef v_HomeDirCache Nothing
53                 -- lazilly fill in the package cache
54         ; writeIORef v_PkgDirCache (unsafePerformIO (newPkgCache pkgs))
55         }
56
57 emptyHomeDirCache :: IO ()
58 emptyHomeDirCache
59    = writeIORef v_HomeDirCache Nothing
60
61 findModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
62 findModule name
63   = do  { j <- maybeHomeModule name
64         ; case j of
65             Just home_module -> return (Just home_module)
66             Nothing          -> maybePackageModule name
67         }
68
69 maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
70 maybeHomeModule mod_name = do
71    home_cache <- readIORef v_HomeDirCache
72
73    home_map <- 
74      case home_cache of
75         Nothing -> do
76            -- populate the home dir cache, using the import path (the import 
77            -- path is changed by -i flags on the command line, and defaults 
78            -- to ["."]).
79            home_imports <- readIORef v_Import_paths
80            let extendFM fm path = do
81                    contents <- getDirectoryContents' path
82                    let clean_contents = filter isUsefulFile contents
83                    return (addListToFM fm (zip clean_contents (repeat path)))
84            home_map <- foldM extendFM emptyFM home_imports
85            writeIORef v_HomeDirCache (Just home_map)
86            return home_map
87
88         Just home_map -> return home_map
89
90    let basename = moduleNameUserString mod_name
91        hs  = basename ++ ".hs"
92        lhs = basename ++ ".lhs"
93
94    case lookupFM home_map hs of {
95         Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename) 
96                                                (path ++ '/':hs);
97         Nothing ->
98
99    case lookupFM home_map lhs of {
100         Just path ->  mkHomeModuleLocn mod_name (path ++ '/':basename) 
101                                                 (path ++ '/':lhs);
102         Nothing -> do
103
104    -- can't find a source file anywhere, check for a lone .hi file.
105    hisuf <- readIORef v_Hi_suf
106    let hi = basename ++ '.':hisuf
107    case lookupFM home_map hi of {
108         Just path ->  mkHomeModuleLocn mod_name (path ++ '/':basename)
109                                                 (path ++ '/':hs);
110         Nothing -> do
111
112    -- last chance: .hi-boot and .hi-boot-<ver>
113    let hi_boot = basename ++ ".hi-boot"
114    let hi_boot_ver = basename ++ ".hi-boot-" ++ cHscIfaceFileVersion
115    case lookupFM home_map hi_boot of {
116         Just path ->  mkHomeModuleLocn mod_name (path ++ '/':basename)
117                                                 (path ++ '/':hs);
118         Nothing -> do
119    case lookupFM home_map hi_boot_ver of {
120         Just path ->  mkHomeModuleLocn mod_name (path ++ '/':basename)
121                                                 (path ++ '/':hs);
122         Nothing -> return Nothing
123    }}}}}
124
125 mkHomeModuleLocn mod_name basename source_fn = do
126
127    -- figure out the .hi file name: it lives in the same dir as the
128    -- source, unless there's a -ohi flag on the command line.
129    ohi    <- readIORef v_Output_hi
130    hisuf  <- readIORef v_Hi_suf
131    let hifile = case ohi of
132                    Nothing -> basename ++ '.':hisuf
133                    Just fn -> fn
134
135    -- figure out the .o file name.  It also lives in the same dir
136    -- as the source, but can be overriden by a -odir flag.
137    o_file <- odir_ify (basename ++ '.':phaseInputExt Ln) >>= osuf_ify
138
139    return (Just (mkHomeModule mod_name,
140                  ModuleLocation{
141                     ml_hspp_file = Nothing,
142                     ml_hs_file   = Just source_fn,
143                     ml_hi_file   = Just hifile,
144                     ml_obj_file  = Just o_file
145                  }
146         ))
147
148
149 newPkgCache :: [Package] -> IO (FiniteMap String (PackageName, FilePath))
150 newPkgCache pkgs = do
151     let extendFM fm pkg = do
152             let dirs = import_dirs pkg
153                 pkg_name = _PK_ (name pkg)
154             let addDir fm dir = do
155                     contents <- getDirectoryContents' dir
156                     let clean_contents = filter isUsefulFile contents
157                     return (addListToFM fm (zip clean_contents 
158                                                (repeat (pkg_name,dir))))
159             foldM addDir fm dirs
160     
161     pkg_map <- foldM extendFM emptyFM pkgs
162     return pkg_map
163
164
165 maybePackageModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
166 maybePackageModule mod_name = do
167   pkg_cache <- readIORef v_PkgDirCache
168
169   -- hi-suffix for packages depends on the build tag.
170   package_hisuf <-
171         do tag <- readIORef v_Build_tag
172            if null tag
173                 then return "hi"
174                 else return (tag ++ "_hi")
175
176   let basename = moduleNameString mod_name
177       hi = basename ++ '.':package_hisuf
178
179   case lookupFM pkg_cache hi of
180         Nothing -> return Nothing
181         Just (pkg_name,path) -> 
182             return (Just (mkModule mod_name pkg_name,
183                           ModuleLocation{ 
184                                 ml_hspp_file = Nothing,
185                                 ml_hs_file   = Nothing,
186                                 ml_hi_file   = Just (path ++ '/':hi),
187                                 ml_obj_file  = Nothing
188                            }
189                    ))
190
191 isUsefulFile fn
192    = let suffix = (reverse . takeWhile (/= '.') . reverse) fn
193      in  suffix `elem` ["hi", "hs", "lhs", "hi-boot", "hi-boot-5"]
194
195 getDirectoryContents' d
196    = IO.catch (getDirectoryContents d)
197           (\_ -> do hPutStr stderr 
198                           ("WARNING: error while reading directory " ++ d)
199                     return []
200           )
201          
202 \end{code}