Improve docs on symbols.
[bpt/guile.git] / doc / sources / libguile-tools.texi
CommitLineData
009e2b30
NJ
1@node Tools to automate adding libraries
2@chapter Tools to automate adding libraries
3
4You want to ...
5
6The chapters @ref{Libguile -- high level interface} and @ref{Libguile --
7SCM interface} showed how to make C libraries available from Scheme.
8Here I will describe some automated tools that the Guile team has made
9available. Some have been written especially for Guile (the Guile Magic
10Snarfer), and some are also in use with other languages (Python, Perl,
11...)
12
13@menu
14* By hand with gh_::
15* By hand with Guile Magic Snarfer::
16* Automatically using libtool::
17* Automatically using SWIG::
18@end menu
19
20@node By hand with gh_
21@section By hand with gh_
22
23@node By hand with Guile Magic Snarfer
24@section By hand with Guile Magic Snarfer
25
26When writing C code for use with Guile, you typically define a set of C
27functions, and then make some of them visible to the Scheme world by
28calling the @code{scm_make_gsubr} function; a C functions published in
29this way is called a @dfn{subr}. If you have many subrs to publish, it
30can sometimes be annoying to keep the list of calls to
31@code{scm_make_gsubr} in sync with the list of function definitions.
32Frequently, a programmer will define a new subr in C, recompile his
33application, and then discover that the Scheme interpreter cannot see
34the subr, because he forgot to call @code{scm_make_gsubr}.
35
36Guile provides the @code{guile-snarf} command to manage this problem.
37Using this tool, you can keep all the information needed to define the
38subr alongside the function definition itself; @code{guile-snarf} will
39extract this information from your source code, and automatically
40generate a file of calls to @code{scm_make_gsubr} which you can
41@code{#include} into an initialization function. (The command name
42comes from the verb ``to snarf'', here meaning ``to unceremoniously
43extract information from a somewhat unwilling source.'')
44
45@menu
46* How guile-snarf works:: Using the @code{guile-snarf} command.
47* Macros guile-snarf recognizes:: How to mark up code for @code{guile-snarf}.
48@end menu
49
50@node How guile-snarf works
51@subsection How @code{guile-snarf} works
52
53For example, here is how you might define a new subr called
54@code{clear-image}, implemented by the C function @code{clear_image}:
55
56@example
57@group
58#include <libguile.h>
59
60@dots{}
61
62SCM_PROC (s_clear_image, "clear-image", 1, 0, 0, clear_image);
63
64SCM
65clear_image (SCM image_smob)
66@{
67 @dots{}
68@}
69
70@dots{}
71
72void
73init_image_type ()
74@{
75#include "image-type.x"
76@}
77@end group
78@end example
79
80The @code{SCM_PROC} declaration says that the C function
81@code{clear_image} implements a Scheme subr called @code{clear-image},
82which takes one required argument, no optional arguments, and no tail
83argument. @code{SCM_PROC} also declares a static array of characters
84named @code{s_clear_image}, initialized to the string
85@code{"clear-image"}. The body of @code{clear_image} may use the array
86in error messages, instead of writing out the literal string; this may
87save string space on some systems.
88
89Assuming the text above lives in a file named @file{image-type.c}, you will
90need to execute the following command to compile this file:
91@example
92guile-snarf image-type.c > image-type.x
93@end example
94@noindent This scans @file{image-type.c} for @code{SCM_PROC}
95declarations, and sends the following output to the file
96@file{image-type.x}:
97@example
98scm_make_gsubr (s_clear_image, 1, 0, 0, clear_image);
99@end example
100When compiled normally, @code{SCM_PROC} is a macro which expands to a
101declaration of the @code{s_clear_image} string.
102
103In other words, @code{guile-snarf} scans source code looking for uses of
104the @code{SCM_PROC} macro, and generates C code to define the
105appropriate subrs. You need to provide all the same information you
106would if you were using @code{scm_make_gsubr} yourself, but you can
107place the information near the function definition itself, so it is less
108likely to become incorrect or out-of-date.
109
110If you have many files that @code{guile-snarf} must process, you should
111consider using a rule like the following in your Makefile:
112@example
113.SUFFIXES: .x
114.c.x:
115 ./guile-snarf $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $< > $@
116@end example
117This tells make to run @code{guile-snarf} to produce each needed
118@file{.x} file from the corresponding @file{.c} file.
119
120@code{guile-snarf} passes all its command-line arguments directly to the
121C preprocessor, which it uses to extract the information it needs from
122the source code. this means you can pass normal compilation flags to
123@code{guile-snarf} to define preprocessor symbols, add header file
124directories, and so on.
125
126
127
128@node Macros guile-snarf recognizes
129@subsection Macros @code{guile-snarf} recognizes
130
131Here are the macros you can use in your source code from which
132@code{guile-snarf} can construct initialization code:
133
134
135@defmac SCM_PROC (@var{namestr}, @var{name}, @var{req}, @var{opt}, @var{tail}, @var{c_func})
136Declare a new Scheme primitive function, or @dfn{subr}. The new subr
137will be named @var{name} in Scheme code, and be implemented by the C
138function @var{c_func}. The subr will take @var{req} required arguments
139and @var{opt} optional arguments. If @var{tail} is non-zero, the
140function will accept any remaining arguments as a list.
141
142Use this macro outside all function bodies, preferably above the
143definition of @var{c_func} itself. When compiled, the @code{SCM_PROC}
144declaration will expand to a definition for the @var{namestr} array,
145initialized to @var{name}. The @code{guile-snarf} command uses this
146declaration to automatically generate initialization code to create the
147subr and bind it in the top-level environment. @xref{How guile-snarf
148works}, for more info.
149
150@xref{Subrs}, for details on argument passing and how to write
151@var{c_func}.
152@end defmac
153
154
155@defmac SCM_GLOBAL (@var{var}, @var{scheme_name})
156Declare a global Scheme variable named @var{scheme_name}, and a static C
157variable named @var{var} to point to it. The value of the Scheme
158variable lives in the @sc{cdr} of the cell @var{var} points to.
159Initialize the variable to @code{#f}.
160
161Use this macro outside all function bodies. When compiled, the
162@code{SCM_GLOBAL} macro will expand to a definition for the variable
163@var{var}, initialized to an innocuous value. The @code{guile-snarf}
164command will use this declaration to automatically generate code to
165create a global variable named @var{scheme_name}, and store a pointer to
166its cell in @var{var}.
167@end defmac
168
169
170@defmac SCM_CONST_LONG (@var{var}, @var{scheme_name}, @var{value})
171Like @code{SCM_GLOBAL}, but initialize the variable to @var{value},
172which must be an integer.
173@end defmac
174
175
176@defmac SCM_SYMBOL (@var{var}, @var{name})
177Declare a C variable of type @code{SCM} named @var{var}, and initialize
178it to the Scheme symbol object whose name is @var{name}.
179
180Use this macro outside all function bodies. When compiled, the
181@code{SCM_SYMBOL} macro will expand to a definition for the variable
182@var{var}, initialized to an innocuous value. The @code{guile-snarf}
183command will use this declaration to automatically generate code to
184create a symbol named @var{name}, and store it in @var{var}.
185@end defmac
186
187@node Automatically using libtool
188@section Automatically using libtool
189
190@node Automatically using SWIG
191@section Automatically using SWIG