1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.46 2004/11/09 16:59:31 simonmar Exp $
4 -- Utils for the driver
6 -- (c) The University of Glasgow 2000
8 -----------------------------------------------------------------------------
11 getOptionsFromSource, softGetDirectoryContents,
12 createDirectoryHierarchy, doesDirNameExist, prefixUnderscore,
13 unknownFlagErr, unknownFlagsErr, missingArgErr,
14 later, handleDyn, handle,
15 split, add, addNoDups,
16 Suffix, splitFilename, getFileSuffix,
17 splitFilename3, remove_suffix, split_longest_prefix,
18 replaceFilenameSuffix, directoryOf, filenameOf,
19 replaceFilenameDirectory, remove_spaces, escapeSpaces,
22 #include "../includes/ghcconfig.h"
23 #include "HsVersions.h"
27 import Config ( cLeadingUnderscore )
29 import EXCEPTION ( Exception(..), finally, throwDyn, catchDyn, throw )
30 import qualified EXCEPTION as Exception
32 import DATA_IOREF ( IORef, readIORef, writeIORef )
40 -----------------------------------------------------------------------------
41 -- Reading OPTIONS pragmas
44 :: String -- input file
45 -> IO [String] -- options, if any
46 getOptionsFromSource file
47 = do h <- openFile file ReadMode
48 look h `finally` hClose h
51 r <- tryJust ioErrors (hGetLine h)
53 Left e | isEOFError e -> return []
54 | otherwise -> ioError e
56 let l = remove_spaces l'
59 | prefixMatch "#" l -> look h
60 | prefixMatch "{-# LINE" l -> look h -- -}
61 | Just opts <- matchOptions l
63 return (words opts ++ rest)
64 | otherwise -> return []
67 | Just s1 <- maybePrefixMatch "{-#" s, -- -}
68 Just s2 <- maybePrefixMatch "OPTIONS" (remove_spaces s1),
69 Just s3 <- maybePrefixMatch "}-#" (reverse s2)
74 -----------------------------------------------------------------------------
75 -- A version of getDirectoryContents that is non-fatal if the
76 -- directory doesn't exist.
78 softGetDirectoryContents d
79 = IO.catch (getDirectoryContents d)
80 (\_ -> do hPutStrLn stderr
81 ("WARNING: error while reading directory " ++ d)
85 -----------------------------------------------------------------------------
86 -- Create a hierarchy of directories
88 createDirectoryHierarchy :: FilePath -> IO ()
89 createDirectoryHierarchy dir = do
90 b <- doesDirectoryExist dir
92 createDirectoryHierarchy (directoryOf dir)
95 -----------------------------------------------------------------------------
96 -- Verify that the 'dirname' portion of a FilePath exists.
98 doesDirNameExist :: FilePath -> IO Bool
99 doesDirNameExist fpath = doesDirectoryExist (directoryOf fpath)
101 -----------------------------------------------------------------------------
102 -- Prefixing underscore to linker-level names
103 prefixUnderscore :: String -> String
105 | cLeadingUnderscore == "YES" = ('_':)
108 -----------------------------------------------------------------------------
111 unknownFlagErr :: String -> a
112 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
114 unknownFlagsErr :: [String] -> a
115 unknownFlagsErr fs = throwDyn (UsageError ("unrecognised flags: " ++ unwords fs))
117 missingArgErr :: String -> a
118 missingArgErr f = throwDyn (UsageError ("missing argument for flag: " ++ f))
122 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
123 handleDyn = flip catchDyn
125 handle :: (Exception -> IO a) -> IO a -> IO a
126 #if __GLASGOW_HASKELL__ < 501
127 handle = flip Exception.catchAllIO
129 handle h f = f `Exception.catch` \e -> case e of
130 ExitException _ -> throw e
134 split :: Char -> String -> [String]
135 split c s = case rest of
137 _:rest -> chunk : split c rest
138 where (chunk, rest) = break (==c) s
140 add :: IORef [a] -> a -> IO ()
143 writeIORef var (x:xs)
145 addNoDups :: Eq a => IORef [a] -> a -> IO ()
148 unless (x `elem` xs) $ writeIORef var (x:xs)
150 ------------------------------------------------------
151 -- Filename manipulation
152 ------------------------------------------------------
156 splitFilename :: String -> (String,Suffix)
157 splitFilename f = split_longest_prefix f (=='.')
159 getFileSuffix :: String -> Suffix
160 getFileSuffix f = drop_longest_prefix f (=='.')
162 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy.ext")
163 splitFilenameDir :: String -> (String,String)
165 = let (dir, rest) = split_longest_prefix str isPathSeparator
166 real_dir | null dir = "."
170 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
171 splitFilename3 :: String -> (String,String,Suffix)
173 = let (dir, rest) = split_longest_prefix str isPathSeparator
174 (name, ext) = splitFilename rest
175 real_dir | null dir = "."
177 in (real_dir, name, ext)
179 remove_suffix :: Char -> String -> Suffix
182 | otherwise = reverse pre
183 where (suf,pre) = break (==c) (reverse s)
185 drop_longest_prefix :: String -> (Char -> Bool) -> String
186 drop_longest_prefix s pred = reverse suf
187 where (suf,_pre) = break pred (reverse s)
189 take_longest_prefix :: String -> (Char -> Bool) -> String
190 take_longest_prefix s pred = reverse pre
191 where (_suf,pre) = break pred (reverse s)
193 -- split a string at the last character where 'pred' is True,
194 -- returning a pair of strings. The first component holds the string
195 -- up (but not including) the last character for which 'pred' returned
196 -- True, the second whatever comes after (but also not including the
199 -- If 'pred' returns False for all characters in the string, the original
200 -- string is returned in the second component (and the first one is just
202 split_longest_prefix :: String -> (Char -> Bool) -> (String,String)
203 split_longest_prefix s pred
205 [] -> ([], reverse suf)
206 (_:pre) -> (reverse pre, reverse suf)
207 where (suf,pre) = break pred (reverse s)
209 replaceFilenameSuffix :: FilePath -> Suffix -> FilePath
210 replaceFilenameSuffix s suf = remove_suffix '.' s ++ suf
212 -- directoryOf strips the filename off the input string, returning
214 directoryOf :: FilePath -> String
215 directoryOf = fst . splitFilenameDir
217 -- filenameOf strips the directory off the input string, returning
219 filenameOf :: FilePath -> String
220 filenameOf = snd . splitFilenameDir
222 replaceFilenameDirectory :: FilePath -> String -> FilePath
223 replaceFilenameDirectory s dir
224 = dir ++ '/':drop_longest_prefix s isPathSeparator
226 remove_spaces :: String -> String
227 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
229 escapeSpaces :: String -> String
230 escapeSpaces = foldr (\c s -> if isSpace c then '\\':c:s else c:s) ""
232 isPathSeparator :: Char -> Bool
234 #ifdef mingw32_TARGET_OS
235 ch == '/' || ch == '\\'