9d9ec56fd307dc3a8e4adae35e55b2165ee91313
[pspp] / doc / gnulib-tool.texi
1 @node Invoking gnulib-tool
2 @chapter Invoking gnulib-tool
3
4 @c Copyright (C) 2005-2008 Free Software Foundation, Inc.
5
6 @c Permission is granted to copy, distribute and/or modify this document
7 @c under the terms of the GNU Free Documentation License, Version 1.2 or
8 @c any later version published by the Free Software Foundation; with no
9 @c Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
10 @c Texts.  A copy of the license is included in the ``GNU Free
11 @c Documentation License'' file as part of this distribution.
12
13 @pindex gnulib-tool
14 @cindex invoking @command{gnulib-tool}
15
16 The @command{gnulib-tool} command is the recommended way to import
17 Gnulib modules.  It is possible to borrow Gnulib modules in a package
18 without using @command{gnulib-tool}, relying only on the
19 meta-information stored in the @file{modules/*} files, but with a
20 growing number of modules this becomes tedious.  @command{gnulib-tool}
21 simplifies the management of source files, @file{Makefile.am}s and
22 @file{configure.ac} in packages incorporating Gnulib modules.
23
24 Run @samp{gnulib-tool --help} for information.  To get familiar with
25 @command{gnulib-tool} without affecting your sources, you can also try
26 some commands with the option @samp{--dry-run}; then
27 @code{gnulib-tool} will only report which actions it would perform in
28 a real run without changing anything.
29
30 @menu
31 * Initial import::              First import of Gnulib modules.
32 * Modified imports::            Changing the import specification.
33 * Simple update::               Tracking Gnulib development.
34 * Source changes::              Impact of Gnulib on your source files.
35 * Localization::                Handling Gnulib's own message translations.
36 * VCS Issues::                  Integration with Version Control Systems.
37 @end menu
38
39
40 @node Initial import
41 @section Initial import
42 @cindex initial import
43
44 Gnulib assumes your project uses Autoconf and Automake.  Invoking
45 @samp{gnulib-tool --import} will copy source files, create a
46 @file{Makefile.am} to build them, generate a file @file{gnulib-comp.m4} with
47 Autoconf M4 macro declarations used by @file{configure.ac}, and generate
48 a file @file{gnulib-cache.m4} containing the cached specification of how
49 Gnulib is used.
50
51 Our example will be a library that uses Autoconf, Automake and
52 Libtool.  It calls @code{strdup}, and you wish to use gnulib to make
53 the package portable to C89 and C99 (which don't have @code{strdup}).
54
55 @example
56 ~/src/libfoo$ gnulib-tool --import strdup
57 Module list with included dependencies:
58   absolute-header
59   extensions
60   strdup
61   string
62 File list:
63   lib/dummy.c
64   lib/strdup.c
65   lib/string.in.h
66   m4/absolute-header.m4
67   m4/extensions.m4
68   m4/gnulib-common.m4
69   m4/strdup.m4
70   m4/string_h.m4
71 Creating directory ./lib
72 Creating directory ./m4
73 Copying file lib/dummy.c
74 Copying file lib/strdup.c
75 Copying file lib/string.in.h
76 Copying file m4/absolute-header.m4
77 Copying file m4/extensions.m4
78 Copying file m4/gnulib-common.m4
79 Copying file m4/gnulib-tool.m4
80 Copying file m4/strdup.m4
81 Copying file m4/string_h.m4
82 Creating lib/Makefile.am
83 Creating m4/gnulib-cache.m4
84 Creating m4/gnulib-comp.m4
85 Finished.
86
87 You may need to add #include directives for the following .h files.
88   #include <string.h>
89
90 Don't forget to
91   - add "lib/Makefile" to AC_CONFIG_FILES in ./configure.ac,
92   - mention "lib" in SUBDIRS in Makefile.am,
93   - mention "-I m4" in ACLOCAL_AMFLAGS in Makefile.am,
94   - invoke gl_EARLY in ./configure.ac, right after AC_PROG_CC,
95   - invoke gl_INIT in ./configure.ac.
96 ~/src/libfoo$
97 @end example
98
99 By default, the source code is copied into @file{lib/} and the M4
100 macros in @file{m4/}.  You can override these paths by using
101 @code{--source-base=DIRECTORY} and @code{--m4-base=DIRECTORY}.  Some
102 modules also provide other files necessary for building. These files
103 are copied into the directory specified by @samp{AC_CONFIG_AUX_DIR} in
104 @file{configure.ac} or by the @code{--aux-dir=DIRECTORY} option.  If
105 neither is specified, the current directory is assumed.
106
107 @code{gnulib-tool} can make symbolic links instead of copying the
108 source files.  The option to specify for this is @samp{--symlink}, or
109 @samp{-s} for short.  This can be useful to save a few kilobytes of disk
110 space.  But it is likely to introduce bugs when @code{gnulib} is updated;
111 it is more reliable to use @samp{gnulib-tool --update} (see below)
112 to update to newer versions of @code{gnulib}.  Furthermore it requires
113 extra effort to create self-contained tarballs, and it may disturb some
114 mechanism the maintainer applies to the sources.  For these reasons,
115 this option is generally discouraged.
116
117 @code{gnulib-tool} will overwrite any pre-existing files, in
118 particular @file{Makefile.am}.  Unfortunately, separating the
119 generated @file{Makefile.am} content (for building the gnulib library)
120 into a separate file, say @file{gnulib.mk}, that could be included
121 by your handwritten @file{Makefile.am} is not possible, due to how
122 variable assignments are handled by Automake.
123
124 Consequently, it is a good idea to choose directories that are not
125 already used by your projects, to separate gnulib imported files from
126 your own files.  This approach is also useful if you want to avoid
127 conflicts between other tools (e.g., @code{gettextize} that also copy
128 M4 files into your package.  Simon Josefsson successfully uses a source
129 base of @file{gl/}, and a M4 base of @file{gl/m4/}, in several
130 packages.
131
132 After the @samp{--import} option on the command line comes the list of
133 Gnulib modules that you want to incorporate in your package.  The names
134 of the modules coincide with the filenames in Gnulib's @file{modules/}
135 directory.
136
137 Some Gnulib modules depend on other Gnulib modules.  @code{gnulib-tool}
138 will automatically add the needed modules as well; you need not list
139 them explicitly.  @code{gnulib-tool} will also memorize which dependent
140 modules it has added, so that when someday a dependency is dropped, the
141 implicitly added module is dropped as well (unless you have explicitly
142 requested that module).
143
144 If you want to cut a dependency, i.e., not add a module although one of
145 your requested modules depends on it, you may use the option
146 @samp{--avoid=@var{module}} to do so.  Multiple uses of this option are
147 possible.  Of course, you will then need to implement the same interface
148 as the removed module.
149
150 A few manual steps are required to finish the initial import.
151 @code{gnulib-tool} printed a summary of these steps.
152
153 First, you must ensure Autoconf can find the macro definitions in
154 @file{gnulib-comp.m4}.  Use the @code{ACLOCAL_AMFLAGS} specifier in
155 your top-level @file{Makefile.am} file, as in:
156
157 @example
158 ACLOCAL_AMFLAGS = -I m4
159 @end example
160
161 You are now ready to call the M4 macros in @code{gnulib-comp.m4} from
162 @file{configure.ac}.  The macro @code{gl_EARLY} must be called as soon
163 as possible after verifying that the C compiler is working.
164 Typically, this is immediately after @code{AC_PROG_CC}, as in:
165
166 @example
167 ...
168 AC_PROG_CC
169 gl_EARLY
170 ...
171 @end example
172
173 The core part of the gnulib checks are done by the macro
174 @code{gl_INIT}.  Place it further down in the file, typically where
175 you normally check for header files or functions.  It must come after
176 other checks which may affect the compiler invocation, such as
177 @code{AC_MINIX}.  For example:
178
179 @example
180 ...
181 # For gnulib.
182 gl_INIT
183 ...
184 @end example
185
186 @code{gl_INIT} will in turn call the macros related with the
187 gnulib functions, be it specific gnulib macros, like @code{gl_FUNC_ALLOCA}
188 or autoconf or automake macros like @code{AC_FUNC_ALLOCA} or
189 @code{AM_FUNC_GETLINE}.  So there is no need to call those macros yourself
190 when you use the corresponding gnulib modules.
191
192 You must also make sure that the gnulib library is built.  Add the
193 @code{Makefile} in the gnulib source base directory to
194 @code{AC_CONFIG_FILES}, as in:
195
196 @example
197 AC_CONFIG_FILES(... lib/Makefile ...)
198 @end example
199
200 You must also make sure that @code{make} will recurse into the gnulib
201 directory.  To achieve this, add the gnulib source base directory to a
202 @code{SUBDIRS} Makefile.am statement, as in:
203
204 @example
205 SUBDIRS = lib
206 @end example
207
208 or if you, more likely, already have a few entries in @code{SUBDIRS},
209 you can add something like:
210
211 @example
212 SUBDIRS += lib
213 @end example
214
215 Finally, you have to add compiler and linker flags in the appropriate
216 source directories, so that you can make use of the gnulib library.
217 Since some modules (@samp{getopt}, for example) may copy files into
218 the build directory, @file{top_builddir/lib} is needed as well
219 as @file{top_srcdir/lib}.  For example:
220
221 @example
222 ...
223 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
224 ...
225 LDADD = lib/libgnu.a
226 ...
227 @end example
228
229 Don't forget to @code{#include} the various header files.  In this
230 example, you would need to make sure that @samp{#include <string.h>}
231 is evaluated when compiling all source code files, that want to make
232 use of @code{strdup}.
233
234 In the usual case where Autoconf is creating a @file{config.h} file,
235 you should include @file{config.h} first, before any other include
236 file.  That way, for example, if @file{config.h} defines
237 @samp{restrict} to be the empty string on a pre-C99 host, or a macro
238 like @samp{_FILE_OFFSET_BITS} that affects the layout of data
239 structures, the definition is consistent for all include files.
240 Also, on some platforms macros like @samp{_FILE_OFFSET_BITS} and
241 @samp{_GNU_SOURCE} may be ineffective, or may have only a limited
242 effect, if defined after the first system header file is included.
243
244 Finally, note that you can not use @code{AC_LIBOBJ} or
245 @code{AC_REPLACE_FUNCS} in your @file{configure.ac} and expect the
246 resulting object files to be automatically added to @file{lib/libgnu.a}.
247 This is because your @code{AC_LIBOBJ} and @code{AC_REPLACE_FUNCS} invocations
248 from @file{configure.ac} augment a variable @code{@@LIBOBJS@@} (and/or
249 @code{@@LTLIBOBJS@@} if using Libtool), whereas @file{lib/libgnu.a}
250 is built from the contents of a different variable, usually
251 @code{@@gl_LIBOBJS@@} (or @code{@@gl_LTLIBOBJS@@} if using Libtool).
252
253
254 @node Modified imports
255 @section Modified imports
256
257 You can at any moment decide to use Gnulib differently than the last time.
258
259 If you only want to use more Gnulib modules, simply invoke
260 @command{gnulib-tool --import @var{new-modules}}.  @code{gnulib-tool}
261 remembers which modules were used last time.  The list of modules that
262 you pass after @samp{--import} is @emph{added} to the previous list of
263 modules.
264
265 For most changes, such as added or removed modules, or even different
266 choices of @samp{--lib}, @samp{--source-base} or @samp{--aux-dir}, there
267 are two ways to perform the change.
268
269 The standard way is to modify manually the file @file{gnulib-cache.m4}
270 in the M4 macros directory, then launch @samp{gnulib-tool --import}.
271
272 The other way is to call @command{gnulib-tool} again, with the changed
273 command-line options.  Note that this doesn't let you remove modules,
274 because as you just learned, the list of modules is always cumulated.
275 Also this way is often impractical, because you don't remember the way
276 you invoked @code{gnulib-tool} last time.
277
278 The only change for which this doesn't work is a change of the
279 @samp{--m4-base} directory.  Because, when you pass a different value of
280 @samp{--m4-base}, @code{gnulib-tool} will not find the previous
281 @file{gnulib-cache.m4} file any more... A possible solution is to manually
282 copy the @file{gnulib-cache.m4} into the new M4 macro directory.
283
284 In the @file{gnulib-cache.m4}, the macros have the following meaning:
285 @table @code
286 @item gl_MODULES
287 The argument is a space separated list of the requested modules, not including
288 dependencies.
289
290 @item gl_AVOID
291 The argument is a space separated list of modules that should not be used,
292 even if they occur as dependencies.  Corresponds to the @samp{--avoid}
293 command line argument.
294
295 @item gl_SOURCE_BASE
296 The argument is the relative file name of the directory containing the gnulib
297 source files (mostly *.c and *.h files).  Corresponds to the
298 @samp{--source-base} command line argument.
299
300 @item gl_M4_BASE
301 The argument is the relative file name of the directory containing the gnulib
302 M4 macros (*.m4 files).  Corresponds to the @samp{--m4-base} command line
303 argument.
304
305 @item gl_TESTS_BASE
306 The argument is the relative file name of the directory containing the gnulib
307 unit test files.  Corresponds to the @samp{--tests-base} command line argument.
308
309 @item gl_LIB
310 The argument is the name of the library to be created.  Corresponds to the
311 @samp{--lib} command line argument.
312
313 @item gl_LGPL
314 The presence of this macro without arguments corresponds to the @samp{--lgpl}
315 command line argument.  The presence of this macro with an argument (whose
316 value must be 2 or 3) corresponds to the @samp{--lgpl=@var{arg}} command line
317 argument.
318
319 @item gl_LIBTOOL
320 The presence of this macro corresponds to the @samp{--libtool} command line
321 argument and to the absence of the @samp{--no-libtool} command line argument.
322 It takes no arguments.
323
324 @item gl_MACRO_PREFIX
325 The argument is the prefix to use for macros in the @file{gnulib-comp.m4}
326 file.  Corresponds to the @samp{--macro-prefix} command line argument.
327 @end table
328
329
330 @node Simple update
331 @section Simple update
332
333 When you want to update to a more recent version of Gnulib, without
334 changing the list of modules or other parameters, a simple call
335 does it:
336
337 @smallexample
338 $ gnulib-tool --import
339 @end smallexample
340
341 @noindent
342 This will create, update or remove files, as needed.
343
344 Note: From time to time, changes are made in Gnulib that are not backward
345 compatible.  When updating to a more recent Gnulib, you should consult
346 Gnulib's @file{NEWS} file to check whether the incompatible changes affect
347 your project.
348
349
350 @node Source changes
351 @section Changing your sources for use with Gnulib
352
353 Gnulib contains some header file overrides.  This means that when building
354 on systems with deficient header files in @file{/usr/include/}, it may create
355 files named @file{string.h}, @file{stdlib.h}, @file{stdint.h} or similar in
356 the build directory.  In the other source directories of your package you
357 will usually pass @samp{-I} options to the compiler, so that these Gnulib
358 substitutes are visible and take precedence over the files in
359 @file{/usr/include/}.
360
361 These Gnulib substitute header files rely on @file{<config.h>} being
362 already included.  Furthermore @file{<config.h>} must be the first include
363 in every compilation unit.  This means that to @emph{all your source files}
364 and likely also to @emph{all your tests source files} you need to add an
365 @samp{#include <config.h>} at the top.  Which source files are affected?
366 Exactly those whose compilation includes a @samp{-I} option that refers to
367 the Gnulib library directory.
368
369 This is annoying, but inevitable: On many systems, @file{<config.h>} is
370 used to set system dependent flags (such as @code{_GNU_SOURCE} on GNU systems),
371 and these flags have no effect after any system header file has been included.
372
373
374 @node Localization
375 @section Handling Gnulib's own message translations
376
377 Gnulib provides some functions that emit translatable messages using GNU
378 @code{gettext}.  The @samp{gnulib} domain at the
379 @url{http://translationproject.org/, Translation Project} collects
380 translations of these messages, which you should incorporate into your
381 own programs.
382
383 There are two basic ways to achieve this.  The first, and older, method
384 is to list all the source files you use from Gnulib in your own
385 @file{po/POTFILES.in} file.  This will cause all the relevant
386 translatable strings to be included in your POT file.  When you send
387 this POT file to the Translation Project, translators will normally fill
388 in the translations of the Gnulib strings from their ``translation
389 memory'', and send you back updated PO files.
390
391 However, this process is error-prone: you might forget to list some
392 source files, or the translator might not be using a translation memory
393 and provide a different translation than another translator, or the
394 translation might not be kept in sync between Gnulib and your package.
395 It is also slow and causes substantial extra work, because a human
396 translator must be in the loop for each language and you will need to
397 incorporate their work on request.
398
399 For these reasons, a new method was designed and is now recommended.  If
400 you pass the @code{--po-base=@var{directory}} and @code{--po-domain=@var{domain}}
401 options to @code{gnulib-tool}, then @code{gnulib-tool} will create a
402 separate directory with its own @file{POTFILES.in}, and fetch current
403 translations directly from the Translation Project (using
404 @command{rsync} or @command{wget}, whichever is available).
405 The POT file in this directory will be called
406 @file{@var{domain}-gnulib.pot}, depending on the @var{domain} you gave to the
407 @code{--po-domain} option (typically the same as the package name).
408 This causes these translations to reside in a separate message domain,
409 so that they do not clash either with the translations for the main part
410 of your package nor with those of other packages on the system that use
411 possibly different versions of Gnulib.
412 When you use these options, the functions in Gnulib are built
413 in such a way that they will always use this domain regardless of the
414 default domain set by @code{textdomain}.
415
416 In order to use this method, you must -- in each program that might use
417 Gnulib code -- add an extra line to the part of the program that
418 initializes locale-dependent behavior.  Where you would normally write
419 something like:
420
421 @example
422 @group
423   setlocale (LC_ALL, "");
424   bindtextdomain (PACKAGE, LOCALEDIR);
425   textdomain (PACKAGE);
426 @end group
427 @end example
428
429 @noindent
430 you should add an additional @code{bindtextdomain} call to inform
431 gettext of where the MO files for the extra message domain may be found:
432
433 @example
434 @group
435   bindtextdomain (PACKAGE "-gnulib", LOCALEDIR);
436 @end group
437 @end example
438
439 (This example assumes that the @var{domain} that you specified
440 to @code{gnulib-tool} is the same as the value of the @code{PACKAGE}
441 preprocessor macro.)
442
443 Since you do not change the @code{textdomain} call, the default message
444 domain for your program remains the same and your own use of @code{gettext}
445 functions will not be affected.
446
447
448 @node VCS Issues
449 @section Issues with Version Control Systems
450
451 If a project stores its source files in a version control system (VCS),
452 such as CVS, SVN, or Git, one needs to decide which files to commit.
453
454 All files created by @code{gnulib-tool}, except @file{gnulib-cache.m4},
455 should be treated like generated source files, like for example a
456 @file{parser.c} file is generated from @file{parser.y}.
457
458 @itemize
459
460 @item
461 In projects which commit all source files, whether generated or not, into
462 their VCS, the @code{gnulib-tool} generated files should all be committed.
463
464 Gnulib also contains files generated by @command{make} (and removed by
465 @code{make clean}), using information determined by @command{configure}
466 They should not be checked into the VCS, but instead added to
467 @file{.cvsignore}.  When you have a Gnulib source file of the form
468 @file{lib/foo_.h}, the corresponding @file{lib/foo.h} is such a file.
469
470 @item
471 In projects which customarily omit from their VCS all files that are generated
472 from other source files, all these files and directories would not be
473 added into the VCS.  The only file that must be added to the VCS is
474 @file{gnulib-cache.m4} in the M4 macros directory.  Also, the script for
475 restoring files not in the VCS, customarily called @file{autogen.sh} or
476 @file{bootstrap.sh}, will typically contain the statement for restoring
477 the omitted files:
478
479 @smallexample
480 $ gnulib-tool --update
481 @end smallexample
482
483 The @samp{--update} option operates much like the @samp{--import} option,
484 but it does not offer the possibility to change the way Gnulib is used.
485 Also it does not report in the ChangeLogs the files that it had to add
486 because they were missing.
487
488 @end itemize