2 % (c) The University of Glasgow, 2000
4 \section[CompManager]{The Compilation Manager}
7 module CompManager ( cmInit, cmLoadModule,
9 CmState, emptyCmState, -- abstract
14 #include "HsVersions.h"
17 import Maybe ( catMaybes, fromMaybe )
18 import Maybes ( maybeToBool )
20 import UniqFM ( emptyUFM, lookupUFM, addToUFM, delListFromUFM,
22 import Unique ( Uniquable )
23 import Digraph ( SCC(..), stronglyConnComp )
28 import Interpreter ( HValue )
29 import Module ( ModuleName, moduleName,
30 isModuleInThisPackage, moduleEnvElts,
31 moduleNameUserString )
32 import CmStaticInfo ( PackageConfigInfo, GhciMode(..) )
35 import HscTypes ( HomeSymbolTable, HomeIfaceTable,
36 PersistentCompilerState, ModDetails(..) )
37 import Name ( lookupNameEnv )
40 import PrelNames ( mainName )
41 import HscMain ( initPersistentCompilerState )
42 import Finder ( findModule, emptyHomeDirCache )
43 import DriverUtil ( BarfKind(..), splitFilename3 )
45 import Panic ( panic )
47 import Exception ( throwDyn )
49 import Time ( ClockTime )
50 import Directory ( getModificationTime, doesFileExist )
57 cmInit :: PackageConfigInfo -> GhciMode -> IO CmState
58 cmInit raw_package_info gmode
59 = emptyCmState raw_package_info gmode
64 -> IO (CmState, Either [SDoc] HValue)
65 cmGetExpr cmstate modhdl expr
66 = return (panic "cmGetExpr:unimp")
68 cmRunExpr :: HValue -> IO ()
70 = return (panic "cmRunExpr:unimp")
73 -- Persistent state just for CM, excluding link & compile subsystems
74 data PersistentCMState
76 hst :: HomeSymbolTable, -- home symbol table
77 hit :: HomeIfaceTable, -- home interface table
78 ui :: UnlinkedImage, -- the unlinked images
79 mg :: ModuleGraph, -- the module graph
80 pci :: PackageConfigInfo, -- NEVER CHANGES
81 gmode :: GhciMode -- NEVER CHANGES
84 emptyPCMS :: PackageConfigInfo -> GhciMode -> PersistentCMState
86 = PersistentCMState { hst = emptyHST, hit = emptyHIT,
87 ui = emptyUI, mg = emptyMG,
88 pci = pci, gmode = gmode }
90 emptyHIT :: HomeIfaceTable
92 emptyHST :: HomeSymbolTable
97 -- Persistent state for the entire system
100 pcms :: PersistentCMState, -- CM's persistent state
101 pcs :: PersistentCompilerState, -- compile's persistent state
102 pls :: PersistentLinkerState -- link's persistent state
105 emptyCmState :: PackageConfigInfo -> GhciMode -> IO CmState
106 emptyCmState pci gmode
107 = do let pcms = emptyPCMS pci gmode
108 pcs <- initPersistentCompilerState
110 return (CmState { pcms = pcms,
115 type UnlinkedImage = [Linkable] -- the unlinked images (should be a set, really)
116 emptyUI :: UnlinkedImage
119 type ModuleGraph = [ModSummary] -- the module graph, topologically sorted
120 emptyMG :: ModuleGraph
125 The real business of the compilation manager: given a system state and
126 a module name, try and bring the module up to date, probably changing
127 the system state at the same time.
130 cmLoadModule :: CmState
132 -> IO (CmState, Maybe ModuleName)
134 cmLoadModule cmstate1 rootname
135 = do -- version 1's are the original, before downsweep
136 let pcms1 = pcms cmstate1
137 let pls1 = pls cmstate1
138 let pcs1 = pcs cmstate1
144 let pcii = pci pcms1 -- this never changes
145 let ghci_mode = gmode pcms1 -- ToDo: fix!
147 -- During upsweep, look at new summaries to see if source has
148 -- changed. Here's a function to pass down; it takes a new
150 let source_changed :: ModSummary -> Bool
151 source_changed = summary_indicates_source_changed mg1
153 -- Do the downsweep to reestablish the module graph
154 -- then generate version 2's by removing from HIT,HST,UI any
155 -- modules in the old MG which are not in the new one.
157 -- Throw away the old home dir cache
160 hPutStr stderr "cmLoadModule: downsweep begins\n"
161 mg2unsorted <- downsweep [rootname]
163 let modnames1 = map name_of_summary mg1
164 let modnames2 = map name_of_summary mg2unsorted
165 let mods_to_zap = filter (`notElem` modnames2) modnames1
167 let (hst2, hit2, ui2)
168 = removeFromTopLevelEnvs mods_to_zap (hst1, hit1, ui1)
169 -- should be cycle free; ignores 'import source's
170 let mg2 = topological_sort False mg2unsorted
171 -- ... whereas this takes them into account. Used for
172 -- backing out partially complete cycles following a failed
173 -- upsweep, and for removing from hst/hit all the modules
174 -- not in strict downwards closure, during calls to compile.
175 let mg2_with_srcimps = topological_sort True mg2unsorted
177 let reachable_from :: ModuleName -> [ModuleName]
178 reachable_from = downwards_closure_of_module mg2unsorted
180 hPutStrLn stderr "after tsort:\n"
181 hPutStrLn stderr (showSDoc (vcat (map ppr mg2)))
183 -- Because we don't take into account source imports when doing
184 -- the topological sort, there shouldn't be any cycles in mg2.
185 -- If there is, we complain and give up -- the user needs to
186 -- break the cycle using a boot file.
188 -- Now do the upsweep, calling compile for each module in
189 -- turn. Final result is version 3 of everything.
191 let threaded2 = CmThreaded pcs1 hst2 hit2
193 (upsweep_complete_success, threaded3, modsDone, newLis)
194 <- upsweep_mods ghci_mode ui2 reachable_from source_changed threaded2 mg2
196 let ui3 = add_to_ui ui2 newLis
197 let (CmThreaded pcs3 hst3 hit3) = threaded3
199 -- At this point, modsDone and newLis should have the same
200 -- length, so there is one new (or old) linkable for each
201 -- mod which was processed (passed to compile).
203 -- Try and do linking in some form, depending on whether the
204 -- upsweep was completely or only partially successful.
206 if upsweep_complete_success
209 -- Easy; just relink it all.
210 do hPutStrLn stderr "UPSWEEP COMPLETELY SUCCESSFUL"
212 <- link ghci_mode (any exports_main (moduleEnvElts hst3))
216 -> panic "cmLoadModule: link failed (1)"
218 -> do let pcms3 = PersistentCMState { hst=hst3, hit=hit3,
220 pci=pcii, gmode=ghci_mode }
222 = CmState { pcms=pcms3, pcs=pcs3, pls=pls3 }
223 return (cmstate3, Just rootname)
226 -- Tricky. We need to back out the effects of compiling any
227 -- half-done cycles, both so as to clean up the top level envs
228 -- and to avoid telling the interactive linker to link them.
229 do hPutStrLn stderr "UPSWEEP PARTIALLY SUCCESSFUL"
232 = map name_of_summary modsDone
233 let mods_to_zap_names
234 = findPartiallyCompletedCycles modsDone_names mg2_with_srcimps
235 let (hst4, hit4, ui4)
236 = removeFromTopLevelEnvs mods_to_zap_names (hst3,hit3,ui3)
238 = filter ((`notElem` mods_to_zap_names).name_of_summary) modsDone
239 let mods_to_keep_names
240 = map name_of_summary mods_to_keep
241 -- we could get the relevant linkables by filtering newLis, but
242 -- it seems easier to drag them out of the updated, cleaned-up UI
243 let linkables_to_link
244 = map (findModuleLinkable ui4) mods_to_keep_names
246 linkresult <- link ghci_mode False linkables_to_link pls1
249 -> panic "cmLoadModule: link failed (2)"
251 -> do let pcms4 = PersistentCMState { hst=hst4, hit=hit4,
252 ui=ui4, mg=mods_to_keep,
253 pci=pcii, gmode=ghci_mode }
255 = CmState { pcms=pcms4, pcs=pcs3, pls=pls4 }
257 -- choose rather arbitrarily who to return
258 if null mods_to_keep then Nothing
259 else Just (last mods_to_keep_names))
262 -- Given a bunch of old summaries and a new summary, try and
263 -- find the corresponding old summary, and, if found, compare
264 -- its source timestamp with that of the new summary. If in
266 summary_indicates_source_changed :: [ModSummary] -> ModSummary -> Bool
267 summary_indicates_source_changed old_summaries new_summary
268 = case [old | old <- old_summaries,
269 name_of_summary old == name_of_summary new_summary] of
271 (_:_:_) -> panic "summary_indicates_newer_source"
273 [] -> -- can't find a corresponding old summary, so
274 -- compare source and iface dates in the new summary.
275 trace (showSDoc (text "SISC: no old summary, new ="
276 <+> pprSummaryTimes new_summary)) (
277 case (ms_hs_date new_summary, ms_hi_date new_summary) of
278 (Just hs_t, Just hi_t) -> hs_t > hi_t
282 [old] -> -- found old summary; compare source timestamps
283 trace (showSDoc (text "SISC: old ="
284 <+> pprSummaryTimes old
285 <+> pprSummaryTimes new_summary)) (
286 case (ms_hs_date old, ms_hs_date new_summary) of
287 (Just old_t, Just new_t) -> new_t > old_t
291 -- Return (names of) all those in modsDone who are part of a cycle
292 -- as defined by theGraph.
293 findPartiallyCompletedCycles :: [ModuleName] -> [SCC ModSummary] -> [ModuleName]
294 findPartiallyCompletedCycles modsDone theGraph
298 chew ((AcyclicSCC v):rest) = chew rest -- acyclic? not interesting.
299 chew ((CyclicSCC vs):rest)
300 = let names_in_this_cycle = nub (map name_of_summary vs)
302 = nub ([done | done <- modsDone,
303 done `elem` names_in_this_cycle])
304 chewed_rest = chew rest
306 if not (null mods_in_this_cycle)
307 && length mods_in_this_cycle < length names_in_this_cycle
308 then mods_in_this_cycle ++ chewed_rest
312 -- Does this ModDetails export Main.main?
313 exports_main :: ModDetails -> Bool
315 = maybeToBool (lookupNameEnv (md_types md) mainName)
318 -- Add the given (LM-form) Linkables to the UI, overwriting previous
319 -- versions if they exist.
320 add_to_ui :: UnlinkedImage -> [Linkable] -> UnlinkedImage
324 add1 :: Linkable -> UnlinkedImage -> UnlinkedImage
326 = li : filter (\li2 -> not (for_same_module li li2)) ui
328 for_same_module :: Linkable -> Linkable -> Bool
329 for_same_module li1 li2
330 = not (is_package_linkable li1)
331 && not (is_package_linkable li2)
332 && modname_of_linkable li1 == modname_of_linkable li2
335 data CmThreaded -- stuff threaded through individual module compilations
336 = CmThreaded PersistentCompilerState HomeSymbolTable HomeIfaceTable
339 -- Compile multiple modules, stopping as soon as an error appears.
340 -- There better had not be any cyclic groups here -- we check for them.
341 upsweep_mods :: GhciMode
342 -> UnlinkedImage -- old linkables
343 -> (ModuleName -> [ModuleName]) -- to construct downward closures
344 -> (ModSummary -> Bool) -- has source changed?
345 -> CmThreaded -- PCS & HST & HIT
346 -> [SCC ModSummary] -- mods to do (the worklist)
347 -- ...... RETURNING ......
348 -> IO (Bool{-complete success?-},
350 [ModSummary], -- mods which succeeded
351 [Linkable]) -- new linkables
353 upsweep_mods ghci_mode oldUI reachable_from source_changed threaded []
354 = return (True, threaded, [], [])
356 upsweep_mods ghci_mode oldUI reachable_from source_changed threaded ((CyclicSCC ms):_)
357 = do hPutStrLn stderr ("ghc: module imports form a cycle for modules:\n\t" ++
358 unwords (map (moduleNameUserString.name_of_summary) ms))
359 return (False, threaded, [], [])
361 upsweep_mods ghci_mode oldUI reachable_from source_changed threaded ((AcyclicSCC mod):mods)
362 = do (threaded1, maybe_linkable)
363 <- upsweep_mod ghci_mode oldUI threaded mod
364 (reachable_from (name_of_summary mod))
366 case maybe_linkable of
368 -> -- No errors; do the rest
369 do (restOK, threaded2, modOKs, linkables)
370 <- upsweep_mods ghci_mode oldUI reachable_from source_changed threaded1 mods
371 return (restOK, threaded2, mod:modOKs, linkable:linkables)
372 Nothing -- we got a compilation error; give up now
373 -> return (False, threaded1, [], [])
376 -- Compile a single module. Always produce a Linkable for it if
377 -- successful. If no compilation happened, return the old Linkable.
378 upsweep_mod :: GhciMode
384 -> IO (CmThreaded, Maybe Linkable)
386 upsweep_mod ghci_mode oldUI threaded1 summary1
387 reachable_from_here source_might_have_changed
388 = do let mod_name = name_of_summary summary1
389 let (CmThreaded pcs1 hst1 hit1) = threaded1
390 let old_iface = lookupUFM hit1 (name_of_summary summary1)
392 -- We *have* to compile it if we're in batch mode and we can't see
393 -- a previous linkable for it on disk.
394 compilation_mandatory
395 <- if ghci_mode /= Batch then return False
396 else case ml_obj_file (ms_location summary1) of
397 Nothing -> do --putStrLn "cmcm: object?!"
399 Just obj_fn -> do --putStrLn ("cmcm: old obj " ++ obj_fn)
400 b <- doesFileExist obj_fn
403 let compilation_might_be_needed
404 = source_might_have_changed || compilation_mandatory
406 = not compilation_might_be_needed
407 (hst1_strictDC, hit1_strictDC)
408 = retainInTopLevelEnvs reachable_from_here (hst1,hit1)
410 compresult <- compile ghci_mode summary1 source_unchanged
411 old_iface hst1_strictDC hit1_strictDC pcs1
413 --putStrLn ( "UPSWEEP_MOD: smhc = " ++ show source_might_have_changed
414 -- ++ ", cman = " ++ show compilation_mandatory)
418 -- Compilation "succeeded", but didn't return a new iface or
419 -- linkable, meaning that compilation wasn't needed, and the
420 -- new details were manufactured from the old iface.
421 CompOK details Nothing pcs2
422 -> let hst2 = addToUFM hst1 mod_name details
424 threaded2 = CmThreaded pcs2 hst2 hit2
426 | ghci_mode == Interactive
427 = findModuleLinkable oldUI mod_name
430 [DotO (unJust (ml_obj_file (ms_location summary1))
432 in return (threaded2, Just old_linkable)
434 -- Compilation really did happen, and succeeded. A new
435 -- details, iface and linkable are returned.
436 CompOK details (Just (new_iface, new_linkable)) pcs2
437 -> let hst2 = addToUFM hst1 mod_name details
438 hit2 = addToUFM hit1 mod_name new_iface
439 threaded2 = CmThreaded pcs2 hst2 hit2
440 in return (threaded2, Just new_linkable)
442 -- Compilation failed. compile may still have updated
445 -> let threaded2 = CmThreaded pcs2 hst1 hit1
446 in return (threaded2, Nothing)
449 -- Remove unwanted modules from the top level envs (HST, HIT, UI).
450 removeFromTopLevelEnvs :: [ModuleName]
451 -> (HomeSymbolTable, HomeIfaceTable, UnlinkedImage)
452 -> (HomeSymbolTable, HomeIfaceTable, UnlinkedImage)
453 removeFromTopLevelEnvs zap_these (hst, hit, ui)
454 = (delListFromUFM hst zap_these,
455 delListFromUFM hit zap_these,
456 filterModuleLinkables (`notElem` zap_these) ui
459 retainInTopLevelEnvs :: [ModuleName]
460 -> (HomeSymbolTable, HomeIfaceTable)
461 -> (HomeSymbolTable, HomeIfaceTable)
462 retainInTopLevelEnvs keep_these (hst, hit)
463 = (retainInUFM hst keep_these,
464 retainInUFM hit keep_these
467 retainInUFM :: Uniquable key => UniqFM elt -> [key] -> UniqFM elt
468 retainInUFM ufm keys_to_keep
469 = listToUFM (concatMap (maybeLookupUFM ufm) keys_to_keep)
471 = case lookupUFM ufm u of Nothing -> []; Just val -> [(u, val)]
473 -- Needed to clean up HIT and HST so that we don't get duplicates in inst env
474 downwards_closure_of_module :: [ModSummary] -> ModuleName -> [ModuleName]
475 downwards_closure_of_module summaries root
476 = let toEdge :: ModSummary -> (ModuleName,[ModuleName])
478 = (name_of_summary summ, ms_srcimps summ ++ ms_imps summ)
479 res = simple_transitive_closure (map toEdge summaries) [root]
481 trace (showSDoc (text "DC of mod" <+> ppr root
482 <+> text "=" <+> ppr res)) (
486 -- Calculate transitive closures from a set of roots given an adjacency list
487 simple_transitive_closure :: Eq a => [(a,[a])] -> [a] -> [a]
488 simple_transitive_closure graph set
489 = let set2 = nub (concatMap dsts set ++ set)
490 dsts node = fromMaybe [] (lookup node graph)
492 if length set == length set2
494 else simple_transitive_closure graph set2
497 -- Calculate SCCs of the module graph, with or without taking into
498 -- account source imports.
499 topological_sort :: Bool -> [ModSummary] -> [SCC ModSummary]
500 topological_sort include_source_imports summaries
502 toEdge :: ModSummary -> (ModSummary,ModuleName,[ModuleName])
504 = (summ, name_of_summary summ,
505 (if include_source_imports
506 then ms_srcimps summ else []) ++ ms_imps summ)
508 mash_edge :: (ModSummary,ModuleName,[ModuleName]) -> (ModSummary,Int,[Int])
509 mash_edge (summ, m, m_imports)
510 = case lookup m key_map of
511 Nothing -> panic "reverse_topological_sort"
512 Just mk -> (summ, mk,
513 -- ignore imports not from the home package
514 catMaybes (map (flip lookup key_map) m_imports))
516 edges = map toEdge summaries
517 key_map = zip [nm | (s,nm,imps) <- edges] [1 ..] :: [(ModuleName,Int)]
518 scc_input = map mash_edge edges
519 sccs = stronglyConnComp scc_input
524 -- Chase downwards from the specified root set, returning summaries
525 -- for all home modules encountered. Only follow source-import
527 downsweep :: [ModuleName] -> IO [ModSummary]
529 = do rootSummaries <- mapM getSummary rootNm
530 loop (filter (isModuleInThisPackage.ms_mod) rootSummaries)
532 getSummary :: ModuleName -> IO ModSummary
534 | trace ("getSummary: "++ showSDoc (ppr nm)) True
535 = do found <- findModule nm
537 -- Be sure not to use the mod and location passed in to
538 -- summarise for any other purpose -- summarise may change
539 -- the module names in them if name of module /= name of file,
540 -- and put the changed versions in the returned summary.
541 -- These will then conflict with the passed-in versions.
542 Just (mod, location) -> summarise mod location
543 Nothing -> throwDyn (OtherError
544 ("no signs of life for module `"
545 ++ showSDoc (ppr nm) ++ "'"))
547 -- loop invariant: homeSummaries doesn't contain package modules
548 loop :: [ModSummary] -> IO [ModSummary]
550 = do let allImps :: [ModuleName]
551 allImps = (nub . concatMap ms_imps) homeSummaries
552 let allHome -- all modules currently in homeSummaries
553 = map (moduleName.ms_mod) homeSummaries
555 = filter (`notElem` allHome) allImps
557 <- mapM getSummary neededImps
559 = filter (isModuleInThisPackage.ms_mod) neededSummaries
560 if null newHomeSummaries
561 then return homeSummaries
562 else loop (newHomeSummaries ++ homeSummaries)
565 -- Summarise a module, and pick up source and interface timestamps.
566 summarise :: Module -> ModuleLocation -> IO ModSummary
567 summarise mod location
568 | isModuleInThisPackage mod
569 = do let hs_fn = unJust (ml_hs_file location) "summarise"
570 hspp_fn <- preprocess hs_fn
571 modsrc <- readFile hspp_fn
572 let (srcimps,imps,mod_name) = getImports modsrc
575 <- case ml_hs_file location of
576 Nothing -> return Nothing
577 Just src_fn -> maybe_getModificationTime src_fn
578 maybe_iface_timestamp
579 <- case ml_hi_file location of
580 Nothing -> return Nothing
581 Just if_fn -> maybe_getModificationTime if_fn
583 -- If the module name is Main, allow it to be in a file
584 -- different from Main.hs, and mash the mod and loc
585 -- to match. Otherwise just moan.
586 (mashed_mod, mashed_loc)
588 () | mod_name == moduleName mod
589 -> return (mod, location)
590 | mod_name /= moduleName mod && mod_name == mkModuleName "Main"
591 -> return (mash mod location "Main")
593 -> do hPutStrLn stderr (showSDoc (
594 text "ghc: warning: file name - module name mismatch:" <+>
595 ppr (moduleName mod) <+> text "vs" <+> ppr mod_name))
596 return (mash mod location (moduleNameUserString (moduleName mod)))
598 mash old_mod old_loc new_nm
599 = (mkHomeModule (mkModuleName new_nm),
600 old_loc{ml_hi_file = maybe_swizzle_basename new_nm
601 (ml_hi_file old_loc)})
603 maybe_swizzle_basename new Nothing = Nothing
604 maybe_swizzle_basename new (Just old)
605 = case splitFilename3 old of
606 (dir, name, ext) -> Just (dir ++ new ++ ext)
608 return (ModSummary mashed_mod
609 mashed_loc{ml_hspp_file=Just hspp_fn}
611 maybe_src_timestamp maybe_iface_timestamp)
614 = return (ModSummary mod location [] [] Nothing Nothing)
617 maybe_getModificationTime :: FilePath -> IO (Maybe ClockTime)
618 maybe_getModificationTime fn
619 = (do time <- getModificationTime fn
622 (\err -> return Nothing)
624 cmLookupSymbol :: RdrName -> CmState -> Maybe HValue
625 cmLookupSymbol nm CmState{ pls = pls } = lookupClosure nm pls