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
28 import System.Directory
29 import System.Environment
31 import System.FilePath
34 #if defined(mingw32_HOST_OS)
37 import Foreign.C.String
43 case parseRunGhcFlags args of
44 (Help, _) -> printUsage
45 (ShowVersion, _) -> printVersion
46 (RunGhcFlags (Just ghc), args') -> doIt ghc args'
47 (RunGhcFlags Nothing, args') -> do
50 Nothing -> dieProg ("cannot find ghc")
52 let ghc = takeDirectory (normalise path) </> "ghc"
55 data RunGhcFlags = RunGhcFlags (Maybe FilePath) -- GHC location
56 | Help -- Print help text
57 | ShowVersion -- Print version info
59 instance Monoid RunGhcFlags where
60 mempty = RunGhcFlags Nothing
61 Help `mappend` _ = Help
62 _ `mappend` Help = Help
63 ShowVersion `mappend` _ = ShowVersion
64 _ `mappend` ShowVersion = ShowVersion
65 RunGhcFlags _ `mappend` right@(RunGhcFlags (Just _)) = right
66 left@(RunGhcFlags _) `mappend` RunGhcFlags Nothing = left
68 parseRunGhcFlags :: [String] -> (RunGhcFlags, [String])
69 parseRunGhcFlags = f mempty
70 where f flags ("-f" : ghc : args)
71 = f (flags `mappend` RunGhcFlags (Just ghc)) args
72 f flags (('-' : 'f' : ghc) : args)
73 = f (flags `mappend` RunGhcFlags (Just ghc)) args
74 f flags ("--help" : args) = f (flags `mappend` Help) args
75 f flags ("--version" : args) = f (flags `mappend` ShowVersion) args
76 -- If you need the first GHC flag to be a -f flag then
77 -- you can pass -- first
78 f flags ("--" : args) = (flags, args)
79 f flags args = (flags, args)
83 putStrLn ("runghc " ++ VERSION)
87 putStrLn "Usage: runghc [runghc flags] [GHC flags] module [program args]"
89 putStrLn "The runghc flags are"
90 putStrLn " -f /path/to/ghc Tell runghc where GHC is"
91 putStrLn " --help Print this usage information"
92 putStrLn " --version Print version number"
94 doIt :: String -> [String] -> IO ()
96 let (ghc_args, rest) = getGhcArgs args
99 -- behave like typical perl, python, ruby interpreters:
101 tmpdir <- getTemporaryDirectory
103 (openTempFile tmpdir "runghcXXXX.hs")
104 (\(filename,h) -> do hClose h; removeFile filename)
105 $ \(filename,h) -> do
106 getContents >>= hPutStr h
108 doIt ghc (ghc_args ++ [filename])
109 filename : prog_args -> do
110 -- If the file exists, and is not a .lhs file, then we
111 -- want to treat it as a .hs file.
113 -- If the file doesn't exist then GHC is going to look for
114 -- filename.hs and filename.lhs, and use the appropriate
116 exists <- doesFileExist filename
117 let xflag = if exists && (takeExtension filename /= ".lhs")
120 c1 = ":set prog " ++ show filename
121 c2 = ":main " ++ show prog_args
122 res <- rawSystem ghc (["-ignore-dot-ghci"] ++
125 [ "-e", c1, "-e", c2, filename])
128 getGhcArgs :: [String] -> ([String], [String])
130 = let (ghcArgs, otherArgs) = case break pastArgs args of
131 (xs, "--":ys) -> (xs, ys)
133 in (map unescape ghcArgs, otherArgs)
134 where unescape ('-':'-':'g':'h':'c':'-':'a':'r':'g':'=':arg) = arg
137 pastArgs :: String -> Bool
138 -- You can use -- to mark the end of the flags, in case you need to use
139 -- a file called -foo.hs for some reason. You almost certainly shouldn't,
142 pastArgs ('-':_) = False
145 dieProg :: String -> IO a
148 hPutStrLn stderr (p ++ ": " ++ msg)
149 exitWith (ExitFailure 1)
152 -- usage = "syntax: runghc [-f GHC-PATH | --] [GHC-ARGS] [--] FILE ARG..."
154 getExecPath :: IO (Maybe String)
155 #if defined(mingw32_HOST_OS)
157 allocaArray len $ \buf -> do
158 ret <- getModuleFileName nullPtr buf len
159 if ret == 0 then return Nothing
160 else liftM Just $ peekCString buf
161 where len = 2048 -- Plenty, PATH_MAX is 512 under Win32.
163 foreign import stdcall unsafe "GetModuleFileNameA"
164 getModuleFileName :: Ptr () -> CString -> Int -> IO Int32
166 getExecPath = return Nothing