[project @ 2000-12-30 20:06:00 by qrczak]
authorqrczak <unknown>
Sat, 30 Dec 2000 20:06:00 +0000 (20:06 +0000)
committerqrczak <unknown>
Sat, 30 Dec 2000 20:06:00 +0000 (20:06 +0000)
Add -I and --include options.
Small corrections and clarifications in the documentation.

ghc/docs/users_guide/utils.sgml
ghc/utils/hsc2hs/Main.hs

index 796ee90..4117f8d 100644 (file)
@@ -135,8 +135,8 @@ tags:
     <sect2>
       <title>Command line syntax</title>
 
-      <para>glue-hsc takes input files as arguments, and flags that
-      modify its behavior:</para>
+      <para><command>hsc2hs</command> takes input files as arguments,
+      and flags that modify its behavior:</para>
 
       <variablelist>
        <varlistentry>
@@ -171,6 +171,13 @@ tags:
        </varlistentry>
 
        <varlistentry>
+         <term><literal>-I DIR</literal></term>
+         <listitem>
+           <para>Passed to the C compiler.</para>
+         </listitem>
+       </varlistentry>
+
+       <varlistentry>
          <term><literal>--lflag=FLAG</literal></term>
          <listitem>
            <para>An extra flag to pass to the linker.</para>
@@ -178,6 +185,14 @@ tags:
        </varlistentry>
 
        <varlistentry>
+         <term><literal>--include=FILE</literal></term>
+         <listitem>
+           <para>As if the appropriate <literal>#include</literal>
+            directive was placed in the source.</para>
+         </listitem>
+       </varlistentry>
+
+       <varlistentry>
          <term><literal>--help</literal></term>
          <listitem>
            <para>Display a summary of the available flags.</para>
@@ -196,11 +211,11 @@ tags:
              <entry>Haskell file</entry>
            </row>
            <row>
-             <entry><literal>_hsc.h</literal></entry>
+             <entry><literal>.hs.h</literal></entry>
              <entry>C header</entry>
            </row>
            <row>
-             <entry><literal>_hsc.c</literal></entry>
+             <entry><literal>.hs.c</literal></entry>
              <entry>C file</entry>
            </row>
          </tbody>
@@ -280,7 +295,7 @@ tags:
             is placed in the <literal>{-# OPTIONS #-}</literal> pragma
             at the top of the Haskell file (see <xref
             linkend="source-file-options">). This is needed because
-            glue-hsc emits its own <literal>OPTIONS</literal> pragma,
+            <command>hsc2hs</command> emits its own <literal>OPTIONS</literal> pragma,
             and only one such pragma is interpreted by GHC.</para>
          </listitem>
        </varlistentry>
@@ -393,6 +408,13 @@ tags:
       prefixed by <literal>hsc_</literal> that handles the construct
       by emitting the expansion to stdout. See
       <filename>template-hsc.h</filename> for examples.</para>
+      
+      <para>Such macros can also be defined directly in the
+      source. They are useful for making a <literal>#let</literal>-like
+      macro whose expansion uses other <literal>#let</literal> macros.
+      Plain <literal>#let</literal> prepends <literal>hsc_</literal>
+      to the macro name and wraps the defininition in a
+      <literal>printf</literal> call.
 
     </sect2>
 
index 5ff8e44..5d88e60 100644 (file)
@@ -1,5 +1,5 @@
 -----------------------------------------------------------------------------
--- $Id: Main.hs,v 1.3 2000/12/28 10:34:56 qrczak Exp $
+-- $Id: Main.hs,v 1.4 2000/12/30 20:06:00 qrczak Exp $
 --
 -- (originally "GlueHsc.hs" by Marcin 'Qrczak' Kowalczyk)
 --
@@ -23,18 +23,27 @@ data Flag
     = Help
     | Template String
     | Compiler String
-    | Linker String
+    | Linker   String
     | CompFlag String
     | LinkFlag String
+    | Include  String
+
+include :: String -> Flag
+include s@('\"':_) = Include s
+include s@('<' :_) = Include s
+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 ""  ["lflag"]    (ReqArg LinkFlag "FLAG") "flag to pass to the linker",
-    Option ""  ["help"]     (NoArg  Help)            "display this help and exit"]
+    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 "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 ""  ["help"]     (NoArg  Help)              "display this help and exit"]
 
 main :: IO ()
 main = do
@@ -145,11 +154,12 @@ output flags name toks = let
         _   -> onlyOne "linker"
         
     writeFile cProgName $
-        concat ["#include \""++t++"\"\n" | Template t <- flags] ++
-        outHeaderCProg specials ++
-        "\nint main (void)\n{\n" ++
-        outHeaderHs (if needsH then Just outHName else Nothing) specials ++
-        concatMap outTokenHs toks ++
+        concat ["#include \""++t++"\"\n" | Template t <- flags]++
+        concat ["#include "++f++"\n"     | Include  f <- flags]++
+        outHeaderCProg specials++
+        "\nint main (void)\n{\n"++
+        outHeaderHs flags (if needsH then Just outHName else Nothing) specials++
+        concatMap outTokenHs toks++
         "    return 0;\n}\n"
     
     compilerStatus <- system $
@@ -183,6 +193,7 @@ output flags name toks = let
         \#include <Rts.h>\n\
         \#endif\n\
         \#include <HsFFI.h>\n"++
+        concat ["#include "++name++"\n" | Include name <- flags]++
         concatMap outTokenH specials++
         "#endif\n"
     
@@ -215,11 +226,11 @@ outHeaderCProg = concatMap $ \(key, arg) -> case key of
     where
     joinLines = concat . intersperse " \\\n" . lines
 
-outHeaderHs :: Maybe String -> [(String, String)] -> String
-outHeaderHs inH toks =
+outHeaderHs :: [Flag] -> Maybe String -> [(String, String)] -> String
+outHeaderHs flags inH toks =
     "    hsc_begin_options();\n"++
-    concatMap outSpecial toks ++
-    includeH ++
+    includeH++
+    concatMap outSpecial toks++
     "    hsc_end_options();\n\n"
     where
     outSpecial (key, arg) = case key of
@@ -240,9 +251,11 @@ outHeaderHs inH toks =
     toOptD arg = case break isSpace arg of
         (name, "")      -> name
         (name, _:value) -> name++'=':dropWhile isSpace value
-    includeH = case inH of
-        Nothing   -> ""
-        Just name -> outOption ("-#include \""++name++"\"")
+    includeH = concat [
+        outOption ("-#include "++name++"")
+        | name <- case inH of
+            Nothing   -> [name | Include name <- flags]
+            Just name -> ["\""++name++"\""]]
     outOption s = "    hsc_option (\""++showCString s++"\");\n"
 
 outTokenHs :: Token -> String