[project @ 2001-03-07 10:28:40 by rrt]
[ghc-hetmet.git] / ghc / compiler / main / DriverUtil.hs
1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.18 2001/03/07 10:28:40 rrt Exp $
3 --
4 -- Utils for the driver
5 --
6 -- (c) The University of Glasgow 2000
7 --
8 -----------------------------------------------------------------------------
9
10 module DriverUtil where
11
12 #include "../includes/config.h"
13 #include "HsVersions.h"
14
15 import Util
16 import Panic
17 import TmpFiles ( v_TmpDir )
18
19 import IOExts
20 import Exception
21 import Dynamic
22 import RegexString
23
24 import IO
25 import System
26 import Directory ( removeFile )
27 import List
28 import Char
29 import Monad
30
31 -----------------------------------------------------------------------------
32 -- Errors
33
34 GLOBAL_VAR(v_Path_usage,  "",  String)
35
36 long_usage = do
37   usage_path <- readIORef v_Path_usage
38   usage <- readFile usage_path
39   dump usage
40   exitWith ExitSuccess
41   where
42      dump "" = return ()
43      dump ('$':'$':s) = hPutStr stderr progName >> dump s
44      dump (c:s) = hPutChar stderr c >> dump s
45
46 -----------------------------------------------------------------------------
47 -- Reading OPTIONS pragmas
48
49 getOptionsFromSource 
50         :: String               -- input file
51         -> IO [String]          -- options, if any
52 getOptionsFromSource file
53   = do h <- openFile file ReadMode
54        catchJust ioErrors (look h)
55           (\e -> if isEOFError e then return [] else ioError e)
56   where
57         look h = do
58             l <- hGetLine h
59             case () of
60                 () | null l -> look h
61                    | prefixMatch "#" l -> look h
62                    | prefixMatch "{-# LINE" l -> look h   -- -}
63                    | Just (opts:_) <- matchRegex optionRegex l
64                         -> do rest <- look h
65                               return (words opts ++ rest)
66                    | otherwise -> return []
67
68 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}"   -- -}
69
70 -----------------------------------------------------------------------------
71 -- Utils
72
73 unknownFlagErr :: String -> a
74 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
75
76 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
77 my_partition _ [] = ([],[])
78 my_partition p (a:as)
79   = let (bs,cs) = my_partition p as in
80     case p a of
81         Nothing -> (bs,a:cs)
82         Just b  -> ((a,b):bs,cs)
83
84 my_prefix_match :: String -> String -> Maybe String
85 my_prefix_match [] rest = Just rest
86 my_prefix_match (_:_) [] = Nothing
87 my_prefix_match (p:pat) (r:rest)
88   | p == r    = my_prefix_match pat rest
89   | otherwise = Nothing
90
91 later = flip finally
92
93 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
94 handleDyn = flip catchDyn
95
96 handle :: (Exception -> IO a) -> IO a -> IO a
97 handle = flip Exception.catchAllIO
98
99 split :: Char -> String -> [String]
100 split c s = case rest of
101                 []     -> [chunk] 
102                 _:rest -> chunk : split c rest
103   where (chunk, rest) = break (==c) s
104
105 add :: IORef [a] -> a -> IO ()
106 add var x = do
107   xs <- readIORef var
108   writeIORef var (x:xs)
109
110 addNoDups :: Eq a => IORef [a] -> a -> IO ()
111 addNoDups var x = do
112   xs <- readIORef var
113   unless (x `elem` xs) $ writeIORef var (x:xs)
114
115 splitFilename :: String -> (String,String)
116 splitFilename f = split_longest_prefix f '.'
117
118 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
119 splitFilename3 :: String -> (String,String,String)
120 splitFilename3 str
121    = let (dir, rest) = split_longest_prefix str '/'
122          (name, ext) = splitFilename rest
123          real_dir | null dir  = "."
124                   | otherwise = dir
125      in  (real_dir, name, ext)
126
127 remove_suffix :: Char -> String -> String
128 remove_suffix c s
129   | null pre  = reverse suf
130   | otherwise = reverse pre
131   where (suf,pre) = break (==c) (reverse s)
132
133 drop_longest_prefix :: String -> Char -> String
134 drop_longest_prefix s c = reverse suf
135   where (suf,_pre) = break (==c) (reverse s)
136
137 take_longest_prefix :: String -> Char -> String
138 take_longest_prefix s c = reverse pre
139   where (_suf,pre) = break (==c) (reverse s)
140
141 -- split a string at the last occurence of 'c', returning the two
142 -- parts of the string with the 'c' removed.  If the string contains
143 -- no 'c's, the entire string is returned in the second component.
144 split_longest_prefix :: String -> Char -> (String,String)
145 split_longest_prefix s c
146   = case pre of
147         []      -> ([], reverse suf)
148         (_:pre) -> (reverse pre, reverse suf)
149   where (suf,pre) = break (==c) (reverse s)
150
151 newsuf :: String -> String -> String
152 newsuf suf s = remove_suffix '.' s ++ suf
153
154 -- getdir strips the filename off the input string, returning the directory.
155 getdir :: String -> String
156 getdir s = if null dir then "." else init dir
157   where dir = take_longest_prefix s '/'
158
159 newdir :: String -> String -> String
160 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
161
162 remove_spaces :: String -> String
163 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
164
165
166 -- system that works feasibly under Windows (i.e. passes the command line to sh,
167 -- because system() under Windows doesn't look at SHELL, and always uses CMD.EXE)
168 kludgedSystem cmd phase_name
169  = do
170 #ifndef mingw32_TARGET_OS
171    exit_code <- system cmd `catchAllIO` 
172                    (\_ -> throwDyn (PhaseFailed phase_name (ExitFailure 1)))
173 #else
174    pid <- myGetProcessID
175    tmp_dir <- readIORef v_TmpDir
176    let tmp = tmp_dir++"/sh"++show pid
177    h <- openFile tmp WriteMode
178    hPutStrLn h cmd
179    hClose h
180    exit_code <- system ("sh - " ++ tmp) `catchAllIO` 
181                    (\_ -> removeFile tmp >>
182                           throwDyn (PhaseFailed phase_name (ExitFailure 1)))
183    removeFile tmp
184 #endif
185    return exit_code