libguile-snarf.texi 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2012, 2014
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Function Snarfing
  7. @section Function Snarfing
  8. When writing C code for use with Guile, you typically define a set of
  9. C functions, and then make some of them visible to the Scheme world by
  10. calling @code{scm_c_define_gsubr} or related functions. If you have
  11. many functions to publish, it can sometimes be annoying to keep the
  12. list of calls to @code{scm_c_define_gsubr} in sync with the list of
  13. function definitions.
  14. Guile provides the @code{guile-snarf} program to manage this problem.
  15. Using this tool, you can keep all the information needed to define the
  16. function alongside the function definition itself; @code{guile-snarf}
  17. will extract this information from your source code, and automatically
  18. generate a file of calls to @code{scm_c_define_gsubr} which you can
  19. @code{#include} into an initialization function.
  20. The snarfing mechanism works for many kind of initialization actions,
  21. not just for collecting calls to @code{scm_c_define_gsubr}. For a
  22. full list of what can be done, @xref{Snarfing Macros}.
  23. @cindex guile-snarf invocation
  24. @cindex guile-snarf example
  25. The @code{guile-snarf} program is invoked like this:
  26. @smallexample
  27. guile-snarf [-o @var{outfile}] [@var{cpp-args} ...]
  28. @end smallexample
  29. This command will extract initialization actions to @var{outfile}.
  30. When no @var{outfile} has been specified or when @var{outfile} is
  31. @code{-}, standard output will be used. The C preprocessor is called
  32. with @var{cpp-args} (which usually include an input file) and the
  33. output is filtered to extract the initialization actions.
  34. If there are errors during processing, @var{outfile} is deleted and the
  35. program exits with non-zero status.
  36. During snarfing, the pre-processor macro @code{SCM_MAGIC_SNARFER} is
  37. defined. You could use this to avoid including snarfer output files
  38. that don't yet exist by writing code like this:
  39. @smallexample
  40. #ifndef SCM_MAGIC_SNARFER
  41. #include "foo.x"
  42. #endif
  43. @end smallexample
  44. Here is how you might define the Scheme function @code{clear-image},
  45. implemented by the C function @code{clear_image}:
  46. @example
  47. @group
  48. #include <libguile.h>
  49. SCM_DEFINE (clear_image, "clear-image", 1, 0, 0,
  50. (SCM image),
  51. "Clear the image.")
  52. @{
  53. /* C code to clear the image in @code{image}... */
  54. @}
  55. void
  56. init_image_type ()
  57. @{
  58. #include "image-type.x"
  59. @}
  60. @end group
  61. @end example
  62. The @code{SCM_DEFINE} declaration says that the C function
  63. @code{clear_image} implements a Scheme function called
  64. @code{clear-image}, which takes one required argument (of type
  65. @code{SCM} and named @code{image}), no optional arguments, and no rest
  66. argument. The string @code{"Clear the image."} provides a short help
  67. text for the function, it is called a @dfn{docstring}.
  68. @code{SCM_DEFINE} macro also defines a static array of characters
  69. initialized to the Scheme name of the function. In this case,
  70. @code{s_clear_image} is set to the C string, "clear-image". You might
  71. want to use this symbol when generating error messages.
  72. Assuming the text above lives in a file named @file{image-type.c}, you
  73. will need to execute the following command to prepare this file for
  74. compilation:
  75. @example
  76. guile-snarf -o image-type.x image-type.c
  77. @end example
  78. This scans @file{image-type.c} for @code{SCM_DEFINE}
  79. declarations, and writes to @file{image-type.x} the output:
  80. @example
  81. scm_c_define_gsubr ("clear-image", 1, 0, 0, (SCM (*)() ) clear_image);
  82. @end example
  83. When compiled normally, @code{SCM_DEFINE} is a macro which expands to
  84. the function header for @code{clear_image}.
  85. Note that the output file name matches the @code{#include} from the
  86. input file. Also, you still need to provide all the same information
  87. you would if you were using @code{scm_c_define_gsubr} yourself, but you
  88. can place the information near the function definition itself, so it is
  89. less 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 fragment like the following in your Makefile:
  92. @example
  93. snarfcppopts = $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
  94. .SUFFIXES: .x
  95. .c.x:
  96. guile-snarf -o $@@ $< $(snarfcppopts)
  97. @end example
  98. This tells make to run @code{guile-snarf} to produce each needed
  99. @file{.x} file from the corresponding @file{.c} file.
  100. The program @code{guile-snarf} passes its command-line arguments
  101. directly to the C preprocessor, which it uses to extract the
  102. information it needs from the source code. this means you can pass
  103. normal compilation flags to @code{guile-snarf} to define preprocessor
  104. symbols, add header file directories, and so on.