+ -- -no-recomp should also work with --make
+ do_recomp <- readIORef v_Recomp
+ let source_unchanged' = source_unchanged && do_recomp
+ hsc_env' = hsc_env { hsc_dflags = dyn_flags' }
+
+ -- run the compiler
+ hsc_result <- hscMain hsc_env' printErrorsAndWarnings this_mod location
+ source_unchanged' have_object old_iface
+
+ case hsc_result of
+ HscFail -> return CompErrs
+
+ HscNoRecomp details iface -> return (CompOK details Nothing iface Nothing)
+
+ HscRecomp details rdr_env iface
+ stub_h_exists stub_c_exists maybe_interpreted_code -> do
+ let
+ maybe_stub_o <- compileStub dyn_flags' stub_c_exists
+ let stub_unlinked = case maybe_stub_o of
+ Nothing -> []
+ Just stub_o -> [ DotO stub_o ]
+
+ (hs_unlinked, unlinked_time) <-
+ case hsc_lang of
+
+ -- in interpreted mode, just return the compiled code
+ -- as our "unlinked" object.
+ HscInterpreted ->
+ case maybe_interpreted_code of
+#ifdef GHCI
+ Just comp_bc -> return ([BCOs comp_bc], src_timestamp)
+ -- Why do we use the timestamp of the source file here,
+ -- rather than the current time? This works better in
+ -- the case where the local clock is out of sync
+ -- with the filesystem's clock. It's just as accurate:
+ -- if the source is modified, then the linkable will
+ -- be out of date.
+#endif
+ Nothing -> panic "compile: no interpreted code"
+
+ -- we're in batch mode: finish the compilation pipeline.
+ _other -> do
+ let object_filename = ml_obj_file location
+
+ runPipeline (StopBefore Ln) ""
+ True Nothing output_fn (Just location)
+ -- the object filename comes from the ModLocation
+
+ o_time <- getModificationTime object_filename
+ return ([DotO object_filename], o_time)
+
+ let linkable = LM unlinked_time mod_name
+ (hs_unlinked ++ stub_unlinked)
+
+ return (CompOK details rdr_env iface (Just linkable))
+
+-----------------------------------------------------------------------------
+-- stub .h and .c files (for foreign export support)
+
+compileStub dflags stub_c_exists
+ | not stub_c_exists = return Nothing
+ | stub_c_exists = do
+ -- compile the _stub.c file w/ gcc
+ let stub_c = hscStubCOutName dflags
+ stub_o <- runPipeline (StopBefore Ln) "stub-compile"
+ True{-persistent output-}
+ Nothing{-no specific output file-}
+ stub_c
+ Nothing{-no ModLocation-}
+ return (Just stub_o)
+
+
+-- ---------------------------------------------------------------------------
+-- Link
+
+link :: GhciMode -- interactive or batch
+ -> DynFlags -- dynamic flags
+ -> Bool -- attempt linking in batch mode?
+ -> HomePackageTable -- what to link
+ -> IO SuccessFlag
+
+-- For the moment, in the batch linker, we don't bother to tell doLink
+-- which packages to link -- it just tries all that are available.
+-- batch_attempt_linking should only be *looked at* in batch mode. It
+-- should only be True if the upsweep was successful and someone
+-- exports main, i.e., we have good reason to believe that linking
+-- will succeed.
+
+#ifdef GHCI
+link Interactive dflags batch_attempt_linking hpt
+ = do -- Not Linking...(demand linker will do the job)
+ return Succeeded
+#endif
+
+link Batch dflags batch_attempt_linking hpt
+ | batch_attempt_linking
+ = do
+ let
+ home_mod_infos = moduleEnvElts hpt
+
+ -- the packages we depend on
+ pkg_deps = concatMap (dep_pkgs . mi_deps . hm_iface) home_mod_infos
+
+ -- the linkables to link
+ linkables = map hm_linkable home_mod_infos
+
+ when (verb >= 3) $ do
+ hPutStrLn stderr "link: linkables are ..."
+ hPutStrLn stderr (showSDoc (vcat (map ppr linkables)))
+
+ -- check for the -no-link flag
+ omit_linking <- readIORef v_NoLink
+ if omit_linking
+ then do when (verb >= 3) $
+ hPutStrLn stderr "link(batch): linking omitted (-no-link flag given)."
+ return Succeeded
+ else do
+
+ when (verb >= 1) $
+ hPutStrLn stderr "Linking ..."
+
+ let getOfiles (LM _ _ us) = map nameOfObject (filter isObject us)
+ obj_files = concatMap getOfiles linkables
+
+ -- Don't showPass in Batch mode; doLink will do that for us.
+ staticLink obj_files pkg_deps
+
+ when (verb >= 3) (hPutStrLn stderr "link: done")
+
+ -- staticLink only returns if it succeeds
+ return Succeeded
+
+ | otherwise
+ = do when (verb >= 3) $ do
+ hPutStrLn stderr "link(batch): upsweep (partially) failed OR"
+ hPutStrLn stderr " Main.main not exported; not linking."
+ return Succeeded
+ where
+ verb = verbosity dflags
+
+-- ---------------------------------------------------------------------------
+-- Run a compilation pipeline, consisting of multiple phases.
+
+runPipeline
+ :: GhcMode -- when to stop
+ -> String -- "stop after" flag
+ -> Bool -- final output is persistent?
+ -> Maybe FilePath -- where to put the output, optionally
+ -> FilePath -- input filename
+ -> Maybe ModLocation -- a ModLocation for this module, if we have one
+ -> IO FilePath -- output filename
+
+runPipeline todo stop_flag keep_output maybe_output_filename input_fn maybe_loc
+ = do
+ split <- readIORef v_Split_object_files
+ let (basename, suffix) = splitFilename input_fn
+ start_phase = startPhase suffix