[project @ 2001-08-03 23:38:50 by ken]
[ghc-hetmet.git] / ghc / compiler / main / Main.hs
index 30dffc3..5a7d6c5 100644 (file)
@@ -1,6 +1,6 @@
 {-# OPTIONS -fno-warn-incomplete-patterns #-}
 -----------------------------------------------------------------------------
--- $Id: Main.hs,v 1.76 2001/06/27 11:39:54 simonmar Exp $
+-- $Id: Main.hs,v 1.83 2001/08/03 23:38:50 ken Exp $
 --
 -- GHC Driver program
 --
@@ -45,7 +45,7 @@ import DriverMkDepend ( beginMkDependHS, endMkDependHS )
 import DriverPhases    ( Phase(Hsc, HCc), haskellish_src_file, objish_file )
 
 import DriverUtil      ( add, handle, handleDyn, later, splitFilename,
-                         unknownFlagErr )
+                         unknownFlagErr, getFileSuffix )
 import CmdLineOpts     ( dynFlag, defaultDynFlags, restoreDynFlags,
                          saveDynFlags, setDynFlags, 
                          DynFlags(..), HscLang(..), v_Static_hsc_opts
@@ -59,7 +59,7 @@ import Panic          ( GhcException(..), panic )
 import IO
 import Directory       ( doesFileExist )
 import IOExts          ( readIORef, writeIORef )
-import Exception       ( throwDyn, Exception(DynException) )
+import Exception       ( throwDyn, Exception(..) )
 import System          ( getArgs, exitWith, ExitCode(..) )
 import Monad
 import List
@@ -67,7 +67,7 @@ import Maybe
 
 #ifndef mingw32_TARGET_OS
 import Concurrent      ( myThreadId )
-#ifdef __GLASGOW_HASKELL__ < 500
+#if __GLASGOW_HASKELL__ < 500
 import Exception        ( raiseInThread )
 #define throwTo  raiseInThread
 #else
@@ -111,13 +111,23 @@ import Dynamic            ( toDyn )
 
 main =
   -- top-level exception handler: any unrecognised exception is a compiler bug.
-  handle (\exception -> do hPutStr stderr (show (Panic (show exception)))
-                          exitWith (ExitFailure 1)
+  handle (\exception -> do
+          case exception of
+               -- an IO exception probably isn't our fault, so don't panic
+               IOException _   ->  hPutStr stderr (show exception)
+#if __GLASGOW_HASKELL__ >= 501
+                 -- let exit exceptions bubble all the way out.
+               ExitException e ->  exitWith e
+#endif
+               _other          ->  hPutStr stderr (show (Panic (show exception)))
+          exitWith (ExitFailure 1)
          ) $ do
 
   -- all error messages are propagated as exceptions
   handleDyn (\dyn -> case dyn of
-                         PhaseFailed _phase code -> exitWith code
+                         PhaseFailed _phase code -> do
+                                       hPutStr stderr "\nCompilation had errors\n"
+                                       exitWith code
                          Interrupted -> exitWith (ExitFailure 1)
                          _ -> do hPutStrLn stderr (show (dyn :: GhcException))
                                  exitWith (ExitFailure 1)
@@ -156,12 +166,6 @@ main =
    (flags2, mode, stop_flag) <- getGhcMode argv'
    writeIORef v_GhcMode mode
 
-       -- Show the GHCi banner?
-#  ifdef GHCI
-   when (mode == DoInteractive) $
-      hPutStrLn stdout ghciWelcomeMsg
-#  endif
-
        -- process all the other arguments, and get the source files
    non_static <- processArgs static_flags flags2 []
 
@@ -229,9 +233,15 @@ main =
        -- complain about any unknown flags
    mapM unknownFlagErr [ f | f@('-':_) <- srcs ]
 
-       -- Display details of the configuration in verbose mode
    verb <- dynFlag verbosity
 
+       -- Show the GHCi banner
+#  ifdef GHCI
+   when (mode == DoInteractive && verb >= 1) $
+      hPutStrLn stdout ghciWelcomeMsg
+#  endif
+
+       -- Display details of the configuration in verbose mode
    when (verb >= 2) 
        (do hPutStr stderr "Glasgow Haskell Compiler, Version "
            hPutStr stderr cProjectVersion
@@ -284,18 +294,20 @@ main =
          let (basename, suffix) = splitFilename src
 
          -- just preprocess (Haskell source only)
+         let src_and_suff = (src, getFileSuffix src)
          pp <- if not (haskellish_src_file src) || mode == StopBefore Hsc
-                       then return src else do
+                       then return src_and_suff else do
                phases <- genPipeline (StopBefore Hsc) stop_flag
-                           False{-not persistent-} defaultHscLang src
-               pipeLoop phases src False{-no linking-} False{-no -o flag-}
+                           False{-not persistent-} defaultHscLang
+                           src_and_suff
+               pipeLoop phases src_and_suff False{-no linking-} False{-no -o flag-}
                        basename suffix
 
          -- rest of compilation
          hsc_lang <- dynFlag hscLang
-         phases <- genPipeline mode stop_flag True hsc_lang pp
-         r <- pipeLoop phases pp (mode==DoLink || mode==DoMkDLL) True{-use -o flag-}
-                       basename suffix
+         phases   <- genPipeline mode stop_flag True hsc_lang pp
+         (r,_)    <- pipeLoop phases pp (mode==DoLink || mode==DoMkDLL)
+                                     True{-use -o flag-} basename suffix
          return r
 
    o_files <- mapM compileFile srcs
@@ -314,7 +326,8 @@ beginMake fileish_args
        case mods of
         []    -> throwDyn (UsageError "no input files")
         mod   -> do state <- cmInit Batch
-                    cmLoadModule state mods
+                    (_, ok, _) <- cmLoadModule state mods
+                    when (not ok) (exitWith (ExitFailure 1))
                     return ()