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