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
30 import System.Directory
31 import System.Environment
33 import System.FilePath
36 #if defined(mingw32_HOST_OS)
39 import Foreign.C.String
45 case parseRunGhcFlags args of
46 (Help, _) -> printUsage
47 (ShowVersion, _) -> printVersion
48 (RunGhcFlags (Just ghc), args') -> doIt ghc args'
49 (RunGhcFlags Nothing, args') -> do
52 Nothing -> dieProg ("cannot find ghc")
54 let ghc = takeDirectory (normalise path) </> "ghc"
57 data RunGhcFlags = RunGhcFlags (Maybe FilePath) -- GHC location
58 | Help -- Print help text
59 | ShowVersion -- Print version info
61 instance Monoid RunGhcFlags where
62 mempty = RunGhcFlags Nothing
63 Help `mappend` _ = Help
64 _ `mappend` Help = Help
65 ShowVersion `mappend` _ = ShowVersion
66 _ `mappend` ShowVersion = ShowVersion
67 RunGhcFlags _ `mappend` right@(RunGhcFlags (Just _)) = right
68 left@(RunGhcFlags _) `mappend` RunGhcFlags Nothing = left
70 parseRunGhcFlags :: [String] -> (RunGhcFlags, [String])
71 parseRunGhcFlags = f mempty
72 where f flags ("-f" : ghc : args)
73 = f (flags `mappend` RunGhcFlags (Just ghc)) args
74 f flags (('-' : 'f' : ghc) : args)
75 = f (flags `mappend` RunGhcFlags (Just ghc)) args
76 f flags ("--help" : args) = f (flags `mappend` Help) args
77 f flags ("--version" : args) = f (flags `mappend` ShowVersion) args
78 -- If you need the first GHC flag to be a -f flag then
79 -- you can pass -- first
80 f flags ("--" : args) = (flags, args)
81 f flags args = (flags, args)
85 putStrLn ("runghc " ++ VERSION)
89 putStrLn "Usage: runghc [runghc flags] [GHC flags] module [program args]"
91 putStrLn "The runghc flags are"
92 putStrLn " -f /path/to/ghc Tell runghc where GHC is"
93 putStrLn " --help Print this usage information"
94 putStrLn " --version Print version number"
96 doIt :: String -> [String] -> IO ()
98 let (ghc_args, rest) = getGhcArgs args
101 -- behave like typical perl, python, ruby interpreters:
103 tmpdir <- getTemporaryDirectory
105 (openTempFile tmpdir "runghcXXXX.hs")
106 (\(filename,h) -> do hClose h; removeFile filename)
107 $ \(filename,h) -> do
108 getContents >>= hPutStr h
110 doIt ghc (ghc_args ++ [filename])
111 filename : prog_args -> do
112 -- If the file exists, and is not a .lhs file, then we
113 -- want to treat it as a .hs file.
115 -- If the file doesn't exist then GHC is going to look for
116 -- filename.hs and filename.lhs, and use the appropriate
118 exists <- doesFileExist filename
119 let xflag = if exists && (takeExtension filename /= ".lhs")
122 c1 = ":set prog " ++ show filename
123 c2 = ":main " ++ show prog_args
124 res <- rawSystem ghc (["-ignore-dot-ghci"] ++
127 [ "-e", c1, "-e", c2, filename])
130 getGhcArgs :: [String] -> ([String], [String])
132 = let (ghcArgs, otherArgs) = case break pastArgs args of
133 (xs, "--":ys) -> (xs, ys)
135 in (map unescape ghcArgs, otherArgs)
136 where unescape ('-':'-':'g':'h':'c':'-':'a':'r':'g':'=':arg) = arg
139 pastArgs :: String -> Bool
140 -- You can use -- to mark the end of the flags, in case you need to use
141 -- a file called -foo.hs for some reason. You almost certainly shouldn't,
144 pastArgs ('-':_) = False
147 dieProg :: String -> IO a
150 hPutStrLn stderr (p ++ ": " ++ msg)
151 exitWith (ExitFailure 1)
154 -- usage = "syntax: runghc [-f GHC-PATH | --] [GHC-ARGS] [--] FILE ARG..."
156 getExecPath :: IO (Maybe String)
157 #if defined(mingw32_HOST_OS)
159 allocaArray len $ \buf -> do
160 ret <- getModuleFileName nullPtr buf len
161 if ret == 0 then return Nothing
162 else liftM Just $ peekCString buf
163 where len = 2048 -- Plenty, PATH_MAX is 512 under Win32.
165 foreign import stdcall unsafe "GetModuleFileNameA"
166 getModuleFileName :: Ptr () -> CString -> Int -> IO Int32
168 getExecPath = return Nothing