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 )
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 )
32 import DATA_IOREF ( IORef, readIORef, writeIORef )
38 import Maybe ( isJust )
40 #if __GLASGOW_HASKELL__ <= 408
41 import Panic ( catchJust, ioErrors )
43 import ErrUtils ( debugTraceMsg )
45 -----------------------------------------------------------------
49 -----------------------------------------------------------------
51 doMkDependHS :: Session -> [FilePath] -> IO ()
52 doMkDependHS session srcs
53 = do { -- Initialisation
54 dflags <- GHC.getSessionDynFlags session
55 ; files <- beginMkDependHS dflags
57 -- Do the downsweep to find all the modules
58 ; targets <- mapM GHC.guessTarget srcs
59 ; GHC.setTargets session targets
60 ; excl_mods <- readIORef v_Dep_exclude_mods
61 ; GHC.depanal session excl_mods
62 ; mod_summaries <- GHC.getModuleGraph session
64 -- Sort into dependency order
65 -- There should be no cycles
66 ; let sorted = GHC.topSortModuleGraph False mod_summaries Nothing
68 -- Print out the dependencies if wanted
69 ; debugTraceMsg dflags 2 (showSDoc (text "Module dependencies" $$ ppr sorted))
71 -- Prcess them one by one, dumping results into makefile
72 -- and complaining about cycles
73 ; mapM (processDeps session (mkd_tmp_hdl files)) sorted
76 ; endMkDependHS dflags files }
78 -----------------------------------------------------------------
81 -- Create a temporary file,
83 -- slurp through it, etc
85 -----------------------------------------------------------------
88 = MkDep { mkd_make_file :: FilePath, -- Name of the makefile
89 mkd_make_hdl :: Maybe Handle, -- Handle for the open makefile
90 mkd_tmp_file :: FilePath, -- Name of the temporary file
91 mkd_tmp_hdl :: Handle } -- Handle of the open temporary file
93 beginMkDependHS :: DynFlags -> IO MkDepFiles
95 beginMkDependHS dflags = do
96 -- slurp in the mkdependHS-style options
97 let flags = getOpts dflags opt_dep
98 _ <- processArgs dep_opts flags
100 -- open a new temp file in which to stuff the dependency info
102 tmp_file <- newTempName dflags "dep"
103 tmp_hdl <- openFile tmp_file WriteMode
106 makefile <- readIORef v_Dep_makefile
107 exists <- doesFileExist makefile
112 makefile_hdl <- openFile makefile ReadMode
114 -- slurp through until we get the magic start string,
115 -- copying the contents into dep_makefile
117 l <- hGetLine makefile_hdl
118 if (l == depStartMarker)
120 else do hPutStrLn tmp_hdl l; slurp
122 -- slurp through until we get the magic end marker,
123 -- throwing away the contents
125 l <- hGetLine makefile_hdl
126 if (l == depEndMarker)
130 catchJust ioErrors slurp
131 (\e -> if isEOFError e then return () else ioError e)
132 catchJust ioErrors chuck
133 (\e -> if isEOFError e then return () else ioError e)
135 return (Just makefile_hdl)
138 -- write the magic marker into the tmp file
139 hPutStrLn tmp_hdl depStartMarker
141 return (MkDep { mkd_make_file = makefile, mkd_make_hdl = mb_make_hdl,
142 mkd_tmp_file = tmp_file, mkd_tmp_hdl = tmp_hdl})
145 -----------------------------------------------------------------
149 -----------------------------------------------------------------
151 processDeps :: Session
152 -> Handle -- Write dependencies to here
155 -- Write suitable dependencies to handle
159 -- If the dependency is on something other than a .hi file:
160 -- this.o this.p_o ... : dep
162 -- this.o ... : dep.hi
163 -- this.p_o ... : dep.p_hi
165 -- (where .o is $osuf, and the other suffixes come from
166 -- the cmdline -s options).
168 -- For {-# SOURCE #-} imports the "hi" will be "hi-boot".
170 processDeps session hdl (CyclicSCC nodes)
171 = -- There shouldn't be any cycles; report them
172 throwDyn (ProgramError (showSDoc $ GHC.cyclicModuleErr nodes))
174 processDeps session hdl (AcyclicSCC node)
175 = do { extra_suffixes <- readIORef v_Dep_suffixes
176 ; hsc_env <- GHC.sessionHscEnv session
177 ; include_pkg_deps <- readIORef v_Dep_include_pkg_deps
178 ; let src_file = msHsFilePath node
179 obj_file = msObjFilePath node
180 obj_files = insertSuffixes obj_file extra_suffixes
182 do_imp is_boot imp_mod
183 = do { mb_hi <- findDependency hsc_env src_file imp_mod
184 is_boot include_pkg_deps
186 Nothing -> return () ;
188 { let hi_files = insertSuffixes hi_file extra_suffixes
189 write_dep (obj,hi) = writeDependency hdl [obj] hi
191 -- Add one dependency for each suffix;
194 ; mapM_ write_dep (obj_files `zip` hi_files) }}}
197 -- Emit std dependency of the object(s) on the source file
198 -- Something like A.o : A.hs
199 ; writeDependency hdl obj_files src_file
201 -- Emit a dependency for each import
202 ; mapM_ (do_imp True) (ms_srcimps node) -- SOURCE imports
203 ; mapM_ (do_imp False) (ms_imps node) -- regular imports
207 findDependency :: HscEnv
208 -> FilePath -- Importing module: used only for error msg
209 -> Module -- Imported module
210 -> IsBootInterface -- Source import
211 -> Bool -- Record dependency on package modules
212 -> IO (Maybe FilePath) -- Interface file file
213 findDependency hsc_env src imp is_boot include_pkg_deps
214 = do { -- Find the module; this will be fast because
215 -- we've done it once during downsweep
216 r <- findModule hsc_env imp True {-explicit-}
219 -- Not in this package: we don't need a dependency
220 | ExtPackage _ <- pkg, not include_pkg_deps
223 -- Home package: just depend on the .hi or hi-boot file
225 -> return (Just (addBootSuffix_maybe is_boot (ml_hi_file loc)))
227 _ -> panic "findDependency"
230 -----------------------------
231 writeDependency :: Handle -> [FilePath] -> FilePath -> IO ()
232 -- (writeDependency h [t1,t2] dep) writes to handle h the dependency
234 writeDependency hdl targets dep
235 = hPutStrLn hdl (unwords (map escapeSpaces targets) ++ " : "
238 -----------------------------
240 :: FilePath -- Original filename; e.g. "foo.o"
241 -> [String] -- Extra suffices e.g. ["x","y"]
242 -> [FilePath] -- Zapped filenames e.g. ["foo.o", "foo.x_o", "foo.y_o"]
243 -- Note that that the extra bit gets inserted *before* the old suffix
244 -- We assume the old suffix contains no dots, so we can strip it with removeSuffix
246 -- NOTE: we used to have this comment
247 -- In order to construct hi files with alternate suffixes, we
248 -- now have to find the "basename" of the hi file. This is
249 -- difficult because we can't just split the hi filename
250 -- at the last dot - the hisuf might have dots in it. So we
251 -- check whether the hi filename ends in hisuf, and if it does,
252 -- we strip off hisuf, otherwise we strip everything after the
254 -- But I'm not sure we care about hisufs with dots in them.
255 -- Lots of other things will break first!
257 insertSuffixes file_name extras
258 = file_name : [ basename ++ "." ++ extra ++ "_" ++ suffix | extra <- extras ]
260 (basename, suffix) = splitFilename file_name
263 -----------------------------------------------------------------
266 -- Complete the makefile, close the tmp file etc
268 -----------------------------------------------------------------
270 endMkDependHS :: DynFlags -> MkDepFiles -> IO ()
273 (MkDep { mkd_make_file = makefile, mkd_make_hdl = makefile_hdl,
274 mkd_tmp_file = tmp_file, mkd_tmp_hdl = tmp_hdl })
276 -- write the magic marker into the tmp file
277 hPutStrLn tmp_hdl depEndMarker
283 -- slurp the rest of the original makefile and copy it into the output
289 catchJust ioErrors slurp
290 (\e -> if isEOFError e then return () else ioError e)
294 hClose tmp_hdl -- make sure it's flushed
296 -- Create a backup of the original makefile
297 when (isJust makefile_hdl)
298 (SysTools.copy dflags ("Backing up " ++ makefile)
299 makefile (makefile++".bak"))
301 -- Copy the new makefile in place
302 SysTools.copy dflags "Installing new makefile" tmp_file makefile
305 -----------------------------------------------------------------
309 -----------------------------------------------------------------
312 GLOBAL_VAR(v_Dep_makefile, "Makefile", String);
313 GLOBAL_VAR(v_Dep_include_pkg_deps, False, Bool);
314 GLOBAL_VAR(v_Dep_exclude_mods, [], [Module]);
315 GLOBAL_VAR(v_Dep_suffixes, [], [String]);
316 GLOBAL_VAR(v_Dep_warnings, True, Bool);
318 depStartMarker = "# DO NOT DELETE: Beginning of Haskell dependencies"
319 depEndMarker = "# DO NOT DELETE: End of Haskell dependencies"
321 -- for compatibility with the old mkDependHS, we accept options of the form
322 -- -optdep-f -optdep.depend, etc.
324 [ ( "s", SepArg (consIORef v_Dep_suffixes) )
325 , ( "f", SepArg (writeIORef v_Dep_makefile) )
326 , ( "w", NoArg (writeIORef v_Dep_warnings False) )
327 , ( "-include-prelude", NoArg (writeIORef v_Dep_include_pkg_deps True) )
328 , ( "-include-pkg-deps", NoArg (writeIORef v_Dep_include_pkg_deps True) )
329 , ( "-exclude-module=", Prefix (consIORef v_Dep_exclude_mods . mkModule) )
330 , ( "x", Prefix (consIORef v_Dep_exclude_mods . mkModule) )