1 {-# OPTIONS -fno-warn-incomplete-patterns #-}
2 -----------------------------------------------------------------------------
3 -- $Id: Main.hs,v 1.67 2001/05/31 11:32:25 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"
23 #ifndef mingw32_TARGET_OS
36 import DriverPhases ( Phase(..), haskellish_src_file, objish_file )
39 import Finder ( initFinder )
57 -----------------------------------------------------------------------------
60 -- * -fglasgow-exts NO LONGER IMPLIES -package lang!!! (-fglasgow-exts is a
61 -- dynamic flag whereas -package is a static flag.)
63 -----------------------------------------------------------------------------
66 -- new mkdependHS doesn't support all the options that the old one did (-X et al.)
67 -- time commands when run with -v
71 -- Win32 support: proper signal handling
72 -- make sure OPTIONS in .hs file propogate to .hc file if -C or -keep-hc-file-too
73 -- reading the package configuration file is too slow
76 -----------------------------------------------------------------------------
77 -- Differences vs. old driver:
79 -- No more "Enter your Haskell program, end with ^D (on a line of its own):"
80 -- consistency checking removed (may do this properly later)
84 -----------------------------------------------------------------------------
88 -- top-level exception handler: any unrecognised exception is a compiler bug.
89 handle (\exception -> do hPutStr stderr (show (Panic (show exception)))
90 exitWith (ExitFailure 1)
93 -- all error messages are propagated as exceptions
94 handleDyn (\dyn -> case dyn of
95 PhaseFailed _phase code -> exitWith code
96 Interrupted -> exitWith (ExitFailure 1)
97 _ -> do hPutStrLn stderr (show (dyn :: GhcException))
98 exitWith (ExitFailure 1)
101 -- make sure we clean up after ourselves
102 later (do forget_it <- readIORef v_Keep_tmp_files
103 unless forget_it $ do
104 verb <- dynFlag verbosity
107 -- exceptions will be blocked while we clean the temporary files,
108 -- so there shouldn't be any difficulty if we receive further
111 -- install signal handlers
112 main_thread <- myThreadId
113 #ifndef mingw32_TARGET_OS
114 let sig_handler = Catch (throwTo main_thread
115 (DynException (toDyn Interrupted)))
116 installHandler sigQUIT sig_handler Nothing
117 installHandler sigINT sig_handler Nothing
122 -- grab any -B options from the command line first
123 argv' <- setTopDir argv
124 top_dir <- readIORef v_TopDir
126 let installed s = top_dir ++ '/':s
127 inplace s = top_dir ++ '/':cCURRENT_DIR ++ '/':s
129 installed_pkgconfig = installed ("package.conf")
130 inplace_pkgconfig = inplace (cGHC_DRIVER_DIR ++ "/package.conf.inplace")
132 -- discover whether we're running in a build tree or in an installation,
133 -- by looking for the package configuration file.
134 am_installed <- doesFileExist installed_pkgconfig
137 then writeIORef v_Path_package_config installed_pkgconfig
138 else do am_inplace <- doesFileExist inplace_pkgconfig
140 then writeIORef v_Path_package_config inplace_pkgconfig
141 else throwDyn (InstallationError
142 ("Can't find package.conf in " ++
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")
151 #if defined(mingw32_TARGET_OS) && defined(MINIMAL_UNIX_DEPS)
152 writeIORef v_Pgm_T (installed cTOUCH)
155 else do writeIORef v_Path_usage (inplace (cGHC_DRIVER_DIR ++ "/ghc-usage.txt"))
156 writeIORef v_Pgm_L (inplace cGHC_UNLIT)
157 writeIORef v_Pgm_m (inplace cGHC_MANGLER)
158 writeIORef v_Pgm_s (inplace cGHC_SPLIT)
159 #if defined(mingw32_TARGET_OS) && defined(MINIMAL_UNIX_DEPS)
160 writeIORef v_Pgm_T (inplace cTOUCH)
163 -- read the package configuration
164 conf_file <- readIORef v_Path_package_config
165 r <- parsePkgConf conf_file
167 Left err -> throwDyn (InstallationError (showSDoc err));
168 Right pkg_details -> do
170 writeIORef v_Package_details pkg_details
172 -- find the phase to stop after (i.e. -E, -C, -c, -S flags)
173 (flags2, mode, stop_flag) <- getGhcMode argv'
174 writeIORef v_GhcMode mode
176 -- Show the GHCi banner?
178 when (mode == DoInteractive) $
179 hPutStrLn stdout ghciWelcomeMsg
182 -- process all the other arguments, and get the source files
183 non_static <- processArgs static_flags flags2 []
185 -- -O and --interactive are not a good combination
186 -- ditto with any kind of way selection
187 orig_opt_level <- readIORef v_OptLevel
188 when (orig_opt_level > 0 && mode == DoInteractive) $
189 do putStr "warning: -O conflicts with --interactive; -O turned off.\n"
190 writeIORef v_OptLevel 0
191 orig_ways <- readIORef v_Ways
192 when (not (null orig_ways) && mode == DoInteractive) $
193 do throwDyn (UsageError
194 "--interactive can't be used with -prof, -ticky, -unreg or -smp.")
196 -- Find the build tag, and re-process the build-specific options.
197 -- Also add in flags for unregisterised compilation, if
198 -- GhcUnregisterised=YES.
199 way_opts <- findBuildTag
200 let unreg_opts | cGhcUnregisterised == "YES" = unregFlags
202 way_non_static <- processArgs static_flags (unreg_opts ++ way_opts) []
204 -- give the static flags to hsc
205 static_opts <- buildStaticHscOpts
206 writeIORef v_Static_hsc_opts static_opts
208 -- build the default DynFlags (these may be adjusted on a per
209 -- module basis by OPTIONS pragmas and settings in the interpreter).
211 core_todo <- buildCoreToDo
212 stg_todo <- buildStgToDo
214 -- set the "global" HscLang. The HscLang can be further adjusted on a module
215 -- by module basis, using only the -fvia-C and -fasm flags. If the global
216 -- HscLang is not HscC or HscAsm, -fvia-C and -fasm have no effect.
217 opt_level <- readIORef v_OptLevel
220 let lang = case mode of
221 StopBefore HCc -> HscC
222 DoInteractive -> HscInterpreted
223 _other | opt_level >= 1 -> HscC -- -O implies -fvia-C
224 | otherwise -> defaultHscLang
226 writeIORef v_DynFlags
227 defaultDynFlags{ coreToDo = core_todo,
230 -- leave out hscOutName for now
231 hscOutName = panic "Main.main:hscOutName not set",
233 verbosity = case mode of
239 -- the rest of the arguments are "dynamic"
240 srcs <- processArgs dynamic_flags (way_non_static ++ non_static) []
241 -- save the "initial DynFlags" away
242 init_dyn_flags <- readIORef v_DynFlags
243 writeIORef v_InitDynFlags init_dyn_flags
245 -- complain about any unknown flags
246 mapM unknownFlagErr [ f | f@('-':_) <- srcs ]
248 verb <- dynFlag verbosity
251 (do hPutStr stderr "Glasgow Haskell Compiler, Version "
252 hPutStr stderr cProjectVersion
253 hPutStr stderr ", for Haskell 98, compiled by GHC version "
254 hPutStrLn stderr cBooterVersion)
257 (hPutStrLn stderr ("Using package config file: " ++ conf_file))
260 (hPutStrLn stderr ("Hsc static flags: " ++ unwords static_opts))
262 -- initialise the finder
263 pkg_avails <- getPackageInfo
264 initFinder pkg_avails
266 -- mkdependHS is special
267 when (mode == DoMkDependHS) beginMkDependHS
269 -- -ohi sanity checking
270 ohi <- readIORef v_Output_hi
272 (mode == DoMake || mode == DoInteractive || length srcs > 1))
273 then throwDyn (UsageError "-ohi can only be used when compiling a single source file")
276 -- make/interactive require invoking the compilation manager
277 if (mode == DoMake) then beginMake srcs else do
278 if (mode == DoInteractive) then beginInteractive srcs else do
280 -- -o sanity checking
281 o_file <- readIORef v_Output_file
282 if (length srcs > 1 && isJust o_file && mode /= DoLink && mode /= DoMkDLL)
283 then throwDyn (UsageError "can't apply -o to multiple source files")
286 if null srcs then throwDyn (UsageError "no input files") else do
288 let compileFile src = do
289 writeIORef v_DynFlags init_dyn_flags
291 exists <- doesFileExist src
293 throwDyn (CmdLineError ("file `" ++ src ++ "' does not exist"))
295 -- We compile in two stages, because the file may have an
296 -- OPTIONS pragma that affects the compilation pipeline (eg. -fvia-C)
297 let (basename, suffix) = splitFilename src
299 -- just preprocess (Haskell source only)
300 pp <- if not (haskellish_src_file src) || mode == StopBefore Hsc
301 then return src else do
302 phases <- genPipeline (StopBefore Hsc) stop_flag
303 False{-not persistent-} defaultHscLang src
304 pipeLoop phases src False{-no linking-} False{-no -o flag-}
307 -- rest of compilation
308 dyn_flags <- readIORef v_DynFlags
309 phases <- genPipeline mode stop_flag True (hscLang dyn_flags) pp
310 r <- pipeLoop phases pp (mode==DoLink || mode==DoMkDLL) True{-use -o flag-}
314 o_files <- mapM compileFile srcs
316 when (mode == DoMkDependHS) endMkDependHS
317 when (mode == DoLink) (doLink o_files)
318 when (mode == DoMkDLL) (doMkDLL o_files)
320 -- grab the last -B option on the command line, and
321 -- set topDir to its value.
322 setTopDir :: [String] -> IO [String]
324 let (minusbs, others) = partition (prefixMatch "-B") args
326 [] -> throwDyn (InstallationError ("missing -B<dir> option"))
327 some -> writeIORef v_TopDir (drop 2 (last some)))
330 beginMake :: [String] -> IO ()
331 beginMake fileish_args
332 = do let (objs, mods) = partition objish_file fileish_args
333 mapM (add v_Ld_inputs) objs
336 [] -> throwDyn (UsageError "no input files")
337 [mod] -> do state <- cmInit Batch
338 cmLoadModule state mod
340 _ -> throwDyn (UsageError "only one module allowed with --make")
343 beginInteractive :: [String] -> IO ()
345 beginInteractive = throwDyn (CmdLineError "not built for interactive use")
347 beginInteractive fileish_args
348 = do minus_ls <- readIORef v_Cmdline_libraries
350 let (objs, mods) = partition objish_file fileish_args
351 libs = map Left objs ++ map Right minus_ls
353 state <- cmInit Interactive
355 [] -> interactiveUI state Nothing libs
356 [mod] -> interactiveUI state (Just mod) libs
357 _ -> throwDyn (UsageError
358 "only one module allowed with --interactive")