tools.texi 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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, 2011, 2014
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @page
  7. @node Miscellaneous Tools
  8. @chapter Miscellaneous Tools
  9. Programming is more fun with a good tools. This chapter describes snarfing
  10. tools, and the @code{guild} program which can be used to invoke the rest
  11. of the tools (which are self-documenting). Some of these are used in Guile
  12. development, too. Imagine that!
  13. @menu
  14. * Snarfing:: Grepping the source in various ways.
  15. * Executable Modules:: Modules callable via guild.
  16. @end menu
  17. @c ---------------------------------------------------------------------------
  18. @node Snarfing
  19. @section Snarfing
  20. @cindex snarfing
  21. Because it's easier to maintain documentation, code, and other metainfo in one
  22. source file than in many files, there have evolved many methods for grepping
  23. source to lift and separate these kinds of info, in the process generating
  24. docs or fragments of source or what have you. This is known generally as
  25. @dfn{snarfing}, which comes from the verb ``to snarf'', here meaning ``to
  26. unceremoniously extract information from a somewhat unwilling source.''
  27. This section documents the installed program @code{guile-snarf} which does
  28. @dfn{init snarfing}, and also touches upon guile's doc snarfing process which
  29. is not yet finalized (i.e., doc snarfing programs are not installed at this
  30. time).
  31. @menu
  32. * Init Snarfing with guile-snarf:: Exposing C subrs and friends to Scheme.
  33. * Doc Snarfing:: Generating GDFv2 or texi from source.
  34. @end menu
  35. @c ---------------------------------------------------------------------------
  36. @node Init Snarfing with guile-snarf
  37. @subsection Init Snarfing with guile-snarf
  38. @c NOTE: This node and two subnodes are adapted from ../sources/snarf.texi.
  39. @cindex snarfing, init
  40. @cindex primitive functions
  41. @cindex subrs, defining
  42. When writing C code for use with Guile, you typically define a set of C
  43. functions, and then make some of them visible to the Scheme world by
  44. calling the @code{scm_c_define_gsubr} function; a C function published in
  45. this way is called a @dfn{subr}. If you have many subrs to publish, it
  46. can sometimes be annoying to keep the list of calls to
  47. @code{scm_c_define_gsubr} in sync with the list of function definitions.
  48. Frequently, a programmer will define a new subr in C, recompile the
  49. application, and then discover that the Scheme interpreter cannot see
  50. the subr, because of a missed call to @code{scm_c_define_gsubr}.
  51. Guile provides the @code{guile-snarf} command to manage this problem.
  52. Using this tool, you can keep all the information needed to define the
  53. subr alongside the function definition itself; @code{guile-snarf} will
  54. extract this information from your source code, and automatically
  55. generate a file of calls to @code{scm_c_define_gsubr} which you can
  56. @code{#include} into an initialization function.
  57. @menu
  58. * How guile-snarf works:: Using @code{guile-snarf}, with example.
  59. * Macros guile-snarf recognizes:: How to mark up code for @code{guile-snarf}.
  60. * Writing your own snarfing macros:: How to define new things to snarf.
  61. @end menu
  62. @c ---------------------------------------------------------------------------
  63. @node How guile-snarf works
  64. @subsubsection How guile-snarf works
  65. @cindex guile-snarf invocation
  66. @cindex guile-snarf example
  67. Usage: guile-snarf [-o @var{outfile}] [@var{cpp-args} ...]
  68. The @code{guile-snarf} program will extract initialization actions to
  69. @var{outfile} or to standard output when no @var{outfile} has been
  70. specified or when @var{outfile} is @code{-}. The C preprocessor is
  71. called with @var{cpp-args} (which usually include an input file) and
  72. the output is filtered to extract the initialization actions.
  73. If there are errors during processing, @var{outfile} is deleted and the
  74. program exits with non-zero status.
  75. During snarfing, the pre-processor macro @code{SCM_MAGIC_SNARFER} is
  76. defined. You could use this to avoid including snarfer output files
  77. that don't yet exist by writing code like this:
  78. @smallexample
  79. #ifndef SCM_MAGIC_SNARFER
  80. #include "foo.x"
  81. #endif
  82. @end smallexample
  83. If the environment variable @code{CPP} is set, use its value instead of the
  84. C pre-processor determined at Guile configure-time.
  85. @xref{Macros guile-snarf recognizes}, for a list of the special (some would
  86. say magic) cpp macros you can use, including the list of deprecated macros.
  87. For example, here is how you might define a new subr called
  88. @code{clear-image}, implemented by the C function @code{clear_image}:
  89. @example
  90. @group
  91. #include <libguile.h>
  92. SCM_DEFINE (clear_image, "clear-image", 1, 0, 0,
  93. (SCM image),
  94. "Clear the image.")
  95. #define FUNC_NAME s_clear_image
  96. @{
  97. /* C code to clear the image in @code{image}... */
  98. @}
  99. #undef FUNC_NAME
  100. void
  101. init_image_type ()
  102. @{
  103. #include "image-type.x"
  104. @}
  105. @end group
  106. @end example
  107. The @code{SCM_DEFINE} declaration says that the C function
  108. @code{clear_image} implements a Scheme subr called @code{clear-image},
  109. which takes one required argument (of type @code{SCM} and named
  110. @code{image}), no optional arguments, and no rest argument. @xref{Doc
  111. Snarfing}, for info on the docstring.
  112. This works in concert with @code{FUNC_NAME} to also define a static
  113. array of characters named @code{s_clear_image}, initialized to the
  114. string "clear-image". The body of @code{clear_image} may use the array
  115. in error messages, instead of writing out the literal string; this may
  116. save string space on some systems.
  117. Assuming the text above lives in a file named @file{image-type.c}, you will
  118. need to execute the following command to prepare this file for compilation:
  119. @example
  120. guile-snarf -o image-type.x image-type.c
  121. @end example
  122. This scans @file{image-type.c} for @code{SCM_DEFINE}
  123. declarations, and writes to @file{image-type.x} the output:
  124. @example
  125. scm_c_define_gsubr (s_clear_image, 1, 0, 0, (SCM (*)() ) clear_image);
  126. @end example
  127. When compiled normally, @code{SCM_DEFINE} is a macro which expands to
  128. a declaration of the @code{s_clear_image} string and the function
  129. header for @code{clear_image}.
  130. Note that the output file name matches the @code{#include} from the
  131. input file. Also, you still need to provide all the same information
  132. you would if you were using @code{scm_c_define_gsubr} yourself, but you
  133. can place the information near the function definition itself, so it is
  134. less likely to become incorrect or out-of-date.
  135. If you have many files that @code{guile-snarf} must process, you should
  136. consider using a fragment like the following in your Makefile:
  137. @example
  138. snarfcppopts = $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
  139. .SUFFIXES: .x
  140. .c.x:
  141. guile-snarf -o $@@ $< $(snarfcppopts)
  142. @end example
  143. This tells make to run @code{guile-snarf} to produce each needed
  144. @file{.x} file from the corresponding @file{.c} file.
  145. The program @code{guile-snarf} passes its command-line arguments
  146. directly to the C preprocessor, which it uses to extract the
  147. information it needs from the source code. this means you can pass
  148. normal compilation flags to @code{guile-snarf} to define preprocessor
  149. symbols, add header file directories, and so on.
  150. @c ---------------------------------------------------------------------------
  151. @node Macros guile-snarf recognizes
  152. @subsubsection Macros guile-snarf recognizes
  153. @cindex guile-snarf recognized macros
  154. @cindex guile-snarf deprecated macros
  155. Here are the macros you can use in your source code from which
  156. @code{guile-snarf} can construct initialization code:
  157. @example
  158. /* procedures */
  159. SCM_DEFINE (FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
  160. SCM_PROC (RANAME, STR, REQ, OPT, VAR, CFN)
  161. SCM_REGISTER_PROC (RANAME, STR, REQ, OPT, VAR, CFN)
  162. SCM_GPROC (RANAME, STR, REQ, OPT, VAR, CFN, GF)
  163. /* everything else */
  164. SCM_SYMBOL (c_name, scheme_name)
  165. SCM_GLOBAL_SYMBOL (c_name, scheme_name)
  166. SCM_KEYWORD (c_name, scheme_name)
  167. SCM_GLOBAL_KEYWORD (c_name, scheme_name)
  168. SCM_VARIABLE (c_name, scheme_name)
  169. SCM_GLOBAL_VARIABLE (c_name, scheme_name)
  170. SCM_VARIABLE_INIT (c_name, scheme_name, init_val)
  171. SCM_GLOBAL_VARIABLE_INIT (c_name, scheme_name, init_val)
  172. @end example
  173. @c i like things dense, but maybe someone else will reformat this
  174. @c into an easier-to-read list. also, all-upcase to me is a form
  175. @c of quoting, so @var{} is not necessary there. --ttn
  176. REQ and OPT are numbers indicating required and optional argument
  177. counts, respectively; VAR is a number that, if non-zero, means the
  178. function will accept any remaining arguments as a list; DOCSTRING is a
  179. string (use @code{\n\} at eol for multi-line); FNAME is a C-language
  180. identifier, CFN and GF and @var{c_name} likewise; PRIMNAME is a string
  181. denoting the name available to Scheme code, STR and @var{scheme_name}
  182. likewise; RANAME is the name of the static string (must match that
  183. declared by the associated definition of cpp macro @var{FUNC_NAME});
  184. ARGLIST is an argument list (in parentheses); and lastly, @var{init_val}
  185. is a expression suitable for initializing a new variable.
  186. For procedures, you can use @code{SCM_DEFINE} for most purposes. Use
  187. @code{SCM_PROC} along with @code{SCM_REGISTER_PROC} when you don't
  188. want to be bothered with docstrings. Use @code{SCM_GPROC} for generic
  189. functions (@pxref{Creating Generic Functions}). All procedures are
  190. declared with return type @code{SCM}.
  191. For everything else, use the appropriate macro (@code{SCM_SYMBOL} for
  192. symbols, and so on). Without "_GLOBAL_", the declarations are
  193. @code{static}.
  194. All these macros should be used at top-level, outside function bodies.
  195. Also, it's a good idea to define @var{FUNC_NAME} immediately after using
  196. @code{SCM_DEFINE} (and similar), and then the function body, and then
  197. @code{#undef FUNC_NAME}.
  198. @xref{How guile-snarf works}, and also libguile source, for examples.
  199. @xref{Subrs}, for details on argument passing and how to write C
  200. functions.
  201. @c ---------------------------------------------------------------------------
  202. @node Writing your own snarfing macros
  203. @subsubsection Writing your own snarfing macros
  204. When you want to use the general snarfing mechanism, but none of the
  205. provided macros fits your need, you can use the macro
  206. @code{SCM_SNARF_INIT}.
  207. For example, the @code{SCM_SYMBOL} macro can be defined like this:
  208. @example
  209. #define SCM_SYMBOL(c_name, scheme_name) \
  210. static SCM c_name \
  211. SCM_SNARF_INIT(c_name = scm_permanent_object (scm_str2symbol (scheme_name)))
  212. @end example
  213. @defmac SCM_SNARF_INIT (code)
  214. When processed normally, @code{SCM_SNARF_INIT} expands to nothing;
  215. when processed by the snarfer, it causes @var{code} to be included in
  216. the initialization action file, followed by a semicolon.
  217. @end defmac
  218. @c ---------------------------------------------------------------------------
  219. @node Doc Snarfing
  220. @subsection Doc Snarfing
  221. In addition to init snarfing (@pxref{Init Snarfing with guile-snarf}),
  222. the libguile sources are also subject to doc snarfing, by programs that
  223. are included in the distribution (but not installed at this time). The
  224. output is the file @file{guile-procedures.txt} which is installed, and
  225. subsequently used by module @code{(ice-9 documentation)}.
  226. Here is a list of what does what according to @file{libguile/Makefile.am}:
  227. @itemize
  228. @item guile-snarf-docs runs cpp defining SCM_MAGIC_SNARF_DOCS
  229. @item guile_filter_doc_snarfage parses guile-snarf-docs output to produce .doc
  230. @item ../scripts/snarf-check-and-output-texi makes guile.texi
  231. @item ../scripts/snarf-check-and-output-texi makes guile-procedures.txt
  232. @item guile-func-name-check checks source snarf-syntax integrity (optional?)
  233. @end itemize
  234. Note that for guile-1.4, a completely different approach was used! All this
  235. is rather byzantine, so for now @emph{NO} doc snarfing programs are installed.
  236. [fixme: Document further once doc snarfing is tamed somewhat. --ttn]
  237. @c ---------------------------------------------------------------------------
  238. @node Executable Modules
  239. @section Executable Modules
  240. @cindex guild
  241. @cindex guile-tools
  242. @cindex modules, executable
  243. @cindex executable modules
  244. @cindex scripts
  245. When Guile is installed, in addition to the @code{(ice-9 FOO)} modules, a set
  246. of @dfn{guild modules} @code{(scripts BAR)} is also installed. Each is
  247. a regular Scheme module that has some additional packaging so that it can be
  248. used by guild, from the shell. For this reason, we sometimes use the
  249. term @dfn{script} in this context to mean the same thing.
  250. As a convenience, the @code{guild} wrapper program is installed along with
  251. @code{guile}; it knows where a particular module is installed and calls it
  252. passing its args to the program. The result is that you need not augment your
  253. @code{PATH}. Usage is straightforward:
  254. @example
  255. guild --help
  256. guild --version
  257. guild [OPTION] PROGRAM [ARGS ...]
  258. If PROGRAM is "list" or omitted, display contents of scripts dir, otherwise
  259. PROGRAM is run w/ ARGS. Options (only one of which may be used at a time):
  260. --scriptsdir DIR -- Look in DIR for scripts
  261. --guileversion VERS -- Look in $pkgdatadir/VERS/scripts for scripts
  262. --source -- Display PROGRAM source (ignore ARGS) to stdout
  263. @end example
  264. The modules are self-documenting. For example, to see the documentation for
  265. @code{lint}, use one (or both) of the shell commands:
  266. @example
  267. guild display-commentary '(scripts lint)'
  268. guild --source lint
  269. @end example
  270. The rest of this section describes the packaging that goes into creating an
  271. executable module. Feel free to skip to the next chapter.
  272. @subsection Writing Executable Modules
  273. @c adapted from scripts/README
  274. See template file @code{PROGRAM} for a quick start.
  275. Programs must follow the @dfn{guild} convention, documented here:
  276. @itemize
  277. @item
  278. The module name must be "(scripts PROGRAM)". A procedure named PROGRAM w/
  279. signature "(PROGRAM . args)" must be exported. Basically, use some variant
  280. of the form:
  281. @example
  282. (define-module (scripts PROGRAM)
  283. #:export (PROGRAM))
  284. @end example
  285. Feel free to export other definitions useful in the module context.
  286. @item
  287. There must be the alias:
  288. @example
  289. (define main PROGRAM)
  290. @end example
  291. However, `main' must NOT be exported.
  292. @end itemize
  293. Following these conventions allows the program file to be used as module
  294. @code{(scripts PROGRAM)} in addition to being invoked by guild. Please
  295. also include a helpful Commentary section w/ some usage info.
  296. @c tools.texi ends here