[project @ 2001-03-04 11:18:03 by qrczak]
[ghc-hetmet.git] / ghc / utils / hsc2hs / Main.hs
index c677e78..f103d4c 100644 (file)
@@ -1,5 +1,5 @@
 -----------------------------------------------------------------------------
--- $Id: Main.hs,v 1.18 2001/02/13 15:09:02 rrt Exp $
+-- $Id: Main.hs,v 1.24 2001/03/04 11:18:03 qrczak Exp $
 --
 -- (originally "GlueHsc.hs" by Marcin 'Qrczak' Kowalczyk)
 --
@@ -13,7 +13,7 @@
 
 import GetOpt
 import System        (getProgName, getArgs, ExitCode(..), exitWith, exitFailure)
-import KludgedSystem (system)
+import KludgedSystem (system, defaultCompiler)
 import Directory     (removeFile)
 import Parsec
 import ParsecError
@@ -22,7 +22,7 @@ import Char          (ord, intToDigit, isSpace, isAlpha, isAlphaNum, toUpper)
 import List          (intersperse)
 
 version :: String
-version = "hsc2hs-0.64"
+version = "hsc2hs-0.65"
 
 data Flag
     = Help
@@ -32,6 +32,7 @@ data Flag
     | Linker   String
     | CompFlag String
     | LinkFlag String
+    | Keep
     | Include  String
 
 include :: String -> Flag
@@ -42,13 +43,14 @@ include s          = Include ("\""++s++"\"")
 options :: [OptDescr Flag]
 options = [
     Option "t" ["template"] (ReqArg Template   "FILE") "template file",
-    Option ""  ["cc"]       (ReqArg Compiler   "PROG") "C compiler to use",
-    Option ""  ["ld"]       (ReqArg Linker     "PROG") "linker to use",
-    Option ""  ["cflag"]    (ReqArg CompFlag   "FLAG") "flag to pass to the C compiler",
+    Option "c" ["cc"]       (ReqArg Compiler   "PROG") "C compiler to use",
+    Option "l" ["ld"]       (ReqArg Linker     "PROG") "linker to use",
+    Option "C" ["cflag"]    (ReqArg CompFlag   "FLAG") "flag to pass to the C compiler",
     Option "I" []           (ReqArg (CompFlag . ("-I"++))
                                                "DIR")  "passed to the C compiler",
-    Option ""  ["lflag"]    (ReqArg LinkFlag   "FLAG") "flag to pass to the linker",
-    Option ""  ["include"]  (ReqArg include    "FILE") "as if placed in the source",
+    Option "L" ["lflag"]    (ReqArg LinkFlag   "FLAG") "flag to pass to the linker",
+    Option ""  ["keep"]     (NoArg  Keep)              "don't delete *.hs_make.c",
+    Option "i" ["include"]  (ReqArg include    "FILE") "as if placed in the source",
     Option ""  ["help"]     (NoArg  Help)              "display this help and exit",
     Option ""  ["version"]  (NoArg  Version)           "output version information and exit"]
 
@@ -143,10 +145,19 @@ special = do
     pos <- getPosition
     char '#'
     skipMany (oneOf " \t")
+    keyArg pos pzero <|> do
+        char '{'
+        skipMany (oneOf " \t")
+        sp <- keyArg pos (string "\n")
+        char '}'
+        return sp
+
+keyArg :: SourcePos -> Parser String -> Parser Token
+keyArg pos eol = do
     key <- liftM2 (:) (letter <|> char '_') (many (alphaNum <|> char '_'))
         <?> "hsc directive"
     skipMany (oneOf " \t")
-    arg <- argument pzero
+    arg <- argument eol
     return (Special pos key arg)
 
 argument :: Parser String -> Parser String
@@ -185,9 +196,9 @@ output flags name toks = let
     baseName = case reverse name of
         'c':base -> reverse base
         _        -> name++".hs"
-    cProgName = baseName++"c_make_hs.c"
-    oProgName = baseName++"c_make_hs.o"
-    progName  = baseName++"c_make_hs"
+    cProgName = baseName++"_make.c"
+    oProgName = baseName++"_make.o"
+    progName  = baseName++"_make"
     outHsName = baseName
     outHName  = baseName++".h"
     outCName  = baseName++".c"
@@ -213,11 +224,7 @@ output flags name toks = let
         [c] -> return c
         _   -> onlyOne "compiler"
     linker <- case [l | Linker l <- flags] of
-#ifndef mingw32_TARGET_OS
-        []  -> return "gcc"
-#else
-        []  -> return "gcc -mno-cygwin"
-#endif
+        []  -> return defaultCompiler
         [l] -> return l
         _   -> onlyOne "linker"
         
@@ -240,7 +247,7 @@ output flags name toks = let
     case compilerStatus of
         e@(ExitFailure _) -> exitWith e
         _                 -> return ()
-    removeFile cProgName
+    when (null [() | Keep <- flags]) $ removeFile cProgName
     
     linkerStatus <- system $
         linker++