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