+ LinkOK pls3
+ -> do let cmstate4
+ = CmState { hst=hst4, hit=hit4,
+ ui=ui4, mg=mods_to_keep,
+ gmode=ghci_mode, pcs=pcs3, pls=pls3 }
+ return (cmstate4, False,
+ map ms_mod mods_to_keep)
+
+
+-----------------------------------------------------------------------------
+-- getValidLinkables
+
+-- For each module (or SCC of modules), we take:
+--
+-- - the old in-core linkable, if available
+-- - an on-disk linkable, if available
+--
+-- and we take the youngest of these, provided it is younger than the
+-- source file. We ignore the on-disk linkables unless all of the
+-- dependents of this SCC also have on-disk linkables.
+--
+-- If a module has a valid linkable, then it may be STABLE (see below),
+-- and it is classified as SOURCE UNCHANGED for the purposes of calling
+-- compile.
+--
+-- ToDo: this pass could be merged with the preUpsweep.
+
+getValidLinkables
+ :: [Linkable] -- old linkables
+ -> [ModuleName] -- all home modules
+ -> [SCC ModSummary] -- all modules in the program, dependency order
+ -> IO [Linkable] -- still-valid linkables
+
+getValidLinkables old_linkables all_home_mods module_graph
+ = foldM (getValidLinkablesSCC old_linkables all_home_mods) [] module_graph
+
+getValidLinkablesSCC old_linkables all_home_mods new_linkables scc0
+ = let
+ scc = flattenSCC scc0
+ scc_names = map name_of_summary scc
+ home_module m = m `elem` all_home_mods && m `notElem` scc_names
+ scc_allhomeimps = nub (filter home_module (concatMap ms_allimps scc))
+
+ has_object m = case findModuleLinkable_maybe new_linkables m of
+ Nothing -> False
+ Just l -> isObjectLinkable l
+
+ objects_allowed = all has_object scc_allhomeimps
+ in do
+
+ these_linkables
+ <- foldM (getValidLinkable old_linkables objects_allowed) [] scc
+
+ -- since an scc can contain only all objects or no objects at all,
+ -- we have to check whether we got all objects or not, and re-do
+ -- the linkable check if not.
+ adjusted_linkables
+ <- if objects_allowed && not (all isObjectLinkable these_linkables)
+ then foldM (getValidLinkable old_linkables False) [] scc
+ else return these_linkables
+
+ return (adjusted_linkables ++ new_linkables)
+
+
+getValidLinkable :: [Linkable] -> Bool -> [Linkable] -> ModSummary
+ -> IO [Linkable]
+getValidLinkable old_linkables objects_allowed new_linkables summary
+ = do
+ let mod_name = name_of_summary summary
+
+ maybe_disk_linkable
+ <- if (not objects_allowed)
+ then return Nothing
+ else case ml_obj_file (ms_location summary) of
+ Just obj_fn -> maybe_getFileLinkable mod_name obj_fn
+ Nothing -> return Nothing
+
+ -- find an old in-core linkable if we have one. (forget about
+ -- on-disk linkables for now, we'll check again whether there's
+ -- one here below, just in case a new one has popped up recently).
+ let old_linkable = findModuleLinkable_maybe old_linkables mod_name
+ maybe_old_linkable =
+ case old_linkable of
+ Just (LM _ _ ls) | all isInterpretable ls -> old_linkable
+ _ -> Nothing
+
+ -- The most recent of the old UI linkable or whatever we could
+ -- find on disk is returned as the linkable if compile
+ -- doesn't think we need to recompile.
+ let linkable_list
+ = case (maybe_old_linkable, maybe_disk_linkable) of
+ (Nothing, Nothing) -> []
+ (Nothing, Just di) -> [di]
+ (Just ui, Nothing) -> [ui]
+ (Just ui, Just di)
+ | linkableTime ui >= linkableTime di -> [ui]
+ | otherwise -> [di]
+
+ -- only linkables newer than the source code are valid
+ let maybe_src_date = ms_hs_date summary
+
+ valid_linkable_list
+ = case maybe_src_date of
+ Nothing -> panic "valid_linkable_list"
+ Just src_date
+ -> filter (\li -> linkableTime li > src_date) linkable_list
+
+ return (valid_linkable_list ++ new_linkables)
+
+
+maybe_getFileLinkable :: ModuleName -> FilePath -> IO (Maybe Linkable)
+maybe_getFileLinkable mod_name obj_fn
+ = do obj_exist <- doesFileExist obj_fn
+ if not obj_exist
+ then return Nothing
+ else
+ do let stub_fn = case splitFilename3 obj_fn of
+ (dir, base, ext) -> dir ++ "/" ++ base ++ ".stub_o"
+ stub_exist <- doesFileExist stub_fn
+ obj_time <- getModificationTime obj_fn
+ if stub_exist
+ then return (Just (LM obj_time mod_name [DotO obj_fn, DotO stub_fn]))
+ else return (Just (LM obj_time mod_name [DotO obj_fn]))