[project @ 2002-10-12 23:28:48 by wolfgang]
[ghc-hetmet.git] / ghc / utils / hsc2hs / Main.hs
index 92aad1f..2527516 100644 (file)
@@ -1,5 +1,5 @@
 ------------------------------------------------------------------------
--- $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.
@@ -9,7 +9,12 @@
 --
 -- 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)
@@ -19,7 +24,7 @@ import List          (intersperse)
 
 #include "../../includes/config.h"
 
-#ifdef mingw32_TARGET_OS
+#ifdef mingw32_HOST_OS
 import Win32DLL
 #endif
 
@@ -71,7 +76,7 @@ main = do
     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"
@@ -93,14 +98,25 @@ main = do
             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.
@@ -445,7 +461,8 @@ output flags name toks = do
     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
@@ -504,8 +521,11 @@ output flags name toks = do
         _                 -> 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\ 
@@ -524,8 +544,10 @@ output flags name toks = do
         "#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