1 -----------------------------------------------------------------------------
3 -- Makefile Dependency Generation
5 -- (c) The University of Glasgow 2005
7 -----------------------------------------------------------------------------
9 module DriverMkDepend (
13 #include "HsVersions.h"
16 import GHC ( Session, ModSummary(..) )
17 import DynFlags ( DynFlags( verbosity, opt_dep ), getOpts )
18 import Util ( escapeSpaces, splitFilename, joinFileExt )
19 import HscTypes ( HscEnv, IsBootInterface, msObjFilePath, msHsFilePath )
20 import Packages ( PackageIdH(..) )
21 import SysTools ( newTempName )
22 import qualified SysTools
23 import Module ( Module, ModLocation(..), mkModule, moduleUserString,
25 import Digraph ( SCC(..) )
26 import Finder ( findModule, FindResult(..) )
27 import Util ( global, consIORef )
30 import SrcLoc ( unLoc )
33 import DATA_IOREF ( IORef, readIORef, writeIORef )
39 import Maybe ( isJust )
41 #if __GLASGOW_HASKELL__ <= 408
42 import Panic ( catchJust, ioErrors )
44 import ErrUtils ( debugTraceMsg )
46 -----------------------------------------------------------------
50 -----------------------------------------------------------------
52 doMkDependHS :: Session -> [FilePath] -> IO ()
53 doMkDependHS session srcs
54 = do { -- Initialisation
55 dflags <- GHC.getSessionDynFlags session
56 ; files <- beginMkDependHS dflags
58 -- Do the downsweep to find all the modules
59 ; targets <- mapM (\s -> GHC.guessTarget s Nothing) srcs
60 ; GHC.setTargets session targets
61 ; excl_mods <- readIORef v_Dep_exclude_mods
62 ; GHC.depanal session excl_mods
63 ; mod_summaries <- GHC.getModuleGraph session
65 -- Sort into dependency order
66 -- There should be no cycles
67 ; let sorted = GHC.topSortModuleGraph False mod_summaries Nothing
69 -- Print out the dependencies if wanted
70 ; debugTraceMsg dflags 2 (showSDoc (text "Module dependencies" $$ ppr sorted))
72 -- Prcess them one by one, dumping results into makefile
73 -- and complaining about cycles
74 ; mapM (processDeps session (mkd_tmp_hdl files)) sorted
77 ; endMkDependHS dflags files }
79 -----------------------------------------------------------------
82 -- Create a temporary file,
84 -- slurp through it, etc
86 -----------------------------------------------------------------
89 = MkDep { mkd_make_file :: FilePath, -- Name of the makefile
90 mkd_make_hdl :: Maybe Handle, -- Handle for the open makefile
91 mkd_tmp_file :: FilePath, -- Name of the temporary file
92 mkd_tmp_hdl :: Handle } -- Handle of the open temporary file
94 beginMkDependHS :: DynFlags -> IO MkDepFiles
96 beginMkDependHS dflags = do
97 -- slurp in the mkdependHS-style options
98 let flags = getOpts dflags opt_dep
99 _ <- processArgs dep_opts flags
101 -- open a new temp file in which to stuff the dependency info
103 tmp_file <- newTempName dflags "dep"
104 tmp_hdl <- openFile tmp_file WriteMode
107 makefile <- readIORef v_Dep_makefile
108 exists <- doesFileExist makefile
113 makefile_hdl <- openFile makefile ReadMode
115 -- slurp through until we get the magic start string,
116 -- copying the contents into dep_makefile
118 l <- hGetLine makefile_hdl
119 if (l == depStartMarker)
121 else do hPutStrLn tmp_hdl l; slurp
123 -- slurp through until we get the magic end marker,
124 -- throwing away the contents
126 l <- hGetLine makefile_hdl
127 if (l == depEndMarker)
131 catchJust ioErrors slurp
132 (\e -> if isEOFError e then return () else ioError e)
133 catchJust ioErrors chuck
134 (\e -> if isEOFError e then return () else ioError e)
136 return (Just makefile_hdl)
139 -- write the magic marker into the tmp file
140 hPutStrLn tmp_hdl depStartMarker
142 return (MkDep { mkd_make_file = makefile, mkd_make_hdl = mb_make_hdl,
143 mkd_tmp_file = tmp_file, mkd_tmp_hdl = tmp_hdl})
146 -----------------------------------------------------------------
150 -----------------------------------------------------------------
152 processDeps :: Session
153 -> Handle -- Write dependencies to here
156 -- Write suitable dependencies to handle
160 -- If the dependency is on something other than a .hi file:
161 -- this.o this.p_o ... : dep
163 -- this.o ... : dep.hi
164 -- this.p_o ... : dep.p_hi
166 -- (where .o is $osuf, and the other suffixes come from
167 -- the cmdline -s options).
169 -- For {-# SOURCE #-} imports the "hi" will be "hi-boot".
171 processDeps session hdl (CyclicSCC nodes)
172 = -- There shouldn't be any cycles; report them
173 throwDyn (ProgramError (showSDoc $ GHC.cyclicModuleErr nodes))
175 processDeps session hdl (AcyclicSCC node)
176 = do { extra_suffixes <- readIORef v_Dep_suffixes
177 ; hsc_env <- GHC.sessionHscEnv session
178 ; include_pkg_deps <- readIORef v_Dep_include_pkg_deps
179 ; let src_file = msHsFilePath node
180 obj_file = msObjFilePath node
181 obj_files = insertSuffixes obj_file extra_suffixes
183 do_imp is_boot imp_mod
184 = do { mb_hi <- findDependency hsc_env src_file imp_mod
185 is_boot include_pkg_deps
187 Nothing -> return () ;
189 { let hi_files = insertSuffixes hi_file extra_suffixes
190 write_dep (obj,hi) = writeDependency hdl [obj] hi
192 -- Add one dependency for each suffix;
195 ; mapM_ write_dep (obj_files `zip` hi_files) }}}
198 -- Emit std dependency of the object(s) on the source file
199 -- Something like A.o : A.hs
200 ; writeDependency hdl obj_files src_file
202 -- Emit a dependency for each import
203 ; mapM_ (do_imp True . unLoc) (ms_srcimps node) -- SOURCE imports
204 ; mapM_ (do_imp False . unLoc) (ms_imps node) -- regular imports
208 findDependency :: HscEnv
209 -> FilePath -- Importing module: used only for error msg
210 -> Module -- Imported module
211 -> IsBootInterface -- Source import
212 -> Bool -- Record dependency on package modules
213 -> IO (Maybe FilePath) -- Interface file file
214 findDependency hsc_env src imp is_boot include_pkg_deps
215 = do { -- Find the module; this will be fast because
216 -- we've done it once during downsweep
217 r <- findModule hsc_env imp True {-explicit-}
220 -- Not in this package: we don't need a dependency
221 | ExtPackage _ <- pkg, not include_pkg_deps
224 -- Home package: just depend on the .hi or hi-boot file
226 -> return (Just (addBootSuffix_maybe is_boot (ml_hi_file loc)))
228 _ -> panic "findDependency"
231 -----------------------------
232 writeDependency :: Handle -> [FilePath] -> FilePath -> IO ()
233 -- (writeDependency h [t1,t2] dep) writes to handle h the dependency
235 writeDependency hdl targets dep
236 = hPutStrLn hdl (unwords (map escapeSpaces targets) ++ " : "
239 -----------------------------
241 :: FilePath -- Original filename; e.g. "foo.o"
242 -> [String] -- Extra suffices e.g. ["x","y"]
243 -> [FilePath] -- Zapped filenames e.g. ["foo.o", "foo.x_o", "foo.y_o"]
244 -- Note that that the extra bit gets inserted *before* the old suffix
245 -- We assume the old suffix contains no dots, so we can strip it with removeSuffix
247 -- NOTE: we used to have this comment
248 -- In order to construct hi files with alternate suffixes, we
249 -- now have to find the "basename" of the hi file. This is
250 -- difficult because we can't just split the hi filename
251 -- at the last dot - the hisuf might have dots in it. So we
252 -- check whether the hi filename ends in hisuf, and if it does,
253 -- we strip off hisuf, otherwise we strip everything after the
255 -- But I'm not sure we care about hisufs with dots in them.
256 -- Lots of other things will break first!
258 insertSuffixes file_name extras
259 = file_name : [ basename `joinFileExt` (extra ++ "_" ++ suffix) | extra <- extras ]
261 (basename, suffix) = splitFilename file_name
264 -----------------------------------------------------------------
267 -- Complete the makefile, close the tmp file etc
269 -----------------------------------------------------------------
271 endMkDependHS :: DynFlags -> MkDepFiles -> IO ()
274 (MkDep { mkd_make_file = makefile, mkd_make_hdl = makefile_hdl,
275 mkd_tmp_file = tmp_file, mkd_tmp_hdl = tmp_hdl })
277 -- write the magic marker into the tmp file
278 hPutStrLn tmp_hdl depEndMarker
284 -- slurp the rest of the original makefile and copy it into the output
290 catchJust ioErrors slurp
291 (\e -> if isEOFError e then return () else ioError e)
295 hClose tmp_hdl -- make sure it's flushed
297 -- Create a backup of the original makefile
298 when (isJust makefile_hdl)
299 (SysTools.copy dflags ("Backing up " ++ makefile)
300 makefile (makefile++".bak"))
302 -- Copy the new makefile in place
303 SysTools.copy dflags "Installing new makefile" tmp_file makefile
306 -----------------------------------------------------------------
310 -----------------------------------------------------------------
313 GLOBAL_VAR(v_Dep_makefile, "Makefile", String);
314 GLOBAL_VAR(v_Dep_include_pkg_deps, False, Bool);
315 GLOBAL_VAR(v_Dep_exclude_mods, [], [Module]);
316 GLOBAL_VAR(v_Dep_suffixes, [], [String]);
317 GLOBAL_VAR(v_Dep_warnings, True, Bool);
319 depStartMarker = "# DO NOT DELETE: Beginning of Haskell dependencies"
320 depEndMarker = "# DO NOT DELETE: End of Haskell dependencies"
322 -- for compatibility with the old mkDependHS, we accept options of the form
323 -- -optdep-f -optdep.depend, etc.
325 [ ( "s", SepArg (consIORef v_Dep_suffixes) )
326 , ( "f", SepArg (writeIORef v_Dep_makefile) )
327 , ( "w", NoArg (writeIORef v_Dep_warnings False) )
328 , ( "-include-prelude", NoArg (writeIORef v_Dep_include_pkg_deps True) )
329 , ( "-include-pkg-deps", NoArg (writeIORef v_Dep_include_pkg_deps True) )
330 , ( "-exclude-module=", Prefix (consIORef v_Dep_exclude_mods . mkModule) )
331 , ( "x", Prefix (consIORef v_Dep_exclude_mods . mkModule) )