1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.49 2005/01/11 16:21:53 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 )
30 import EXCEPTION ( Exception(..), finally, throwDyn, catchDyn, throw )
31 import qualified EXCEPTION as Exception
33 import DATA_IOREF ( IORef, readIORef, writeIORef )
41 -----------------------------------------------------------------------------
42 -- Reading OPTIONS pragmas
45 :: String -- input file
46 -> IO [String] -- options, if any
47 getOptionsFromSource file
48 = do h <- openFile file ReadMode
49 look h `finally` hClose h
52 r <- tryJust ioErrors (hGetLine h)
54 Left e | isEOFError e -> return []
55 | otherwise -> ioError e
57 let l = remove_spaces l'
60 | prefixMatch "#" l -> look h
61 | prefixMatch "{-# LINE" l -> look h -- -}
62 | Just opts <- matchOptions l
64 return (words opts ++ rest)
65 | otherwise -> return []
67 -- detect {-# OPTIONS_GHC ... #-}. For the time being, we accept OPTIONS
68 -- instead of OPTIONS_GHC, but that is deprecated.
70 | Just s1 <- maybePrefixMatch "{-#" s -- -}
71 = matchOptions1 (remove_spaces s1)
76 | Just s2 <- maybePrefixMatch "OPTIONS" s
78 _ | Just s3 <- maybePrefixMatch "_GHC" s2, not (is_ident (head s3))
80 | not (is_ident (head s2))
83 -> Just [] -- OPTIONS_anything is ignored, not treated as start of source
86 | Just s3 <- maybePrefixMatch "}-#" (reverse s) = Just (reverse s3)
89 -----------------------------------------------------------------------------
90 -- A version of getDirectoryContents that is non-fatal if the
91 -- directory doesn't exist.
93 softGetDirectoryContents d
94 = IO.catch (getDirectoryContents d)
95 (\_ -> do hPutStrLn stderr
96 ("WARNING: error while reading directory " ++ d)
100 -----------------------------------------------------------------------------
101 -- Create a hierarchy of directories
103 createDirectoryHierarchy :: FilePath -> IO ()
104 createDirectoryHierarchy dir = do
105 b <- doesDirectoryExist dir
107 createDirectoryHierarchy (directoryOf dir)
110 -----------------------------------------------------------------------------
111 -- Verify that the 'dirname' portion of a FilePath exists.
113 doesDirNameExist :: FilePath -> IO Bool
114 doesDirNameExist fpath = doesDirectoryExist (directoryOf fpath)
116 -----------------------------------------------------------------------------
117 -- Prefixing underscore to linker-level names
118 prefixUnderscore :: String -> String
120 | cLeadingUnderscore == "YES" = ('_':)
123 -----------------------------------------------------------------------------
126 unknownFlagErr :: String -> a
127 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
129 unknownFlagsErr :: [String] -> a
130 unknownFlagsErr fs = throwDyn (UsageError ("unrecognised flags: " ++ unwords fs))
132 missingArgErr :: String -> a
133 missingArgErr f = throwDyn (UsageError ("missing argument for flag: " ++ f))
137 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
138 handleDyn = flip catchDyn
140 handle :: (Exception -> IO a) -> IO a -> IO a
141 #if __GLASGOW_HASKELL__ < 501
142 handle = flip Exception.catchAllIO
144 handle h f = f `Exception.catch` \e -> case e of
145 ExitException _ -> throw e
149 split :: Char -> String -> [String]
150 split c s = case rest of
152 _:rest -> chunk : split c rest
153 where (chunk, rest) = break (==c) s
155 add :: IORef [a] -> a -> IO ()
158 writeIORef var (x:xs)
160 addNoDups :: Eq a => IORef [a] -> a -> IO ()
163 unless (x `elem` xs) $ writeIORef var (x:xs)
165 ------------------------------------------------------
166 -- Filename manipulation
167 ------------------------------------------------------
171 splitFilename :: String -> (String,Suffix)
172 splitFilename f = split_longest_prefix f (=='.')
174 getFileSuffix :: String -> Suffix
175 getFileSuffix f = drop_longest_prefix f (=='.')
177 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy.ext")
178 splitFilenameDir :: String -> (String,String)
180 = let (dir, rest) = split_longest_prefix str isPathSeparator
181 real_dir | null dir = "."
185 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
186 splitFilename3 :: String -> (String,String,Suffix)
188 = let (dir, rest) = split_longest_prefix str isPathSeparator
189 (name, ext) = splitFilename rest
190 real_dir | null dir = "."
192 in (real_dir, name, ext)
194 remove_suffix :: Char -> String -> Suffix
197 | otherwise = reverse pre
198 where (suf,pre) = break (==c) (reverse s)
200 drop_longest_prefix :: String -> (Char -> Bool) -> String
201 drop_longest_prefix s pred = reverse suf
202 where (suf,_pre) = break pred (reverse s)
204 take_longest_prefix :: String -> (Char -> Bool) -> String
205 take_longest_prefix s pred = reverse pre
206 where (_suf,pre) = break pred (reverse s)
208 -- split a string at the last character where 'pred' is True,
209 -- returning a pair of strings. The first component holds the string
210 -- up (but not including) the last character for which 'pred' returned
211 -- True, the second whatever comes after (but also not including the
214 -- If 'pred' returns False for all characters in the string, the original
215 -- string is returned in the second component (and the first one is just
217 split_longest_prefix :: String -> (Char -> Bool) -> (String,String)
218 split_longest_prefix s pred
220 [] -> ([], reverse suf)
221 (_:pre) -> (reverse pre, reverse suf)
222 where (suf,pre) = break pred (reverse s)
224 replaceFilenameSuffix :: FilePath -> Suffix -> FilePath
225 replaceFilenameSuffix s suf = remove_suffix '.' s ++ suf
227 -- directoryOf strips the filename off the input string, returning
229 directoryOf :: FilePath -> String
230 directoryOf = fst . splitFilenameDir
232 -- filenameOf strips the directory off the input string, returning
234 filenameOf :: FilePath -> String
235 filenameOf = snd . splitFilenameDir
237 replaceFilenameDirectory :: FilePath -> String -> FilePath
238 replaceFilenameDirectory s dir
239 = dir ++ '/':drop_longest_prefix s isPathSeparator
241 remove_spaces :: String -> String
242 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
244 escapeSpaces :: String -> String
245 escapeSpaces = foldr (\c s -> if isSpace c then '\\':c:s else c:s) ""
247 isPathSeparator :: Char -> Bool
249 #ifdef mingw32_TARGET_OS
250 ch == '/' || ch == '\\'