------------------------------------------------------------------------
--- $Id: Main.hs,v 1.35 2002/01/17 08:37:57 sof Exp $
+-- $Id: Main.hs,v 1.40 2002/09/09 11:39:42 simonmar Exp $
--
-- Program for converting .hsc files to .hs files, by converting the
-- file into a C program which is run to generate the Haskell source.
--
-- See the documentation in the Users' Guide for more details.
+#if __GLASGOW_HASKELL__ >= 504
+import System.Console.GetOpt
+#else
import GetOpt
+#endif
+
import Config
import System (getProgName, getArgs, ExitCode(..), exitWith, exitFailure, system)
import Directory (removeFile)
#include "../../includes/config.h"
-#ifdef mingw32_TARGET_OS
+#ifdef mingw32_HOST_OS
import Win32DLL
#endif
let header = "Usage: "++prog++" [OPTIONS] INPUT.hsc [...]"
args <- getArgs
let opts@(flags, files, errs) = getOpt Permute options args
-#ifdef mingw32_TARGET_OS
+#ifdef mingw32_HOST_OS
h <- getModuleHandle Nothing
n <- getModuleFileName h
let tempName = reverse (drop (length "\\bin\\hsc2hs.exe") (reverse n)) ++ "\\template-hsc.h"
exitFailure
processFile :: [Flag] -> String -> IO ()
-processFile flags name = do
- s <- readFile name
- case parser of
- Parser p -> case p (SourcePos name 1) s of
- Success _ _ _ toks -> output flags name toks
- Failure (SourcePos name' line) msg -> do
- putStrLn (name'++":"++show line++": "++msg)
- exitFailure
+processFile flags name
+ = do let file_name = dosifyPath name
+ s <- readFile file_name
+ case parser of
+ Parser p -> case p (SourcePos file_name 1) s of
+ Success _ _ _ toks -> output flags file_name toks
+ Failure (SourcePos name' line) msg -> do
+ putStrLn (name'++":"++show line++": "++msg)
+ exitFailure
+
+------------------------------------------------------------------------
+-- Convert paths foo/baz to foo\baz on Windows
+
+#if defined(mingw32_HOST_OS)
+subst a b ls = map (\ x -> if x == a then b else x) ls
+dosifyPath xs = subst '/' '\\' xs
+#else
+dosifyPath xs = xs
+#endif
------------------------------------------------------------------------
-- A deterministic parser which remembers the text which has been parsed.
let cProgName = outDir++outBase++"_hsc_make.c"
oProgName = outDir++outBase++"_hsc_make.o"
progName = outDir++outBase++"_hsc_make" ++ progNameSuffix
- outHName = outDir++outBase++"_hsc.h"
+ outHFile = outBase++"_hsc.h"
+ outHName = outDir++outHFile
outCName = outDir++outBase++"_hsc.c"
let execProgName
_ -> return ()
removeFile oProgName
- system (execProgName++" >"++outName)
+ progStatus <- system (execProgName++" >"++outName)
removeFile progName
+ case progStatus of
+ e@(ExitFailure _) -> exitWith e
+ _ -> return ()
when needsH $ writeFile outHName $
"#ifndef "++includeGuard++"\n\
"#endif\n"
when needsC $ writeFile outCName $
- "#include \""++outHName++"\"\n"++
+ "#include \""++outHFile++"\"\n"++
concatMap outTokenC specials
+ -- NB. outHFile not outHName; works better when processed
+ -- by gcc or mkdependC.
onlyOne :: String -> IO a
onlyOne what = do