Emphasize the drawbacks of the --symlink option.
[gnulib.git] / doc / gnulib-tool.texi
1 @node Invoking gnulib-tool
2 @chapter Invoking gnulib-tool
3
4 @c Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5
6 @c Permission is granted to copy, distribute and/or modify this document
7 @c under the terms of the GNU Free Documentation License, Version 1.2 or
8 @c any later version published by the Free Software Foundation; with no
9 @c Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
10 @c Texts.  A copy of the license is included in the ``GNU Free
11 @c Documentation License'' file as part of this distribution.
12
13 @pindex gnulib-tool
14 @cindex invoking @command{gnulib-tool}
15
16 The @command{gnulib-tool} command is the recommended way to import
17 Gnulib modules.  It is possible to borrow Gnulib modules in a package
18 without using @command{gnulib-tool}, relying only on the
19 meta-information stored in the @file{modules/*} files, but with a
20 growing number of modules this becomes tedious.  @command{gnulib-tool}
21 simplifies the management of source files, @file{Makefile.am}s and
22 @file{configure.ac} in packages incorporating Gnulib modules.
23
24 Run @samp{gnulib-tool --help} for information.  To get familiar with
25 @command{gnulib-tool} without affecting your sources, you can also try
26 some commands with the option @samp{--dry-run}; then
27 @code{gnulib-tool} will only report which actions it would perform in
28 a real run without changing anything.
29
30 @menu
31 * Initial import::              First import of Gnulib modules.
32 * Modified imports::            Changing the import specification.
33 * Simple update::               Tracking Gnulib development.
34 * CVS Issues::                  Integration with CVS.
35 @end menu
36
37
38 @node Initial import
39 @section Initial import
40 @cindex initial import
41
42 Gnulib assumes your project uses Autoconf and Automake.  Invoking
43 @samp{gnulib-tool --import} will copy source files, create a
44 @file{Makefile.am} to build them, generate a file @file{gnulib-comp.m4} with
45 Autoconf M4 macro declarations used by @file{configure.ac}, and generate
46 a file @file{gnulib-cache.m4} containing the cached specification of how
47 Gnulib is used.
48
49 Our example will be a library that uses Autoconf, Automake and
50 Libtool.  It calls @code{strdup}, and you wish to use gnulib to make
51 the package portable to C89 (which doesn't have @code{strdup}).
52
53 @example
54 ~/src/libfoo$ gnulib-tool --import strdup
55 Module list with included dependencies:
56   strdup
57 File list:
58   lib/strdup.c
59   lib/strdup.h
60   m4/onceonly_2_57.m4
61   m4/strdup.m4
62 Copying file m4/gnulib-tool.m4
63 Copying file m4/onceonly_2_57.m4
64 Copying file lib/strdup.c
65 Copying file lib/strdup.h
66 Copying file m4/strdup.m4
67 Creating lib/Makefile.am
68 Creating m4/gnulib-cache.m4
69 Creating m4/gnulib-comp.m4
70 Finished.
71
72 You may need to add #include directives for the following .h files.
73   #include "strdup.h"
74
75 Don't forget to
76   - add "lib/Makefile" to AC_CONFIG_FILES in ./configure.ac,
77   - mention "lib" in SUBDIRS in Makefile.am,
78   - mention "-I m4" in ACLOCAL_AMFLAGS in Makefile.am,
79   - invoke gl_EARLY in ./configure.ac, right after AC_PROG_CC,
80   - invoke gl_INIT in ./configure.ac.
81 ~/src/libfoo$
82 @end example
83
84 By default, the source code is copied into @file{lib/} and the M4
85 macros in @file{m4/}.  You can override these paths by using
86 @code{--source-base=DIRECTORY} and @code{--m4-base=DIRECTORY}.  Some
87 modules also provide other files necessary for building. These files
88 are copied into the directory specified by @samp{AC_CONFIG_AUX_DIR} in
89 @file{configure.ac} or by the @code{--aux-dir=DIRECTORY} option.  If
90 neither is specified, the current directory is assumed.
91
92 @code{gnulib-tool} can make symbolic links instead of copying the
93 source files.  The option to specify for this is @samp{--symlink}, or
94 @samp{-s} for short.  This can be useful to save a few kilobytes of disk
95 space.  But it is likely to introduce bugs when @code{gnulib} is updated;
96 it is more reliable to use @samp{gnulib-tool --update} (see below)
97 to update to newer versions of @code{gnulib}.  Furthermore it requires
98 extra effort to create self-contained tarballs, and it may disturb some
99 mechanism the maintainer applies to the sources.  For these reasons,
100 this option is generally discouraged.
101
102 @code{gnulib-tool} will overwrite any pre-existing files, in
103 particular @file{Makefile.am}.  Unfortunately, separating the
104 generated @file{Makefile.am} content (for building the gnulib library)
105 into a separate file, say @file{gnulib.mk}, that could be included
106 by your handwritten @file{Makefile.am} is not possible, due to how
107 variable assignments are handled by Automake.
108
109 Consequently, it is a good idea to choose directories that are not
110 already used by your projects, to separate gnulib imported files from
111 your own files.  This approach is also useful if you want to avoid
112 conflicts between other tools (e.g., @code{gettextize} that also copy
113 M4 files into your package.  Simon Josefsson successfully uses a source
114 base of @file{gl/}, and a M4 base of @file{gl/m4/}, in several
115 packages.
116
117 After the @samp{--import} option on the command line comes the list of
118 Gnulib modules that you want to incorporate in your package.  The names
119 of the modules coincide with the filenames in Gnulib's @file{modules/}
120 directory.
121
122 Some Gnulib modules depend on other Gnulib modules.  @code{gnulib-tool}
123 will automatically add the needed modules as well; you need not list
124 them explicitly.  @code{gnulib-tool} will also memorize which dependent
125 modules it has added, so that when someday a dependency is dropped, the
126 implicitly added module is dropped as well (unless you have explicitly
127 requested that module).
128
129 If you want to cut a dependency, i.e., not add a module although one of
130 your requested modules depends on it, you may use the option
131 @samp{--avoid=@var{module}} to do so.  Multiple uses of this option are
132 possible.  Of course, you will then need to implement the same interface
133 as the removed module.
134
135 A few manual steps are required to finish the initial import.
136 @code{gnulib-tool} printed a summary of these steps.
137
138 First, you must ensure Autoconf can find the macro definitions in
139 @file{gnulib-comp.m4}.  Use the @code{ACLOCAL_AMFLAGS} specifier in
140 your top-level @file{Makefile.am} file, as in:
141
142 @example
143 ACLOCAL_AMFLAGS = -I m4
144 @end example
145
146 You are now ready to call the M4 macros in @code{gnulib-comp.m4} from
147 @file{configure.ac}.  The macro @code{gl_EARLY} must be called as soon
148 as possible after verifying that the C compiler is working.
149 Typically, this is immediately after @code{AC_PROG_CC}, as in:
150
151 @example
152 ...
153 AC_PROG_CC
154 gl_EARLY
155 ...
156 @end example
157
158 The core part of the gnulib checks are done by the macro
159 @code{gl_INIT}.  Place it further down in the file, typically where
160 you normally check for header files or functions.  It must come after
161 other checks which may affect the compiler invocation, such as
162 @code{AC_MINIX}.  For example:
163
164 @example
165 ...
166 # For gnulib.
167 gl_INIT
168 ...
169 @end example
170
171 @code{gl_INIT} will in turn call the macros related with the
172 gnulib functions, be it specific gnulib macros, like @code{gl_FUNC_ALLOCA}
173 or autoconf or automake macros like @code{AC_FUNC_ALLOCA} or
174 @code{AM_FUNC_GETLINE}.  So there is no need to call those macros yourself
175 when you use the corresponding gnulib modules.
176
177 You must also make sure that the gnulib library is built.  Add the
178 @code{Makefile} in the gnulib source base directory to
179 @code{AC_CONFIG_FILES}, as in:
180
181 @example
182 AC_CONFIG_FILES(... lib/Makefile ...)
183 @end example
184
185 You must also make sure that @code{make} will recurse into the gnulib
186 directory.  To achieve this, add the gnulib source base directory to a
187 @code{SUBDIRS} Makefile.am statement, as in:
188
189 @example
190 SUBDIRS = lib
191 @end example
192
193 or if you, more likely, already have a few entries in @code{SUBDIRS},
194 you can add something like:
195
196 @example
197 SUBDIRS += lib
198 @end example
199
200 Finally, you have to add compiler and linker flags in the appropriate
201 source directories, so that you can make use of the gnulib library.
202 Since some modules (@samp{getopt}, for example) may copy files into
203 the build directory, @file{top_builddir/lib} is needed as well
204 as @file{top_srcdir/lib}.  For example:
205
206 @example
207 ...
208 AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_builddir)/lib
209 ...
210 LDADD = lib/libgnu.a
211 ...
212 @end example
213
214 Don't forget to @code{#include} the various header files.  In this
215 example, you would need to make sure that @samp{#include "strdup.h"}
216 is evaluated when compiling all source code files, that want to make
217 use of @code{strdup}.
218
219 When an include file is provided by Gnulib
220 you shouldn't try to include the corresponding system header files
221 yourself, but let the gnulib header file do it.  The ordering
222 of the definition for some symbols may be significant; the Gnulib
223 header files take care of that.
224
225 For example, to use the @code{time_r} gnulib module you should
226 use include header file provided by the gnulib, and so
227 @samp{#include "time_r.h"}, but you shouldn't explicitly
228 @samp{#include <time.h>} as it is already done in @file{time_r.h}
229 before the redefinition of some symbols.
230
231 A final word of warning: Gnulib currently assumes it will be
232 responsible for @emph{all} functions that end up in the Autoconf
233 @code{@@LIBOBJS@@} variables (and/or @code{@@LTLIBOBJS@@} if using
234 Libtool), e.g., those specified in @code{AC_REPLACE_FUNCS} in your
235 @file{configure.ac}.  Therefore, if you have any functions which are
236 not covered by Gnulib which need that treatment, you have to
237 essentially reimplement AC_REPLACE_FUNCS using different names; for an
238 example, see the Findutils sources.  Perhaps this will be improved in
239 the future.
240
241
242 @node Modified imports
243 @section Modified imports
244
245 You can at any moment decide to use Gnulib differently than the last time.
246
247 If you only want to use more Gnulib modules, simply invoke
248 @command{gnulib-tool --import @var{new-modules}}.  @code{gnulib-tool}
249 remembers which modules were used last time.  The list of modules that
250 you pass after @samp{--import} is @emph{added} to the previous list of
251 modules.
252
253 For most changes, such as added or removed modules, or even different
254 choices of @samp{--lib}, @samp{--source-base} or @samp{--aux-dir}, there
255 are two ways to perform the change.
256
257 The standard way is to modify manually the file @file{gnulib-cache.m4}
258 in the M4 macros directory, then launch @samp{gnulib-tool --import}.
259
260 The other way is to call @command{gnulib-tool} again, with the changed
261 command-line options.  Note that this doesn't let you remove modules,
262 because as you just learned, the list of modules is always cumulated.
263 Also this way is often impractical, because you don't remember the way
264 you invoked @code{gnulib-tool} last time.
265
266 The only change for which this doesn't work is a change of the
267 @samp{--m4-base} directory.  Because, when you pass a different value of
268 @samp{--m4-base}, @code{gnulib-tool} will not find the previous
269 @file{gnulib-cache.m4} file any more... A possible solution is to manually
270 copy the @file{gnulib-cache.m4} into the new M4 macro directory.
271
272 In the @file{gnulib-cache.m4}, the macros have the following meaning:
273 @table @code
274 @item gl_MODULES
275 The argument is a space separated list of the requested modules, not including
276 dependencies.
277
278 @item gl_AVOID
279 The argument is a space separated list of modules that should not be used,
280 even if they occur as dependencies.  Corresponds to the @samp{--avoid}
281 command line argument.
282
283 @item gl_SOURCE_BASE
284 The argument is the relative file name of the directory containing the gnulib
285 source files (mostly *.c and *.h files).  Corresponds to the
286 @samp{--source-base} command line argument.
287
288 @item gl_M4_BASE
289 The argument is the relative file name of the directory containing the gnulib
290 M4 macros (*.m4 files).  Corresponds to the @samp{--m4-base} command line
291 argument.
292
293 @item gl_TESTS_BASE
294 The argument is the relative file name of the directory containing the gnulib
295 unit test files.  Corresponds to the @samp{--tests-base} command line argument.
296
297 @item gl_LIB
298 The argument is the name of the library to be created.  Corresponds to the
299 @samp{--lib} command line argument.
300
301 @item gl_LGPL
302 The presence of this macro corresponds to the @samp{--lgpl} command line
303 argument.  It takes no arguments.
304
305 @item gl_LIBTOOL
306 The presence of this macro corresponds to the @samp{--libtool} command line
307 argument and to the absence of the @samp{--no-libtool} command line argument.
308 It takes no arguments.
309
310 @item gl_MACRO_PREFIX
311 The argument is the prefix to use for macros in the @file{gnulib-comp.m4}
312 file.  Corresponds to the @samp{--macro-prefix} command line argument.
313 @end table
314
315 @node Simple update
316 @section Simple update
317
318 When you want to update to a more recent version of Gnulib, without
319 changing the list of modules or other parameters, a simple call
320 does it:
321
322 @smallexample
323 $ gnulib-tool --import
324 @end smallexample
325
326 This will create, update or remove files, as needed.
327
328 @node CVS Issues
329 @section CVS Issues
330
331 All files created by @code{gnulib-tool}, except @file{gnulib-cache.m4},
332 should be treated like generated source files, like for example a
333 @file{parser.c} file is generated from @file{parser.y}.
334
335 In projects which commit all source files, whether generated or not, into
336 CVS, the @code{gnulib-tool} generated files should all be committed.
337
338 In projects which customarily omit from the CVS all files that generated
339 from other source files, all these files and directories would not be
340 added into CVS.  The only file that must be added to CVS is
341 @file{gnulib-cache.m4} in the M4 macros directory.  Also, the script for
342 restoring files not in CVS, customarily called @file{autogen.sh} or
343 @file{bootstrap.sh}, will typically contain the statement for restoring
344 the omitted files:
345
346 @smallexample
347 $ gnulib-tool --update
348 @end smallexample
349
350 The @samp{--update} option operates much like the @samp{--import} option,
351 but it does not offer the possibility to change the way Gnulib is used.
352 Also it does not report in the ChangeLogs the files that it had to add
353 because they were missing.