doc: Add more details regarding the undefined behaviour sanitizer.
[gnulib.git] / doc / gnulib-tool.texi
blobfb2b01922b1ed26db87f62bc800feada182751c0
1 @node Invoking gnulib-tool
2 @chapter Invoking gnulib-tool
4 @c Copyright (C) 2005--2020 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 Texts.  A
10 @c copy of the license is at <https://www.gnu.org/licenses/fdl-1.3.en.html>.
12 @pindex gnulib-tool
13 @cindex invoking @command{gnulib-tool}
15 The @command{gnulib-tool} command is the recommended way to import
16 Gnulib modules.  It is possible to borrow Gnulib modules in a package
17 without using @command{gnulib-tool}, relying only on the
18 meta-information stored in the @file{modules/*} files, but with a
19 growing number of modules this becomes tedious.  @command{gnulib-tool}
20 simplifies the management of source files, @file{Makefile.am}s and
21 @file{configure.ac} in packages incorporating Gnulib modules.
23 @file{gnulib-tool} is not installed in a standard directory that is
24 contained in the @code{PATH} variable.  It needs to be run directly in
25 the directory that contains the Gnulib source code.  You can do this
26 either by specifying the absolute filename of @file{gnulib-tool}, or
27 you can also use a symbolic link from a place inside your @code{PATH}
28 to the @file{gnulib-tool} file of your preferred and most up-to-date
29 Gnulib checkout, like this:
30 @smallexample
31 $ ln -s $HOME/gnu/src/gnulib.git/gnulib-tool $HOME/bin/gnulib-tool
32 @end smallexample
34 Run @samp{gnulib-tool --help} for information.  To get familiar with
35 @command{gnulib-tool} without affecting your sources, you can also try
36 some commands with the option @samp{--dry-run}; then
37 @code{gnulib-tool} will only report which actions it would perform in
38 a real run without changing anything.
40 @menu
41 * Which modules?::              Determining the needed set of Gnulib modules
42 * Initial import::              First import of Gnulib modules.
43 * Modified imports::            Changing the import specification.
44 * Simple update::               Tracking Gnulib development.
45 * Source changes::              Impact of Gnulib on your source files.
46 * Link-time requirements::      Which libraries to link against
47 * Finding POSIX substitutes::   Determining additional suitable Gnulib modules
48 * Modified build rules::        Modifying the build rules of a Gnulib import
49 * Multiple instances::          Using Gnulib for both a library and a program
50 * gettextize and autopoint::    Caveat: @code{gettextize} and @code{autopoint} users!
51 * Localization::                Handling Gnulib's own message translations.
52 * VCS Issues::                  Integration with Version Control Systems.
53 * Unit tests::                  Bundling the unit tests of the Gnulib modules.
54 * Conditional dependencies::    Avoiding unnecessary checks and compilations.
55 @end menu
58 @node Which modules?
59 @section Finding modules
60 @cindex Finding modules
62 There are four ways of finding the names of Gnulib modules that you can use
63 in your package:
65 @itemize
66 @item
67 You have the complete module list, sorted according to categories, in
68 @url{https://www.gnu.org/software/gnulib/MODULES.html}.
70 @item
71 If you are looking for POSIX function replacements that you don't know about
72 yet, follow the procedure described in section @ref{Finding POSIX substitutes}.
74 @item
75 If you are looking for a particular POSIX header or function replacement,
76 look in the chapters @ref{Header File Substitutes} and
77 @ref{Function Substitutes}.  For headers and functions that are provided by
78 Glibc but not standardized by POSIX, look in the chapters
79 @ref{Glibc Header File Substitutes} and @ref{Glibc Function Substitutes}.
81 @item
82 If you have already found the source file in Gnulib and are looking for the
83 module that contains this source file, you can use the command
84 @samp{gnulib-tool --find @var{filename}}.
85 @end itemize
88 @node Initial import
89 @section Initial import
90 @cindex initial import
92 Gnulib assumes that your project uses Autoconf.  When using Gnulib, you
93 will need to have Autoconf among your build tools.
95 Gnulib also assumes that your project's @file{configure.ac} contains the
96 line
97 @smallexample
98 AC_CONFIG_HEADERS([config.h])
99 @end smallexample
100 The @file{config.h} file gets generated with platform dependent C macro
101 definitions, and the source files include it (see @ref{Source changes}).
103 Unless you use @command{gnulib-tool}'s @option{--gnu-make} option,
104 Gnulib also assumes that your project uses Automake at least in a
105 subdirectory of your project.  While the use of Automake in your
106 project's top level directory is an easy way to fulfil the Makefile
107 conventions of the GNU coding standards, Gnulib does not require it.
109 Invoking @samp{gnulib-tool --import} will copy source files, create a
110 @file{Makefile.am} to build them, generate a file @file{gnulib-comp.m4} with
111 Autoconf M4 macro declarations used by @file{configure.ac}, and generate
112 a file @file{gnulib-cache.m4} containing the cached specification of how
113 Gnulib is used.
115 Our example will be a library that uses Autoconf, Automake and
116 Libtool.  It calls @code{strdup}, and you wish to use gnulib to make
117 the package portable to C99 and C11 (which don't have @code{strdup}).
119 @example
120 ~/src/libfoo$ gnulib-tool --import strdup
121 Module list with included dependencies:
122   absolute-header
123   extensions
124   strdup
125   string
126 File list:
127   lib/dummy.c
128   lib/strdup.c
129   lib/string.in.h
130   m4/absolute-header.m4
131   m4/extensions.m4
132   m4/gnulib-common.m4
133   m4/strdup.m4
134   m4/string_h.m4
135 Creating directory ./lib
136 Creating directory ./m4
137 Copying file lib/dummy.c
138 Copying file lib/strdup.c
139 Copying file lib/string.in.h
140 Copying file m4/absolute-header.m4
141 Copying file m4/extensions.m4
142 Copying file m4/gnulib-common.m4
143 Copying file m4/gnulib-tool.m4
144 Copying file m4/strdup.m4
145 Copying file m4/string_h.m4
146 Creating lib/Makefile.am
147 Creating m4/gnulib-cache.m4
148 Creating m4/gnulib-comp.m4
149 Finished.
151 You may need to add #include directives for the following .h files.
152   #include <string.h>
154 Don't forget to
155   - add "lib/Makefile" to AC_CONFIG_FILES in ./configure.ac,
156   - mention "lib" in SUBDIRS in Makefile.am,
157   - mention "-I m4" in ACLOCAL_AMFLAGS in Makefile.am,
158   - invoke gl_EARLY in ./configure.ac, right after AC_PROG_CC,
159   - invoke gl_INIT in ./configure.ac.
160 ~/src/libfoo$
161 @end example
163 By default, the source code is copied into @file{lib/} and the M4
164 macros in @file{m4/}.  You can override these paths by using
165 @code{--source-base=DIRECTORY} and @code{--m4-base=DIRECTORY}.  Some
166 modules also provide other files necessary for building.  These files
167 are copied into the directory specified by @samp{AC_CONFIG_AUX_DIR} in
168 @file{configure.ac} or by the @code{--aux-dir=DIRECTORY} option.  If
169 neither is specified, the current directory is assumed.
171 @code{gnulib-tool} can make symbolic links instead of copying the
172 source files.  The option to specify for this is @samp{--symlink}, or
173 @samp{-s} for short.  This can be useful to save a few kilobytes of disk
174 space.  But it is likely to introduce bugs when @code{gnulib} is updated;
175 it is more reliable to use @samp{gnulib-tool --update} (see below)
176 to update to newer versions of @code{gnulib}.  Furthermore it requires
177 extra effort to create self-contained tarballs, and it may disturb some
178 mechanism the maintainer applies to the sources.  For these reasons,
179 this option is generally discouraged.
181 @code{gnulib-tool} will overwrite any pre-existing files, in
182 particular @file{Makefile.am}.  It is also possible to separate the
183 generated @file{Makefile.am} content (for building the gnulib library)
184 into a separate file, say @file{gnulib.mk}, that can be included by your
185 handwritten @file{Makefile.am}, but this is a more advanced use of
186 @code{gnulib-tool}.
188 Consequently, it is a good idea to choose directories that are not
189 already used by your projects, to separate gnulib imported files from
190 your own files.  This approach is also useful if you want to avoid
191 conflicts between other tools (e.g., @code{gettextize} that also copy
192 M4 files into your package.  Simon Josefsson successfully uses a source
193 base of @file{gl/}, and a M4 base of @file{gl/m4/}, in several
194 packages.
196 After the @samp{--import} option on the command line comes the list of
197 Gnulib modules that you want to incorporate in your package.  The names
198 of the modules coincide with the filenames in Gnulib's @file{modules/}
199 directory.
201 Some Gnulib modules depend on other Gnulib modules.  @code{gnulib-tool}
202 will automatically add the needed modules as well; you need not list
203 them explicitly.  @code{gnulib-tool} will also memorize which dependent
204 modules it has added, so that when someday a dependency is dropped, the
205 implicitly added module is dropped as well (unless you have explicitly
206 requested that module).
208 If you want to cut a dependency, i.e., not add a module although one of
209 your requested modules depends on it, you may use the option
210 @samp{--avoid=@var{module}} to do so.  Multiple uses of this option are
211 possible.  Of course, you will then need to implement the same interface
212 as the removed module.
214 A few manual steps are required to finish the initial import.
215 @code{gnulib-tool} printed a summary of these steps.
217 First, you must ensure Autoconf can find the macro definitions in
218 @file{gnulib-comp.m4}.  Use the @code{ACLOCAL_AMFLAGS} specifier in
219 your top-level @file{Makefile.am} file, as in:
221 @example
222 ACLOCAL_AMFLAGS = -I m4
223 @end example
225 You are now ready to call the M4 macros in @code{gnulib-comp.m4} from
226 @file{configure.ac}.  The macro @code{gl_EARLY} must be called as soon
227 as possible after verifying that the C compiler is working.
228 Typically, this is immediately after @code{AC_PROG_CC}, as in:
230 @example
232 AC_PROG_CC
233 gl_EARLY
235 @end example
237 If you are using @code{AC_PROG_CC_STDC}, the macro @code{gl_EARLY} must
238 be called after it, like this:
240 @example
242 AC_PROG_CC
243 AC_PROG_CC_STDC
244 gl_EARLY
246 @end example
248 The core part of the gnulib checks are done by the macro
249 @code{gl_INIT}.  Place it further down in the file, typically where
250 you normally check for header files or functions.  It must come after
251 other checks which may affect the compiler invocation, such as
252 @code{AC_MINIX}.  For example:
254 @example
256 # For gnulib.
257 gl_INIT
259 @end example
261 @code{gl_INIT} will in turn call the macros related with the
262 gnulib functions, be it specific gnulib macros, like @code{gl_FUNC_ALLOCA}
263 or Autoconf or Automake macros like @code{AC_FUNC_ALLOCA} or
264 @code{AM_FUNC_GETLINE}.  So there is no need to call those macros yourself
265 when you use the corresponding gnulib modules.
267 You must also make sure that the gnulib library is built.  Add the
268 @code{Makefile} in the gnulib source base directory to
269 @code{AC_CONFIG_FILES}, as in:
271 @example
272 AC_CONFIG_FILES(... lib/Makefile ...)
273 @end example
275 You must also make sure that @code{make} will recurse into the gnulib
276 directory.  To achieve this, add the gnulib source base directory to a
277 @code{SUBDIRS} Makefile.am statement, as in:
279 @example
280 SUBDIRS = lib
281 @end example
283 or if you, more likely, already have a few entries in @code{SUBDIRS},
284 you can add something like:
286 @example
287 SUBDIRS += lib
288 @end example
290 Finally, you have to add compiler and linker flags in the appropriate
291 source directories, so that you can make use of the gnulib library.
292 Since some modules (@samp{getopt}, for example) may copy files into
293 the build directory, @file{top_builddir/lib} is needed as well
294 as @file{top_srcdir/lib}.  For example:
296 @example
298 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
300 LDADD = lib/libgnu.a
302 @end example
304 Don't forget to @code{#include} the various header files.  In this
305 example, you would need to make sure that @samp{#include <string.h>}
306 is evaluated when compiling all source code files, that want to make
307 use of @code{strdup}.
309 In the usual case where Autoconf is creating a @file{config.h} file,
310 you should include @file{config.h} first, before any other include
311 file.  That way, for example, if @file{config.h} defines
312 @samp{restrict} to be the empty string on a non-C99 host, or a macro
313 like @samp{_FILE_OFFSET_BITS} that affects the layout of data
314 structures, the definition is consistent for all include files.
315 Also, on some platforms macros like @samp{_FILE_OFFSET_BITS} and
316 @samp{_GNU_SOURCE} may be ineffective, or may have only a limited
317 effect, if defined after the first system header file is included.
319 Finally, note that you cannot use @code{AC_LIBOBJ} or
320 @code{AC_REPLACE_FUNCS} in your @file{configure.ac} and expect the
321 resulting object files to be automatically added to @file{lib/libgnu.a}.
322 This is because your @code{AC_LIBOBJ} and @code{AC_REPLACE_FUNCS} invocations
323 from @file{configure.ac} augment a variable @code{@@LIBOBJS@@} (and/or
324 @code{@@LTLIBOBJS@@} if using Libtool), whereas @file{lib/libgnu.a}
325 is built from the contents of a different variable, usually
326 @code{@@gl_LIBOBJS@@} (or @code{@@gl_LTLIBOBJS@@} if using Libtool).
329 @node Modified imports
330 @section Modified imports
332 You can at any moment decide to use Gnulib differently than the last time.
334 There are two ways to change how Gnulib is used.  Which one you'll use,
335 depends on where you keep track of options and module names that you pass
336 to @code{gnulib-tool}.
338 @itemize @bullet
339 @item
340 If you store the options and module names in a file under your own
341 control, such as @file{autogen.sh}, @file{bootstrap},
342 @file{bootstrap.conf}, or similar, simply invoke @command{gnulib-tool}
343 again, with modified options and more or fewer module names.
345 @item
346 @code{gnulib-tool} remembers which modules were used last time.  If you
347 want to rely on @code{gnulib-tool}'s own memory of the last used
348 options and module names, you can use the commands
349 @command{gnulib-tool --add-import} and
350 @command{gnulib-tool --remove-import}.
352 So, if you only want to use more Gnulib modules, simply invoke
353 @command{gnulib-tool --add-import @var{new-modules}}.  The list of
354 modules that you pass after @samp{--add-import} is @emph{added} to the
355 previous list of modules.
357 Similarly, if you want to use fewer Gnulib modules, simply invoke
358 @command{gnulib-tool --remove-import @var{unneeded-modules}}.  The list
359 of modules that you pass after @samp{--remove-import} is @emph{removed}
360 from the previous list of modules.  Note that if a module is then still
361 needed as dependency of other modules, it will be used nevertheless.
362 If you want to @emph{really} not use a module any more, regardless of
363 whether other modules may need it, you need to use the @samp{--avoid}
364 option.
366 For other changes, such as different choices of @samp{--lib},
367 @samp{--source-base} or @samp{--aux-dir}, the normal way is to
368 modify manually the file @file{gnulib-cache.m4} in the M4 macros
369 directory, then launch @samp{gnulib-tool --add-import}.
371 The only change for which this doesn't work is a change of the
372 @samp{--m4-base} directory.  Because, when you pass a different value of
373 @samp{--m4-base}, @code{gnulib-tool} will not find the previous
374 @file{gnulib-cache.m4} file any more.  A possible solution is to
375 manually copy the @file{gnulib-cache.m4} into the new M4 macro directory.
377 In the @file{gnulib-cache.m4} file, the macros have the following meaning:
378 @table @code
379 @item gl_MODULES
380 The argument is a space separated list of the requested modules, not including
381 dependencies.
383 @item gl_AVOID
384 The argument is a space separated list of modules that should not be used,
385 even if they occur as dependencies.  Corresponds to the @samp{--avoid}
386 command line argument.
388 @item gl_SOURCE_BASE
389 The argument is the relative file name of the directory containing the gnulib
390 source files (mostly *.c and *.h files).  Corresponds to the
391 @samp{--source-base} command line argument.
393 @item gl_M4_BASE
394 The argument is the relative file name of the directory containing the gnulib
395 M4 macros (*.m4 files).  Corresponds to the @samp{--m4-base} command line
396 argument.
398 @item gl_TESTS_BASE
399 The argument is the relative file name of the directory containing the gnulib
400 unit test files.  Corresponds to the @samp{--tests-base} command line argument.
402 @item gl_LIB
403 The argument is the name of the library to be created.  Corresponds to the
404 @samp{--lib} command line argument.
406 @item gl_LGPL
407 The presence of this macro without arguments corresponds to the @samp{--lgpl}
408 command line argument.  The presence of this macro with an argument (whose
409 value must be 2 or 3) corresponds to the @samp{--lgpl=@var{arg}} command line
410 argument.
412 @item gl_LIBTOOL
413 The presence of this macro corresponds to the @samp{--libtool} command line
414 argument and to the absence of the @samp{--no-libtool} command line argument.
415 It takes no arguments.
417 @item gl_MACRO_PREFIX
418 The argument is the prefix to use for macros in the @file{gnulib-comp.m4}
419 file.  Corresponds to the @samp{--macro-prefix} command line argument.
420 @end table
422 @end itemize
424 @node Simple update
425 @section Simple update
427 When you want to update to a more recent version of Gnulib, without
428 changing the list of modules or other parameters, a simple call
429 does it:
431 @smallexample
432 $ gnulib-tool --add-import
433 @end smallexample
435 @noindent
436 This will create, update or remove files, as needed.
438 Note: From time to time, changes are made in Gnulib that are not backward
439 compatible.  When updating to a more recent Gnulib, you should consult
440 Gnulib's @file{NEWS} file to check whether the incompatible changes affect
441 your project.
444 @node Source changes
445 @section Changing your sources for use with Gnulib
447 Gnulib contains some header file overrides.  This means that when building
448 on systems with deficient header files in @file{/usr/include/}, it may create
449 files named @file{string.h}, @file{stdlib.h}, @file{stdint.h} or similar in
450 the build directory.  In the other source directories of your package you
451 will usually pass @samp{-I} options to the compiler, so that these Gnulib
452 substitutes are visible and take precedence over the files in
453 @file{/usr/include/}.
455 These Gnulib substitute header files rely on @file{<config.h>} being
456 already included.  Furthermore @file{<config.h>} must be the first include
457 in every compilation unit.  This means that to @emph{all your source files}
458 and likely also to @emph{all your tests source files} you need to add an
459 @samp{#include <config.h>} at the top.  Which source files are affected?
460 Exactly those whose compilation includes a @samp{-I} option that refers to
461 the Gnulib library directory.
463 This is annoying, but inevitable: On many systems, @file{<config.h>} is
464 used to set system dependent flags (such as @code{_GNU_SOURCE} on GNU systems),
465 and these flags have no effect after any system header file has been included.
468 @node Link-time requirements
469 @section Changing your link commands for use with Gnulib
471 When you use Gnulib, you need to augment the set of libraries against which
472 your programs and libraries are linked.  This is done by augmenting the
473 Automake variable @code{LDADD} (for all programs) or
474 @code{@var{prog}_LDADD} (for a single program @code{@var{prog}}) or
475 @code{@var{library}_la_LIBADD} (for a single library @code{@var{library}.la}).
477 What do you need to add to this Automake variable?
478 @enumerate
479 @item
480 The reference to the Gnulib library.  In the example of section
481 @ref{Initial import}, this would be @code{lib/libgnu.a} for source in the
482 top-level directory, or @code{../lib/libgnu.a} for source in a sibling
483 directory of @code{lib/}.
485 @item
486 References to additional libraries, brought in by some of the Gnulib
487 modules that you use (directly or indirectly).  The complete list of such
488 libraries is printed when you invoke @code{gnulib-tool}.  Alternatively,
489 you can retrieve the set of additional libraries required by a specific
490 Gnulib module by running
491 @smallexample
492 ./gnulib-tool --extract-recursive-link-directive @var{module}
493 @end smallexample
494 @noindent
495 Beware: By looking into the module description file @code{modules/@var{module}}
496 or by running
497 @smallexample
498 ./gnulib-tool --extract-link-directive @var{module}
499 @end smallexample
500 @noindent
501 you would miss the link dependencies of indirectly used modules.
502 @end enumerate
505 @node Finding POSIX substitutes
506 @section Finding recommended ISO C and POSIX function substitutes
508 Gnulib contains a wealth of portability workarounds for ISO C and POSIX
509 functions.  They are listed in detail in the chapter @ref{Function Substitutes}.
510 If you want to know which function substitutes are recommended for your
511 package, you can search your source code for ISO C and POSIX functions
512 that it uses and read the corresponding sections of said documentation
513 chapter.  But this is a tedious task.  Here is an alternative approach
514 that makes this task easier.
516 @enumerate
517 @item
518 Add the Gnulib module @samp{posixcheck} to the Gnulib imports of your package,
519 as described earlier in this chapter.
521 @item
522 Do a @code{make distclean} if you previously built in the top-level directory.
523 Then regenerate the Autotools-generated parts of the package.
525 @item
526 On a glibc system, build your package.  Pay attention to the compiler warnings.
527 Warnings are generated for uses of ISO C and POSIX functions that have
528 portability problems or other important pitfalls and for which you have not yet
529 imported the corresponding Gnulib module.  If you get, say, a warning
530 ``warning: call to 'close' declared with attribute warning: close does not
531 portably work on sockets - use gnulib module close for portability'',
532 put @samp{close} on your list of modules to import.
534 @item
535 Add the modules you noted to the Gnulib imports of your package.
537 @item
538 Optionally, you can do the same steps again, and make sure that there are no
539 warnings left except those that you want to intentionally ignore.
541 @item
542 Finally, remove the Gnulib module @samp{posixcheck} from the Gnulib imports,
543 and run @code{make distclean}.
544 @end enumerate
547 @node Modified build rules
548 @section Modifying the build rules of a Gnulib import directory
550 In some cases, you may want to set additional compiler options for
551 use within the Gnulib import directory.  For example, the
552 @samp{relocatable} module operates better if you define the C macros
553 @code{ENABLE_COSTLY_RELOCATABLE} and @code{INSTALLDIR} during its
554 compilation.
556 There are two ways to do so: Use of the @code{gnulib-tool} option
557 @code{--makefile-name}, and a kitchen-sink module.
559 With the @code{gnulib-tool} option @code{--makefile-name}, you are
560 telling @code{gnulib-tool} to generate an includable @code{Makefile.am}
561 portion, rather than a self-contained @code{Makefile.am}.  For example,
562 when you use @code{--makefile-name=Makefile.gnulib}, @code{gnulib-tool}
563 will generate @code{Makefile.gnulib}, and you will provide a
564 hand-written @code{Makefile.am} that includes @code{Makefile.gnulib}
565 through a line such as
566 @smallexample
567 include Makefile.gnulib
568 @end smallexample
569 Before this include, you need to initialize this set of @code{Makefile.am}
570 variables:
571 @itemize @bullet
572 @item
573 @code{AUTOMAKE_OPTIONS}
574 @item
575 @code{SUBDIRS}
576 @item
577 @code{noinst_HEADERS}
578 @item
579 @code{noinst_LIBRARIES}
580 @item
581 @code{noinst_LTLIBRARIES}
582 @item
583 @code{pkgdata_DATA} (only with Automake @geq{} 1.11.4)
584 @item
585 @code{EXTRA_DIST}
586 @item
587 @code{BUILT_SOURCES}
588 @item
589 @code{SUFFIXES}
590 @item
591 @code{MOSTLYCLEANFILES}
592 @item
593 @code{MOSTLYCLEANDIRS}
594 @item
595 @code{CLEANFILES}
596 @item
597 @code{DISTCLEANFILES}
598 @item
599 @code{MAINTAINERCLEANFILES}
600 @item
601 @code{AM_CPPFLAGS}
602 @item
603 @code{AM_CFLAGS}
604 @end itemize
605 @code{AUTOMAKE_OPTIONS} should be initialized as described in
606 @ref{Options,,Changing Automake's Behavior,automake,GNU Automake}.
607 The other variables can be initialized to empty.  However, you will most
608 likely want to initialize some of them with non-empty values, in order
609 to achieve the desired customization.
611 The other approach, the kitchen-sink module, is more advanced.  See
612 chapter @ref{Extending Gnulib}.
615 @node Multiple instances
616 @section Using Gnulib for both a library and a program
618 Your project might build both a library and some accompanying programs
619 in the same source tree. In that case you might want to use different
620 modules for the library than for the programs. Typically the programs
621 might want to make use of @code{getopt-posix} or @code{version-etc},
622 while the library wants to stay clear of these modules for technical
623 or licensing reasons.
625 Let's assume that your project contains a @file{lib} directory where
626 the source of the library resides and a @file{src} directory for the
627 sources of the programs as follows.
629 @example
631 |-- configure.ac
632 |-- lib
633 |   |-- foo.c
634 |   `-- Makefile.am
635 |-- Makefile.am
636 `-- src
637     |-- bar.c
638     `-- Makefile.am
639 @end example
641 You can now add two instances of Gnulib to your project in separate
642 source trees:
644 @example
645 ~/src/libfoo$ gnulib-tool --import --lib=libgnu --source-base=gnulib \
646               --m4-base=gnulib/m4 --macro-prefix=gl strndup
647 ~/src/libfoo$ gnulib-tool --import --lib=libgnutools \
648               --source-base=src/gnulib --m4-base=src/gnulib/m4 \
649               --macro-prefix=gl_tools getopt-gnu
650 @end example
652 The first one will import the module @code{strndup} in @file{gnulib}
653 and the second one will import @code{getopt-gnu} in @file{src/gnulib}
654 and you will end up with the following source tree (many files omitted
655 in the interest of brevity):
657 @example
659 |-- configure.ac
660 |-- gnulib
661 |   |-- m4
662 |   |-- strndup.c
663 |-- lib
664 |   |-- foo.c
665 |   `-- Makefile.am
666 |-- Makefile.am
667 `-- src
668     |-- bar.c
669     |-- gnulib
670     |   |-- getopt.c
671     |   |-- getopt.in.h
672     |   |-- m4
673     `-- Makefile.am
674 @end example
676 Integration with your code is basically the same as outlined in
677 @ref{Initial import} with the one exception that you have to add both
678 the macro @code{gl_EARLY} and the macro @code{gl_tools_EARLY} to your
679 @file{configure.ac} (and of course also both macros @code{gl_INIT} and
680 @code{gl_tools_INIT}). Obviously the name of the second macro is
681 dependent on the value of the @option{--macro-prefix} option in your
682 @command{gnulib-tool} invocation.
684 @example
686 AC_PROG_CC
687 gl_EARLY
688 gl_tools_EARLY
690 # For gnulib.
691 gl_INIT
692 gl_tools_INIT
694 @end example
696 Also as outlined in @ref{Initial import} you will have to add compiler
697 and linker flags. For the library you might have to add something
698 along the line of the following to your @file{Makefile.am}:
700 @example
702 AM_CPPFLAGS = -I$(top_srcdir)/gnulib -I$(top_builddir)/gnulib
704 libfoo_la_LIBADD = $(top_builddir)/gnulib/libgnu.la
706 @end example
708 Correspondingly for the programs you will have to add something like
709 this:
711 @example
713 AM_CPPFLAGS = -I$(top_srcdir)/src/gnulib -I$(top_builddir)/src/gnulib
715 LIBADD = $(top_builddir)/src/gnulib/libgnutools.la
717 @end example
719 The name of the library that you have pass in the linker option
720 depends on the @option{--lib} option in @command{gnulib-tool}
721 invocation.
724 @node gettextize and autopoint
725 @section Caveat: @code{gettextize} and @code{autopoint} users
727 @cindex gettextize, caveat
728 @cindex autopoint, caveat
729 The programs @code{gettextize} and @code{autopoint}, part of
730 GNU @code{gettext}, import or update the internationalization infrastructure.
731 Some of this infrastructure, namely ca.@: 20 Autoconf macro files and the
732 @file{config.rpath} file, is also contained in Gnulib and may be imported
733 by @code{gnulib-tool}.  The use of @code{gettextize} or @code{autopoint}
734 will therefore overwrite some of the files that @code{gnulib-tool} has
735 imported, and vice versa.
737 Avoiding to use @code{gettextize} (manually, as package maintainer) or
738 @code{autopoint} (as part of a script like @code{autoreconf} or
739 @code{autogen.sh}) is not the solution: These programs also import the
740 infrastructure in the @file{po/} and optionally in the @file{intl/} directory.
742 The copies of the conflicting files in Gnulib are more up-to-date than
743 the copies brought in by @code{gettextize} and @code{autopoint}.  When a
744 new @code{gettext} release is made, the copies of the files in Gnulib will
745 be updated immediately.
747 The choice of which version of gettext to require depends on the needs
748 of your package.  For a package that wants to comply to GNU Coding
749 Standards, the steps are:
751 @enumerate
752 @item
753 When you run @code{gettextize}, always use the @code{gettextize} from the
754 matching GNU gettext release.  For the most recent Gnulib checkout, this is
755 the newest release found on @url{https://ftp.gnu.org/gnu/gettext/}.  For an
756 older Gnulib snapshot, it is the release that was the most recent release
757 at the time the Gnulib snapshot was taken.
759 @item
760 After running @code{gettextize}, invoke @code{gnulib-tool} and import
761 the @code{gettext} module.  Also, copy the latest version of gnulib's
762 @file{build-aux/po/Makefile.in.in} to your @file{po/} directory (this
763 is done for you if you use gnulib's @file{bootstrap} script).
765 @item
766 If you get an error message like
767 @code{*** error: gettext infrastructure mismatch:
768 using a Makefile.in.in from gettext version ...
769 but the Autoconf macros are from gettext version ...},
770 it means that a new GNU gettext release was made, and its Autoconf macros
771 were integrated into Gnulib and now mismatch the @file{po/} infrastructure.
772 In this case, fetch and install the new GNU gettext release and run
773 @code{gettextize} followed by @code{gnulib-tool}.
774 @end enumerate
776 On the other hand, if your package is not as concerned with compliance
777 to the latest standards, but instead favors development on stable
778 environments, the steps are:
780 @enumerate
781 @item
782 Determine the oldest version of @code{gettext} that you intend to
783 support during development (at this time, gnulib recommends going no
784 older than version 0.17).  Run @code{autopoint} (not
785 @code{gettextize}) to copy infrastructure into place (newer versions
786 of gettext will install the older infrastructure that you requested).
788 @item
789 Invoke @code{gnulib-tool}, and import the @code{gettext-h} module.
790 @end enumerate
792 Regardless of which approach you used to get the infrastructure in
793 place, the following steps must then be used to preserve that
794 infrastructure (gnulib's @file{bootstrap} script follows these rules):
796 @enumerate
797 @item
798 When a script of yours run @code{autopoint}, invoke @code{gnulib-tool}
799 afterwards.
801 @item
802 When you invoke @code{autoreconf} after @code{gnulib-tool}, make sure to
803 not invoke @code{autopoint} a second time, by setting the @code{AUTOPOINT}
804 environment variable, like this:
805 @smallexample
806 $ env AUTOPOINT=true autoreconf --install
807 @end smallexample
808 @end enumerate
811 @node Localization
812 @section Handling Gnulib's own message translations
814 Gnulib provides some functions that emit translatable messages using GNU
815 @code{gettext}.  The @samp{gnulib} domain at the
816 @url{https://translationproject.org/, Translation Project} collects
817 translations of these messages, which you should incorporate into your
818 own programs.
820 There are two basic ways to achieve this.  The first, and older, method
821 is to list all the source files you use from Gnulib in your own
822 @file{po/POTFILES.in} file.  This will cause all the relevant
823 translatable strings to be included in your POT file.  When you send
824 this POT file to the Translation Project, translators will normally fill
825 in the translations of the Gnulib strings from their ``translation
826 memory'', and send you back updated PO files.
828 However, this process is error-prone: you might forget to list some
829 source files, or the translator might not be using a translation memory
830 and provide a different translation than another translator, or the
831 translation might not be kept in sync between Gnulib and your package.
832 It is also slow and causes substantial extra work, because a human
833 translator must be in the loop for each language and you will need to
834 incorporate their work on request.
836 For these reasons, a new method was designed and is now recommended.  If
837 you pass the @code{--po-base=@var{directory}} and @code{--po-domain=@var{domain}}
838 options to @code{gnulib-tool}, then @code{gnulib-tool} will create a
839 separate directory with its own @file{POTFILES.in}, and fetch current
840 translations directly from the Translation Project (using
841 @command{rsync} or @command{wget}, whichever is available).
842 The POT file in this directory will be called
843 @file{@var{domain}-gnulib.pot}, depending on the @var{domain} you gave to the
844 @code{--po-domain} option (typically the same as the package name).
845 This causes these translations to reside in a separate message domain,
846 so that they do not clash either with the translations for the main part
847 of your package nor with those of other packages on the system that use
848 possibly different versions of Gnulib.
849 When you use these options, the functions in Gnulib are built
850 in such a way that they will always use this domain regardless of the
851 default domain set by @code{textdomain}.
853 In order to use this method, you must---in each program that might use
854 Gnulib code---add an extra line to the part of the program that
855 initializes locale-dependent behavior.  Where you would normally write
856 something like:
858 @example
859 @group
860   setlocale (LC_ALL, "");
861   bindtextdomain (PACKAGE, LOCALEDIR);
862   textdomain (PACKAGE);
863 @end group
864 @end example
866 @noindent
867 you should add an additional @code{bindtextdomain} call to inform
868 gettext of where the MO files for the extra message domain may be found:
870 @example
871 @group
872   bindtextdomain (PACKAGE "-gnulib", LOCALEDIR);
873 @end group
874 @end example
876 (This example assumes that the @var{domain} that you specified
877 to @code{gnulib-tool} is the same as the value of the @code{PACKAGE}
878 preprocessor macro.)
880 Since you do not change the @code{textdomain} call, the default message
881 domain for your program remains the same and your own use of @code{gettext}
882 functions will not be affected.
885 @node VCS Issues
886 @section Issues with Version Control Systems
888 If a project stores its source files in a version control system (VCS),
889 such as CVS, Subversion, or Git, one needs to decide which files to commit.
891 In principle, all files created by @code{gnulib-tool}, except
892 @file{gnulib-cache.m4}, can be treated like generated source files,
893 like for example a @file{parser.c} file generated from
894 @file{parser.y}.  Alternatively, they can be considered source files
895 and updated manually.
897 Here are the three different approaches in common use.  Each has its
898 place, and you should use whichever best suits your particular project
899 and development methods.
901 @enumerate
902 @item
903 In projects which commit all source files, whether generated or not,
904 into their VCS, the @code{gnulib-tool} generated files should all be
905 committed.  In this case, you should pass the option
906 @samp{--no-vc-files} to @code{gnulib-tool}, which avoids alteration of
907 VCS-related files such as @file{.gitignore}.
909 Gnulib also contains files generated by @command{make} (and removed by
910 @code{make clean}), using information determined by
911 @command{configure}.  For a Gnulib source file of the form
912 @file{lib/foo.in.h}, the corresponding @file{lib/foo.h} is such a
913 @command{make}-generated file.  These should @emph{not} be checked
914 into the VCS, but instead added to @file{.gitignore} or equivalent.
916 @item
917 In projects which customarily omit from their VCS all files that are
918 generated from other source files, none of these files and directories
919 are added into the VCS@.  As described in @ref{Modified imports}, there
920 are two ways to keep track of options and module names that are passed
921 to @code{gnulib-tool}.  The command for restoring the omitted files
922 depends on it:
924 @itemize @bullet
925 @item
926 If they are stored in a file other than @code{gnulib-cache.m4}, such as
927 @file{autogen.sh}, @file{bootstrap}, @file{bootstrap.conf}, or similar,
928 the restoration command is the entire @code{gnulib-tool ... --import ...}
929 invocation with all options and module names.
931 @item
932 If the project relies on @code{gnulib-tool}'s memory of the last used
933 options and module names, then the file @file{gnulib-cache.m4} in the M4
934 macros directory must be added to the VCS, and the restoration command
937 @smallexample
938 $ gnulib-tool --update
939 @end smallexample
941 The @samp{--update} option operates much like the @samp{--add-import}
942 option, but it does not offer the possibility to change the way Gnulib is
943 used.  Also it does not report in the ChangeLogs the files that it had to
944 add because they were missing.
946 @end itemize
948 Gnulib includes the file @file{build-aux/bootstrap} to aid a developer
949 in using this setup.  Furthermore, in projects that use git for
950 version control, it is possible to use a git submodule containing the
951 precise commit of the gnulib repository, so that each developer
952 running @file{bootstrap} will get the same version of all
953 gnulib-provided files.  The location of the submodule can be chosen to
954 fit the package's needs; here's how to initially create the submodule
955 in the directory @file{.gnulib}:
957 @smallexample
958 $ dir=.gnulib
959 $ git submodule add -- git://git.sv.gnu.org/gnulib.git $dir
960 $ git config alias.syncsub "submodule foreach git pull origin master"
961 @end smallexample
963 @noindent
964 Thereafter, @file{bootstrap} can run this command to update the
965 submodule to the recorded checkout level:
967 @smallexample
968 git submodule update --init $dir
969 @end smallexample
971 @noindent
972 and a developer can use this sequence to update to a newer version of
973 gnulib:
975 @smallexample
976 $ git syncsub
977 $ git add $dir
978 $ ./bootstrap
979 @end smallexample
981 @item
982 Some projects take a ``middle road'': they do commit Gnulib source
983 files as in the first approach, but they do not commit other derived
984 files, such as a @code{Makefile.in} generated by Automake.  This
985 increases the size and complexity of the repository, but can help
986 occasional contributors by not requiring them to have a full Gnulib
987 checkout to do a build, and all developers by ensuring that all
988 developers are working with the same version of Gnulib in the
989 repository.  It also supports multiple Gnulib instances within a
990 project.  It remains important not to commit the
991 @command{make}-generated files, as described above.
993 @end enumerate
996 @node Unit tests
997 @section Bundling the unit tests of the Gnulib modules
999 You can bundle the unit tests of the Gnulib modules together with your
1000 package, through the @samp{--with-tests} option.  Together with
1001 @samp{--with-tests}, you also specify the directory for these tests
1002 through the @samp{--tests-base} option.  Of course, you need to add this
1003 directory to the @code{SUBDIRS} variable in the @code{Makefile.am} of
1004 the parent directory.
1006 The advantage of having the unit tests bundled is that when your program
1007 has a problem on a particular platform, running the unit tests may help
1008 determine quickly if the problem is on Gnulib's side or on your package's
1009 side.  Also, it helps verifying Gnulib's portability, of course.
1011 The unit tests will be compiled and run when the user runs @samp{make check}.
1012 When the user runs only @samp{make}, the unit tests will not be compiled.
1014 In the @code{SUBDIRS} variable, it is useful to put the Gnulib tests directory
1015 after the directory containing the other tests, not before:
1017 @smallexample
1018 SUBDIRS = gnulib-lib src man tests gnulib-tests
1019 @end smallexample
1021 @noindent
1022 This will ensure that on platforms where there are test failures in either
1023 directory, users will see and report the failures from the tests of your
1024 program.
1026 Note: In packages which use more than one invocation of @code{gnulib-tool}
1027 in the scope of the same @code{configure.ac}, you cannot use
1028 @samp{--with-tests}.  You will have to use a separate @code{configure.ac}
1029 in this case.
1032 @node Conditional dependencies
1033 @section Avoiding unnecessary checks and compilations
1035 @cindex conditional dependencies
1036 In some cases, a module is needed by another module only on specific
1037 platforms.  But when a module is present, its Autoconf checks are always
1038 executed, and its @code{Makefile.am} additions are always enabled.  So
1039 it can happen that some Autoconf checks are executed and some source files
1040 are compiled, although no other module needs them on this particular
1041 platform, just @emph{in case} some other module would need them.
1043 The option @samp{--conditional-dependencies} enables an optimization of
1044 configure checks and @code{Makefile.am} snippets that avoids this.  With
1045 this option, whether a module is considered ``present'' is no longer decided
1046 when @code{gnulib-tool} is invoked, but later, when @code{configure} is run.
1047 This applies to modules that were added as dependencies while
1048 @code{gnulib-tool} was run; modules that were passed on the command line
1049 explicitly are always ``present''.
1051 For example, the @code{timegm} module needs, on platforms
1052 where the system's @code{timegm} function is missing or buggy, a replacement
1053 that is based on a function @code{mktime_internal}.  The module
1054 @code{mktime-internal} that provides this function provides it on all
1055 platforms.  So, by default, the file @file{mktime-internal.c} will be
1056 compiled on all platforms, even on glibc and BSD systems which have a
1057 working @code{timegm} function.  When the option
1058 @samp{--conditional-dependencies} is given, on the other hand, and if
1059 @code{mktime-internal} was not explicitly required on the command line,
1060 the file @file{mktime-internal.c} will only be compiled on the platforms
1061 where the @code{timegm} needs them.
1063 Conditional dependencies are specified in the module description by putting
1064 the condition on the same line as the dependent module, enclosed in brackets.
1065 The condition is a boolean shell expression that can assume that the
1066 @code{configure.ac} snippet from the module description has already been
1067 executed.  In the example above, the dependency from @code{timegm} to
1068 @code{mktime-internal} is written like this:
1070 @smallexample
1071 Depends-on:
1073 mktime-internal [test $HAVE_TIMEGM = 0 || test $REPLACE_TIMEGM = 1]
1075 @end smallexample
1077 Note: The option @samp{--conditional-dependencies} cannot be used together
1078 with the option @samp{--with-tests}.  It also cannot be used when a package
1079 uses @code{gnulib-tool} for several subdirectories, with different values
1080 of @samp{--source-base}, in the scope of a single @code{configure.ac} file.