libguile-tools.texi 7.1 KB

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