1 -----------------------------------------------------------------------------
2 -- $Id: DriverMkDepend.hs,v 1.25 2002/10/17 14:26:18 simonmar Exp $
6 -- (c) Simon Marlow 2000
8 -----------------------------------------------------------------------------
10 module DriverMkDepend where
12 #include "HsVersions.h"
15 import DriverUtil ( add, softGetDirectoryContents, replaceFilenameSuffix )
17 import SysTools ( newTempName )
18 import qualified SysTools
19 import Module ( ModuleName, ModLocation(..),
20 moduleNameUserString, isHomeModule )
21 import Finder ( findModule, hiBootExt, hiBootVerExt )
22 import Util ( global )
25 import DATA_IOREF ( IORef, readIORef, writeIORef )
31 import Maybe ( isJust )
33 #if __GLASGOW_HASKELL__ <= 408
34 import Panic ( catchJust, ioErrors )
37 -------------------------------------------------------------------------------
41 GLOBAL_VAR(v_Dep_makefile, "Makefile", String);
42 GLOBAL_VAR(v_Dep_include_prelude, False, Bool);
43 GLOBAL_VAR(v_Dep_exclude_mods, ["GHC.Prim"], [String]);
44 GLOBAL_VAR(v_Dep_suffixes, [], [String]);
45 GLOBAL_VAR(v_Dep_warnings, True, Bool);
48 GLOBAL_VAR(v_Dep_makefile_hdl, error "dep_makefile_hdl", Maybe Handle);
49 GLOBAL_VAR(v_Dep_tmp_file, error "dep_tmp_file", String);
50 GLOBAL_VAR(v_Dep_tmp_hdl, error "dep_tmp_hdl", Handle);
51 GLOBAL_VAR(v_Dep_dir_contents, error "dep_dir_contents", [(String,[String])]);
53 depStartMarker = "# DO NOT DELETE: Beginning of Haskell dependencies"
54 depEndMarker = "# DO NOT DELETE: End of Haskell dependencies"
56 -- for compatibility with the old mkDependHS, we accept options of the form
57 -- -optdep-f -optdep.depend, etc.
59 [ ( "s", SepArg (add v_Dep_suffixes) )
60 , ( "f", SepArg (writeIORef v_Dep_makefile) )
61 , ( "w", NoArg (writeIORef v_Dep_warnings False) )
62 , ( "-include-prelude", NoArg (writeIORef v_Dep_include_prelude True) )
63 , ( "-exclude-module=", Prefix (add v_Dep_exclude_mods) )
64 , ( "x", Prefix (add v_Dep_exclude_mods) )
67 beginMkDependHS :: IO ()
70 -- slurp in the mkdependHS-style options
71 flags <- getStaticOpts v_Opt_dep
72 _ <- processArgs dep_opts flags []
74 -- open a new temp file in which to stuff the dependency info
76 dep_file <- newTempName "dep"
77 writeIORef v_Dep_tmp_file dep_file
78 tmp_hdl <- openFile dep_file WriteMode
79 writeIORef v_Dep_tmp_hdl tmp_hdl
82 makefile <- readIORef v_Dep_makefile
83 exists <- doesFileExist makefile
86 writeIORef v_Dep_makefile_hdl Nothing
90 makefile_hdl <- openFile makefile ReadMode
91 writeIORef v_Dep_makefile_hdl (Just makefile_hdl)
93 -- slurp through until we get the magic start string,
94 -- copying the contents into dep_makefile
96 l <- hGetLine makefile_hdl
97 if (l == depStartMarker)
99 else do hPutStrLn tmp_hdl l; slurp
101 -- slurp through until we get the magic end marker,
102 -- throwing away the contents
104 l <- hGetLine makefile_hdl
105 if (l == depEndMarker)
109 catchJust ioErrors slurp
110 (\e -> if isEOFError e then return () else ioError e)
111 catchJust ioErrors chuck
112 (\e -> if isEOFError e then return () else ioError e)
115 -- write the magic marker into the tmp file
116 hPutStrLn tmp_hdl depStartMarker
118 -- cache the contents of all the import directories, for future
120 import_dirs <- readIORef v_Import_paths
121 pkg_import_dirs <- getPackageImportPath
122 import_dir_contents <- mapM softGetDirectoryContents import_dirs
123 pkg_import_dir_contents <- mapM softGetDirectoryContents pkg_import_dirs
124 writeIORef v_Dep_dir_contents
125 (zip import_dirs import_dir_contents ++
126 zip pkg_import_dirs pkg_import_dir_contents)
131 endMkDependHS :: IO ()
133 makefile <- readIORef v_Dep_makefile
134 makefile_hdl <- readIORef v_Dep_makefile_hdl
135 tmp_file <- readIORef v_Dep_tmp_file
136 tmp_hdl <- readIORef v_Dep_tmp_hdl
138 -- write the magic marker into the tmp file
139 hPutStrLn tmp_hdl depEndMarker
145 -- slurp the rest of the original makefile and copy it into the output
151 catchJust ioErrors slurp
152 (\e -> if isEOFError e then return () else ioError e)
156 hClose tmp_hdl -- make sure it's flushed
158 -- Create a backup of the original makefile
159 when (isJust makefile_hdl)
160 (SysTools.copy ("Backing up " ++ makefile) makefile (makefile++".bak"))
162 -- Copy the new makefile in place
163 SysTools.copy "Installing new makefile" tmp_file makefile
166 findDependency :: Bool -> FilePath -> ModuleName -> IO (Maybe (String, Bool))
167 findDependency is_source src imp = do
168 excl_mods <- readIORef v_Dep_exclude_mods
169 include_prelude <- readIORef v_Dep_include_prelude
170 let imp_mod = moduleNameUserString imp
171 if imp_mod `elem` excl_mods
177 -- not in this package: we don't need a dependency
178 | not (isHomeModule mod) && not include_prelude
181 -- normal import: just depend on the .hi file
183 -> return (Just (ml_hi_file loc, not is_source))
185 -- if it's a source import, we want to generate a dependency
186 -- on the .hi-boot file, not the .hi file
188 -> let hi_file = ml_hi_file loc
189 boot_hi_file = replaceFilenameSuffix hi_file hiBootExt
190 boot_ver_hi_file = replaceFilenameSuffix hi_file hiBootVerExt
192 b <- doesFileExist boot_hi_file
194 then return (Just (boot_hi_file, not is_source))
196 b <- doesFileExist boot_ver_hi_file
198 then return (Just (boot_ver_hi_file, not is_source))
199 else return (Just (hi_file, not is_source))
201 Nothing -> throwDyn (ProgramError
202 (src ++ ": " ++ "can't locate import `" ++ imp_mod ++ "'" ++
203 if is_source then " (SOURCE import)" else ""))