1 {-# OPTIONS -fno-warn-incomplete-patterns #-}
2 -----------------------------------------------------------------------------
3 -- $Id: Main.hs,v 1.47 2001/01/16 12:41:03 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(..), haskellish_file )
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
79 -----------------------------------------------------------------------------
80 -- Differences vs. old driver:
82 -- No more "Enter your Haskell program, end with ^D (on a line of its own):"
83 -- consistency checking removed (may do this properly later)
87 -----------------------------------------------------------------------------
91 -- top-level exception handler: any unrecognised exception is a compiler bug.
92 handle (\exception -> do hPutStr stderr (show (Panic (show exception)))
93 exitWith (ExitFailure 1)
96 -- all error messages are propagated as exceptions
97 handleDyn (\dyn -> case dyn of
98 PhaseFailed _phase code -> exitWith code
99 Interrupted -> exitWith (ExitFailure 1)
100 _ -> do hPutStrLn stderr (show (dyn :: GhcException))
101 exitWith (ExitFailure 1)
104 -- make sure we clean up after ourselves
105 later (do forget_it <- readIORef v_Keep_tmp_files
106 unless forget_it $ do
107 verb <- dynFlag verbosity
108 cleanTempFiles (verb >= 2)
110 -- exceptions will be blocked while we clean the temporary files,
111 -- so there shouldn't be any difficulty if we receive further
114 -- install signal handlers
115 main_thread <- myThreadId
116 #ifndef mingw32_TARGET_OS
117 let sig_handler = Catch (throwTo main_thread
118 (DynException (toDyn Interrupted)))
119 installHandler sigQUIT sig_handler Nothing
120 installHandler sigINT sig_handler Nothing
125 -- grab any -B options from the command line first
126 argv' <- setTopDir argv
127 top_dir <- readIORef v_TopDir
129 let installed s = top_dir ++ '/':s
130 inplace s = top_dir ++ '/':cCURRENT_DIR ++ '/':s
132 installed_pkgconfig = installed ("package.conf")
133 inplace_pkgconfig = inplace (cGHC_DRIVER_DIR ++ "/package.conf.inplace")
135 -- discover whether we're running in a build tree or in an installation,
136 -- by looking for the package configuration file.
137 am_installed <- doesFileExist installed_pkgconfig
140 then writeIORef v_Path_package_config installed_pkgconfig
141 else do am_inplace <- doesFileExist inplace_pkgconfig
143 then writeIORef v_Path_package_config inplace_pkgconfig
144 else throwDyn (OtherError "can't find package.conf")
146 -- set the location of our various files
148 then do writeIORef v_Path_usage (installed "ghc-usage.txt")
149 writeIORef v_Pgm_L (installed "unlit")
150 writeIORef v_Pgm_m (installed "ghc-asm")
151 writeIORef v_Pgm_s (installed "ghc-split")
153 else do writeIORef v_Path_usage (inplace (cGHC_DRIVER_DIR ++ "/ghc-usage.txt"))
154 writeIORef v_Pgm_L (inplace cGHC_UNLIT)
155 writeIORef v_Pgm_m (inplace cGHC_MANGLER)
156 writeIORef v_Pgm_s (inplace cGHC_SPLIT)
158 -- read the package configuration
159 conf_file <- readIORef v_Path_package_config
160 contents <- readFile conf_file
161 let pkg_details = read contents -- ToDo: faster
162 writeIORef v_Package_details pkg_details
164 -- find the phase to stop after (i.e. -E, -C, -c, -S flags)
165 (flags2, mode, stop_flag) <- getGhcMode argv'
166 writeIORef v_GhcMode mode
168 -- process all the other arguments, and get the source files
169 non_static <- processArgs static_flags flags2 []
171 -- Find the build tag, and re-process the build-specific options.
172 -- Also add in flags for unregisterised compilation, if
173 -- GhcUnregisterised=YES.
174 way_opts <- findBuildTag
175 let unreg_opts | cGhcUnregisterised == "YES" = unregFlags
177 way_non_static <- processArgs static_flags (unreg_opts ++ way_opts) []
179 -- give the static flags to hsc
180 static_opts <- buildStaticHscOpts
181 writeIORef v_Static_hsc_opts static_opts
184 warn_level <- readIORef v_Warning_opt
186 let warn_opts = case warn_level of
187 W_default -> standardWarnings
189 W_all -> minusWallOpts
192 -- build the default DynFlags (these may be adjusted on a per
193 -- module basis by OPTIONS pragmas and settings in the interpreter).
195 core_todo <- buildCoreToDo
196 stg_todo <- buildStgToDo
198 -- set the "global" HscLang. The HscLang can be further adjusted on a module
199 -- by module basis, using only the -fvia-C and -fasm flags. If the global
200 -- HscLang is not HscC or HscAsm, -fvia-C and -fasm have no effect.
201 opt_level <- readIORef v_OptLevel
202 let lang = case mode of
203 StopBefore HCc -> HscC
204 DoInteractive -> HscInterpreted
205 _other | opt_level >= 1 -> HscC -- -O implies -fvia-C
206 | otherwise -> defaultHscLang
208 writeIORef v_DynFlags
209 DynFlags{ coreToDo = core_todo,
212 -- leave out hscOutName for now
213 hscOutName = panic "Main.main:hscOutName not set",
215 verbosity = case mode of
222 -- the rest of the arguments are "dynamic"
223 srcs <- processArgs dynamic_flags (way_non_static ++
224 non_static ++ warn_opts) []
225 -- save the "initial DynFlags" away
226 init_dyn_flags <- readIORef v_DynFlags
227 writeIORef v_InitDynFlags init_dyn_flags
229 -- complain about any unknown flags
230 mapM unknownFlagErr [ f | f@('-':_) <- srcs ]
232 -- save the flag state, because this could be modified by OPTIONS
233 -- pragmas during the compilation, and we'll need to restore it
234 -- before starting the next compilation.
235 saved_driver_state <- readIORef v_Driver_state
236 writeIORef v_InitDriverState saved_driver_state
238 verb <- dynFlag verbosity
241 (do hPutStr stderr "Glasgow Haskell Compiler, Version "
242 hPutStr stderr cProjectVersion
243 hPutStr stderr ", for Haskell 98, compiled by GHC version "
244 hPutStrLn stderr cBooterVersion)
247 (hPutStrLn stderr ("Using package config file: " ++ conf_file))
250 (hPutStrLn stderr ("Hsc static flags: " ++ unwords static_opts))
252 -- initialise the finder
253 pkg_avails <- getPackageInfo
254 initFinder pkg_avails
256 -- mkdependHS is special
257 when (mode == DoMkDependHS) beginMkDependHS
259 -- make/interactive require invoking the compilation manager
260 if (mode == DoMake) then beginMake srcs else do
261 if (mode == DoInteractive) then beginInteractive srcs else do
264 o_file <- readIORef v_Output_file
265 ohi <- readIORef v_Output_hi
266 if length srcs > 1 && (isJust ohi || (isJust o_file && mode /= DoLink && mode /= DoMkDLL))
267 then throwDyn (UsageError "can't apply -o or -ohi options to multiple source files")
270 if null srcs then throwDyn (UsageError "no input files") else do
272 let compileFile src = do
273 writeIORef v_Driver_state saved_driver_state
274 writeIORef v_DynFlags init_dyn_flags
276 -- We compile in two stages, because the file may have an
277 -- OPTIONS pragma that affects the compilation pipeline (eg. -fvia-C)
279 let (basename, suffix) = splitFilename src
282 pp <- if not (haskellish_file src) || mode == StopBefore Hsc
283 then return src else do
284 phases <- genPipeline (StopBefore Hsc) stop_flag
285 False{-not persistent-} defaultHscLang src
286 pipeLoop phases src False{-no linking-} False{-no -o flag-}
289 -- rest of compilation
290 dyn_flags <- readIORef v_DynFlags
291 phases <- genPipeline mode stop_flag True (hscLang dyn_flags) pp
292 r <- pipeLoop phases pp (mode==DoLink || mode==DoMkDLL) True{-use -o flag-}
296 o_files <- mapM compileFile srcs
298 when (mode == DoMkDependHS) endMkDependHS
299 when (mode == DoLink) (doLink o_files)
300 when (mode == DoMkDLL) (doMkDLL o_files)
302 -- grab the last -B option on the command line, and
303 -- set topDir to its value.
304 setTopDir :: [String] -> IO [String]
306 let (minusbs, others) = partition (prefixMatch "-B") args
308 [] -> writeIORef v_TopDir clibdir
309 some -> writeIORef v_TopDir (drop 2 (last some)))
312 beginMake :: [String] -> IO ()
315 [] -> throwDyn (UsageError "no input files")
316 [mod] -> do state <- cmInit Batch
317 cmLoadModule state mod
319 _ -> throwDyn (UsageError "only one module allowed with --make")
322 beginInteractive :: [String] -> IO ()
324 beginInteractive = throwDyn (OtherError "not build for interactive use")
326 beginInteractive mods
327 = do state <- cmInit Interactive
328 let mod = case mods of
331 _ -> throwDyn (UsageError
332 "only one module allowed with --interactive")
333 interactiveUI state mod