Bump to 1.14.1
[platform/upstream/augeas.git] / doc / gnulib-tool.texi
1 @node Invoking gnulib-tool
2 @chapter Invoking gnulib-tool
3
4 @c Copyright (C) 2005-2016 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.3 or
8 @c any later version published by the Free Software Foundation; with no
9 @c Invariant Sections, no Front-Cover Texts, and 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 @file{gnulib-tool} is not installed in a standard directory that is
25 contained in the @code{PATH} variable.  It needs to be run directly in
26 the directory that contains the Gnulib source code.  You can do this
27 either by specifying the absolute filename of @file{gnulib-tool}, or
28 you can also use a symbolic link from a place inside your @code{PATH}
29 to the @file{gnulib-tool} file of your preferred and most up-to-date
30 Gnulib checkout, like this:
31 @smallexample
32 $ ln -s $HOME/gnu/src/gnulib.git/gnulib-tool $HOME/bin/gnulib-tool
33 @end smallexample
34
35 Run @samp{gnulib-tool --help} for information.  To get familiar with
36 @command{gnulib-tool} without affecting your sources, you can also try
37 some commands with the option @samp{--dry-run}; then
38 @code{gnulib-tool} will only report which actions it would perform in
39 a real run without changing anything.
40
41 @menu
42 * Which modules?::              Determining the needed set of Gnulib modules
43 * Initial import::              First import of Gnulib modules.
44 * Modified imports::            Changing the import specification.
45 * Simple update::               Tracking Gnulib development.
46 * Source changes::              Impact of Gnulib on your source files.
47 * Multiple instances::          Using Gnulib for both a library and a binary
48 * gettextize and autopoint::    Caveat: @code{gettextize} and @code{autopoint} users!
49 * Localization::                Handling Gnulib's own message translations.
50 * VCS Issues::                  Integration with Version Control Systems.
51 * Unit tests::                  Bundling the unit tests of the Gnulib modules.
52 * Conditional dependencies::    Avoiding unnecessary checks and compilations.
53 @end menu
54
55
56 @node Which modules?
57 @section Finding modules
58 @cindex Finding modules
59
60 There are three ways of finding the names of Gnulib modules that you can use
61 in your package:
62
63 @itemize
64 @item
65 You have the complete module list, sorted according to categories, in
66 @url{http://www.gnu.org/software/gnulib/MODULES.html}.
67
68 @item
69 If you are looking for a particular POSIX header or function replacement,
70 look in the chapters @ref{Header File Substitutes} and
71 @ref{Function Substitutes}.  For headers and functions that are provided by
72 Glibc but not standardized by POSIX, look in the chapters
73 @ref{Glibc Header File Substitutes} and @ref{Glibc Function Substitutes}.
74
75 @item
76 If you have already found the source file in Gnulib and are looking for the
77 module that contains this source file, you can use the command
78 @samp{gnulib-tool --find @var{filename}}.
79 @end itemize
80
81
82 @node Initial import
83 @section Initial import
84 @cindex initial import
85
86 Gnulib assumes that your project uses Autoconf.  When using Gnulib, you
87 will need to have Autoconf and Automake among your build tools.  Note that
88 while the use of Automake in your project's top level directory is an
89 easy way to fulfil the Makefile conventions of the GNU coding standards,
90 Gnulib does not require it.  But when you use Gnulib, Automake will be
91 used at least in a subdirectory of your project.
92
93 Invoking @samp{gnulib-tool --import} will copy source files, create a
94 @file{Makefile.am} to build them, generate a file @file{gnulib-comp.m4} with
95 Autoconf M4 macro declarations used by @file{configure.ac}, and generate
96 a file @file{gnulib-cache.m4} containing the cached specification of how
97 Gnulib is used.
98
99 Our example will be a library that uses Autoconf, Automake and
100 Libtool.  It calls @code{strdup}, and you wish to use gnulib to make
101 the package portable to C89 and C99 (which don't have @code{strdup}).
102
103 @example
104 ~/src/libfoo$ gnulib-tool --import strdup
105 Module list with included dependencies:
106   absolute-header
107   extensions
108   strdup
109   string
110 File list:
111   lib/dummy.c
112   lib/strdup.c
113   lib/string.in.h
114   m4/absolute-header.m4
115   m4/extensions.m4
116   m4/gnulib-common.m4
117   m4/strdup.m4
118   m4/string_h.m4
119 Creating directory ./lib
120 Creating directory ./m4
121 Copying file lib/dummy.c
122 Copying file lib/strdup.c
123 Copying file lib/string.in.h
124 Copying file m4/absolute-header.m4
125 Copying file m4/extensions.m4
126 Copying file m4/gnulib-common.m4
127 Copying file m4/gnulib-tool.m4
128 Copying file m4/strdup.m4
129 Copying file m4/string_h.m4
130 Creating lib/Makefile.am
131 Creating m4/gnulib-cache.m4
132 Creating m4/gnulib-comp.m4
133 Finished.
134
135 You may need to add #include directives for the following .h files.
136   #include <string.h>
137
138 Don't forget to
139   - add "lib/Makefile" to AC_CONFIG_FILES in ./configure.ac,
140   - mention "lib" in SUBDIRS in Makefile.am,
141   - mention "-I m4" in ACLOCAL_AMFLAGS in Makefile.am,
142   - invoke gl_EARLY in ./configure.ac, right after AC_PROG_CC,
143   - invoke gl_INIT in ./configure.ac.
144 ~/src/libfoo$
145 @end example
146
147 By default, the source code is copied into @file{lib/} and the M4
148 macros in @file{m4/}.  You can override these paths by using
149 @code{--source-base=DIRECTORY} and @code{--m4-base=DIRECTORY}.  Some
150 modules also provide other files necessary for building.  These files
151 are copied into the directory specified by @samp{AC_CONFIG_AUX_DIR} in
152 @file{configure.ac} or by the @code{--aux-dir=DIRECTORY} option.  If
153 neither is specified, the current directory is assumed.
154
155 @code{gnulib-tool} can make symbolic links instead of copying the
156 source files.  The option to specify for this is @samp{--symlink}, or
157 @samp{-s} for short.  This can be useful to save a few kilobytes of disk
158 space.  But it is likely to introduce bugs when @code{gnulib} is updated;
159 it is more reliable to use @samp{gnulib-tool --update} (see below)
160 to update to newer versions of @code{gnulib}.  Furthermore it requires
161 extra effort to create self-contained tarballs, and it may disturb some
162 mechanism the maintainer applies to the sources.  For these reasons,
163 this option is generally discouraged.
164
165 @code{gnulib-tool} will overwrite any pre-existing files, in
166 particular @file{Makefile.am}.  It is also possible to separate the
167 generated @file{Makefile.am} content (for building the gnulib library)
168 into a separate file, say @file{gnulib.mk}, that can be included by your
169 handwritten @file{Makefile.am}, but this is a more advanced use of
170 @code{gnulib-tool}.
171
172 Consequently, it is a good idea to choose directories that are not
173 already used by your projects, to separate gnulib imported files from
174 your own files.  This approach is also useful if you want to avoid
175 conflicts between other tools (e.g., @code{gettextize} that also copy
176 M4 files into your package.  Simon Josefsson successfully uses a source
177 base of @file{gl/}, and a M4 base of @file{gl/m4/}, in several
178 packages.
179
180 After the @samp{--import} option on the command line comes the list of
181 Gnulib modules that you want to incorporate in your package.  The names
182 of the modules coincide with the filenames in Gnulib's @file{modules/}
183 directory.
184
185 Some Gnulib modules depend on other Gnulib modules.  @code{gnulib-tool}
186 will automatically add the needed modules as well; you need not list
187 them explicitly.  @code{gnulib-tool} will also memorize which dependent
188 modules it has added, so that when someday a dependency is dropped, the
189 implicitly added module is dropped as well (unless you have explicitly
190 requested that module).
191
192 If you want to cut a dependency, i.e., not add a module although one of
193 your requested modules depends on it, you may use the option
194 @samp{--avoid=@var{module}} to do so.  Multiple uses of this option are
195 possible.  Of course, you will then need to implement the same interface
196 as the removed module.
197
198 A few manual steps are required to finish the initial import.
199 @code{gnulib-tool} printed a summary of these steps.
200
201 First, you must ensure Autoconf can find the macro definitions in
202 @file{gnulib-comp.m4}.  Use the @code{ACLOCAL_AMFLAGS} specifier in
203 your top-level @file{Makefile.am} file, as in:
204
205 @example
206 ACLOCAL_AMFLAGS = -I m4
207 @end example
208
209 You are now ready to call the M4 macros in @code{gnulib-comp.m4} from
210 @file{configure.ac}.  The macro @code{gl_EARLY} must be called as soon
211 as possible after verifying that the C compiler is working.
212 Typically, this is immediately after @code{AC_PROG_CC}, as in:
213
214 @example
215 ...
216 AC_PROG_CC
217 gl_EARLY
218 ...
219 @end example
220
221 If you are using @code{AC_PROG_CC_STDC}, the macro @code{gl_EARLY} must
222 be called after it, like this:
223
224 @example
225 ...
226 AC_PROG_CC
227 AC_PROG_CC_STDC
228 gl_EARLY
229 ...
230 @end example
231
232 The core part of the gnulib checks are done by the macro
233 @code{gl_INIT}.  Place it further down in the file, typically where
234 you normally check for header files or functions.  It must come after
235 other checks which may affect the compiler invocation, such as
236 @code{AC_MINIX}.  For example:
237
238 @example
239 ...
240 # For gnulib.
241 gl_INIT
242 ...
243 @end example
244
245 @code{gl_INIT} will in turn call the macros related with the
246 gnulib functions, be it specific gnulib macros, like @code{gl_FUNC_ALLOCA}
247 or autoconf or automake macros like @code{AC_FUNC_ALLOCA} or
248 @code{AM_FUNC_GETLINE}.  So there is no need to call those macros yourself
249 when you use the corresponding gnulib modules.
250
251 You must also make sure that the gnulib library is built.  Add the
252 @code{Makefile} in the gnulib source base directory to
253 @code{AC_CONFIG_FILES}, as in:
254
255 @example
256 AC_CONFIG_FILES(... lib/Makefile ...)
257 @end example
258
259 You must also make sure that @code{make} will recurse into the gnulib
260 directory.  To achieve this, add the gnulib source base directory to a
261 @code{SUBDIRS} Makefile.am statement, as in:
262
263 @example
264 SUBDIRS = lib
265 @end example
266
267 or if you, more likely, already have a few entries in @code{SUBDIRS},
268 you can add something like:
269
270 @example
271 SUBDIRS += lib
272 @end example
273
274 Finally, you have to add compiler and linker flags in the appropriate
275 source directories, so that you can make use of the gnulib library.
276 Since some modules (@samp{getopt}, for example) may copy files into
277 the build directory, @file{top_builddir/lib} is needed as well
278 as @file{top_srcdir/lib}.  For example:
279
280 @example
281 ...
282 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
283 ...
284 LDADD = lib/libgnu.a
285 ...
286 @end example
287
288 Don't forget to @code{#include} the various header files.  In this
289 example, you would need to make sure that @samp{#include <string.h>}
290 is evaluated when compiling all source code files, that want to make
291 use of @code{strdup}.
292
293 In the usual case where Autoconf is creating a @file{config.h} file,
294 you should include @file{config.h} first, before any other include
295 file.  That way, for example, if @file{config.h} defines
296 @samp{restrict} to be the empty string on a pre-C99 host, or a macro
297 like @samp{_FILE_OFFSET_BITS} that affects the layout of data
298 structures, the definition is consistent for all include files.
299 Also, on some platforms macros like @samp{_FILE_OFFSET_BITS} and
300 @samp{_GNU_SOURCE} may be ineffective, or may have only a limited
301 effect, if defined after the first system header file is included.
302
303 Finally, note that you cannot use @code{AC_LIBOBJ} or
304 @code{AC_REPLACE_FUNCS} in your @file{configure.ac} and expect the
305 resulting object files to be automatically added to @file{lib/libgnu.a}.
306 This is because your @code{AC_LIBOBJ} and @code{AC_REPLACE_FUNCS} invocations
307 from @file{configure.ac} augment a variable @code{@@LIBOBJS@@} (and/or
308 @code{@@LTLIBOBJS@@} if using Libtool), whereas @file{lib/libgnu.a}
309 is built from the contents of a different variable, usually
310 @code{@@gl_LIBOBJS@@} (or @code{@@gl_LTLIBOBJS@@} if using Libtool).
311
312
313 @node Modified imports
314 @section Modified imports
315
316 You can at any moment decide to use Gnulib differently than the last time.
317
318 There are two ways to change how Gnulib is used.  Which one you'll use,
319 depends on where you keep track of options and module names that you pass
320 to @code{gnulib-tool}.
321
322 @itemize @bullet
323 @item
324 If you store the options and module names in a file under your own
325 control, such as @file{autogen.sh}, @file{bootstrap},
326 @file{bootstrap.conf}, or similar, simply invoke @command{gnulib-tool}
327 again, with modified options and more or fewer module names.
328
329 @item
330 @code{gnulib-tool} remembers which modules were used last time.  If you
331 want to rely on @code{gnulib-tool}'s own memory of the last used
332 options and module names, you can use the commands
333 @command{gnulib-tool --add-import} and
334 @command{gnulib-tool --remove-import}.
335
336 So, if you only want to use more Gnulib modules, simply invoke
337 @command{gnulib-tool --add-import @var{new-modules}}.  The list of
338 modules that you pass after @samp{--add-import} is @emph{added} to the
339 previous list of modules.
340
341 Similarly, if you want to use fewer Gnulib modules, simply invoke
342 @command{gnulib-tool --remove-import @var{unneeded-modules}}.  The list
343 of modules that you pass after @samp{--remove-import} is @emph{removed}
344 from the previous list of modules.  Note that if a module is then still
345 needed as dependency of other modules, it will be used nevertheless.
346 If you want to @emph{really} not use a module any more, regardless of
347 whether other modules may need it, you need to use the @samp{--avoid}
348 option.
349
350 For other changes, such as different choices of @samp{--lib},
351 @samp{--source-base} or @samp{--aux-dir}, the normal way is to
352 modify manually the file @file{gnulib-cache.m4} in the M4 macros
353 directory, then launch @samp{gnulib-tool --add-import}.
354
355 The only change for which this doesn't work is a change of the
356 @samp{--m4-base} directory.  Because, when you pass a different value of
357 @samp{--m4-base}, @code{gnulib-tool} will not find the previous
358 @file{gnulib-cache.m4} file any more.  A possible solution is to
359 manually copy the @file{gnulib-cache.m4} into the new M4 macro directory.
360
361 In the @file{gnulib-cache.m4} file, the macros have the following meaning:
362 @table @code
363 @item gl_MODULES
364 The argument is a space separated list of the requested modules, not including
365 dependencies.
366
367 @item gl_AVOID
368 The argument is a space separated list of modules that should not be used,
369 even if they occur as dependencies.  Corresponds to the @samp{--avoid}
370 command line argument.
371
372 @item gl_SOURCE_BASE
373 The argument is the relative file name of the directory containing the gnulib
374 source files (mostly *.c and *.h files).  Corresponds to the
375 @samp{--source-base} command line argument.
376
377 @item gl_M4_BASE
378 The argument is the relative file name of the directory containing the gnulib
379 M4 macros (*.m4 files).  Corresponds to the @samp{--m4-base} command line
380 argument.
381
382 @item gl_TESTS_BASE
383 The argument is the relative file name of the directory containing the gnulib
384 unit test files.  Corresponds to the @samp{--tests-base} command line argument.
385
386 @item gl_LIB
387 The argument is the name of the library to be created.  Corresponds to the
388 @samp{--lib} command line argument.
389
390 @item gl_LGPL
391 The presence of this macro without arguments corresponds to the @samp{--lgpl}
392 command line argument.  The presence of this macro with an argument (whose
393 value must be 2 or 3) corresponds to the @samp{--lgpl=@var{arg}} command line
394 argument.
395
396 @item gl_LIBTOOL
397 The presence of this macro corresponds to the @samp{--libtool} command line
398 argument and to the absence of the @samp{--no-libtool} command line argument.
399 It takes no arguments.
400
401 @item gl_MACRO_PREFIX
402 The argument is the prefix to use for macros in the @file{gnulib-comp.m4}
403 file.  Corresponds to the @samp{--macro-prefix} command line argument.
404 @end table
405
406 @end itemize
407
408 @node Simple update
409 @section Simple update
410
411 When you want to update to a more recent version of Gnulib, without
412 changing the list of modules or other parameters, a simple call
413 does it:
414
415 @smallexample
416 $ gnulib-tool --add-import
417 @end smallexample
418
419 @noindent
420 This will create, update or remove files, as needed.
421
422 Note: From time to time, changes are made in Gnulib that are not backward
423 compatible.  When updating to a more recent Gnulib, you should consult
424 Gnulib's @file{NEWS} file to check whether the incompatible changes affect
425 your project.
426
427
428 @node Source changes
429 @section Changing your sources for use with Gnulib
430
431 Gnulib contains some header file overrides.  This means that when building
432 on systems with deficient header files in @file{/usr/include/}, it may create
433 files named @file{string.h}, @file{stdlib.h}, @file{stdint.h} or similar in
434 the build directory.  In the other source directories of your package you
435 will usually pass @samp{-I} options to the compiler, so that these Gnulib
436 substitutes are visible and take precedence over the files in
437 @file{/usr/include/}.
438
439 These Gnulib substitute header files rely on @file{<config.h>} being
440 already included.  Furthermore @file{<config.h>} must be the first include
441 in every compilation unit.  This means that to @emph{all your source files}
442 and likely also to @emph{all your tests source files} you need to add an
443 @samp{#include <config.h>} at the top.  Which source files are affected?
444 Exactly those whose compilation includes a @samp{-I} option that refers to
445 the Gnulib library directory.
446
447 This is annoying, but inevitable: On many systems, @file{<config.h>} is
448 used to set system dependent flags (such as @code{_GNU_SOURCE} on GNU systems),
449 and these flags have no effect after any system header file has been included.
450
451
452 @node Multiple instances
453 @section Using Gnulib for both a library and a binary
454
455 Your project might build both a library and some accompanying binaries
456 in the same source tree. In that case you might want to use different
457 modules for the library than for the binaries. Typically the binaries
458 might want to make use of @code{getopt-posix} or @code{version-etc},
459 while the library wants to stay clear of these modules for technical
460 or licensing reasons.
461
462 Let's assume that your project contains a @file{lib} directory where
463 the source of the library resides and a @file{src} directory for the
464 sources of the binaries as follows.
465
466 @example
467 .
468 |-- configure.ac
469 |-- lib
470 |   |-- foo.c
471 |   `-- Makefile.am
472 |-- Makefile.am
473 `-- src
474     |-- bar.c
475     `-- Makefile.am
476 @end example
477
478 You can now add two instances of Gnulib to your project in separate
479 source trees:
480
481 @example
482 ~/src/libfoo$ gnulib-tool --import --lib=libgnu --source-base=gnulib \
483               --m4-base=gnulib/m4 --macro-prefix=gl strndup
484 ~/src/libfoo$ gnulib-tool --import --lib=libgnutools \
485               --source-base=src/gnulib --m4-base=src/gnulib/m4 \
486               --macro-prefix=gl_tools getopt-gnu
487 @end example
488
489 The first one will import the module @code{strndup} in @file{gnulib}
490 and the second one will import @code{getopt-gnu} in @file{src/gnulib}
491 and you will end up with the following source tree (many files omitted
492 in the interest of brevity):
493
494 @example
495 .
496 |-- configure.ac
497 |-- gnulib
498 |   |-- m4
499 |   |-- strndup.c
500 |-- lib
501 |   |-- foo.c
502 |   `-- Makefile.am
503 |-- Makefile.am
504 `-- src
505     |-- bar.c
506     |-- gnulib
507     |   |-- getopt.c
508     |   |-- getopt.in.h
509     |   |-- m4
510     `-- Makefile.am
511 @end example
512
513 Integration with your code is basically the same as outlined in
514 @ref{Initial import} with the one exception that you have to add both
515 the macro @code{gl_EARLY} and the macro @code{gl_tools_EARLY} to your
516 @file{configure.ac} (and of course also both macros @code{gl_INIT} and
517 @code{gl_tools_INIT}). Obviously the name of the second macro is
518 dependent on the value of the @option{--macro-prefix} option in your
519 @command{gnulib-tool} invocation.
520
521 @example
522 ...
523 AC_PROG_CC
524 gl_EARLY
525 gl_tools_EARLY
526 ...
527 # For gnulib.
528 gl_INIT
529 gl_tools_INIT
530 ...
531 @end example
532
533 Also as outlined in @ref{Initial import} you will have to add compiler
534 and linker flags. For the library you might have to add something
535 along the line of the following to your @file{Makefile.am}:
536
537 @example
538 ...
539 AM_CPPFLAGS = -I$(top_srcdir)/gnulib -I$(top_builddir)/gnulib
540 ...
541 libfoo_la_LIBADD = $(top_builddir)/gnulib/libgnu.la
542 ...
543 @end example
544
545 Correspondingly for the binary you will have to add something along
546 the lines of to the following:
547
548 @example
549 ...
550 AM_CPPFLAGS = -I$(top_srcdir)/src/gnulib -I$(top_builddir)/src/gnulib
551 ...
552 LIBADD = $(top_builddir)/src/gnulib/libgnutools.la
553 ...
554 @end example
555
556 The name of the library that you have pass in the linker option
557 depends on the @option{--lib} option in @command{gnulib-tool}
558 invocation.
559
560 @node gettextize and autopoint
561 @section Caveat: @code{gettextize} and @code{autopoint} users
562
563 @cindex gettextize, caveat
564 @cindex autopoint, caveat
565 The programs @code{gettextize} and @code{autopoint}, part of
566 GNU @code{gettext}, import or update the internationalization infrastructure.
567 Some of this infrastructure, namely ca.@: 20 autoconf macro files and the
568 @file{config.rpath} file, is also contained in Gnulib and may be imported
569 by @code{gnulib-tool}.  The use of @code{gettextize} or @code{autopoint}
570 will therefore overwrite some of the files that @code{gnulib-tool} has
571 imported, and vice versa.
572
573 Avoiding to use @code{gettextize} (manually, as package maintainer) or
574 @code{autopoint} (as part of a script like @code{autoreconf} or
575 @code{autogen.sh}) is not the solution: These programs also import the
576 infrastructure in the @file{po/} and optionally in the @file{intl/} directory.
577
578 The copies of the conflicting files in Gnulib are more up-to-date than
579 the copies brought in by @code{gettextize} and @code{autopoint}.  When a
580 new @code{gettext} release is made, the copies of the files in Gnulib will
581 be updated immediately.
582
583 The choice of which version of gettext to require depends on the needs
584 of your package.  For a package that wants to comply to GNU Coding
585 Standards, the steps are:
586
587 @enumerate
588 @item
589 When you run @code{gettextize}, always use the @code{gettextize} from the
590 matching GNU gettext release.  For the most recent Gnulib checkout, this is
591 the newest release found on @url{http://ftp.gnu.org/gnu/gettext/}.  For an
592 older Gnulib snapshot, it is the release that was the most recent release
593 at the time the Gnulib snapshot was taken.
594
595 @item
596 After running @code{gettextize}, invoke @code{gnulib-tool} and import
597 the @code{gettext} module.  Also, copy the latest version of gnulib's
598 @file{build-aux/po/Makefile.in.in} to your @file{po/} directory (this
599 is done for you if you use gnulib's @file{bootstrap} script).
600
601 @item
602 If you get an error message like
603 @code{*** error: gettext infrastructure mismatch:
604 using a Makefile.in.in from gettext version ...
605 but the autoconf macros are from gettext version ...},
606 it means that a new GNU gettext release was made, and its autoconf macros
607 were integrated into Gnulib and now mismatch the @file{po/} infrastructure.
608 In this case, fetch and install the new GNU gettext release and run
609 @code{gettextize} followed by @code{gnulib-tool}.
610 @end enumerate
611
612 On the other hand, if your package is not as concerned with compliance
613 to the latest standards, but instead favors development on stable
614 environments, the steps are:
615
616 @enumerate
617 @item
618 Determine the oldest version of @code{gettext} that you intend to
619 support during development (at this time, gnulib recommends going no
620 older than version 0.17).  Run @code{autopoint} (not
621 @code{gettextize}) to copy infrastructure into place (newer versions
622 of gettext will install the older infrastructure that you requested).
623
624 @item
625 Invoke @code{gnulib-tool}, and import the @code{gettext-h} module.
626 @end enumerate
627
628 Regardless of which approach you used to get the infrastructure in
629 place, the following steps must then be used to preserve that
630 infrastructure (gnulib's @file{bootstrap} script follows these rules):
631
632 @enumerate
633 @item
634 When a script of yours run @code{autopoint}, invoke @code{gnulib-tool}
635 afterwards.
636
637 @item
638 When you invoke @code{autoreconf} after @code{gnulib-tool}, make sure to
639 not invoke @code{autopoint} a second time, by setting the @code{AUTOPOINT}
640 environment variable, like this:
641 @smallexample
642 $ env AUTOPOINT=true autoreconf --install
643 @end smallexample
644 @end enumerate
645
646
647 @node Localization
648 @section Handling Gnulib's own message translations
649
650 Gnulib provides some functions that emit translatable messages using GNU
651 @code{gettext}.  The @samp{gnulib} domain at the
652 @url{http://translationproject.org/, Translation Project} collects
653 translations of these messages, which you should incorporate into your
654 own programs.
655
656 There are two basic ways to achieve this.  The first, and older, method
657 is to list all the source files you use from Gnulib in your own
658 @file{po/POTFILES.in} file.  This will cause all the relevant
659 translatable strings to be included in your POT file.  When you send
660 this POT file to the Translation Project, translators will normally fill
661 in the translations of the Gnulib strings from their ``translation
662 memory'', and send you back updated PO files.
663
664 However, this process is error-prone: you might forget to list some
665 source files, or the translator might not be using a translation memory
666 and provide a different translation than another translator, or the
667 translation might not be kept in sync between Gnulib and your package.
668 It is also slow and causes substantial extra work, because a human
669 translator must be in the loop for each language and you will need to
670 incorporate their work on request.
671
672 For these reasons, a new method was designed and is now recommended.  If
673 you pass the @code{--po-base=@var{directory}} and @code{--po-domain=@var{domain}}
674 options to @code{gnulib-tool}, then @code{gnulib-tool} will create a
675 separate directory with its own @file{POTFILES.in}, and fetch current
676 translations directly from the Translation Project (using
677 @command{rsync} or @command{wget}, whichever is available).
678 The POT file in this directory will be called
679 @file{@var{domain}-gnulib.pot}, depending on the @var{domain} you gave to the
680 @code{--po-domain} option (typically the same as the package name).
681 This causes these translations to reside in a separate message domain,
682 so that they do not clash either with the translations for the main part
683 of your package nor with those of other packages on the system that use
684 possibly different versions of Gnulib.
685 When you use these options, the functions in Gnulib are built
686 in such a way that they will always use this domain regardless of the
687 default domain set by @code{textdomain}.
688
689 In order to use this method, you must---in each program that might use
690 Gnulib code---add an extra line to the part of the program that
691 initializes locale-dependent behavior.  Where you would normally write
692 something like:
693
694 @example
695 @group
696   setlocale (LC_ALL, "");
697   bindtextdomain (PACKAGE, LOCALEDIR);
698   textdomain (PACKAGE);
699 @end group
700 @end example
701
702 @noindent
703 you should add an additional @code{bindtextdomain} call to inform
704 gettext of where the MO files for the extra message domain may be found:
705
706 @example
707 @group
708   bindtextdomain (PACKAGE "-gnulib", LOCALEDIR);
709 @end group
710 @end example
711
712 (This example assumes that the @var{domain} that you specified
713 to @code{gnulib-tool} is the same as the value of the @code{PACKAGE}
714 preprocessor macro.)
715
716 Since you do not change the @code{textdomain} call, the default message
717 domain for your program remains the same and your own use of @code{gettext}
718 functions will not be affected.
719
720
721 @node VCS Issues
722 @section Issues with Version Control Systems
723
724 If a project stores its source files in a version control system (VCS),
725 such as CVS, Subversion, or Git, one needs to decide which files to commit.
726
727 In principle, all files created by @code{gnulib-tool}, except
728 @file{gnulib-cache.m4}, can be treated like generated source files,
729 like for example a @file{parser.c} file generated from
730 @file{parser.y}.  Alternatively, they can be considered source files
731 and updated manually.
732
733 Here are the three different approaches in common use.  Each has its
734 place, and you should use whichever best suits your particular project
735 and development methods.
736
737 @enumerate
738 @item
739 In projects which commit all source files, whether generated or not,
740 into their VCS, the @code{gnulib-tool} generated files should all be
741 committed.  In this case, you should pass the option
742 @samp{--no-vc-files} to @code{gnulib-tool}, which avoids alteration of
743 VCS-related files such as @file{.gitignore}.
744
745 Gnulib also contains files generated by @command{make} (and removed by
746 @code{make clean}), using information determined by
747 @command{configure}.  For a Gnulib source file of the form
748 @file{lib/foo.in.h}, the corresponding @file{lib/foo.h} is such a
749 @command{make}-generated file.  These should @emph{not} be checked
750 into the VCS, but instead added to @file{.gitignore} or equivalent.
751
752 @item
753 In projects which customarily omit from their VCS all files that are
754 generated from other source files, none of these files and directories
755 are added into the VCS.  As described in @ref{Modified imports}, there
756 are two ways to keep track of options and module names that are passed
757 to @code{gnulib-tool}.  The command for restoring the omitted files
758 depends on it:
759
760 @itemize @bullet
761 @item
762 If they are stored in a file other than @code{gnulib-cache.m4}, such as
763 @file{autogen.sh}, @file{bootstrap}, @file{bootstrap.conf}, or similar,
764 the restoration command is the entire @code{gnulib-tool ... --import ...}
765 invocation with all options and module names.
766
767 @item
768 If the project relies on @code{gnulib-tool}'s memory of the last used
769 options and module names, then the file @file{gnulib-cache.m4} in the M4
770 macros directory must be added to the VCS, and the restoration command
771 is:
772
773 @smallexample
774 $ gnulib-tool --update
775 @end smallexample
776
777 The @samp{--update} option operates much like the @samp{--add-import}
778 option, but it does not offer the possibility to change the way Gnulib is
779 used.  Also it does not report in the ChangeLogs the files that it had to
780 add because they were missing.
781
782 @end itemize
783
784 Gnulib includes the file @file{build-aux/bootstrap} to aid a developer
785 in using this setup.  Furthermore, in projects that use git for
786 version control, it is possible to use a git submodule containing the
787 precise commit of the gnulib repository, so that each developer
788 running @file{bootstrap} will get the same version of all
789 gnulib-provided files.  The location of the submodule can be chosen to
790 fit the package's needs; here's how to initially create the submodule
791 in the directory @file{.gnulib}:
792
793 @smallexample
794 $ dir=.gnulib
795 $ git submodule add -- git://git.sv.gnu.org/gnulib.git $dir
796 $ git config alias.syncsub "submodule foreach git pull origin master"
797 @end smallexample
798
799 @noindent
800 Thereafter, @file{bootstrap} can run this command to update the
801 submodule to the recorded checkout level:
802
803 @smallexample
804 git submodule update --init $dir
805 @end smallexample
806
807 @noindent
808 and a developer can use this sequence to update to a newer version of
809 gnulib:
810
811 @smallexample
812 $ git syncsub
813 $ git add $dir
814 $ ./bootstrap
815 @end smallexample
816
817 @item
818 Some projects take a ``middle road'': they do commit Gnulib source
819 files as in the first approach, but they do not commit other derived
820 files, such as a @code{Makefile.in} generated by Automake.  This
821 increases the size and complexity of the repository, but can help
822 occasional contributors by not requiring them to have a full Gnulib
823 checkout to do a build, and all developers by ensuring that all
824 developers are working with the same version of Gnulib in the
825 repository.  It also supports multiple Gnulib instances within a
826 project.  It remains important not to commit the
827 @command{make}-generated files, as described above.
828
829 @end enumerate
830
831
832 @node Unit tests
833 @section Bundling the unit tests of the Gnulib modules
834
835 You can bundle the unit tests of the Gnulib modules together with your
836 package, through the @samp{--with-tests} option.  Together with
837 @samp{--with-tests}, you also specify the directory for these tests
838 through the @samp{--tests-base} option.  Of course, you need to add this
839 directory to the @code{SUBDIRS} variable in the @code{Makefile.am} of
840 the parent directory.
841
842 The advantage of having the unit tests bundled is that when your program
843 has a problem on a particular platform, running the unit tests may help
844 determine quickly if the problem is on Gnulib's side or on your package's
845 side.  Also, it helps verifying Gnulib's portability, of course.
846
847 The unit tests will be compiled and run when the user runs @samp{make check}.
848 When the user runs only @samp{make}, the unit tests will not be compiled.
849
850 In the @code{SUBDIRS} variable, it is useful to put the Gnulib tests directory
851 after the directory containing the other tests, not before:
852
853 @smallexample
854 SUBDIRS = gnulib-lib src man tests gnulib-tests
855 @end smallexample
856
857 @noindent
858 This will ensure that on platforms where there are test failures in either
859 directory, users will see and report the failures from the tests of your
860 program.
861
862 Note: In packages which use more than one invocation of @code{gnulib-tool}
863 in the scope of the same @code{configure.ac}, you cannot use
864 @samp{--with-tests}.  You will have to use a separate @code{configure.ac}
865 in this case.
866
867
868 @node Conditional dependencies
869 @section Avoiding unnecessary checks and compilations
870
871 @cindex conditional dependencies
872 In some cases, a module is needed by another module only on specific
873 platforms.  But when a module is present, its autoconf checks are always
874 executed, and its @code{Makefile.am} additions are always enabled.  So
875 it can happen that some autoconf checks are executed and some source files
876 are compiled, although no other module needs them on this particular
877 platform, just @emph{in case} some other module would need them.
878
879 The option @samp{--conditional-dependencies} enables an optimization of
880 configure checks and @code{Makefile.am} snippets that avoids this.  With
881 this option, whether a module is considered ``present'' is no longer decided
882 when @code{gnulib-tool} is invoked, but later, when @code{configure} is run.
883 This applies to modules that were added as dependencies while
884 @code{gnulib-tool} was run; modules that were passed on the command line
885 explicitly are always ``present''.
886
887 For example, the @code{timegm} module needs, on platforms
888 where the system's @code{timegm} function is missing or buggy, a replacement
889 that is based on a function @code{mktime_internal}.  The module
890 @code{mktime-internal} that provides this function provides it on all
891 platforms.  So, by default, the file @file{mktime-internal.c} will be
892 compiled on all platforms, even on glibc and BSD systems which have a
893 working @code{timegm} function.  When the option
894 @samp{--conditional-dependencies} is given, on the other hand, and if
895 @code{mktime-internal} was not explicitly required on the command line,
896 the file @file{mktime-internal.c} will only be compiled on the platforms
897 where the @code{timegm} needs them.
898
899 Conditional dependencies are specified in the module description by putting
900 the condition on the same line as the dependent module, enclosed in brackets.
901 The condition is a boolean shell expression that can assume that the
902 @code{configure.ac} snippet from the module description has already been
903 executed.  In the example above, the dependency from @code{timegm} to
904 @code{mktime-internal} is written like this:
905
906 @smallexample
907 Depends-on:
908 ...
909 mktime-internal [test $HAVE_TIMEGM = 0 || test $REPLACE_TIMEGM = 1]
910 ...
911 @end smallexample
912
913 Note: The option @samp{--conditional-dependencies} cannot be used together
914 with the option @samp{--with-tests}.  It also cannot be used when a package
915 uses @code{gnulib-tool} for several subdirectories, with different values
916 of @samp{--source-base}, in the scope of a single @code{configure.ac} file.