[project @ 2005-01-28 12:55:17 by simonmar]
[ghc-hetmet.git] / ghc / compiler / main / DriverPipeline.hs
index d1d5c3b..84b7a69 100644 (file)
@@ -6,12 +6,12 @@
 --
 -----------------------------------------------------------------------------
 
-#include "../includes/config.h"
+#include "../includes/ghcconfig.h"
 
 module DriverPipeline (
 
        -- Interfaces for the batch-mode driver
-   runPipeline, staticLink,
+   compileFile, staticLink,
 
        -- Interfaces for the compilation manager (interpreted/batch-mode)
    preprocess, 
@@ -28,7 +28,6 @@ import Packages
 import GetImports
 import DriverState
 import DriverUtil
-import DriverMkDepend
 import DriverPhases
 import DriverFlags
 import SysTools                ( newTempName, addFilesToClean, getSysMan, copy )
@@ -44,6 +43,7 @@ import Config
 import RdrName         ( GlobalRdrEnv )
 import Panic
 import Util
+import StringBuffer    ( hGetStringBuffer )
 import BasicTypes      ( SuccessFlag(..) )
 import Maybes          ( expectJust )
 
@@ -52,9 +52,6 @@ import ParserCoreUtils ( getCoreModuleName )
 import EXCEPTION
 import DATA_IOREF      ( readIORef, writeIORef )
 
-#ifdef GHCI
-import Time            ( getClockTime )
-#endif
 import Directory
 import System
 import IO
@@ -67,17 +64,43 @@ import Maybe
 
 -- Just preprocess a file, put the result in a temp. file (used by the
 -- compilation manager during the summary phase).
+--
+-- We return the augmented DynFlags, because they contain the result
+-- of slurping in the OPTIONS pragmas
 
-preprocess :: FilePath -> IO FilePath
-preprocess filename =
-  ASSERT(isHaskellSrcFilename filename) 
-  do restoreDynFlags   -- Restore to state of last save
-     runPipeline (StopBefore Hsc) ("preprocess") 
+preprocess :: DynFlags -> FilePath -> IO (DynFlags, FilePath)
+preprocess dflags filename =
+  ASSERT2(isHaskellSrcFilename filename, text filename) 
+  runPipeline (StopBefore anyHsc) dflags ("preprocess") 
        False{-temporary output file-}
        Nothing{-no specific output file-}
        filename
        Nothing{-no ModLocation-}
 
+
+
+-- ---------------------------------------------------------------------------
+--             Compile a file
+--     This is used in batch mode 
+compileFile :: GhcMode -> DynFlags -> FilePath -> IO FilePath
+compileFile mode dflags src = do
+   exists <- doesFileExist src
+   when (not exists) $ 
+       throwDyn (CmdLineError ("file `" ++ src ++ "' does not exist"))
+   
+   o_file  <- readIORef v_Output_file
+   no_link <- readIORef v_NoLink       -- Set by -c or -no-link
+       -- When linking, the -o argument refers to the linker's output. 
+       -- otherwise, we use it as the name for the pipeline's output.
+   let maybe_o_file | no_link   = o_file
+                   | otherwise = Nothing
+
+   stop_flag <- readIORef v_GhcModeFlag
+   (_, out_file) <- runPipeline mode dflags stop_flag True maybe_o_file
+                               src Nothing{-no ModLocation-}
+   return out_file
+
+
 -- ---------------------------------------------------------------------------
 -- Compile
 
@@ -97,11 +120,10 @@ preprocess filename =
 -- NB.  No old interface can also mean that the source has changed.
 
 compile :: HscEnv
-       -> Module
-       -> ModLocation
-       -> Bool                    -- True <=> source unchanged
-       -> Bool                    -- True <=> have object
-        -> Maybe ModIface          -- old interface, if available
+       -> ModSummary
+       -> Bool                 -- True <=> source unchanged
+       -> Bool                 -- True <=> have object
+        -> Maybe ModIface       -- Old interface, if available
         -> IO CompResult
 
 data CompResult
@@ -116,49 +138,62 @@ data CompResult
    | CompErrs 
 
 
-compile hsc_env this_mod location
-       source_unchanged have_object 
-       old_iface = do 
+compile hsc_env mod_summary
+       source_unchanged have_object old_iface = do 
 
-   dyn_flags <- restoreDynFlags                -- Restore to the state of the last save
+   let dyn_flags   = hsc_dflags hsc_env
+       this_mod    = ms_mod mod_summary
+       src_flavour = ms_hsc_src mod_summary
 
-   showPass dyn_flags 
-       (showSDoc (text "Compiling" <+> ppr this_mod))
+   showPass dyn_flags ("Compiling " ++ showModMsg have_object mod_summary)
 
    let verb      = verbosity dyn_flags
+   let location          = ms_location mod_summary
    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
+   let input_fnpp = expectJust "compile:hspp" (ms_hspp_file mod_summary)
 
    when (verb >= 2) (hPutStrLn stderr ("compile: input file " ++ input_fnpp))
 
+   -- Add in the OPTIONS from the source file
+   -- This is nasty: we've done this once already, in the compilation manager
+   -- It might be better to cache the flags in the ml_hspp_file field,say
    opts <- getOptionsFromSource input_fnpp
-   processArgs dynamic_flags opts []
-   dyn_flags <- getDynFlags
+   (dyn_flags,unhandled_flags) <- processDynamicFlags opts dyn_flags
+   checkProcessArgsResult unhandled_flags input_fn
 
    let (basename, _) = splitFilename input_fn
-       
-   -- figure out what lang we're generating
-   hsc_lang <- hscMaybeAdjustLang (hscLang dyn_flags)
-   -- figure out what the next phase should be
-   next_phase <- hscNextPhase hsc_lang
-   -- figure out what file to generate the output into
-   get_output_fn <- genOutputFilenameFunc False Nothing next_phase basename
-   output_fn <- get_output_fn next_phase (Just location)
-
-   let dyn_flags' = dyn_flags { hscLang = hsc_lang,
+
+  -- We add the directory in which the .hs files resides) to the import path.
+  -- This is needed when we try to compile the .hc file later, if it
+  -- imports a _stub.h file that we created here.
+   let current_dir = directoryOf basename
+   old_paths <- readIORef v_Include_paths
+   writeIORef v_Include_paths (current_dir : old_paths)
+   -- put back the old include paths afterward.
+   later (writeIORef v_Include_paths old_paths) $ do
+
+   -- Figure out what lang we're generating
+   todo     <- readIORef v_GhcMode
+   hsc_lang <- hscMaybeAdjustTarget todo src_flavour (hscTarget dyn_flags)
+   -- ... and what the next phase should be
+   next_phase <- hscNextPhase src_flavour hsc_lang
+   -- ... and what file to generate the output into
+   get_output_fn <- genOutputFilenameFunc next_phase False Nothing basename
+   output_fn     <- get_output_fn next_phase (Just location)
+
+   let dyn_flags' = dyn_flags { hscTarget = hsc_lang,
                                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
+   let do_recomp = recompFlag dyn_flags
+       source_unchanged' = source_unchanged && do_recomp
        hsc_env' = hsc_env { hsc_dflags = dyn_flags' }
 
    -- run the compiler
-   hsc_result <- hscMain hsc_env' this_mod location
+   hsc_result <- hscMain hsc_env' printErrorsAndWarnings mod_summary
                         source_unchanged' have_object old_iface
 
    case hsc_result of
@@ -167,7 +202,13 @@ compile hsc_env this_mod location
       HscNoRecomp details iface -> return (CompOK details Nothing iface Nothing)
 
       HscRecomp details rdr_env iface
-       stub_h_exists stub_c_exists maybe_interpreted_code -> do
+               stub_h_exists stub_c_exists maybe_interpreted_code 
+
+       | isHsBoot src_flavour  -- No further compilation to do
+       -> return (CompOK details rdr_env iface Nothing)
+
+       | otherwise             -- Normal Haskell source files
+       -> do
           let 
           maybe_stub_o <- compileStub dyn_flags' stub_c_exists
           let stub_unlinked = case maybe_stub_o of
@@ -182,8 +223,13 @@ compile hsc_env this_mod location
                HscInterpreted -> 
                    case maybe_interpreted_code of
 #ifdef GHCI
-                      Just comp_bc -> do tm <- getClockTime 
-                                          return ([BCOs comp_bc], tm)
+                      Just comp_bc -> return ([BCOs comp_bc], ms_hs_date mod_summary)
+                       -- 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"
 
@@ -191,14 +237,14 @@ compile hsc_env this_mod location
                _other -> do
                   let object_filename = ml_obj_file location
 
-                  runPipeline (StopBefore Ln) ""
-                       True Nothing output_fn (Just location)
+                  runPipeline DoLink dyn_flags ""
+                              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
+          let linkable = LM unlinked_time this_mod
                             (hs_unlinked ++ stub_unlinked)
 
           return (CompOK details rdr_env iface (Just linkable))
@@ -211,11 +257,11 @@ compileStub dflags stub_c_exists
   | 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-}
+       (_, stub_o) <- runPipeline DoLink dflags "stub-compile"
+                           True{-persistent output-} 
+                           Nothing{-no specific output file-}
+                           stub_c
+                           Nothing{-no ModLocation-}
        return (Just stub_o)
 
 
@@ -261,7 +307,7 @@ link Batch dflags batch_attempt_linking hpt
        omit_linking <- readIORef v_NoLink
        if omit_linking 
          then do when (verb >= 3) $
-                   hPutStrLn stderr "link(batch): linking omitted (-no-link flag given)."
+                   hPutStrLn stderr "link(batch): linking omitted (-c flag given)."
                  return Succeeded
          else do
 
@@ -272,7 +318,7 @@ link Batch dflags batch_attempt_linking hpt
            obj_files = concatMap getOfiles linkables
 
        -- Don't showPass in Batch mode; doLink will do that for us.
-        staticLink obj_files pkg_deps
+        staticLink dflags obj_files pkg_deps
 
         when (verb >= 3) (hPutStrLn stderr "link: done")
 
@@ -290,27 +336,30 @@ link Batch dflags batch_attempt_linking hpt
 -- ---------------------------------------------------------------------------
 -- Run a compilation pipeline, consisting of multiple phases.
 
+-- The DynFlags can be modified by phases in the pipeline (eg. by
+-- OPTIONS pragmas), and the changes affect later phases in the
+-- pipeline, but we throw away the resulting DynFlags at the end.
+
 runPipeline
   :: GhcMode           -- when to stop
+  -> DynFlags          -- dynamic flags
   -> 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
+  -> IO (DynFlags, FilePath)   -- (final flags, output filename)
 
-runPipeline todo stop_flag keep_output maybe_output_filename input_fn maybe_loc
+runPipeline todo dflags 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
 
-      stop_phase = case todo of 
-                       StopBefore As | split -> SplitAs
-                       StopBefore phase      -> phase
-                       DoMkDependHS          -> Ln
-                       DoLink                -> Ln
-                       DoMkDLL               -> Ln
+      todo' = case todo of
+               StopBefore As | split -> StopBefore SplitAs
+               other                 -> todo
 
   -- We want to catch cases of "you can't get there from here" before
   -- we start the pipeline, because otherwise it will just run off the
@@ -319,6 +368,10 @@ runPipeline todo stop_flag keep_output maybe_output_filename input_fn maybe_loc
   -- There is a partial ordering on phases, where A < B iff A occurs
   -- before B in a normal compilation pipeline.
   --
+  let stop_phase = case todo' of 
+                       StopBefore phase -> phase
+                       other            -> StopLn
+
   when (not (start_phase `happensBefore` stop_phase)) $
        throwDyn (UsageError 
                    ("flag `" ++ stop_flag
@@ -327,63 +380,58 @@ runPipeline todo stop_flag keep_output maybe_output_filename input_fn maybe_loc
 
   -- generate a function which will be used to calculate output file names
   -- as we go along.
-  get_output_fn <- genOutputFilenameFunc keep_output maybe_output_filename
-                       stop_phase basename
+  get_output_fn <- genOutputFilenameFunc stop_phase keep_output 
+                                        maybe_output_filename basename
 
-  -- and execute the pipeline...
-  (output_fn, maybe_loc) <- 
-       pipeLoop start_phase stop_phase input_fn basename suffix 
-                get_output_fn maybe_loc
+  -- Execute the pipeline...
+  (dflags', output_fn, maybe_loc) <- pipeLoop todo' dflags start_phase stop_phase input_fn 
+                                             basename suffix get_output_fn maybe_loc
 
-  -- sometimes, a compilation phase doesn't actually generate any output
+  -- Sometimes, a compilation phase doesn't actually generate any output
   -- (eg. the CPP phase when -fcpp is not turned on).  If we end on this
   -- stage, but we wanted to keep the output, then we have to explicitly
   -- copy the file.
-  if keep_output
+  if keep_output 
        then do final_fn <- get_output_fn stop_phase maybe_loc
                when (final_fn /= output_fn) $
-                 copy ("Copying `" ++ output_fn ++ "' to `" ++ final_fn
+                 copy dflags ("Copying `" ++ output_fn ++ "' to `" ++ final_fn
                        ++ "'") output_fn final_fn
-               return final_fn
+               return (dflags', final_fn)
        else
-            return output_fn
+               return (dflags', output_fn)
 
 
-pipeLoop :: Phase -> Phase -> FilePath -> String -> Suffix
-  -> (Phase -> Maybe ModLocation -> IO FilePath)
-  -> Maybe ModLocation -> IO (FilePath, Maybe ModLocation)
+pipeLoop :: GhcMode -> DynFlags -> Phase -> Phase 
+        -> FilePath  -> String -> Suffix
+        -> (Phase -> Maybe ModLocation -> IO FilePath)
+        -> Maybe ModLocation
+        -> IO (DynFlags, FilePath, Maybe ModLocation)
 
-pipeLoop phase stop_phase input_fn orig_basename orig_suff 
-       get_output_fn maybe_loc
+pipeLoop orig_todo dflags phase stop_phase 
+        input_fn orig_basename orig_suff 
+        orig_get_output_fn maybe_loc
 
-  | phase == stop_phase  =  return (input_fn, maybe_loc)  -- all done
+  | phase `eqPhase` stop_phase           -- All done
+  = return (dflags, input_fn, maybe_loc)
 
-  | not (phase `happensBefore` stop_phase)  = 
+  | not (phase `happensBefore` stop_phase)
        -- Something has gone wrong.  We'll try to cover all the cases when
        -- this could happen, so if we reach here it is a panic.
        -- eg. it might happen if the -C flag is used on a source file that
        -- has {-# OPTIONS -fasm #-}.
-       panic ("pipeLoop: at phase " ++ show phase ++ 
-               " but I wanted to stop at phase " ++ show stop_phase)
-
-  | otherwise = do
-       maybe_next_phase <- runPhase phase orig_basename orig_suff input_fn
-                               get_output_fn maybe_loc
-       case maybe_next_phase of
-         (Nothing, maybe_loc, output_fn) -> do
-               -- we stopped early, but return the *final* filename
-               -- (it presumably already exists)
-               final_fn <- get_output_fn stop_phase maybe_loc
-               return (final_fn, maybe_loc)
-         (Just next_phase, maybe_loc, output_fn) ->
-               pipeLoop next_phase stop_phase output_fn
-                       orig_basename orig_suff get_output_fn maybe_loc
-
-  
-genOutputFilenameFunc :: Bool -> Maybe FilePath -> Phase -> String
+  = panic ("pipeLoop: at phase " ++ show phase ++ 
+          " but I wanted to stop at phase " ++ show stop_phase)
+
+  | otherwise 
+  = do { (next_phase, dflags', maybe_loc, output_fn)
+               <- runPhase phase orig_todo dflags orig_basename 
+                           orig_suff input_fn orig_get_output_fn maybe_loc
+       ; pipeLoop orig_todo dflags' next_phase stop_phase output_fn
+                  orig_basename orig_suff orig_get_output_fn maybe_loc }
+
+genOutputFilenameFunc :: Phase -> Bool -> Maybe FilePath -> String
   -> IO (Phase{-next phase-} -> Maybe ModLocation -> IO FilePath)
-genOutputFilenameFunc keep_final_output maybe_output_filename 
-               stop_phase basename
+genOutputFilenameFunc stop_phase keep_final_output maybe_output_filename basename
  = do
    hcsuf      <- readIORef v_HC_suf
    odir       <- readIORef v_Output_dir
@@ -396,9 +444,9 @@ genOutputFilenameFunc keep_final_output maybe_output_filename
    keep_raw_s <- readIORef v_Keep_raw_s_files
    keep_s     <- readIORef v_Keep_s_files
    let
-        myPhaseInputExt HCc | Just s <- hcsuf = s
-        myPhaseInputExt Ln    = osuf
-        myPhaseInputExt other = phaseInputExt other
+        myPhaseInputExt HCc    = hcsuf
+        myPhaseInputExt StopLn = osuf
+        myPhaseInputExt other  = phaseInputExt other
 
        func next_phase maybe_location
                | is_last_phase, Just f <- maybe_output_filename = return f
@@ -407,12 +455,12 @@ genOutputFilenameFunc keep_final_output maybe_output_filename
                | otherwise                          = newTempName suffix
 
           where
-               is_last_phase = next_phase == stop_phase
+               is_last_phase = next_phase `eqPhase` stop_phase
 
                -- sometimes, we keep output from intermediate stages
                keep_this_output = 
                     case next_phase of
-                            Ln                  -> True
+                            StopLn              -> True
                             Mangle | keep_raw_s -> True
                             As     | keep_s     -> True
                             HCc    | keep_hc    -> True
@@ -422,8 +470,8 @@ genOutputFilenameFunc keep_final_output maybe_output_filename
 
                -- persistent object files get put in odir
                persistent_fn 
-                  | Ln <- next_phase  = return odir_persistent
-                  | otherwise         = return persistent
+                  | StopLn <- next_phase = return odir_persistent
+                  | otherwise            = return persistent
 
                persistent = basename ++ '.':suffix
 
@@ -446,119 +494,89 @@ genOutputFilenameFunc keep_final_output maybe_output_filename
 -- taking the via-C route to using the native code generator.
 
 runPhase :: Phase
-         -> String     -- basename of original input source
-         -> String     -- its extension
-         -> FilePath   -- name of file which contains the input to this phase.
-         -> (Phase -> Maybe ModLocation -> IO FilePath)
+        -> GhcMode
+        -> DynFlags
+        -> String      -- basename of original input source
+        -> String      -- its extension
+        -> FilePath    -- name of file which contains the input to this phase.
+        -> (Phase -> Maybe ModLocation -> IO FilePath)
                        -- how to calculate the output filename
-         -> Maybe ModLocation          -- the ModLocation, if we have one
-         -> IO (Maybe Phase,           -- next phase
-                Maybe ModLocation,     -- the ModLocation, if we have one
-                FilePath)              -- output filename
+        -> Maybe ModLocation           -- the ModLocation, if we have one
+        -> IO (Phase,                  -- next phase
+               DynFlags,               -- new dynamic flags
+               Maybe ModLocation,      -- the ModLocation, if we have one
+               FilePath)               -- output filename
+
+       -- Invariant: the output filename always contains the output
+       -- Interesting case: Hsc when there is no recompilation to do
+       --                   Then the output filename is still a .o file 
 
 -------------------------------------------------------------------------------
 -- Unlit phase 
 
-runPhase Unlit _basename _suff input_fn get_output_fn maybe_loc
-  = do unlit_flags <- getOpts opt_L
+runPhase (Unlit sf) _todo dflags _basename _suff input_fn get_output_fn maybe_loc
+  = do let unlit_flags = getOpts dflags opt_L
        -- The -h option passes the file name for unlit to put in a #line directive
-       output_fn <- get_output_fn Cpp maybe_loc
+       output_fn <- get_output_fn (Cpp sf) maybe_loc
 
-       SysTools.runUnlit (map SysTools.Option unlit_flags ++
+       SysTools.runUnlit dflags 
+               (map SysTools.Option unlit_flags ++
                                  [ SysTools.Option     "-h"
                          , SysTools.Option     input_fn
                          , SysTools.FileOption "" input_fn
                          , SysTools.FileOption "" output_fn
                          ])
 
-       return (Just Cpp, maybe_loc, output_fn)
+       return (Cpp sf, dflags, maybe_loc, output_fn)
 
 -------------------------------------------------------------------------------
--- Cpp phase 
+-- Cpp phase : (a) gets OPTIONS out of file
+--            (b) runs cpp if necessary
 
-runPhase Cpp basename suff input_fn get_output_fn maybe_loc
+runPhase (Cpp sf) _todo dflags basename suff input_fn get_output_fn maybe_loc
   = do src_opts <- getOptionsFromSource input_fn
-       unhandled_flags <- processArgs dynamic_flags src_opts []
-       checkProcessArgsResult unhandled_flags basename suff
+       (dflags,unhandled_flags) <- processDynamicFlags src_opts dflags
+       checkProcessArgsResult unhandled_flags (basename++'.':suff)
 
-       do_cpp <- dynFlag cppFlag
-       if not do_cpp then
+       if not (cppFlag dflags) then
            -- no need to preprocess CPP, just pass input file along
           -- to the next phase of the pipeline.
-          return (Just HsPp, maybe_loc, input_fn)
+          return (HsPp sf, dflags, maybe_loc, input_fn)
        else do
-           hscpp_opts      <- getOpts opt_P
-                   hs_src_cpp_opts <- readIORef v_Hs_source_cpp_opts
-
-           cmdline_include_paths <- readIORef v_Include_paths
-
-           pkg_include_dirs <- getPackageIncludePath []
-           let include_paths = foldr (\ x xs -> "-I" : x : xs) []
-                                 (cmdline_include_paths ++ pkg_include_dirs)
-
-           verb <- getVerbFlag
-           (md_c_flags, _) <- machdepCCOpts
-
-           output_fn <- get_output_fn HsPp maybe_loc
-
-           SysTools.runCpp ([SysTools.Option verb]
-                           ++ map SysTools.Option include_paths
-                           ++ map SysTools.Option hs_src_cpp_opts
-                           ++ map SysTools.Option hscpp_opts
-                           ++ map SysTools.Option md_c_flags
-                           ++ [ SysTools.Option     "-x"
-                              , SysTools.Option     "c"
-                              , SysTools.Option     input_fn
-       -- We hackily use Option instead of FileOption here, so that the file
-       -- name is not back-slashed on Windows.  cpp is capable of
-       -- dealing with / in filenames, so it works fine.  Furthermore
-       -- if we put in backslashes, cpp outputs #line directives
-       -- with *double* backslashes.   And that in turn means that
-       -- our error messages get double backslashes in them.
-       -- In due course we should arrange that the lexer deals
-       -- with these \\ escapes properly.
-                              , SysTools.Option     "-o"
-                              , SysTools.FileOption "" output_fn
-                              ])
-
-           return (Just HsPp, maybe_loc, output_fn)
+           output_fn <- get_output_fn (HsPp sf) maybe_loc
+           doCpp dflags True{-raw-} False{-no CC opts-} input_fn output_fn
+           return (HsPp sf, dflags, maybe_loc, output_fn)
 
 -------------------------------------------------------------------------------
 -- HsPp phase 
 
-runPhase HsPp basename suff input_fn get_output_fn maybe_loc
-  = do do_pp   <- dynFlag ppFlag
-       if not do_pp then
+runPhase (HsPp sf) _todo dflags basename suff input_fn get_output_fn maybe_loc
+  = do if not (ppFlag dflags) then
            -- no need to preprocess, just pass input file along
           -- to the next phase of the pipeline.
-          return (Just Hsc, maybe_loc, input_fn)
+          return (Hsc sf, dflags, maybe_loc, input_fn)
        else do
-           hspp_opts      <- getOpts opt_F
+           let hspp_opts = getOpts dflags opt_F
                    hs_src_pp_opts <- readIORef v_Hs_source_pp_opts
            let orig_fn = basename ++ '.':suff
-           output_fn <- get_output_fn Hsc maybe_loc
-           SysTools.runPp ( [ SysTools.Option     orig_fn
+           output_fn <- get_output_fn (Hsc sf) maybe_loc
+           SysTools.runPp dflags
+                          ( [ SysTools.Option     orig_fn
                             , SysTools.Option     input_fn
                             , SysTools.FileOption "" output_fn
                             ] ++
                             map SysTools.Option hs_src_pp_opts ++
                             map SysTools.Option hspp_opts
                           )
-           return (Just Hsc, maybe_loc, output_fn)
+           return (Hsc sf, dflags, maybe_loc, output_fn)
 
 -----------------------------------------------------------------------------
 -- Hsc phase
 
 -- Compilation of a single module, in "legacy" mode (_not_ under
 -- the direction of the compilation manager).
-runPhase Hsc basename suff input_fn get_output_fn _maybe_loc = do
-  todo <- readIORef v_GhcMode
-  if todo == DoMkDependHS then do
-       locn <- doMkDependHSPhase basename suff input_fn
-       return (Nothing, Just locn, input_fn)  -- Ln is a dummy stop phase 
-
-   else do
-      -- normal Hsc mode, not mkdependHS
+runPhase (Hsc src_flavour) todo dflags basename suff input_fn get_output_fn _maybe_loc 
+ = do  -- normal Hsc mode, not mkdependHS
 
   -- we add the current directory (i.e. the directory in which
   -- the .hs files resides) to the import path, since this is
@@ -569,24 +587,68 @@ runPhase Hsc basename suff input_fn get_output_fn _maybe_loc = do
        writeIORef v_Include_paths (current_dir : paths)
        
   -- gather the imports and module name
-        (_,_,mod_name) <- 
-            if isExtCoreFilename ('.':suff)
-            then do
-               -- no explicit imports in ExtCore input.
-              m <- getCoreModuleName input_fn
-              return ([], [], mkModuleName m)
-            else 
-              getImportsFromFile input_fn
-
-  -- build a ModLocation to pass to hscMain.
-       (mod, location') <- mkHomeModLocation mod_name (basename ++ '.':suff)
-
-  -- take -ohi into account if present
+        (hspp_buf,mod_name) <- 
+            case src_flavour of
+               ExtCoreFile -> do {  -- no explicit imports in ExtCore input.
+                                 ; m <- getCoreModuleName input_fn
+                                 ; return (Nothing, mkModule m) }
+
+               other -> do { buf <- hGetStringBuffer input_fn
+                           ; (_,_,mod_name) <- getImports dflags buf input_fn
+                           ; return (Just buf, mod_name) }
+
+  -- Build a ModLocation to pass to hscMain.
+  -- The source filename is rather irrelevant by now, but it's used
+  -- by hscMain for messages.  hscMain also needs 
+  -- the .hi and .o filenames, and this is as good a way
+  -- as any to generate them, and better than most. (e.g. takes 
+  -- into accout the -osuf flags)
+       location1 <- mkHomeModLocation2 mod_name basename suff
+
+  -- Boot-ify it if necessary
+       let location2 | isHsBoot src_flavour = addBootSuffixLocn location1
+                     | otherwise            = location1 
+                                       
+
+  -- Take -ohi into account if present
+  -- This can't be done in mkHomeModuleLocation because
+  -- it only applies to the module being compiles
        ohi <- readIORef v_Output_hi
-       let location | Just fn <- ohi = location'{ ml_hi_file = fn }
-                    | otherwise      = location'
-
-  -- figure out if the source has changed, for recompilation avoidance.
+       let location3 | Just fn <- ohi = location2{ ml_hi_file = fn }
+                     | otherwise      = location2
+
+  -- Take -o into account if present
+  -- Very like -ohi, but we must *only* do this if we aren't linking
+  -- (If we're linking then the -o applies to the linked thing, not to
+  -- the object file for one module.)
+  -- Note the nasty duplication with the same computation in compileFile above
+       expl_o_file <- readIORef v_Output_file
+       no_link     <- readIORef v_NoLink
+       let location4 | Just ofile <- expl_o_file, no_link 
+                     = location3 { ml_obj_file = ofile }
+                     | otherwise = location3
+
+  -- Tell the finder cache about this module
+       addHomeModuleToFinder mod_name location4
+
+  -- Make the ModSummary to hand to hscMain
+       src_timestamp <- getModificationTime (basename ++ '.':suff)
+       let
+           unused_field = panic "runPhase:ModSummary field"
+               -- Some fields are not looked at by hscMain
+           mod_summary = ModSummary {  ms_mod       = mod_name, 
+                                       ms_hsc_src   = src_flavour,
+                                       ms_hspp_file = Just input_fn,
+                                       ms_hspp_buf  = hspp_buf,
+                                       ms_location  = location4,
+                                       ms_hs_date   = src_timestamp,
+                                       ms_imps      = unused_field,
+                                       ms_srcimps   = unused_field }
+
+           o_file = ml_obj_file location4      -- The real object file
+
+
+  -- Figure out if the source has changed, for recompilation avoidance.
   -- only do this if we're eventually going to generate a .o file.
   -- (ToDo: do when generating .hc files too?)
   --
@@ -595,89 +657,102 @@ runPhase Hsc basename suff input_fn get_output_fn _maybe_loc = do
   -- changed (which the compiler itself figures out).
   -- Setting source_unchanged to False tells the compiler that M.o is out of
   -- date wrt M.hs (or M.o doesn't exist) so we must recompile regardless.
-       do_recomp   <- readIORef v_Recomp
-       expl_o_file <- readIORef v_Output_file
-
-       let o_file -- if the -o option is given and IT IS THE OBJECT FILE FOR
-                  -- THIS COMPILATION, then use that to determine if the 
-                  -- source is unchanged.
-               | Just x <- expl_o_file, todo == StopBefore Ln  =  x
-               | otherwise = ml_obj_file location
-
+       let do_recomp = recompFlag dflags
        source_unchanged <- 
-          if not (do_recomp && ( todo == DoLink || todo == StopBefore Ln ))
+          if not (do_recomp && case todo of { DoLink -> True; other -> False })
             then return False
-            else do t1 <- getModificationTime (basename ++ '.':suff)
-                    o_file_exists <- doesFileExist o_file
+            else do o_file_exists <- doesFileExist o_file
                     if not o_file_exists
                        then return False       -- Need to recompile
                        else do t2 <- getModificationTime o_file
-                               if t2 > t1
+                               if t2 > src_timestamp
                                  then return True
                                  else return False
 
   -- get the DynFlags
-        dyn_flags <- getDynFlags
-       hsc_lang <- hscMaybeAdjustLang (hscLang dyn_flags)
-       next_phase <- hscNextPhase hsc_lang
-       output_fn <- get_output_fn next_phase (Just location)
-
-        let dyn_flags' = dyn_flags { hscLang = hsc_lang,
-                                    hscOutName = output_fn,
-                                    hscStubCOutName = basename ++ "_stub.c",
-                                    hscStubHOutName = basename ++ "_stub.h",
-                                    extCoreName = basename ++ ".hcr" }
-       hsc_env <- newHscEnv OneShot dyn_flags'
+       hsc_lang   <- hscMaybeAdjustTarget todo src_flavour (hscTarget dflags)
+       next_phase <- hscNextPhase src_flavour hsc_lang
+       output_fn  <- get_output_fn next_phase (Just location4)
+
+        let dflags' = dflags { hscTarget = hsc_lang,
+                              hscOutName = output_fn,
+                              hscStubCOutName = basename ++ "_stub.c",
+                              hscStubHOutName = basename ++ "_stub.h",
+                              extCoreName = basename ++ ".hcr" }
+
+       hsc_env <- newHscEnv OneShot dflags'
 
   -- run the compiler!
-       result <- hscMain hsc_env mod
-                         location{ ml_hspp_file=Just input_fn }
-                         source_unchanged
-                         False
-                         Nothing        -- no iface
+       result <- hscMain hsc_env printErrorsAndWarnings
+                         mod_summary source_unchanged 
+                         False         -- No object file
+                         Nothing       -- No iface
 
        case result of
 
            HscFail -> throwDyn (PhaseFailed "hsc" (ExitFailure 1))
 
             HscNoRecomp details iface -> do
-               SysTools.touch "Touching object file" o_file
-               return (Nothing, Just location, output_fn)
+               SysTools.touch dflags' "Touching object file" o_file
+               return (StopLn, dflags', Just location4, o_file)
 
            HscRecomp _details _rdr_env _iface 
                      stub_h_exists stub_c_exists
                      _maybe_interpreted_code -> do
 
-               -- deal with stubs
-               maybe_stub_o <- compileStub dyn_flags' stub_c_exists
+               -- Deal with stubs 
+               maybe_stub_o <- compileStub dflags' stub_c_exists
                case maybe_stub_o of
-                     Nothing -> return ()
+                     Nothing     -> return ()
                      Just stub_o -> add v_Ld_inputs stub_o
-               case hscLang dyn_flags of
-                      HscNothing -> return (Nothing, Just location, output_fn)
-                     _ -> return (Just next_phase, Just location, output_fn)
+
+               -- In the case of hs-boot files, generate a dummy .o-boot 
+               -- stamp file for the benefit of Make
+               case src_flavour of
+                 HsBootFile -> SysTools.touch dflags' "Touching object file" o_file
+                 other      -> return ()
+
+               return (next_phase, dflags', Just location4, output_fn)
 
 -----------------------------------------------------------------------------
--- Cc phase
+-- Cmm phase
 
--- we don't support preprocessing .c files (with -E) now.  Doing so introduces
--- way too many hacks, and I can't say I've ever used it anyway.
+runPhase CmmCpp todo dflags basename suff input_fn get_output_fn maybe_loc
+  = do
+       output_fn <- get_output_fn Cmm maybe_loc
+       doCpp dflags False{-not raw-} True{-include CC opts-} input_fn output_fn        
+       return (Cmm, dflags, maybe_loc, output_fn)
 
-runPhase cc_phase basename suff input_fn get_output_fn maybe_loc
-   | cc_phase == Cc || cc_phase == HCc
-   = do        cc_opts <- getOpts opt_c
-               cmdline_include_paths <- readIORef v_Include_paths
+runPhase Cmm todo dflags basename suff input_fn get_output_fn maybe_loc
+  = do
+       hsc_lang <- hscMaybeAdjustTarget todo HsSrcFile (hscTarget dflags)
+       next_phase <- hscNextPhase HsSrcFile hsc_lang
+       output_fn <- get_output_fn next_phase maybe_loc
 
-       split  <- readIORef v_Split_object_files
-       mangle <- readIORef v_Do_asm_mangling
+        let dflags' = dflags { hscTarget = hsc_lang,
+                              hscOutName = output_fn,
+                              hscStubCOutName = basename ++ "_stub.c",
+                              hscStubHOutName = basename ++ "_stub.h",
+                              extCoreName = basename ++ ".hcr" }
 
-        let hcc = cc_phase == HCc
+       ok <- hscCmmFile dflags' input_fn
 
-           next_phase
-               | hcc && mangle     = Mangle
-               | otherwise         = As
+       when (not ok) $ throwDyn (PhaseFailed "cmm" (ExitFailure 1))
 
-       output_fn <- get_output_fn next_phase maybe_loc
+       return (next_phase, dflags, maybe_loc, output_fn)
+
+-----------------------------------------------------------------------------
+-- Cc phase
+
+-- we don't support preprocessing .c files (with -E) now.  Doing so introduces
+-- way too many hacks, and I can't say I've ever used it anyway.
+
+runPhase cc_phase todo dflags basename suff input_fn get_output_fn maybe_loc
+   | cc_phase `eqPhase` Cc || cc_phase `eqPhase` HCc
+   = do        let cc_opts = getOpts dflags opt_c
+           hcc = cc_phase `eqPhase` HCc
+
+               cmdline_include_paths <- readIORef v_Include_paths
 
        -- HC files have the dependent packages stamped into them
        pkgs <- if hcc then getHCFilePackages input_fn else return []
@@ -685,16 +760,15 @@ runPhase cc_phase basename suff input_fn get_output_fn maybe_loc
        -- add package include paths even if we're just compiling .c
        -- files; this is the Value Add(TM) that using ghc instead of
        -- gcc gives you :)
-        pkg_include_dirs <- getPackageIncludePath pkgs
+        pkg_include_dirs <- getPackageIncludePath dflags pkgs
         let include_paths = foldr (\ x xs -> "-I" : x : xs) []
                              (cmdline_include_paths ++ pkg_include_dirs)
 
-       mangle <- readIORef v_Do_asm_mangling
-       (md_c_flags, md_regd_c_flags) <- machdepCCOpts
+       (md_c_flags, md_regd_c_flags) <- machdepCCOpts dflags
 
-        verb <- getVerbFlag
+        let verb = getVerbFlag dflags
 
-       pkg_extra_cc_opts <- getPackageExtraCcOpts pkgs
+       pkg_extra_cc_opts <- getPackageExtraCcOpts dflags pkgs
 
        split_objs <- readIORef v_Split_object_files
        let split_opt | hcc && split_objs = [ "-DUSE_SPLIT_MARKERS" ]
@@ -702,20 +776,26 @@ runPhase cc_phase basename suff input_fn get_output_fn maybe_loc
 
        excessPrecision <- readIORef v_Excess_precision
 
+       -- Decide next phase
+       mangle <- readIORef v_Do_asm_mangling
+        let next_phase
+               | hcc && mangle     = Mangle
+               | otherwise         = As
+       output_fn <- get_output_fn next_phase maybe_loc
+
        -- force the C compiler to interpret this file as C when
        -- compiling .hc files, by adding the -x c option.
-       let langopt
-               | cc_phase == HCc = [ SysTools.Option "-x", SysTools.Option "c"]
-               | otherwise       = [ ]
+       let langopt | hcc = [ SysTools.Option "-x", SysTools.Option "c"]
+                   | otherwise = [ ]
 
-       SysTools.runCc (langopt ++
+       SysTools.runCc dflags (langopt ++
                        [ SysTools.FileOption "" input_fn
                        , SysTools.Option "-o"
                        , SysTools.FileOption "" output_fn
                        ]
                       ++ map SysTools.Option (
                          md_c_flags
-                      ++ (if cc_phase == HCc && mangle
+                      ++ (if hcc && mangle
                             then md_regd_c_flags
                             else [])
                       ++ [ verb, "-S", "-Wimplicit", "-O" ]
@@ -727,19 +807,21 @@ runPhase cc_phase basename suff input_fn get_output_fn maybe_loc
                       ++ pkg_extra_cc_opts
                       ))
 
-       return (Just next_phase, maybe_loc, output_fn)
+       return (next_phase, dflags, maybe_loc, output_fn)
 
        -- ToDo: postprocess the output from gcc
 
 -----------------------------------------------------------------------------
 -- Mangle phase
 
-runPhase Mangle _basename _suff input_fn get_output_fn maybe_loc
-   = do mangler_opts <- getOpts opt_m
-        machdep_opts <- if (prefixMatch "i386" cTARGETPLATFORM)
-                         then do n_regs <- dynFlag stolen_x86_regs
-                                 return [ show n_regs ]
-                         else return []
+runPhase Mangle todo dflags _basename _suff input_fn get_output_fn maybe_loc
+   = do let mangler_opts = getOpts dflags opt_m
+
+#if i386_TARGET_ARCH
+        machdep_opts <- return [ show (stolen_x86_regs dflags) ]
+#else
+       machdep_opts <- return []
+#endif
 
        split <- readIORef v_Split_object_files
        let next_phase
@@ -747,24 +829,25 @@ runPhase Mangle _basename _suff input_fn get_output_fn maybe_loc
                | otherwise = As
        output_fn <- get_output_fn next_phase maybe_loc
 
-       SysTools.runMangle (map SysTools.Option mangler_opts
+       SysTools.runMangle dflags (map SysTools.Option mangler_opts
                          ++ [ SysTools.FileOption "" input_fn
                             , SysTools.FileOption "" output_fn
                             ]
                          ++ map SysTools.Option machdep_opts)
 
-       return (Just next_phase, maybe_loc, output_fn)
+       return (next_phase, dflags, maybe_loc, output_fn)
 
 -----------------------------------------------------------------------------
 -- Splitting phase
 
-runPhase SplitMangle _basename _suff input_fn get_output_fn maybe_loc
+runPhase SplitMangle todo dflags _basename _suff input_fn get_output_fn maybe_loc
   = do  -- tmp_pfx is the prefix used for the split .s files
        -- We also use it as the file to contain the no. of split .s files (sigh)
        split_s_prefix <- SysTools.newTempName "split"
        let n_files_fn = split_s_prefix
 
-       SysTools.runSplit [ SysTools.FileOption "" input_fn
+       SysTools.runSplit dflags
+                         [ SysTools.FileOption "" input_fn
                          , SysTools.FileOption "" split_s_prefix
                          , SysTools.FileOption "" n_files_fn
                          ]
@@ -778,23 +861,24 @@ runPhase SplitMangle _basename _suff input_fn get_output_fn maybe_loc
        addFilesToClean [ split_s_prefix ++ "__" ++ show n ++ ".s"
                        | n <- [1..n_files]]
 
-       return (Just SplitAs, maybe_loc, "**splitmangle**")
+       return (SplitAs, dflags, maybe_loc, "**splitmangle**")
          -- we don't use the filename
 
 -----------------------------------------------------------------------------
 -- As phase
 
-runPhase As _basename _suff input_fn get_output_fn maybe_loc
-  = do as_opts               <- getOpts opt_a
+runPhase As todo dflags _basename _suff input_fn get_output_fn maybe_loc
+  = do let as_opts =  getOpts dflags opt_a
         cmdline_include_paths <- readIORef v_Include_paths
 
-       output_fn <- get_output_fn Ln maybe_loc
+       output_fn <- get_output_fn StopLn maybe_loc
 
        -- we create directories for the object file, because it
        -- might be a hierarchical module.
        createDirectoryHierarchy (directoryOf output_fn)
 
-       SysTools.runAs (map SysTools.Option as_opts
+       SysTools.runAs dflags   
+                      (map SysTools.Option as_opts
                       ++ [ SysTools.Option ("-I" ++ p) | p <- cmdline_include_paths ]
                       ++ [ SysTools.Option "-c"
                          , SysTools.FileOption "" input_fn
@@ -802,11 +886,11 @@ runPhase As _basename _suff input_fn get_output_fn maybe_loc
                          , SysTools.FileOption "" output_fn
                          ])
 
-       return (Just Ln, maybe_loc, output_fn)
+       return (StopLn, dflags, maybe_loc, output_fn)
 
 
-runPhase SplitAs basename _suff _input_fn get_output_fn maybe_loc
-  = do  as_opts <- getOpts opt_a
+runPhase SplitAs todo dflags basename _suff _input_fn get_output_fn maybe_loc
+  = do  let as_opts = getOpts dflags opt_a
 
        (split_s_prefix, n) <- readIORef v_Split_info
 
@@ -821,7 +905,8 @@ runPhase SplitAs basename _suff _input_fn get_output_fn maybe_loc
                                        (basename ++ "__" ++ show n ++ ".o")
                                         real_odir
                    real_o <- osuf_ify output_o
-                   SysTools.runAs (map SysTools.Option as_opts ++
+                   SysTools.runAs dflags
+                                (map SysTools.Option as_opts ++
                                    [ SysTools.Option "-c"
                                    , SysTools.Option "-o"
                                    , SysTools.FileOption "" real_o
@@ -830,16 +915,16 @@ runPhase SplitAs basename _suff _input_fn get_output_fn maybe_loc
        
        mapM_ assemble_file [1..n]
 
-       output_fn <- get_output_fn Ln maybe_loc
-       return (Just Ln, maybe_loc, output_fn)
+       output_fn <- get_output_fn StopLn maybe_loc
+       return (StopLn, dflags, maybe_loc, output_fn)
 
 #ifdef ILX
 -----------------------------------------------------------------------------
 -- Ilx2Il phase
 -- Run ilx2il over the ILX output, getting an IL file
 
-runPhase Ilx2Il _basename _suff input_fn get_output_fn maybe_loc
-  = do ilx2il_opts <- getOpts opt_I
+runPhase Ilx2Il todo dflags _basename _suff input_fn get_output_fn maybe_loc
+  = do let ilx2il_opts = getOpts dflags opt_I
         SysTools.runIlx2il (map SysTools.Option ilx2il_opts
                            ++ [ SysTools.Option "--no-add-suffix-to-assembly",
                                SysTools.Option "mscorlib",
@@ -852,8 +937,8 @@ runPhase Ilx2Il _basename _suff input_fn get_output_fn maybe_loc
 -- Ilasm phase
 -- Run ilasm over the IL, getting a DLL
 
-runPhase Ilasm _basename _suff input_fn get_output_fn maybe_loc
-  = do ilasm_opts <- getOpts opt_i
+runPhase Ilasm todo dflags _basename _suff input_fn get_output_fn maybe_loc
+  = do let ilasm_opts = getOpts dflags opt_i
         SysTools.runIlasm (map SysTools.Option ilasm_opts
                           ++ [ SysTools.Option "/QUIET",
                                SysTools.Option "/DLL",
@@ -950,9 +1035,9 @@ mk_pvm_wrapper_script pvm_executable pvm_executable_base sysMan = unlines $
 -----------------------------------------------------------------------------
 -- Complain about non-dynamic flags in OPTIONS pragmas
 
-checkProcessArgsResult flags basename suff
+checkProcessArgsResult flags filename
   = do when (notNull flags) (throwDyn (ProgramError (
-         showSDoc (hang (text basename <> text ('.':suff) <> char ':')
+         showSDoc (hang (text filename <> char ':')
                      4 (text "unknown flags in  {-# OPTIONS #-} pragma:" <+>
                          hsep (map text flags)))
        )))
@@ -960,13 +1045,13 @@ checkProcessArgsResult flags basename suff
 -----------------------------------------------------------------------------
 -- Look for the /* GHC_PACKAGES ... */ comment at the top of a .hc file
 
-getHCFilePackages :: FilePath -> IO [PackageName]
+getHCFilePackages :: FilePath -> IO [PackageId]
 getHCFilePackages filename =
   EXCEPTION.bracket (openFile filename ReadMode) hClose $ \h -> do
     l <- hGetLine h
     case l of
       '/':'*':' ':'G':'H':'C':'_':'P':'A':'C':'K':'A':'G':'E':'S':rest ->
-         return (map mkPackageName (words rest))
+         return (map stringToPackageId (words rest))
       _other ->
          return []
 
@@ -983,9 +1068,9 @@ getHCFilePackages filename =
 -- read any interface files), so the user must explicitly specify all
 -- the packages.
 
-staticLink :: [FilePath] -> [PackageName] -> IO ()
-staticLink o_files dep_packages = do
-    verb       <- getVerbFlag
+staticLink :: DynFlags -> [FilePath] -> [PackageId] -> IO ()
+staticLink dflags o_files dep_packages = do
+    let verb = getVerbFlag dflags
     static     <- readIORef v_Static
     no_hs_main <- readIORef v_NoHsMain
 
@@ -994,26 +1079,29 @@ staticLink o_files dep_packages = do
     -- dependencies, and eliminating duplicates.
 
     o_file <- readIORef v_Output_file
+#if defined(mingw32_HOST_OS)
+    let output_fn = case o_file of { Just s -> s; Nothing -> "main.exe"; }
+#else
     let output_fn = case o_file of { Just s -> s; Nothing -> "a.out"; }
+#endif
 
-    pkg_lib_paths <- getPackageLibraryPath dep_packages
+    pkg_lib_paths <- getPackageLibraryPath dflags dep_packages
     let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
 
     lib_paths <- readIORef v_Library_paths
     let lib_path_opts = map ("-L"++) lib_paths
 
-    pkg_link_opts <- getPackageLinkOpts dep_packages
+    pkg_link_opts <- getPackageLinkOpts dflags dep_packages
 
 #ifdef darwin_TARGET_OS
-    pkg_framework_paths <- getPackageFrameworkPath dep_packages
+    pkg_framework_paths <- getPackageFrameworkPath dflags dep_packages
     let pkg_framework_path_opts = map ("-F"++) pkg_framework_paths
 
     framework_paths <- readIORef v_Framework_paths
     let framework_path_opts = map ("-F"++) framework_paths
 
-    pkg_frameworks <- getPackageFrameworks dep_packages
+    pkg_frameworks <- getPackageFrameworks dflags dep_packages
     let pkg_framework_opts = concat [ ["-framework", fw] | fw <- pkg_frameworks ]
-
     frameworks <- readIORef v_Cmdline_frameworks
     let framework_opts = concat [ ["-framework", fw] | fw <- reverse frameworks ]
         -- reverse because they're added in reverse order from the cmd line
@@ -1025,22 +1113,39 @@ staticLink o_files dep_packages = do
        -- opts from -optl-<blah> (including -l<blah> options)
     extra_ld_opts <- getStaticOpts v_Opt_l
 
-    [rts_pkg, std_pkg] <- getPackageDetails [rtsPackage, basePackage]
+    ways <- readIORef v_Ways
 
-    let extra_os = if static || no_hs_main
-                   then []
-                   else [ head (library_dirs rts_pkg) ++ "/Main.dll_o",
-                          head (library_dirs std_pkg) ++ "/PrelMain.dll_o" ]
+    -- Here are some libs that need to be linked at the *end* of
+    -- the command line, because they contain symbols that are referred to
+    -- by the RTS.  We can't therefore use the ordinary way opts for these.
+    let
+       debug_opts | WayDebug `elem` ways = [ 
+#if defined(HAVE_LIBBFD)
+                       "-lbfd", "-liberty"
+#endif
+                        ]
+                  | otherwise            = []
 
-    (md_c_flags, _) <- machdepCCOpts
-    SysTools.runLink ( [ SysTools.Option verb
+    let
+       thread_opts | WayThreaded `elem` ways = [ 
+#if !defined(mingw32_TARGET_OS) && !defined(freebsd_TARGET_OS)
+                       "-lpthread"
+#endif
+#if defined(osf3_TARGET_OS)
+                       , "-lexc"
+#endif
+                       ]
+                   | otherwise               = []
+
+    (md_c_flags, _) <- machdepCCOpts dflags
+    SysTools.runLink dflags ( 
+                      [ SysTools.Option verb
                       , SysTools.Option "-o"
                       , SysTools.FileOption "" output_fn
                       ]
                      ++ map SysTools.Option (
                         md_c_flags
                      ++ o_files
-                     ++ extra_os
                      ++ extra_ld_inputs
                      ++ lib_path_opts
                      ++ extra_ld_opts
@@ -1054,6 +1159,8 @@ staticLink o_files dep_packages = do
                      ++ pkg_framework_path_opts
                      ++ pkg_framework_opts
 #endif
+                     ++ debug_opts
+                     ++ thread_opts
                    ))
 
     -- parallel only: move binary to another dir -- HWL
@@ -1066,22 +1173,22 @@ staticLink o_files dep_packages = do
 -----------------------------------------------------------------------------
 -- Making a DLL (only for Win32)
 
-doMkDLL :: [String] -> [PackageName] -> IO ()
-doMkDLL o_files dep_packages = do
-    verb       <- getVerbFlag
+doMkDLL :: DynFlags -> [String] -> [PackageId] -> IO ()
+doMkDLL dflags o_files dep_packages = do
+    let verb = getVerbFlag dflags
     static     <- readIORef v_Static
     no_hs_main <- readIORef v_NoHsMain
 
     o_file <- readIORef v_Output_file
     let output_fn = case o_file of { Just s -> s; Nothing -> "HSdll.dll"; }
 
-    pkg_lib_paths <- getPackageLibraryPath dep_packages
+    pkg_lib_paths <- getPackageLibraryPath dflags dep_packages
     let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
 
     lib_paths <- readIORef v_Library_paths
     let lib_path_opts = map ("-L"++) lib_paths
 
-    pkg_link_opts <- getPackageLinkOpts dep_packages
+    pkg_link_opts <- getPackageLinkOpts dflags dep_packages
 
        -- probably _stub.o files
     extra_ld_inputs <- readIORef v_Ld_inputs
@@ -1089,15 +1196,21 @@ doMkDLL o_files dep_packages = do
        -- opts from -optdll-<blah>
     extra_ld_opts <- getStaticOpts v_Opt_dll
 
-    [rts_pkg, std_pkg] <- getPackageDetails [rtsPackage, basePackage]
+    let pstate = pkgState dflags
+       rts_id | ExtPackage id <- rtsPackageId pstate = id
+              | otherwise = panic "staticLink: rts package missing"
+       base_id | ExtPackage id <- basePackageId pstate = id
+               | otherwise = panic "staticLink: base package missing"
+       rts_pkg  = getPackageDetails pstate rts_id
+        base_pkg = getPackageDetails pstate base_id
 
     let extra_os = if static || no_hs_main
                    then []
-                   else [ head (library_dirs rts_pkg) ++ "/Main.dll_o",
-                          head (library_dirs std_pkg) ++ "/PrelMain.dll_o" ]
+                   else [ head (libraryDirs rts_pkg) ++ "/Main.dll_o",
+                          head (libraryDirs base_pkg) ++ "/PrelMain.dll_o" ]
 
-    (md_c_flags, _) <- machdepCCOpts
-    SysTools.runMkDLL
+    (md_c_flags, _) <- machdepCCOpts dflags
+    SysTools.runMkDLL dflags
         ([ SysTools.Option verb
          , SysTools.Option "-o"
          , SysTools.FileOption "" output_fn
@@ -1120,29 +1233,86 @@ doMkDLL o_files dep_packages = do
 -- -----------------------------------------------------------------------------
 -- Misc.
 
-hscNextPhase :: HscLang -> IO Phase
-hscNextPhase hsc_lang = do
+doCpp :: DynFlags -> Bool -> Bool -> FilePath -> FilePath -> IO ()
+doCpp dflags raw include_cc_opts input_fn output_fn = do
+    let hscpp_opts = getOpts dflags opt_P
+
+    cmdline_include_paths <- readIORef v_Include_paths
+
+    pkg_include_dirs <- getPackageIncludePath dflags []
+    let include_paths = foldr (\ x xs -> "-I" : x : xs) []
+                         (cmdline_include_paths ++ pkg_include_dirs)
+
+    let verb = getVerbFlag dflags
+
+    cc_opts <- if not include_cc_opts 
+                 then return []
+                 else do let optc = getOpts dflags opt_c
+                         (md_c_flags, _) <- machdepCCOpts dflags
+                         return (optc ++ md_c_flags)
+
+    let cpp_prog args | raw       = SysTools.runCpp dflags args
+                     | otherwise = SysTools.runCc dflags (SysTools.Option "-E" : args)
+
+    let target_defs = 
+         [ "-D" ++ HOST_OS     ++ "BUILD_OS=1",
+           "-D" ++ HOST_ARCH   ++ "BUILD_ARCH=1",
+           "-D" ++ TARGET_OS   ++ "HOST_OS=1",
+           "-D" ++ TARGET_ARCH ++ "HOST_ARCH=1" ]
+       -- remember, in code we *compile*, the HOST is the same our TARGET,
+       -- and BUILD is the same as our HOST.
+
+    cpp_prog       ([SysTools.Option verb]
+                   ++ map SysTools.Option include_paths
+                   ++ map SysTools.Option hsSourceCppOpts
+                   ++ map SysTools.Option hscpp_opts
+                   ++ map SysTools.Option cc_opts
+                   ++ map SysTools.Option target_defs
+                   ++ [ SysTools.Option     "-x"
+                      , SysTools.Option     "c"
+                      , SysTools.Option     input_fn
+       -- We hackily use Option instead of FileOption here, so that the file
+       -- name is not back-slashed on Windows.  cpp is capable of
+       -- dealing with / in filenames, so it works fine.  Furthermore
+       -- if we put in backslashes, cpp outputs #line directives
+       -- with *double* backslashes.   And that in turn means that
+       -- our error messages get double backslashes in them.
+       -- In due course we should arrange that the lexer deals
+       -- with these \\ escapes properly.
+                      , SysTools.Option     "-o"
+                      , SysTools.FileOption "" output_fn
+                      ])
+
+-- -----------------------------------------------------------------------------
+-- Misc.
+
+hscNextPhase :: HscSource -> HscTarget -> IO Phase
+hscNextPhase HsBootFile hsc_lang 
+  = return StopLn
+
+hscNextPhase other hsc_lang = do
   split <- readIORef v_Split_object_files
   return (case hsc_lang of
                HscC -> HCc
                HscAsm | split -> SplitMangle
                       | otherwise -> As
-               HscNothing     -> HCc  -- dummy (no output will be generated)
-               HscInterpreted -> HCc  -- "" ""
-               _other         -> HCc  -- "" ""
+               HscNothing     -> StopLn
+               HscInterpreted -> StopLn
+               _other         -> StopLn
        )
 
-hscMaybeAdjustLang :: HscLang -> IO HscLang
-hscMaybeAdjustLang current_hsc_lang = do
-  todo    <- readIORef v_GhcMode
-  keep_hc <- readIORef v_Keep_hc_files
-  let hsc_lang
-       -- don't change the lang if we're interpreting
-        | current_hsc_lang == HscInterpreted = current_hsc_lang
-       -- force -fvia-C if we are being asked for a .hc file
-        | todo == StopBefore HCc  || keep_hc = HscC
-       -- force -fvia-C when profiling or ticky-ticky is on
-        | opt_SccProfilingOn || opt_DoTickyProfiling = HscC
-       -- otherwise, stick to the plan
-        | otherwise = current_hsc_lang
-  return hsc_lang
+hscMaybeAdjustTarget :: GhcMode -> HscSource -> HscTarget -> IO HscTarget
+hscMaybeAdjustTarget todo HsBootFile current_hsc_lang 
+  = return HscNothing          -- No output (other than Foo.hi-boot) for hs-boot files
+hscMaybeAdjustTarget todo other current_hsc_lang 
+  = do { keep_hc <- readIORef v_Keep_hc_files
+       ; let hsc_lang
+               -- don't change the lang if we're interpreting
+                | current_hsc_lang == HscInterpreted = current_hsc_lang
+
+               -- force -fvia-C if we are being asked for a .hc file
+                | StopBefore HCc <- todo = HscC
+                | keep_hc                = HscC
+               -- otherwise, stick to the plan
+                | otherwise = current_hsc_lang
+       ; return hsc_lang }