1 {-# OPTIONS -fno-warn-incomplete-patterns #-}
2 -----------------------------------------------------------------------------
3 -- $Id: Main.hs,v 1.51 2001/02/13 15:51:57 sewardj 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"
23 #ifndef mingw32_TARGET_OS
35 import DriverPhases ( Phase(..), haskellish_file )
38 import Finder ( initFinder )
52 import Char ( toLower )
57 -----------------------------------------------------------------------------
60 -- * -fglasgow-exts NO LONGER IMPLIES -package lang!!! (-fglasgow-exts is a
61 -- dynamic flag whereas -package is a static flag.)
63 -----------------------------------------------------------------------------
67 -- new mkdependHS doesn't support all the options that the old one did (-X et al.)
68 -- time commands when run with -v
73 -- Win32 support: proper signal handling
74 -- make sure OPTIONS in .hs file propogate to .hc file if -C or -keep-hc-file-too
75 -- reading the package configuration file is too slow
78 -----------------------------------------------------------------------------
79 -- Differences vs. old driver:
81 -- No more "Enter your Haskell program, end with ^D (on a line of its own):"
82 -- consistency checking removed (may do this properly later)
86 -----------------------------------------------------------------------------
90 -- top-level exception handler: any unrecognised exception is a compiler bug.
91 handle (\exception -> do hPutStr stderr (show (Panic (show exception)))
92 exitWith (ExitFailure 1)
95 -- all error messages are propagated as exceptions
96 handleDyn (\dyn -> case dyn of
97 PhaseFailed _phase code -> exitWith code
98 Interrupted -> exitWith (ExitFailure 1)
99 _ -> do hPutStrLn stderr (show (dyn :: GhcException))
100 exitWith (ExitFailure 1)
103 -- make sure we clean up after ourselves
104 later (do forget_it <- readIORef v_Keep_tmp_files
105 unless forget_it $ do
106 verb <- dynFlag verbosity
109 -- exceptions will be blocked while we clean the temporary files,
110 -- so there shouldn't be any difficulty if we receive further
113 -- install signal handlers
114 main_thread <- myThreadId
115 #ifndef mingw32_TARGET_OS
116 let sig_handler = Catch (throwTo main_thread
117 (DynException (toDyn Interrupted)))
118 installHandler sigQUIT sig_handler Nothing
119 installHandler sigINT sig_handler Nothing
124 -- grab any -B options from the command line first
125 argv' <- setTopDir argv
126 top_dir <- readIORef v_TopDir
128 let installed s = top_dir ++ '/':s
129 inplace s = top_dir ++ '/':cCURRENT_DIR ++ '/':s
131 installed_pkgconfig = installed ("package.conf")
132 inplace_pkgconfig = inplace (cGHC_DRIVER_DIR ++ "/package.conf.inplace")
134 -- discover whether we're running in a build tree or in an installation,
135 -- by looking for the package configuration file.
136 am_installed <- doesFileExist installed_pkgconfig
139 then writeIORef v_Path_package_config installed_pkgconfig
140 else do am_inplace <- doesFileExist inplace_pkgconfig
142 then writeIORef v_Path_package_config inplace_pkgconfig
143 else throwDyn (OtherError "can't find package.conf")
145 -- set the location of our various files
147 then do writeIORef v_Path_usage (installed "ghc-usage.txt")
148 writeIORef v_Pgm_L (installed "unlit")
149 writeIORef v_Pgm_m (installed "ghc-asm")
150 writeIORef v_Pgm_s (installed "ghc-split")
152 else do writeIORef v_Path_usage (inplace (cGHC_DRIVER_DIR ++ "/ghc-usage.txt"))
153 writeIORef v_Pgm_L (inplace cGHC_UNLIT)
154 writeIORef v_Pgm_m (inplace cGHC_MANGLER)
155 writeIORef v_Pgm_s (inplace cGHC_SPLIT)
157 -- read the package configuration
158 conf_file <- readIORef v_Path_package_config
159 contents <- readFile conf_file
160 let pkg_details = read contents -- ToDo: faster
161 writeIORef v_Package_details pkg_details
163 -- find the phase to stop after (i.e. -E, -C, -c, -S flags)
164 (flags2, mode, stop_flag) <- getGhcMode argv'
165 writeIORef v_GhcMode mode
167 -- process all the other arguments, and get the source files
168 non_static <- processArgs static_flags flags2 []
170 -- Find the build tag, and re-process the build-specific options.
171 -- Also add in flags for unregisterised compilation, if
172 -- GhcUnregisterised=YES.
173 way_opts <- findBuildTag
174 let unreg_opts | cGhcUnregisterised == "YES" = unregFlags
176 way_non_static <- processArgs static_flags (unreg_opts ++ way_opts) []
178 -- give the static flags to hsc
179 static_opts <- buildStaticHscOpts
180 writeIORef v_Static_hsc_opts static_opts
183 warn_level <- readIORef v_Warning_opt
185 let warn_opts = case warn_level of
186 W_default -> standardWarnings
188 W_all -> minusWallOpts
191 -- build the default DynFlags (these may be adjusted on a per
192 -- module basis by OPTIONS pragmas and settings in the interpreter).
194 core_todo <- buildCoreToDo
195 stg_todo <- buildStgToDo
197 -- set the "global" HscLang. The HscLang can be further adjusted on a module
198 -- by module basis, using only the -fvia-C and -fasm flags. If the global
199 -- HscLang is not HscC or HscAsm, -fvia-C and -fasm have no effect.
200 opt_level <- readIORef v_OptLevel
201 let lang = case mode of
202 StopBefore HCc -> HscC
203 DoInteractive -> HscInterpreted
204 _other | opt_level >= 1 -> HscC -- -O implies -fvia-C
205 | otherwise -> defaultHscLang
207 writeIORef v_DynFlags
208 defaultDynFlags{ coreToDo = core_todo,
211 -- leave out hscOutName for now
212 hscOutName = panic "Main.main:hscOutName not set",
214 verbosity = case mode of
220 -- the rest of the arguments are "dynamic"
221 srcs <- processArgs dynamic_flags (way_non_static ++
222 non_static ++ warn_opts) []
223 -- save the "initial DynFlags" away
224 init_dyn_flags <- readIORef v_DynFlags
225 writeIORef v_InitDynFlags init_dyn_flags
227 -- complain about any unknown flags
228 mapM unknownFlagErr [ f | f@('-':_) <- srcs ]
230 verb <- dynFlag verbosity
233 (do hPutStr stderr "Glasgow Haskell Compiler, Version "
234 hPutStr stderr cProjectVersion
235 hPutStr stderr ", for Haskell 98, compiled by GHC version "
236 hPutStrLn stderr cBooterVersion)
239 (hPutStrLn stderr ("Using package config file: " ++ conf_file))
242 (hPutStrLn stderr ("Hsc static flags: " ++ unwords static_opts))
244 -- initialise the finder
245 pkg_avails <- getPackageInfo
246 initFinder pkg_avails
248 -- mkdependHS is special
249 when (mode == DoMkDependHS) beginMkDependHS
251 -- make/interactive require invoking the compilation manager
252 if (mode == DoMake) then beginMake srcs else do
253 if (mode == DoInteractive) then beginInteractive srcs else do
256 o_file <- readIORef v_Output_file
257 ohi <- readIORef v_Output_hi
258 if length srcs > 1 && (isJust ohi || (isJust o_file && mode /= DoLink && mode /= DoMkDLL))
259 then throwDyn (UsageError "can't apply -o or -ohi options to multiple source files")
262 if null srcs then throwDyn (UsageError "no input files") else do
264 let compileFile src = do
265 writeIORef v_DynFlags init_dyn_flags
267 -- We compile in two stages, because the file may have an
268 -- OPTIONS pragma that affects the compilation pipeline (eg. -fvia-C)
270 let (basename, suffix) = splitFilename src
273 pp <- if not (haskellish_file src) || mode == StopBefore Hsc
274 then return src else do
275 phases <- genPipeline (StopBefore Hsc) stop_flag
276 False{-not persistent-} defaultHscLang src
277 pipeLoop phases src False{-no linking-} False{-no -o flag-}
280 -- rest of compilation
281 dyn_flags <- readIORef v_DynFlags
282 phases <- genPipeline mode stop_flag True (hscLang dyn_flags) pp
283 r <- pipeLoop phases pp (mode==DoLink || mode==DoMkDLL) True{-use -o flag-}
287 o_files <- mapM compileFile srcs
289 when (mode == DoMkDependHS) endMkDependHS
290 when (mode == DoLink) (doLink o_files)
291 when (mode == DoMkDLL) (doMkDLL o_files)
293 -- grab the last -B option on the command line, and
294 -- set topDir to its value.
295 setTopDir :: [String] -> IO [String]
297 let (minusbs, others) = partition (prefixMatch "-B") args
299 [] -> writeIORef v_TopDir clibdir
300 some -> writeIORef v_TopDir (drop 2 (last some)))
303 beginMake :: [String] -> IO ()
306 [] -> throwDyn (UsageError "no input files")
307 [mod] -> do state <- cmInit Batch
308 cmLoadModule state mod
310 _ -> throwDyn (UsageError "only one module allowed with --make")
313 beginInteractive :: [String] -> IO ()
315 beginInteractive = throwDyn (OtherError "not built for interactive use")
317 beginInteractive fileish_args
318 = let is_libraryish nm
319 = let nmr = map toLower (reverse nm)
320 in take 2 nmr == "o." ||
321 take 3 nmr == "os." ||
323 libs = filter is_libraryish fileish_args
324 mods = filter (not.is_libraryish) fileish_args
328 _ -> throwDyn (UsageError
329 "only one module allowed with --interactive")
331 do state <- cmInit Interactive
332 interactiveUI state mod libs