1 {-# OPTIONS -fno-warn-incomplete-patterns #-}
2 -----------------------------------------------------------------------------
3 -- $Id: Main.hs,v 1.76 2001/06/27 11:39:54 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"
20 import InteractiveUI(ghciWelcomeMsg, interactiveUI)
24 import Finder ( initFinder )
25 import CompManager ( cmInit, cmLoadModule )
26 import HscTypes ( GhciMode(..) )
27 import Config ( cBooterVersion, cGhcUnregisterised, cProjectVersion )
28 import SysTools ( packageConfigPath, initSysTools, cleanTempFiles )
29 import Packages ( showPackages, mungePackagePaths )
30 import ParsePkgConf ( loadPackageConfig )
32 import DriverPipeline ( GhcMode(..), doLink, doMkDLL, genPipeline,
33 getGhcMode, pipeLoop, v_GhcMode
35 import DriverState ( buildCoreToDo, buildStgToDo, defaultHscLang,
36 findBuildTag, getPackageInfo, unregFlags,
37 v_Cmdline_libraries, v_Keep_tmp_files, v_Ld_inputs,
38 v_OptLevel, v_Output_file, v_Output_hi,
39 v_Package_details, v_Ways, getPackageExtraGhcOpts
41 import DriverFlags ( dynFlag, buildStaticHscOpts, dynamic_flags,
42 processArgs, static_flags)
44 import DriverMkDepend ( beginMkDependHS, endMkDependHS )
45 import DriverPhases ( Phase(Hsc, HCc), haskellish_src_file, objish_file )
47 import DriverUtil ( add, handle, handleDyn, later, splitFilename,
49 import CmdLineOpts ( dynFlag, defaultDynFlags, restoreDynFlags,
50 saveDynFlags, setDynFlags,
51 DynFlags(..), HscLang(..), v_Static_hsc_opts
56 import Panic ( GhcException(..), panic )
58 -- Standard Haskell libraries
60 import Directory ( doesFileExist )
61 import IOExts ( readIORef, writeIORef )
62 import Exception ( throwDyn, Exception(DynException) )
63 import System ( getArgs, exitWith, ExitCode(..) )
68 #ifndef mingw32_TARGET_OS
69 import Concurrent ( myThreadId )
70 #ifdef __GLASGOW_HASKELL__ < 500
71 import Exception ( raiseInThread )
72 #define throwTo raiseInThread
74 import Exception ( throwTo )
77 import Posix ( Handler(Catch), installHandler, sigINT, sigQUIT )
78 import Dynamic ( toDyn )
82 -----------------------------------------------------------------------------
85 -- * -fglasgow-exts NO LONGER IMPLIES -package lang!!! (-fglasgow-exts is a
86 -- dynamic flag whereas -package is a static flag.)
88 -----------------------------------------------------------------------------
91 -- new mkdependHS doesn't support all the options that the old one did (-X et al.)
92 -- time commands when run with -v
96 -- Win32 support: proper signal handling
97 -- make sure OPTIONS in .hs file propogate to .hc file if -C or -keep-hc-file-too
98 -- reading the package configuration file is too slow
101 -----------------------------------------------------------------------------
102 -- Differences vs. old driver:
104 -- No more "Enter your Haskell program, end with ^D (on a line of its own):"
105 -- consistency checking removed (may do this properly later)
109 -----------------------------------------------------------------------------
113 -- top-level exception handler: any unrecognised exception is a compiler bug.
114 handle (\exception -> do hPutStr stderr (show (Panic (show exception)))
115 exitWith (ExitFailure 1)
118 -- all error messages are propagated as exceptions
119 handleDyn (\dyn -> case dyn of
120 PhaseFailed _phase code -> exitWith code
121 Interrupted -> exitWith (ExitFailure 1)
122 _ -> do hPutStrLn stderr (show (dyn :: GhcException))
123 exitWith (ExitFailure 1)
126 -- make sure we clean up after ourselves
127 later (do forget_it <- readIORef v_Keep_tmp_files
128 unless forget_it $ do
129 verb <- dynFlag verbosity
132 -- exceptions will be blocked while we clean the temporary files,
133 -- so there shouldn't be any difficulty if we receive further
136 -- install signal handlers
137 #ifndef mingw32_TARGET_OS
138 main_thread <- myThreadId
139 let sig_handler = Catch (throwTo main_thread
140 (DynException (toDyn Interrupted)))
141 installHandler sigQUIT sig_handler Nothing
142 installHandler sigINT sig_handler Nothing
146 let (minusB_args, argv') = partition (prefixMatch "-B") argv
147 top_dir <- initSysTools minusB_args
149 -- Read the package configuration
150 conf_file <- packageConfigPath
151 proto_pkg_details <- loadPackageConfig conf_file
152 let pkg_details = mungePackagePaths top_dir proto_pkg_details
153 writeIORef v_Package_details pkg_details
155 -- find the phase to stop after (i.e. -E, -C, -c, -S flags)
156 (flags2, mode, stop_flag) <- getGhcMode argv'
157 writeIORef v_GhcMode mode
159 -- Show the GHCi banner?
161 when (mode == DoInteractive) $
162 hPutStrLn stdout ghciWelcomeMsg
165 -- process all the other arguments, and get the source files
166 non_static <- processArgs static_flags flags2 []
168 -- -O and --interactive are not a good combination
169 -- ditto with any kind of way selection
170 orig_opt_level <- readIORef v_OptLevel
171 when (orig_opt_level > 0 && mode == DoInteractive) $
172 do putStr "warning: -O conflicts with --interactive; -O turned off.\n"
173 writeIORef v_OptLevel 0
174 orig_ways <- readIORef v_Ways
175 when (not (null orig_ways) && mode == DoInteractive) $
176 do throwDyn (UsageError
177 "--interactive can't be used with -prof, -ticky, -unreg or -smp.")
179 -- Find the build tag, and re-process the build-specific options.
180 -- Also add in flags for unregisterised compilation, if
181 -- GhcUnregisterised=YES.
182 way_opts <- findBuildTag
183 let unreg_opts | cGhcUnregisterised == "YES" = unregFlags
185 pkg_extra_opts <- getPackageExtraGhcOpts
186 extra_non_static <- processArgs static_flags
187 (unreg_opts ++ way_opts ++ pkg_extra_opts) []
189 -- give the static flags to hsc
190 static_opts <- buildStaticHscOpts
191 writeIORef v_Static_hsc_opts static_opts
193 -- build the default DynFlags (these may be adjusted on a per
194 -- module basis by OPTIONS pragmas and settings in the interpreter).
196 core_todo <- buildCoreToDo
197 stg_todo <- buildStgToDo
199 -- set the "global" HscLang. The HscLang can be further adjusted on a module
200 -- by module basis, using only the -fvia-C and -fasm flags. If the global
201 -- HscLang is not HscC or HscAsm, -fvia-C and -fasm have no effect.
202 opt_level <- readIORef v_OptLevel
205 let lang = case mode of
206 StopBefore HCc -> HscC
207 DoInteractive -> HscInterpreted
208 _other | opt_level >= 1 -> HscC -- -O implies -fvia-C
209 | otherwise -> defaultHscLang
211 setDynFlags (defaultDynFlags{ coreToDo = core_todo,
214 -- leave out hscOutName for now
215 hscOutName = panic "Main.main:hscOutName not set",
217 verbosity = case mode of
223 -- the rest of the arguments are "dynamic"
224 srcs <- processArgs dynamic_flags (extra_non_static ++ non_static) []
226 -- save the "initial DynFlags" away
229 -- complain about any unknown flags
230 mapM unknownFlagErr [ f | f@('-':_) <- srcs ]
232 -- Display details of the configuration in verbose mode
233 verb <- dynFlag verbosity
236 (do hPutStr stderr "Glasgow Haskell Compiler, Version "
237 hPutStr stderr cProjectVersion
238 hPutStr stderr ", for Haskell 98, compiled by GHC version "
239 hPutStrLn stderr cBooterVersion)
242 (hPutStrLn stderr ("Using package config file: " ++ conf_file))
245 (hPutStrLn stderr ("Hsc static flags: " ++ unwords static_opts))
247 showPackages pkg_details
249 -- initialise the finder
250 pkg_avails <- getPackageInfo
251 initFinder pkg_avails
253 -- mkdependHS is special
254 when (mode == DoMkDependHS) beginMkDependHS
256 -- -ohi sanity checking
257 ohi <- readIORef v_Output_hi
259 (mode == DoMake || mode == DoInteractive || length srcs > 1))
260 then throwDyn (UsageError "-ohi can only be used when compiling a single source file")
263 -- make/interactive require invoking the compilation manager
264 if (mode == DoMake) then beginMake srcs else do
265 if (mode == DoInteractive) then beginInteractive srcs else do
267 -- -o sanity checking
268 o_file <- readIORef v_Output_file
269 if (length srcs > 1 && isJust o_file && mode /= DoLink && mode /= DoMkDLL)
270 then throwDyn (UsageError "can't apply -o to multiple source files")
273 if null srcs then throwDyn (UsageError "no input files") else do
275 let compileFile src = do
278 exists <- doesFileExist src
280 throwDyn (CmdLineError ("file `" ++ src ++ "' does not exist"))
282 -- We compile in two stages, because the file may have an
283 -- OPTIONS pragma that affects the compilation pipeline (eg. -fvia-C)
284 let (basename, suffix) = splitFilename src
286 -- just preprocess (Haskell source only)
287 pp <- if not (haskellish_src_file src) || mode == StopBefore Hsc
288 then return src else do
289 phases <- genPipeline (StopBefore Hsc) stop_flag
290 False{-not persistent-} defaultHscLang src
291 pipeLoop phases src False{-no linking-} False{-no -o flag-}
294 -- rest of compilation
295 hsc_lang <- dynFlag hscLang
296 phases <- genPipeline mode stop_flag True hsc_lang pp
297 r <- pipeLoop phases pp (mode==DoLink || mode==DoMkDLL) True{-use -o flag-}
301 o_files <- mapM compileFile srcs
303 when (mode == DoMkDependHS) endMkDependHS
304 when (mode == DoLink) (doLink o_files)
305 when (mode == DoMkDLL) (doMkDLL o_files)
309 beginMake :: [String] -> IO ()
310 beginMake fileish_args
311 = do let (objs, mods) = partition objish_file fileish_args
312 mapM (add v_Ld_inputs) objs
315 [] -> throwDyn (UsageError "no input files")
316 mod -> do state <- cmInit Batch
317 cmLoadModule state mods
321 beginInteractive :: [String] -> IO ()
323 beginInteractive = throwDyn (CmdLineError "not built for interactive use")
325 beginInteractive fileish_args
326 = do minus_ls <- readIORef v_Cmdline_libraries
328 let (objs, mods) = partition objish_file fileish_args
329 libs = map Left objs ++ map Right minus_ls
331 state <- cmInit Interactive
332 interactiveUI state mods libs