1 {-# OPTIONS -cpp -fffi #-}
2 #if __GLASGOW_HASKELL__ < 603
7 -----------------------------------------------------------------------------
9 -- (c) The University of Glasgow, 2004
11 -- runghc program, for invoking from a #! line in a script. For example:
15 -- > main = putStrLn "hello!"
17 -- runghc accepts one flag:
19 -- -f <path> specify the path
21 -- -----------------------------------------------------------------------------
25 import System.Environment
32 import Compat.RawSystem ( rawSystem )
33 import Compat.Directory ( findExecutable )
35 import System.Cmd ( rawSystem )
36 import System.Directory ( findExecutable )
42 ("-f" : ghc : args) -> do
44 ('-':'f' : ghc) : args -> do
45 doIt (dropWhile isSpace ghc) args
47 mb_ghc <- findExecutable "ghc"
49 Nothing -> dieProg ("cannot find ghc")
50 Just ghc -> doIt ghc args
54 (ghc_args, rest) = break notArg args
57 [] -> dieProg "syntax: runghc [-f GHCPATH] [GHC-ARGS] FILE ARG..."
58 filename : prog_args -> do
59 res <- rawSystem ghc (
60 "-ignore-dot-ghci" : ghc_args ++
61 [ "-e","System.Environment.withProgName "++show filename++" (System.Environment.withArgs ["
62 ++ concat (intersperse "," (map show prog_args))
63 ++ "] (GHC.TopHandler.runIOFastExit (Main.main Prelude.>> (Prelude.return ()))))", filename])
64 -- runIOFastExit: makes exceptions raised by Main.main
65 -- behave in the same way as for a compiled program.
66 -- The "fast exit" part just calls exit() directly
67 -- instead of doing an orderly runtime shutdown,
68 -- otherwise the main GHCi thread will complain about
71 -- Why (main >> return ()) rather than just main? Because
72 -- otherwise GHCi by default tries to evaluate the result
73 -- of the IO in order to show it (see #1200).
76 notArg ('-':_) = False
79 dieProg :: String -> IO a
82 hPutStrLn stderr (p ++ ": " ++ msg)
83 exitWith (ExitFailure 1)