1 {-# LANGUAGE CPP, ForeignFunctionInterface #-}
3 -----------------------------------------------------------------------------
5 -- (c) The University of Glasgow, 2004
7 -- runghc program, for invoking from a #! line in a script. For example:
10 -- #!/usr/bin/env /usr/bin/runghc
11 -- > main = putStrLn "hello!"
13 -- runghc accepts one flag:
15 -- -f <path> specify the path
17 -- -----------------------------------------------------------------------------
19 module Main (main) where
21 import Control.Exception
24 import System.Directory
25 import System.Environment
27 import System.FilePath
30 #if defined(mingw32_HOST_OS)
33 import Foreign.C.String
39 case parseRunGhcFlags args of
40 (Help, _) -> printUsage
41 (ShowVersion, _) -> printVersion
42 (RunGhcFlags (Just ghc), args') -> doIt ghc args'
43 (RunGhcFlags Nothing, args') -> do
46 Nothing -> dieProg ("cannot find ghc")
48 let ghc = takeDirectory (normalise path) </> "ghc"
51 data RunGhcFlags = RunGhcFlags (Maybe FilePath) -- GHC location
52 | Help -- Print help text
53 | ShowVersion -- Print version info
55 instance Monoid RunGhcFlags where
56 mempty = RunGhcFlags Nothing
57 Help `mappend` _ = Help
58 _ `mappend` Help = Help
59 ShowVersion `mappend` _ = ShowVersion
60 _ `mappend` ShowVersion = ShowVersion
61 RunGhcFlags _ `mappend` right@(RunGhcFlags (Just _)) = right
62 left@(RunGhcFlags _) `mappend` RunGhcFlags Nothing = left
64 parseRunGhcFlags :: [String] -> (RunGhcFlags, [String])
65 parseRunGhcFlags = f mempty
66 where f flags ("-f" : ghc : args)
67 = f (flags `mappend` RunGhcFlags (Just ghc)) args
68 f flags (('-' : 'f' : ghc) : args)
69 = f (flags `mappend` RunGhcFlags (Just ghc)) args
70 f flags ("--help" : args) = f (flags `mappend` Help) args
71 f flags ("--version" : args) = f (flags `mappend` ShowVersion) args
72 -- If you need the first GHC flag to be a -f flag then
73 -- you can pass -- first
74 f flags ("--" : args) = (flags, args)
75 f flags args = (flags, args)
79 putStrLn ("runghc " ++ VERSION)
83 putStrLn "Usage: runghc [runghc flags] [GHC flags] module [program args]"
85 putStrLn "The runghc flags are"
86 putStrLn " -f /path/to/ghc Tell runghc where GHC is"
87 putStrLn " --help Print this usage information"
88 putStrLn " --version Print version number"
90 doIt :: String -> [String] -> IO ()
92 let (ghc_args, rest) = getGhcArgs args
95 -- behave like typical perl, python, ruby interpreters:
97 tmpdir <- getTemporaryDirectory
99 (openTempFile tmpdir "runghcXXXX.hs")
100 (\(filename,h) -> do hClose h; removeFile filename)
101 $ \(filename,h) -> do
102 getContents >>= hPutStr h
104 doIt ghc (ghc_args ++ [filename])
105 filename : prog_args -> do
106 -- If the file exists, and is not a .lhs file, then we
107 -- want to treat it as a .hs file.
109 -- If the file doesn't exist then GHC is going to look for
110 -- filename.hs and filename.lhs, and use the appropriate
112 exists <- doesFileExist filename
113 let xflag = if exists && (takeExtension filename /= ".lhs")
116 c1 = ":set prog " ++ show filename
117 c2 = ":main " ++ show prog_args
118 res <- rawSystem ghc (["-ignore-dot-ghci"] ++
121 [ "-e", c1, "-e", c2, filename])
124 getGhcArgs :: [String] -> ([String], [String])
126 = let (ghcArgs, otherArgs) = case break pastArgs args of
127 (xs, "--":ys) -> (xs, ys)
129 in (map unescape ghcArgs, otherArgs)
130 where unescape ('-':'-':'g':'h':'c':'-':'a':'r':'g':'=':arg) = arg
133 pastArgs :: String -> Bool
134 -- You can use -- to mark the end of the flags, in case you need to use
135 -- a file called -foo.hs for some reason. You almost certainly shouldn't,
138 pastArgs ('-':_) = False
141 dieProg :: String -> IO a
144 hPutStrLn stderr (p ++ ": " ++ msg)
145 exitWith (ExitFailure 1)
148 -- usage = "syntax: runghc [-f GHC-PATH | --] [GHC-ARGS] [--] FILE ARG..."
150 getExecPath :: IO (Maybe String)
151 #if defined(mingw32_HOST_OS)
153 allocaArray len $ \buf -> do
154 ret <- getModuleFileName nullPtr buf len
155 if ret == 0 then return Nothing
156 else liftM Just $ peekCString buf
157 where len = 2048 -- Plenty, PATH_MAX is 512 under Win32.
159 foreign import stdcall unsafe "GetModuleFileNameA"
160 getModuleFileName :: Ptr () -> CString -> Int -> IO Int32
162 getExecPath = return Nothing