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 -- -----------------------------------------------------------------------------
23 module Main (main) where
25 import System.Environment
32 import Compat.RawSystem ( rawSystem )
33 import Compat.Directory ( findExecutable )
35 import System.Cmd ( rawSystem )
36 import System.Directory ( findExecutable )
43 ("-f" : ghc : args) -> do
45 ('-' : 'f' : ghc) : args -> do
46 doIt (dropWhile isSpace ghc) args
48 mb_ghc <- findExecutable "ghc"
50 Nothing -> dieProg ("cannot find ghc")
51 Just ghc -> doIt ghc args
53 doIt :: String -> [String] -> IO ()
55 let (ghc_args, rest) = break notArg args
57 [] -> dieProg "syntax: runghc [-f GHCPATH] [GHC-ARGS] FILE ARG..."
58 filename : prog_args -> do
59 let expr = "System.Environment.withProgName " ++ show filename ++
60 " (System.Environment.withArgs " ++ show prog_args ++
61 " (GHC.TopHandler.runIOFastExit" ++
62 " (Main.main Prelude.>> Prelude.return ())))"
63 res <- rawSystem ghc (["-ignore-dot-ghci"] ++ ghc_args ++
64 [ "-e", expr, filename])
65 -- runIOFastExit: makes exceptions raised by Main.main
66 -- behave in the same way as for a compiled program.
67 -- The "fast exit" part just calls exit() directly
68 -- instead of doing an orderly runtime shutdown,
69 -- otherwise the main GHCi thread will complain about
72 -- Why (main >> return ()) rather than just main? Because
73 -- otherwise GHCi by default tries to evaluate the result
74 -- of the IO in order to show it (see #1200).
77 notArg :: String -> Bool
78 notArg ('-':_) = False
81 dieProg :: String -> IO a
84 hPutStrLn stderr (p ++ ": " ++ msg)
85 exitWith (ExitFailure 1)