8c4507f1c07fe453a6653a75d8c29c8bf1732313
[ghc-hetmet.git] / ghc / compiler / ghci / CmStaticInfo.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section[CmStaticInfo]{Session-static info for the Compilation Manager}
5
6 \begin{code}
7 module CmStaticInfo ( Package(..), PCI(..), mkPCI )
8 where
9
10 #include "HsVersions.h"
11
12 import List             ( nub )
13 import Char             ( isUpper )
14 import Directory        ( getDirectoryContents )
15
16 import Module           ( ModuleName, PackageName )
17 \end{code}
18
19 \begin{code}
20 data PackageConfigInfo
21    = PackageConfigInfo { 
22         pci_rawinfo  :: [Package],  -- contents of packages.conf
23         pci_modtable :: [(ModuleName, PackageName, FilePath)]
24                                     -- maps each available module to pkg and path
25      }
26
27 -- copied from the driver
28 data Package
29    = Package {
30         name            :: String,
31         import_dirs     :: [String],
32         library_dirs    :: [String],
33         hs_libraries    :: [String],
34         extra_libraries :: [String],
35         include_dirs    :: [String],
36         c_includes      :: [String],
37         package_deps    :: [String],
38         extra_ghc_opts  :: [String],
39         extra_cc_opts   :: [String],
40         extra_ld_opts   :: [String]
41      }
42   deriving Read
43
44 mkPCI :: [Package] -> IO PCI
45 mkPCI raw_package_info
46    = do mtab <- mk_module_table raw_package_info
47         return (PCI { pci_rawinfo  = raw_package_info,
48                       pci_modtable = mtab })
49
50 mk_module_table :: [Package] -> IO [(ModuleName,PackageName,FilePath)]
51 mk_module_table raw_info
52    = do 
53         -- the list of directories where package interfaces are
54         let -- p_i_dirs :: [(PkgName,Path)]
55             p_i_dirs = concatMap nm_and_paths raw_info
56
57         -- interface names in each directory
58         ifacess <- mapM ifaces_in_dir p_i_dirs
59         let -- iface_table :: [(ModName,PkgName,Path)] 
60             iface_table = map fsifyStrings (concat ifacess)
61
62         -- ToDo: allow a range of home package directories
63         return iface_table
64      where
65         fsifyStrings (mod_str, pkg_str, path_str)
66            = (mkFastString mod_str, mkFastString pkg_str, path_str)
67         -- nm_and_paths :: Package -> [(PkgName,Path)]
68         nm_and_paths package 
69            = [(name package, path) | path <- nub (import_dirs package)]
70
71         -- ifaces_in_dir :: (PkgName,Path) -> IO [(ModName,PkgName,Path)]
72         ifaces_in_dir (pkgname,path)
73            = getDirectoryContents path >>= \ entries ->
74              return [(zap_hi if_nm, pkgname, path) 
75                     | if_nm <- entries, looks_like_iface_name if_nm]
76         looks_like_iface_name e
77            = not (null e) && isUpper (head e) 
78                           && take 3 (reverse e) == "ih."
79         zap_hi 
80            = reverse . drop 3 . reverse
81
82 \end{code}