1 -----------------------------------------------------------------------------
2 -- $Id: DriverMkDepend.hs,v 1.32 2004/06/24 09:41:11 simonmar Exp $
6 -- (c) Simon Marlow 2000
8 -----------------------------------------------------------------------------
10 module DriverMkDepend (
11 doMkDependHSPhase, beginMkDependHS, endMkDependHS
14 #include "HsVersions.h"
16 import GetImports ( getImports )
20 import SysTools ( newTempName )
21 import qualified SysTools
22 import Module ( ModuleName, ModLocation(..),
23 moduleNameUserString, isHomeModule )
24 import Finder ( findModule, hiBootExt, hiBootVerExt,
26 import Util ( global, maybePrefixMatch )
29 import DATA_IOREF ( IORef, readIORef, writeIORef )
35 import Maybe ( isJust )
37 #if __GLASGOW_HASKELL__ <= 408
38 import Panic ( catchJust, ioErrors )
41 -------------------------------------------------------------------------------
45 GLOBAL_VAR(v_Dep_makefile, "Makefile", String);
46 GLOBAL_VAR(v_Dep_include_prelude, False, Bool);
47 GLOBAL_VAR(v_Dep_exclude_mods, ["GHC.Prim"], [String]);
48 GLOBAL_VAR(v_Dep_suffixes, [], [String]);
49 GLOBAL_VAR(v_Dep_warnings, True, Bool);
52 GLOBAL_VAR(v_Dep_makefile_hdl, error "dep_makefile_hdl", Maybe Handle);
53 GLOBAL_VAR(v_Dep_tmp_file, error "dep_tmp_file", String);
54 GLOBAL_VAR(v_Dep_tmp_hdl, error "dep_tmp_hdl", Handle);
55 GLOBAL_VAR(v_Dep_dir_contents, error "dep_dir_contents", [(String,[String])]);
57 depStartMarker = "# DO NOT DELETE: Beginning of Haskell dependencies"
58 depEndMarker = "# DO NOT DELETE: End of Haskell dependencies"
60 -- for compatibility with the old mkDependHS, we accept options of the form
61 -- -optdep-f -optdep.depend, etc.
63 [ ( "s", SepArg (add v_Dep_suffixes) )
64 , ( "f", SepArg (writeIORef v_Dep_makefile) )
65 , ( "w", NoArg (writeIORef v_Dep_warnings False) )
66 , ( "-include-prelude", NoArg (writeIORef v_Dep_include_prelude True) )
67 , ( "-exclude-module=", Prefix (add v_Dep_exclude_mods) )
68 , ( "x", Prefix (add v_Dep_exclude_mods) )
71 beginMkDependHS :: IO ()
74 -- slurp in the mkdependHS-style options
75 flags <- getStaticOpts v_Opt_dep
76 _ <- processArgs dep_opts flags []
78 -- open a new temp file in which to stuff the dependency info
80 dep_file <- newTempName "dep"
81 writeIORef v_Dep_tmp_file dep_file
82 tmp_hdl <- openFile dep_file WriteMode
83 writeIORef v_Dep_tmp_hdl tmp_hdl
86 makefile <- readIORef v_Dep_makefile
87 exists <- doesFileExist makefile
90 writeIORef v_Dep_makefile_hdl Nothing
94 makefile_hdl <- openFile makefile ReadMode
95 writeIORef v_Dep_makefile_hdl (Just makefile_hdl)
97 -- slurp through until we get the magic start string,
98 -- copying the contents into dep_makefile
100 l <- hGetLine makefile_hdl
101 if (l == depStartMarker)
103 else do hPutStrLn tmp_hdl l; slurp
105 -- slurp through until we get the magic end marker,
106 -- throwing away the contents
108 l <- hGetLine makefile_hdl
109 if (l == depEndMarker)
113 catchJust ioErrors slurp
114 (\e -> if isEOFError e then return () else ioError e)
115 catchJust ioErrors chuck
116 (\e -> if isEOFError e then return () else ioError e)
119 -- write the magic marker into the tmp file
120 hPutStrLn tmp_hdl depStartMarker
122 -- cache the contents of all the import directories, for future
124 import_dirs <- readIORef v_Import_paths
125 pkg_import_dirs <- getPackageImportPath
126 import_dir_contents <- mapM softGetDirectoryContents import_dirs
127 pkg_import_dir_contents <- mapM softGetDirectoryContents pkg_import_dirs
128 writeIORef v_Dep_dir_contents
129 (zip import_dirs import_dir_contents ++
130 zip pkg_import_dirs pkg_import_dir_contents)
135 doMkDependHSPhase basename suff input_fn
136 = do src <- readFile input_fn
137 let (import_sources, import_normals, mod_name) = getImports src
138 let orig_fn = basename ++ '.':suff
139 (_, location') <- mkHomeModLocation mod_name orig_fn
141 -- take -ohi into account if present
142 ohi <- readIORef v_Output_hi
143 let location | Just fn <- ohi = location'{ ml_hi_file = fn }
144 | otherwise = location'
146 deps_sources <- mapM (findDependency True orig_fn) import_sources
147 deps_normals <- mapM (findDependency False orig_fn) import_normals
148 let deps = deps_sources ++ deps_normals
150 osuf <- readIORef v_Object_suf
151 extra_suffixes <- readIORef v_Dep_suffixes
152 let suffixes = map (++ ('_':osuf)) extra_suffixes
153 obj_file = ml_obj_file location
154 objs = obj_file : map (replaceFilenameSuffix obj_file) suffixes
156 -- Handle for file that accumulates dependencies
157 hdl <- readIORef v_Dep_tmp_hdl
159 -- std dependency of the object(s) on the source file
160 hPutStrLn hdl (unwords (map escapeSpaces objs) ++ " : " ++
161 escapeSpaces (basename ++ '.':suff))
163 let genDep (dep, False {- not an hi file -}) =
164 hPutStrLn hdl (unwords (map escapeSpaces objs) ++ " : " ++
166 genDep (dep, True {- is an hi file -}) = do
167 hisuf <- readIORef v_Hi_suf
169 -- In order to construct hi files with alternate suffixes, we
170 -- now have to find the "basename" of the hi file. This is
171 -- difficult because we can't just split the hi filename
172 -- at the last dot - the hisuf might have dots in it. So we
173 -- check whether the hi filename ends in hisuf, and if it does,
174 -- we strip off hisuf, otherwise we strip everything after the
177 | Just rest <- maybePrefixMatch rev_hisuf rev_dep
180 = remove_suffix '.' dep
182 rev_hisuf = reverse hisuf
183 rev_dep = reverse dep
185 deps = dep : map (\suf -> dep_base ++ suf ++ '_':hisuf)
187 -- length objs should be == length deps
188 sequence_ (zipWith (\o d -> hPutStrLn hdl (escapeSpaces o ++ " : " ++ escapeSpaces d)) objs deps)
190 sequence_ (map genDep [ d | Just d <- deps ])
193 -- add the lines to dep_makefile:
197 -- if the dependency is on something other than a .hi file:
198 -- this.o this.p_o ... : dep
200 -- if the import is {-# SOURCE #-}
201 -- this.o this.p_o ... : dep.hi-boot[-$vers]
204 -- this.o ... : dep.hi
205 -- this.p_o ... : dep.p_hi
208 -- (where .o is $osuf, and the other suffixes come from
209 -- the cmdline -s options).
213 endMkDependHS :: IO ()
215 makefile <- readIORef v_Dep_makefile
216 makefile_hdl <- readIORef v_Dep_makefile_hdl
217 tmp_file <- readIORef v_Dep_tmp_file
218 tmp_hdl <- readIORef v_Dep_tmp_hdl
220 -- write the magic marker into the tmp file
221 hPutStrLn tmp_hdl depEndMarker
227 -- slurp the rest of the original makefile and copy it into the output
233 catchJust ioErrors slurp
234 (\e -> if isEOFError e then return () else ioError e)
238 hClose tmp_hdl -- make sure it's flushed
240 -- Create a backup of the original makefile
241 when (isJust makefile_hdl)
242 (SysTools.copy ("Backing up " ++ makefile) makefile (makefile++".bak"))
244 -- Copy the new makefile in place
245 SysTools.copy "Installing new makefile" tmp_file makefile
248 findDependency :: Bool -> FilePath -> ModuleName -> IO (Maybe (String, Bool))
249 findDependency is_source src imp = do
250 excl_mods <- readIORef v_Dep_exclude_mods
251 include_prelude <- readIORef v_Dep_include_prelude
252 let imp_mod = moduleNameUserString imp
253 if imp_mod `elem` excl_mods
259 -- not in this package: we don't need a dependency
260 | not (isHomeModule mod) && not include_prelude
263 -- normal import: just depend on the .hi file
265 -> return (Just (ml_hi_file loc, not is_source))
267 -- if it's a source import, we want to generate a dependency
268 -- on the .hi-boot file, not the .hi file
270 -> let hi_file = ml_hi_file loc
271 boot_hi_file = replaceFilenameSuffix hi_file hiBootExt
272 boot_ver_hi_file = replaceFilenameSuffix hi_file hiBootVerExt
274 b <- doesFileExist boot_ver_hi_file
276 then return (Just (boot_ver_hi_file, not is_source))
278 b <- doesFileExist boot_hi_file
280 then return (Just (boot_hi_file, not is_source))
281 else return (Just (hi_file, not is_source))
283 Left _ -> throwDyn (ProgramError
284 (src ++ ": " ++ "can't locate import `" ++ imp_mod ++ "'" ++
285 if is_source then " (SOURCE import)" else ""))