Finder.lhs 6.26 KB
Newer Older
1
2
3
4
5
6
7
%
% (c) The University of Glasgow, 2000
%
\section[Finder]{Module Finder}

\begin{code}
module Finder (
8
    initFinder, 	-- :: PackageConfigInfo -> IO (), 
9
10
    findModule,		-- :: ModuleName -> IO (Maybe (Module, ModuleLocation))
    emptyHomeDirCache	-- :: IO ()
11
12
13
14
  ) where

#include "HsVersions.h"

15
import HscTypes		( ModuleLocation(..) )
16
17
18
19
20
21
import CmStaticInfo
import DriverPhases
import DriverState
import Module
import FiniteMap
import Util
22
import Panic		( panic )
23
import Config
24
25
26
27
28
29

import IOExts
import Directory
import List
import IO
import Monad
30
import Outputable	( showSDoc, ppr )	-- debugging only
31
32
\end{code}

33
34
35
36
37
The Finder provides a thin filesystem abstraction to the rest of the
compiler.  For a given module, it knows (a) which package the module
lives in, so it can make a Module from a ModuleName, and (b) where the
source, interface, and object files for a module live.

38
39
\begin{code}

40
-- v_PkgDirCache caches contents of package directories, never expunged
41
42
GLOBAL_VAR(v_PkgDirCache, panic "no pkg cache!", 
           FiniteMap String (PackageName, FilePath))
43

44
45
-- v_HomeDirCache caches contents of home directories, 
-- expunged whenever we create a new finder.
46
GLOBAL_VAR(v_HomeDirCache, Nothing, Maybe (FiniteMap String FilePath))
47
48


49
initFinder :: PackageConfigInfo -> IO ()
50
51
52
53
54
55
56
57
58
59
initFinder pkgs 
  = do	{	-- expunge our home cache
	; writeIORef v_HomeDirCache Nothing
		-- lazilly fill in the package cache
	; writeIORef v_PkgDirCache (unsafePerformIO (newPkgCache pkgs))
	
-- Debug output
--	; pkg_dbg_info <- readIORef v_PkgDirCache
--	; putStrLn (unlines (map show (fmToList pkg_dbg_info)))
	}
60

61
62
63
64
emptyHomeDirCache :: IO ()
emptyHomeDirCache
   = writeIORef v_HomeDirCache Nothing

65
findModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
66
67
68
69
70
71
72
73
74
findModule name
  = do 	{ hPutStr stderr ("findModule: " ++ moduleNameUserString name ++ " ... ")
	; maybe_m <- findModule_wrk name
	; case maybe_m of
	     Nothing -> hPutStrLn stderr "Not Found"
	     Just mm -> hPutStrLn stderr (showSDoc (ppr (snd mm)))
	; return maybe_m
	}

75
findModule_wrk :: ModuleName -> IO (Maybe (Module, ModuleLocation))
76
77
78
79
findModule_wrk name
  = do	{ j <- maybeHomeModule name
	; case j of
	    Just home_module -> return (Just home_module)
80
	    Nothing	     -> maybePackageModule name
81
	}
82
83
84

maybeHomeModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
maybeHomeModule mod_name = do
85
   home_cache <- readIORef v_HomeDirCache
86

87
88
89
90
91
92
93
94
95
   home_map <- 
     case home_cache of
	Nothing -> do
	   -- populate the home dir cache, using the import path (the import 
	   -- path is changed by -i flags on the command line, and defaults 
	   -- to ["."]).
	   home_imports <- readIORef v_Import_paths
	   let extendFM fm path = do
		   contents <- getDirectoryContents' path
96
97
                   let clean_contents = filter isUsefulFile contents
		   return (addListToFM fm (zip clean_contents (repeat path)))
98
99
100
101
102
103
	   home_map <- foldM extendFM emptyFM home_imports
	   writeIORef v_HomeDirCache (Just home_map)
	   return home_map

        Just home_map -> return home_map

104
105
106
107
   let basename = moduleNameString mod_name
       hs  = basename ++ ".hs"
       lhs = basename ++ ".lhs"

108
   case lookupFM home_map hs of {
109
	Just path -> mkHomeModuleLocn mod_name (path ++ '/':basename) hs;
110
111
	Nothing ->

112
   case lookupFM home_map lhs of {
113
	Just path ->  mkHomeModuleLocn mod_name (path ++ '/':basename) lhs;
114
115
116
117
118
119
120
121
	Nothing -> do

   -- can't find a source file anywhere, check for a lone .hi file.
   hisuf <- readIORef v_Hi_suf
   let hi = basename ++ '.':hisuf
   case lookupFM home_map hi of {
	Just path ->  mkHomeModuleLocn mod_name (path ++ '/':basename) hs;
	Nothing -> do
122

123
124
125
126
127
128
129
130
131
132
   -- last chance: .hi-boot and .hi-boot-<ver>
   let hi_boot = basename ++ ".hi-boot"
   let hi_boot_ver = basename ++ ".hi-boot-" ++ cHscIfaceFileVersion
   case lookupFM home_map hi_boot of {
	Just path ->  mkHomeModuleLocn mod_name (path ++ '/':basename) hs;
	Nothing -> do
   case lookupFM home_map hi_boot_ver of {
	Just path ->  mkHomeModuleLocn mod_name (path ++ '/':basename) hs;
	Nothing -> return Nothing
   }}}}}
133

134
mkHomeModuleLocn mod_name basename source_fn = do
135
136
137

   -- figure out the .hi file name: it lives in the same dir as the
   -- source, unless there's a -ohi flag on the command line.
138
139
   ohi    <- readIORef v_Output_hi
   hisuf  <- readIORef v_Hi_suf
140
   let hifile = case ohi of
141
		   Nothing -> basename ++ '.':hisuf
142
143
144
145
		   Just fn -> fn

   -- figure out the .o file name.  It also lives in the same dir
   -- as the source, but can be overriden by a -odir flag.
146
   o_file <- odir_ify (basename ++ '.':phaseInputExt Ln) >>= osuf_ify
147
148
149

   return (Just (mkHomeModule mod_name,
                 ModuleLocation{
150
151
152
153
                    ml_hspp_file = Nothing,
		    ml_hs_file   = Just source_fn,
		    ml_hi_file   = Just hifile,
		    ml_obj_file  = Just o_file
154
155
156
	         }
	))

157

158
159
160
161
162
163
164
newPkgCache :: [Package] -> IO (FiniteMap String (PackageName, FilePath))
newPkgCache pkgs = do
    let extendFM fm pkg = do
    	    let dirs = import_dirs pkg
    		pkg_name = _PK_ (name pkg)
    	    let addDir fm dir = do
    		    contents <- getDirectoryContents' dir
165
166
		    let clean_contents = filter isUsefulFile contents
    		    return (addListToFM fm (zip clean_contents 
167
168
169
170
171
    					       (repeat (pkg_name,dir))))
    	    foldM addDir fm dirs
    
    pkg_map <- foldM extendFM emptyFM pkgs
    return pkg_map
172
173


174
175
176
maybePackageModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
maybePackageModule mod_name = do
  pkg_cache <- readIORef v_PkgDirCache
177
178
179

  -- hi-suffix for packages depends on the build tag.
  package_hisuf <-
180
	do tag <- readIORef v_Build_tag
181
182
183
184
185
	   if null tag
		then return "hi"
		else return (tag ++ "_hi")

  let basename = moduleNameString mod_name
186
      hi = basename ++ '.':package_hisuf
187
188
189
190
191
192

  case lookupFM pkg_cache hi of
	Nothing -> return Nothing
	Just (pkg_name,path) -> 
	    return (Just (mkModule mod_name pkg_name,
			  ModuleLocation{ 
193
194
195
196
                                ml_hspp_file = Nothing,
				ml_hs_file   = Nothing,
				ml_hi_file   = Just (path ++ '/':hi),
				ml_obj_file  = Nothing
197
198
199
			   }
		   ))

200
201
202
203
isUsefulFile fn
   = let suffix = (reverse . takeWhile (/= '.') . reverse) fn
     in  suffix `elem` ["hi", "hs", "lhs", "hi-boot", "hi-boot-5"]

204
205
206
207
208
209
210
211
getDirectoryContents' d
   = IO.catch (getDirectoryContents d)
	  (\_ -> do hPutStr stderr 
		          ("WARNING: error while reading directory " ++ d)
		    return []
	  )
	 
\end{code}