[MLton-devel] cvs commit: handling types in C header for _exports
Stephen Weeks
sweeks@users.sourceforge.net
Tue, 08 Jul 2003 18:03:41 -0700
sweeks 03/07/08 18:03:41
Modified: doc/examples/ffi Makefile
doc/user-guide ffi.tex
mlton/atoms ffi.fun
mlton/main compile.sml
Log:
I went with the solution of textually copying types.h at compile time
into the exported header file. That means that the file is completely
self-contained, but does leave around the typedefs. So, C code that
#includes the header will have to know this. That doesn't seem so
bad, since it has to know the types it dealing with when it calls the
exported functions anyway.
Revision Changes Path
1.9 +2 -2 mlton/doc/examples/ffi/Makefile
Index: Makefile
===================================================================
RCS file: /cvsroot/mlton/mlton/doc/examples/ffi/Makefile,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Makefile 9 Jul 2003 00:36:48 -0000 1.8
+++ Makefile 9 Jul 2003 01:03:40 -0000 1.9
@@ -6,8 +6,8 @@
./export
export: export.sml ffi-export.c
- $(mlton) -export-header true >export.h export.sml
- gcc -c -I/usr/lib/mlton/self/include ffi-export.c
+ $(mlton) -export-header true export.sml >export.h
+ gcc -c ffi-export.c
$(mlton) export.sml ffi-export.o
import: import.sml ffi-import.o
1.14 +14 -11 mlton/doc/user-guide/ffi.tex
Index: ffi.tex
===================================================================
RCS file: /cvsroot/mlton/mlton/doc/user-guide/ffi.tex,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- ffi.tex 8 Jul 2003 19:48:52 -0000 1.13
+++ ffi.tex 9 Jul 2003 01:03:41 -0000 1.14
@@ -65,17 +65,20 @@
(fn (x, c) => 13 + Real.floor x + Char.ord c)
\end{verbatim}
-To type check your C files, you can use the {\tt -export-header true}
-option, which generates a C header file with prototypes for all of the
-functions exported from SML. An example in the {\tt examples/ffi}
-directory demonstrates the use of {\export} expressions and generating
-the header file. Running {\tt make export} will produce an
-executable, {\tt export}, that should output {\tt success} when run.
+{\mlton}'s {\tt -export-header true} option generates a C header file
+with prototypes for all of the functions exported from SML. Include
+this header file in your C files to type check calls to functions
+exported from SML. This header file includes {\tt typedef}s for the
+types that can be passed between SML and C, as described in the next
+section. An example in the {\tt examples/ffi} directory demonstrates
+the use of {\export} expressions and generating the header file.
+Running {\tt make export} will produce an executable, {\tt export},
+that should output {\tt success} when run.
\begin{verbatim}
% make export
-mlton -export-header true >export.h export.sml
-gcc -c -I/usr/lib/mlton/self/include ffi-export.c
+mlton -export-header true export.sml >export.h
+gcc -c ffi-export.c
mlton export.sml ffi-export.o
% ./export
g starting
@@ -102,9 +105,9 @@
passing tuples or datatypes is not allowed because that would
interfere with representation optimizations.
-The file {\tt types.h} in the MLton include directory includes
-typedefs for the C types corresponding to the SML types.
-Here is the mapping between SML types and C types.
+The C header file that {\tt -export-header true} generates includes
+{\tt typedef}s for the C types corresponding to the SML types. Here
+is the mapping between SML types and C types.
\begin{center}
\begin{tabular}{lll}
1.3 +5 -4 mlton/mlton/atoms/ffi.fun
Index: ffi.fun
===================================================================
RCS file: /cvsroot/mlton/mlton/mlton/atoms/ffi.fun,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ffi.fun 24 Jun 2003 22:58:39 -0000 1.2
+++ ffi.fun 9 Jul 2003 01:03:41 -0000 1.3
@@ -65,10 +65,7 @@
end
val headers: string list ref = ref []
-
-fun declareHeaders {print} =
- List.foreach (!headers, fn s => (print s; print ";\n"))
-
+
fun declareExports {print} =
let
val maxMap = Type.memo (fn _ => ref ~1)
@@ -155,5 +152,9 @@
; print "}\n"
end)
end
+
+fun declareHeaders {print} =
+ (declareExports {print = fn _ => ()}
+ ; List.foreach (!headers, fn s => (print s; print ";\n")))
end
1.57 +5 -2 mlton/mlton/main/compile.sml
Index: compile.sml
===================================================================
RCS file: /cvsroot/mlton/mlton/mlton/main/compile.sml,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -r1.56 -r1.57
--- compile.sml 5 Jul 2003 23:30:26 -0000 1.56
+++ compile.sml 9 Jul 2003 01:03:41 -0000 1.57
@@ -355,8 +355,11 @@
then ()
else
let
- val _ = Ffi.declareExports {print = fn _ => ()}
- val _ = print "#include \"types.h\"\n"
+ val _ =
+ File.outputContents
+ (concat [!Control.libDir, "/include/types.h"],
+ Out.standard)
+ val _ = print "\n"
val _ = Ffi.declareHeaders {print = print}
in
Process.succeed ()
-------------------------------------------------------
This SF.Net email sponsored by: Parasoft
Error proof Web apps, automate testing & more.
Download & eval WebKing and get a free book.
www.parasoft.com/bulletproofapps
_______________________________________________
MLton-devel mailing list
MLton-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-devel