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 (RunGhcFlags (Just ghc), args') -> doIt ghc args'
48 (RunGhcFlags Nothing, args') -> do
51 Nothing -> dieProg ("cannot find ghc")
53 let ghc = takeDirectory (normalise path) </> "ghc"
56 data RunGhcFlags = RunGhcFlags (Maybe FilePath) -- GHC location
57 | Help -- Print help text
59 instance Monoid RunGhcFlags where
60 mempty = RunGhcFlags Nothing
61 Help `mappend` _ = Help
62 _ `mappend` Help = Help
63 RunGhcFlags _ `mappend` right@(RunGhcFlags (Just _)) = right
64 left@(RunGhcFlags _) `mappend` RunGhcFlags Nothing = left
66 parseRunGhcFlags :: [String] -> (RunGhcFlags, [String])
67 parseRunGhcFlags = f mempty
68 where f flags ("-f" : ghc : args)
69 = f (flags `mappend` RunGhcFlags (Just ghc)) args
70 f flags (('-' : 'f' : ghc) : args)
71 = f (flags `mappend` RunGhcFlags (Just ghc)) args
72 f flags ("--help" : args) = f (flags `mappend` Help) args
73 -- If you need the first GHC flag to be a -f flag then
74 -- you can pass -- first
75 f flags ("--" : args) = (flags, args)
76 f flags args = (flags, args)
80 putStrLn "Usage: runghc [runghc flags] [GHC flags] module [program args]"
82 putStrLn "The runghc flags are"
83 putStrLn " -f /path/to/ghc Tell runghc where GHC is"
84 putStrLn " --help Print this usage information"
86 doIt :: String -> [String] -> IO ()
88 let (ghc_args, rest) = getGhcArgs args
91 -- behave like typical perl, python, ruby interpreters:
93 tmpdir <- getTemporaryDirectory
95 (openTempFile tmpdir "runghcXXXX.hs")
96 (\(filename,h) -> do hClose h; removeFile filename)
98 getContents >>= hPutStr h
100 doIt ghc (ghc_args ++ [filename])
101 filename : prog_args -> do
102 -- If the file exists, and is not a .lhs file, then we
103 -- want to treat it as a .hs file.
105 -- If the file doesn't exist then GHC is going to look for
106 -- filename.hs and filename.lhs, and use the appropriate
108 exists <- doesFileExist filename
109 let xflag = if exists && (takeExtension filename /= ".lhs")
112 c1 = ":set prog " ++ show filename
113 c2 = ":main " ++ show prog_args
114 res <- rawSystem ghc (["-ignore-dot-ghci"] ++
117 [ "-e", c1, "-e", c2, filename])
120 getGhcArgs :: [String] -> ([String], [String])
122 = let (ghcArgs, otherArgs) = case break pastArgs args of
123 (xs, "--":ys) -> (xs, ys)
125 in (map unescape ghcArgs, otherArgs)
126 where unescape ('-':'-':'g':'h':'c':'-':'a':'r':'g':'=':arg) = arg
129 pastArgs :: String -> Bool
130 -- You can use -- to mark the end of the flags, in case you need to use
131 -- a file called -foo.hs for some reason. You almost certainly shouldn't,
134 pastArgs ('-':_) = False
137 dieProg :: String -> IO a
140 hPutStrLn stderr (p ++ ": " ++ msg)
141 exitWith (ExitFailure 1)
144 -- usage = "syntax: runghc [-f GHC-PATH | --] [GHC-ARGS] [--] FILE ARG..."
146 getExecPath :: IO (Maybe String)
147 #if defined(mingw32_HOST_OS)
149 allocaArray len $ \buf -> do
150 ret <- getModuleFileName nullPtr buf len
151 if ret == 0 then return Nothing
152 else liftM Just $ peekCString buf
153 where len = 2048 -- Plenty, PATH_MAX is 512 under Win32.
155 foreign import stdcall unsafe "GetModuleFileNameA"
156 getModuleFileName :: Ptr () -> CString -> Int -> IO Int32
158 getExecPath = return Nothing