1 {-# OPTIONS -fno-warn-incomplete-patterns #-}
2 -----------------------------------------------------------------------------
3 -- $Id: Main.hs,v 1.39 2000/12/12 14:42:43 simonmar Exp $
7 -- (c) Simon Marlow 2000
9 -----------------------------------------------------------------------------
11 -- with path so that ghc -M can find config.h
12 #include "../includes/config.h"
14 module Main (main) where
16 #include "HsVersions.h"
24 #ifndef mingw32_TARGET_OS
36 import DriverPhases ( Phase(..) )
37 import CmdLineOpts ( HscLang(..), DynFlags(..), v_Static_hsc_opts )
39 import Finder ( initFinder )
58 -----------------------------------------------------------------------------
61 -- * -fglasgow-exts NO LONGER IMPLIES -package lang!!! (-fglasgow-exts is a
62 -- dynamic flag whereas -package is a static flag.)
64 -----------------------------------------------------------------------------
68 -- new mkdependHS doesn't support all the options that the old one did (-X et al.)
69 -- time commands when run with -v
74 -- Win32 support: proper signal handling
75 -- make sure OPTIONS in .hs file propogate to .hc file if -C or -keep-hc-file-too
76 -- reading the package configuration file is too slow
77 -- -H, -K, -Rghc-timing
79 -- -ddump-all doesn't do anything
81 -----------------------------------------------------------------------------
82 -- Differences vs. old driver:
84 -- No more "Enter your Haskell program, end with ^D (on a line of its own):"
85 -- consistency checking removed (may do this properly later)
87 -- no hi diffs (could be added later)
90 -----------------------------------------------------------------------------
94 -- top-level exception handler: any unrecognised exception is a compiler bug.
95 handle (\exception -> hPutStr stderr (show (Panic (show exception)))) $ do
97 -- all error messages are propagated as exceptions
98 handleDyn (\dyn -> case dyn of
99 PhaseFailed _phase code -> exitWith code
100 Interrupted -> exitWith (ExitFailure 1)
101 _ -> do hPutStrLn stderr (show (dyn :: GhcException))
102 exitWith (ExitFailure 1)
105 -- make sure we clean up after ourselves
106 later (do forget_it <- readIORef v_Keep_tmp_files
107 unless forget_it $ do
108 verb <- dynFlag verbosity
109 cleanTempFiles (verb >= 2)
111 -- exceptions will be blocked while we clean the temporary files,
112 -- so there shouldn't be any difficulty if we receive further
115 -- install signal handlers
116 main_thread <- myThreadId
117 #ifndef mingw32_TARGET_OS
118 let sig_handler = Catch (throwTo main_thread
119 (DynException (toDyn Interrupted)))
120 installHandler sigQUIT sig_handler Nothing
121 installHandler sigINT sig_handler Nothing
126 -- grab any -B options from the command line first
127 argv' <- setTopDir argv
128 top_dir <- readIORef v_TopDir
130 let installed s = top_dir ++ '/':s
131 inplace s = top_dir ++ '/':cCURRENT_DIR ++ '/':s
133 installed_pkgconfig = installed ("package.conf")
134 inplace_pkgconfig = inplace (cGHC_DRIVER_DIR ++ "/package.conf.inplace")
136 -- discover whether we're running in a build tree or in an installation,
137 -- by looking for the package configuration file.
138 am_installed <- doesFileExist installed_pkgconfig
141 then writeIORef v_Path_package_config installed_pkgconfig
142 else do am_inplace <- doesFileExist inplace_pkgconfig
144 then writeIORef v_Path_package_config inplace_pkgconfig
145 else throwDyn (OtherError "can't find package.conf")
147 -- set the location of our various files
149 then do writeIORef v_Path_usage (installed "ghc-usage.txt")
150 writeIORef v_Pgm_L (installed "unlit")
151 writeIORef v_Pgm_m (installed "ghc-asm")
152 writeIORef v_Pgm_s (installed "ghc-split")
154 else do writeIORef v_Path_usage (inplace (cGHC_DRIVER_DIR ++ "/ghc-usage.txt"))
155 writeIORef v_Pgm_L (inplace cGHC_UNLIT)
156 writeIORef v_Pgm_m (inplace cGHC_MANGLER)
157 writeIORef v_Pgm_s (inplace cGHC_SPLIT)
159 -- read the package configuration
160 conf_file <- readIORef v_Path_package_config
161 contents <- readFile conf_file
162 let pkg_details = read contents -- ToDo: faster
163 writeIORef v_Package_details pkg_details
165 -- find the phase to stop after (i.e. -E, -C, -c, -S flags)
166 (flags2, mode, stop_flag) <- getGhcMode argv'
167 writeIORef v_GhcMode mode
169 -- process all the other arguments, and get the source files
170 non_static <- processArgs static_flags flags2 []
172 -- find the build tag, and re-process the build-specific options
173 more_opts <- findBuildTag
174 way_non_static <- processArgs static_flags more_opts []
176 -- give the static flags to hsc
177 static_opts <- buildStaticHscOpts
178 writeIORef v_Static_hsc_opts static_opts
181 warn_level <- readIORef v_Warning_opt
183 let warn_opts = case warn_level of
184 W_default -> standardWarnings
186 W_all -> minusWallOpts
189 -- build the default DynFlags (these may be adjusted on a per
190 -- module basis by OPTIONS pragmas and settings in the interpreter).
192 core_todo <- buildCoreToDo
193 stg_todo <- buildStgToDo
195 -- set the "global" HscLang. The HscLang can be further adjusted on a module
196 -- by module basis, using only the -fvia-C and -fasm flags. If the global
197 -- HscLang is not HscC or HscAsm, -fvia-C and -fasm have no effect.
198 opt_level <- readIORef v_OptLevel
199 let lang = case mode of
200 StopBefore HCc -> HscC
201 DoInteractive -> HscInterpreted
202 _other | opt_level >= 1 -> HscC -- -O implies -fvia-C
203 | otherwise -> defaultHscLang
205 writeIORef v_DynFlags
206 DynFlags{ coreToDo = core_todo,
209 -- leave out hscOutName for now
210 hscOutName = panic "Main.main:hscOutName not set",
212 verbosity = case mode of
219 -- the rest of the arguments are "dynamic"
220 srcs <- processArgs dynamic_flags (way_non_static ++
221 non_static ++ warn_opts) []
222 -- save the "initial DynFlags" away
223 init_dyn_flags <- readIORef v_DynFlags
224 writeIORef v_InitDynFlags init_dyn_flags
226 -- complain about any unknown flags
227 mapM unknownFlagErr [ f | f@('-':_) <- srcs ]
229 -- save the flag state, because this could be modified by OPTIONS
230 -- pragmas during the compilation, and we'll need to restore it
231 -- before starting the next compilation.
232 saved_driver_state <- readIORef v_Driver_state
233 writeIORef v_InitDriverState saved_driver_state
235 verb <- dynFlag verbosity
238 (do hPutStr stderr "Glasgow Haskell Compiler, Version "
239 hPutStr stderr cProjectVersion
240 hPutStr stderr ", for Haskell 98, compiled by GHC version "
241 hPutStrLn stderr cBooterVersion)
244 (hPutStrLn stderr ("Using package config file: " ++ conf_file))
246 -- initialise the finder
247 pkg_avails <- getPackageInfo
248 initFinder pkg_avails
250 -- mkdependHS is special
251 when (mode == DoMkDependHS) beginMkDependHS
253 -- make/interactive require invoking the compilation manager
254 if (mode == DoMake) then beginMake srcs else do
255 if (mode == DoInteractive) then beginInteractive srcs else do
257 -- for each source file, find which phases to run
258 let lang = hscLang init_dyn_flags
259 pipelines <- mapM (genPipeline mode stop_flag True lang) srcs
260 let src_pipelines = zip srcs pipelines
263 o_file <- readIORef v_Output_file
264 ohi <- readIORef v_Output_hi
265 if length srcs > 1 && (isJust ohi || (isJust o_file && mode /= DoLink))
266 then throwDyn (UsageError "can't apply -o or -ohi options to multiple source files")
269 if null srcs then throwDyn (UsageError "no input files") else do
271 let compileFile (src, phases) = do
272 writeIORef v_Driver_state saved_driver_state
273 writeIORef v_DynFlags init_dyn_flags
274 r <- runPipeline phases src (mode==DoLink) True
277 o_files <- mapM compileFile src_pipelines
279 when (mode == DoMkDependHS) endMkDependHS
280 when (mode == DoLink) (doLink o_files)
282 -- grab the last -B option on the command line, and
283 -- set topDir to its value.
284 setTopDir :: [String] -> IO [String]
286 let (minusbs, others) = partition (prefixMatch "-B") args
288 [] -> writeIORef v_TopDir clibdir
289 some -> writeIORef v_TopDir (drop 2 (last some)))
292 beginMake :: [String] -> IO ()
295 [] -> throwDyn (UsageError "no input files")
296 [mod] -> do state <- cmInit Batch
297 cmLoadModule state mod
299 _ -> throwDyn (UsageError "only one module allowed with --make")
302 beginInteractive :: [String] -> IO ()
304 beginInteractive = throwDyn (OtherError "not build for interactive use")
306 beginInteractive mods
307 = do state <- cmInit Interactive
308 let mod = case mods of
311 _ -> throwDyn (UsageError
312 "only one module allowed with --interactive")
313 interactiveUI state mod