+-- This is the interface between the compilation manager and the
+-- compiler proper (hsc), where we deal with tedious details like
+-- reading the OPTIONS pragma from the source file, and passing the
+-- output of hsc through the C compiler.
+
+-- The driver sits between 'compile' and 'hscMain', translating calls
+-- to the former into calls to the latter, and results from the latter
+-- into results from the former. It does things like preprocessing
+-- the .hs file if necessary, and compiling up the .stub_c files to
+-- generate Linkables.
+
+-- NB. No old interface can also mean that the source has changed.
+
+compile :: GhciMode -- distinguish batch from interactive
+ -> Module
+ -> ModLocation
+ -> Bool -- True <=> source unchanged
+ -> Bool -- True <=> have object
+ -> Maybe ModIface -- old interface, if available
+ -> HomePackageTable -- For home-module stuff
+ -> PersistentCompilerState -- persistent compiler state
+ -> IO CompResult
+
+data CompResult
+ = CompOK PersistentCompilerState -- Updated PCS
+ ModDetails -- New details
+ ModIface -- New iface
+ (Maybe Linkable) -- New code; Nothing => compilation was not reqd
+ -- (old code is still valid)
+
+ | CompErrs PersistentCompilerState -- Updated PCS
+
+
+compile ghci_mode this_mod location
+ source_unchanged have_object
+ old_iface hpt pcs = do
+
+ dyn_flags <- restoreDynFlags -- Restore to the state of the last save
+
+ showPass dyn_flags
+ (showSDoc (text "Compiling" <+> ppr this_mod))
+
+ let verb = verbosity dyn_flags
+ let input_fn = expectJust "compile:hs" (ml_hs_file location)
+ let input_fnpp = expectJust "compile:hspp" (ml_hspp_file location)
+ let mod_name = moduleName this_mod
+
+ when (verb >= 2) (hPutStrLn stderr ("compile: input file " ++ input_fnpp))
+
+ opts <- getOptionsFromSource input_fnpp
+ processArgs dynamic_flags opts []
+ dyn_flags <- getDynFlags
+
+ let hsc_lang = hscLang dyn_flags
+ (basename, _) = splitFilename input_fn
+
+ keep_hc <- readIORef v_Keep_hc_files
+#ifdef ILX
+ keep_il <- readIORef v_Keep_il_files
+#endif
+ keep_s <- readIORef v_Keep_s_files
+
+ output_fn <-
+ case hsc_lang of
+ HscAsm | keep_s -> return (basename ++ '.':phaseInputExt As)
+ | otherwise -> newTempName (phaseInputExt As)
+ HscC | keep_hc -> return (basename ++ '.':phaseInputExt HCc)
+ | otherwise -> newTempName (phaseInputExt HCc)
+ HscJava -> newTempName "java" -- ToDo
+#ifdef ILX
+ HscILX | keep_il -> return (basename ++ '.':phaseInputExt Ilasm)
+ | otherwise -> newTempName (phaseInputExt Ilx2Il)
+#endif
+ HscInterpreted -> return (error "no output file")
+ HscNothing -> return (error "no output file")
+
+ let dyn_flags' = dyn_flags { hscOutName = output_fn,
+ hscStubCOutName = basename ++ "_stub.c",
+ hscStubHOutName = basename ++ "_stub.h",
+ extCoreName = basename ++ ".hcr" }
+
+ -- -no-recomp should also work with --make
+ do_recomp <- readIORef v_Recomp
+ let source_unchanged' = source_unchanged && do_recomp
+ hsc_env = HscEnv { hsc_mode = ghci_mode,
+ hsc_dflags = dyn_flags',
+ hsc_HPT = hpt }
+
+ -- run the compiler
+ hsc_result <- hscMain hsc_env pcs this_mod location
+ source_unchanged' have_object old_iface
+
+ case hsc_result of
+ HscFail pcs -> return (CompErrs pcs)
+
+ HscNoRecomp pcs details iface -> return (CompOK pcs details iface Nothing)
+
+ HscRecomp pcs details 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 -> do tm <- getClockTime
+ return ([BCOs comp_bc], tm)
+#endif
+ Nothing -> panic "compile: no interpreted code"
+
+ -- we're in batch mode: finish the compilation pipeline.
+ _other -> do pipe <- genPipeline (StopBefore Ln) "" True
+ hsc_lang (output_fn, getFileSuffix output_fn)
+ -- runPipeline takes input_fn so it can split off
+ -- the base name and use it as the base of
+ -- the output object file.
+ let (basename, suffix) = splitFilename input_fn
+ (o_file,_) <-
+ pipeLoop pipe (output_fn, getFileSuffix output_fn)
+ False False
+ basename suffix
+ o_time <- getModificationTime o_file
+ return ([DotO o_file], o_time)
+
+ let linkable = LM unlinked_time mod_name
+ (hs_unlinked ++ stub_unlinked)
+
+ return (CompOK pcs details 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
+ pipeline <- genPipeline (StopBefore Ln) "" True defaultHscLang (stub_c,"c")
+ (stub_o,_) <- runPipeline pipeline (stub_c,"c") False{-no linking-}
+ False{-no -o option-}
+ 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
+
+
+
+
+-- --------------------------------------------------------------------------
+-- genPipeline: Pipeline construction
+