1 {-# LANGUAGE CPP, ForeignFunctionInterface #-}
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:
14 -- #!/usr/bin/env /usr/bin/runghc
15 -- > main = putStrLn "hello!"
17 -- runghc accepts one flag:
19 -- -f <path> specify the path
21 -- -----------------------------------------------------------------------------
23 module Main (main) where
25 import Control.Exception
31 import System.Directory
32 import System.Environment
34 import System.FilePath
37 #if defined(mingw32_HOST_OS)
40 import Foreign.C.String
46 case parseRunGhcFlags args of
47 (Help, _) -> printUsage
48 (ShowVersion, _) -> printVersion
49 (RunGhcFlags (Just ghc), args') -> doIt ghc args'
50 (RunGhcFlags Nothing, args') -> do
53 Nothing -> dieProg ("cannot find ghc")
55 let ghc = takeDirectory (normalise path) </> "ghc"
58 data RunGhcFlags = RunGhcFlags (Maybe FilePath) -- GHC location
59 | Help -- Print help text
60 | ShowVersion -- Print version info
62 instance Monoid RunGhcFlags where
63 mempty = RunGhcFlags Nothing
64 Help `mappend` _ = Help
65 _ `mappend` Help = Help
66 ShowVersion `mappend` _ = ShowVersion
67 _ `mappend` ShowVersion = ShowVersion
68 RunGhcFlags _ `mappend` right@(RunGhcFlags (Just _)) = right
69 left@(RunGhcFlags _) `mappend` RunGhcFlags Nothing = left
71 parseRunGhcFlags :: [String] -> (RunGhcFlags, [String])
72 parseRunGhcFlags = f mempty
73 where f flags ("-f" : ghc : args)
74 = f (flags `mappend` RunGhcFlags (Just ghc)) args
75 f flags (('-' : 'f' : ghc) : args)
76 = f (flags `mappend` RunGhcFlags (Just ghc)) args
77 f flags ("--help" : args) = f (flags `mappend` Help) args
78 f flags ("--version" : args) = f (flags `mappend` ShowVersion) args
79 -- If you need the first GHC flag to be a -f flag then
80 -- you can pass -- first
81 f flags ("--" : args) = (flags, args)
82 f flags args = (flags, args)
86 putStrLn ("runghc " ++ VERSION)
90 putStrLn "Usage: runghc [runghc flags] [GHC flags] module [program args]"
92 putStrLn "The runghc flags are"
93 putStrLn " -f /path/to/ghc Tell runghc where GHC is"
94 putStrLn " --help Print this usage information"
95 putStrLn " --version Print version number"
97 doIt :: String -> [String] -> IO ()
99 let (ghc_args, rest) = getGhcArgs args
102 -- behave like typical perl, python, ruby interpreters:
104 tmpdir <- getTemporaryDirectory
106 (openTempFile tmpdir "runghcXXXX.hs")
107 (\(filename,h) -> do hClose h; removeFile filename)
108 $ \(filename,h) -> do
109 getContents >>= hPutStr h
111 doIt ghc (ghc_args ++ [filename])
112 filename : prog_args -> do
113 -- If the file exists, and is not a .lhs file, then we
114 -- want to treat it as a .hs file.
116 -- If the file doesn't exist then GHC is going to look for
117 -- filename.hs and filename.lhs, and use the appropriate
119 exists <- doesFileExist filename
120 let xflag = if exists && (takeExtension filename /= ".lhs")
123 c1 = ":set prog " ++ show filename
124 c2 = ":main " ++ show prog_args
125 res <- rawSystem ghc (["-ignore-dot-ghci"] ++
128 [ "-e", c1, "-e", c2, filename])
131 getGhcArgs :: [String] -> ([String], [String])
133 = let (ghcArgs, otherArgs) = case break pastArgs args of
134 (xs, "--":ys) -> (xs, ys)
136 in (map unescape ghcArgs, otherArgs)
137 where unescape ('-':'-':'g':'h':'c':'-':'a':'r':'g':'=':arg) = arg
140 pastArgs :: String -> Bool
141 -- You can use -- to mark the end of the flags, in case you need to use
142 -- a file called -foo.hs for some reason. You almost certainly shouldn't,
145 pastArgs ('-':_) = False
148 dieProg :: String -> IO a
151 hPutStrLn stderr (p ++ ": " ++ msg)
152 exitWith (ExitFailure 1)
155 -- usage = "syntax: runghc [-f GHC-PATH | --] [GHC-ARGS] [--] FILE ARG..."
157 getExecPath :: IO (Maybe String)
158 #if defined(mingw32_HOST_OS)
160 allocaArray len $ \buf -> do
161 ret <- getModuleFileName nullPtr buf len
162 if ret == 0 then return Nothing
163 else liftM Just $ peekCString buf
164 where len = 2048 -- Plenty, PATH_MAX is 512 under Win32.
166 foreign import stdcall unsafe "GetModuleFileNameA"
167 getModuleFileName :: Ptr () -> CString -> Int -> IO Int32
169 getExecPath = return Nothing