ef8e9ac89c957164a3206a7e4fcb59f8496197be
[gnulib.git] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2007 Free Software Foundation, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19
20 # This program is meant for authors or maintainers which want to import
21 # modules from gnulib into their packages.
22
23 progname=$0
24 package=gnulib
25 cvsdatestamp='$Date: 2007-06-23 07:40:58 $'
26 last_checkin_date=`echo "$cvsdatestamp" | sed -e 's,^\$[D]ate: ,,'`
27 version=`echo "$last_checkin_date" | sed -e 's/ .*$//' -e 's,/,-,g'`
28 nl='
29 '
30 IFS=" ""        $nl"
31
32 # You can set AUTOCONFPATH to empty if autoconf 2.57 is already in your PATH.
33 AUTOCONFPATH=
34 #case $USER in
35 #  bruno )
36 #    AUTOCONFBINDIR=/packages/gnu-inst-autoconf/2.57/bin
37 #    AUTOCONFPATH="eval env PATH=${AUTOCONFBINDIR}:\$PATH "
38 #    ;;
39 #esac
40
41 # You can set AUTOMAKEPATH to empty if automake 1.9.x is already in your PATH.
42 AUTOMAKEPATH=
43
44 # You can set GETTEXTPATH to empty if autopoint 0.15 is already in your PATH.
45 GETTEXTPATH=
46
47 # If you didn't set AUTOCONFPATH and AUTOMAKEPATH, you can also set the
48 # variables AUTOCONF, AUTOHEADER, ACLOCAL, AUTOMAKE, AUTORECONF individually.
49 if test -z "${AUTOCONF}" || test -n "${AUTOCONFPATH}"; then
50   AUTOCONF="${AUTOCONFPATH}autoconf"
51 fi
52 if test -z "${AUTOHEADER}" || test -n "${AUTOCONFPATH}"; then
53   AUTOHEADER="${AUTOCONFPATH}autoheader"
54 fi
55 if test -z "${ACLOCAL}" || test -n "${AUTOMAKEPATH}"; then
56   ACLOCAL="${AUTOMAKEPATH}aclocal"
57 fi
58 if test -z "${AUTOMAKE}" || test -n "${AUTOMAKEPATH}"; then
59   AUTOMAKE="${AUTOMAKEPATH}automake"
60 fi
61 if test -z "${AUTORECONF}" || test -n "${AUTOCONFPATH}"; then
62   AUTORECONF="${AUTOCONFPATH}autoreconf"
63 fi
64
65 # If you didn't set GETTEXTPATH, you can also set the variable AUTOPOINT.
66 if test -z "${AUTOPOINT}" || test -n "${GETTEXTPATH}"; then
67   AUTOPOINT="${GETTEXTPATH}autopoint"
68 fi
69
70 # When using GNU sed, turn off as many GNU extensions as possible,
71 # to minimize the risk of accidentally using non-portable features.
72 # However, do this only for gnulib-tool itself, not for the code that
73 # gnulib-tool generates, since we don't want "sed --posix" to leak
74 # into makefiles.
75 if (alias) > /dev/null 2>&1 && echo | sed --posix -e d >/dev/null 2>&1; then
76   alias sed='sed --posix'
77 fi
78
79 # func_usage
80 # outputs to stdout the --help usage message.
81 func_usage ()
82 {
83   echo "\
84 Usage: gnulib-tool --list
85        gnulib-tool --import [module1 ... moduleN]
86        gnulib-tool --update
87        gnulib-tool --create-testdir --dir=directory [module1 ... moduleN]
88        gnulib-tool --create-megatestdir --dir=directory [module1 ... moduleN]
89        gnulib-tool --test --dir=directory module1 ... moduleN
90        gnulib-tool --megatest --dir=directory [module1 ... moduleN]
91        gnulib-tool --extract-description module
92        gnulib-tool --extract-filelist module
93        gnulib-tool --extract-dependencies module
94        gnulib-tool --extract-autoconf-snippet module
95        gnulib-tool --extract-automake-snippet module
96        gnulib-tool --extract-include-directive module
97        gnulib-tool --extract-link-directive module
98        gnulib-tool --extract-license module
99        gnulib-tool --extract-maintainer module
100        gnulib-tool --extract-tests-module module
101
102 Operation modes:
103       --list                print the available module names
104       --import              import the given modules into the current package;
105                             if no modules are specified, update the current
106                             package from the current gnulib
107       --update              update the current package, restore files omitted
108                             from CVS
109       --create-testdir      create a scratch package with the given modules
110       --create-megatestdir  create a mega scratch package with the given modules
111                             one by one and all together
112       --test                test the combination of the given modules
113                             (recommended to use CC=\"gcc -Wall\" here)
114       --megatest            test the given modules one by one and all together
115                             (recommended to use CC=\"gcc -Wall\" here)
116       --extract-description        extract the description
117       --extract-filelist           extract the list of files
118       --extract-dependencies       extract the dependencies
119       --extract-autoconf-snippet   extract the snippet for configure.ac
120       --extract-automake-snippet   extract the snippet for library makefile
121       --extract-include-directive  extract the #include directive
122       --extract-link-directive     extract the linker directive
123       --extract-license            report the license terms of the source files
124                                    under lib/
125       --extract-maintainer         report the maintainer(s) inside gnulib
126       --extract-tests-module       report the unit test module, if it exists
127
128 General options:
129       --dir=DIRECTORY       Specify the target directory.
130                             For --import, this specifies where your
131                             configure.ac can be found.  Defaults to current
132                             directory.
133       --local-dir=DIRECTORY  Specify a local override directory where to look
134                             up files before looking in gnulib's directory.
135
136 Options for --import:
137       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
138       --source-base=DIRECTORY
139                             Directory relative to --dir where source code is
140                             placed (default \"lib\").
141       --m4-base=DIRECTORY   Directory relative to --dir where *.m4 macros are
142                             placed (default \"m4\").
143       --doc-base=DIRECTORY  Directory relative to --dir where doc files are
144                             placed (default \"doc\").
145       --tests-base=DIRECTORY
146                             Directory relative to --dir where unit tests are
147                             placed (default \"tests\").
148       --aux-dir=DIRECTORY   Directory relative to --dir where auxiliary build
149                             tools are placed (default \"build-aux\").
150       --with-tests          Include unit tests for the included modules.
151       --avoid=MODULE        Avoid including the given MODULE. Useful if you
152                             have code that provides equivalent functionality.
153                             This option can be repeated.
154       --lgpl                Abort if modules aren't available under the LGPL.
155                             Also modify license template from GPL to LGPL.
156       --makefile-name=NAME  Name of makefile in automake syntax in the
157                             source-base and tests-base directories
158                             (default \"Makefile.am\").
159       --libtool             Use libtool rules.
160       --no-libtool          Don't use libtool rules.
161       --macro-prefix=PREFIX  Specify the prefix of the macros 'gl_EARLY' and
162                             'gl_INIT'. Default is 'gl'.
163       --no-changelog        don't update or create ChangeLog files
164
165 Options for --import and --update:
166       --dry-run             For --import, only print what would have been done.
167   -s, --symbolic, --symlink Make symbolic links instead of copying files.
168       --local-symlink       Make symbolic links instead of copying files, only
169                             for files from the local override directory.
170
171 Report bugs to <bug-gnulib@gnu.org>."
172 }
173
174 # func_version
175 # outputs to stdout the --version message.
176 func_version ()
177 {
178   year=`echo "$last_checkin_date" | sed -e 's,/.*$,,'`
179   echo "\
180 $progname (GNU $package) $version
181 Copyright (C) $year Free Software Foundation, Inc.
182 This is free software; see the source for copying conditions.  There is NO
183 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
184 Written by" "Bruno Haible" "and" "Simon Josefsson"
185 }
186
187 # func_emit_copyright_notice
188 # outputs to stdout a header for a generated file.
189 func_emit_copyright_notice ()
190 {
191   echo "# Copyright (C) 2004-2007 Free Software Foundation, Inc."
192   echo "#"
193   echo "# This file is free software, distributed under the terms of the GNU"
194   echo "# General Public License.  As a special exception to the GNU General"
195   echo "# Public License, this file may be distributed as part of a program"
196   echo "# that contains a configuration script generated by Autoconf, under"
197   echo "# the same distribution terms as the rest of that program."
198   echo "#"
199   echo "# Generated by gnulib-tool."
200 }
201
202 # func_exit STATUS
203 # exit with status
204 func_exit ()
205 {
206   (exit $1); exit $1
207 }
208
209 # func_tmpdir
210 # creates a temporary directory.
211 # Sets variable
212 # - tmp             pathname of freshly created temporary directory
213 func_tmpdir ()
214 {
215   # Use the environment variable TMPDIR, falling back to /tmp. This allows
216   # users to specify a different temporary directory, for example, if their
217   # /tmp is filled up or too small.
218   : ${TMPDIR=/tmp}
219   {
220     # Use the mktemp program if available. If not available, hide the error
221     # message.
222     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
223     test -n "$tmp" && test -d "$tmp"
224   } ||
225   {
226     # Use a simple mkdir command. It is guaranteed to fail if the directory
227     # already exists.  $RANDOM is bash specific and expands to empty in shells
228     # other than bash, ksh and zsh.  Its use does not increase security;
229     # rather, it minimizes the probability of failure in a very cluttered /tmp
230     # directory.
231     tmp=$TMPDIR/gl$$-$RANDOM
232     (umask 077 && mkdir "$tmp")
233   } ||
234   {
235     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
236     func_exit 1
237   }
238 }
239
240 # func_append var value
241 # appends the given value to the shell variable var.
242 if ( foo=bar; foo+=baz && test "$foo" = barbaz ) >/dev/null 2>&1; then
243   # Use bash's += operator. It reduces complexity of appending repeatedly to
244   # a single variable from O(n^2) to O(n).
245   func_append ()
246   {
247     eval "$1+=\"\$2\""
248   }
249 else
250   func_append ()
251   {
252     eval "$1=\"\$$1\$2\""
253   }
254 fi
255
256 # func_fatal_error message
257 # outputs to stderr a fatal error message, and terminates the program.
258 func_fatal_error ()
259 {
260   echo "gnulib-tool: *** $1" 1>&2
261   echo "gnulib-tool: *** Stop." 1>&2
262   func_exit 1
263 }
264
265 # func_readlink SYMLINK
266 # outputs the target of the given symlink.
267 if (type -p readlink) > /dev/null 2>&1; then
268   func_readlink ()
269   {
270     # Use the readlink program from GNU coreutils.
271     readlink "$1"
272   }
273 else
274   func_readlink ()
275   {
276     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
277     # would do the wrong thing if the link target contains " -> ".
278     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
279   }
280 fi
281
282 # func_relativize DIR1 DIR2
283 # computes a relative pathname RELDIR such that DIR1/RELDIR = DIR2.
284 # Input:
285 # - DIR1            relative pathname, relative to the current directory
286 # - DIR2            relative pathname, relative to the current directory
287 # Output:
288 # - reldir          relative pathname of DIR2, relative to DIR1
289 func_relativize ()
290 {
291   dir0=`pwd`
292   dir1="$1"
293   dir2="$2"
294   sed_first='s,^\([^/]*\)/.*$,\1,'
295   sed_rest='s,^[^/]*/*,,'
296   sed_last='s,^.*/\([^/]*\)$,\1,'
297   sed_butlast='s,/*[^/]*$,,'
298   while test -n "$dir1"; do
299     first=`echo "$dir1" | sed -e "$sed_first"`
300     if test "$first" != "."; then
301       if test "$first" = ".."; then
302         dir2=`echo "$dir0" | sed -e "$sed_last"`/"$dir2"
303         dir0=`echo "$dir0" | sed -e "$sed_butlast"`
304       else
305         first2=`echo "$dir2" | sed -e "$sed_first"`
306         if test "$first2" = "$first"; then
307           dir2=`echo "$dir2" | sed -e "$sed_rest"`
308         else
309           dir2="../$dir2"
310         fi
311         dir0="$dir0"/"$first"
312       fi
313     fi
314     dir1=`echo "$dir1" | sed -e "$sed_rest"`
315   done
316   reldir="$dir2"
317 }
318
319 # func_relconcat DIR1 DIR2
320 # computes a relative pathname DIR1/DIR2, with obvious simplifications.
321 # Input:
322 # - DIR1            relative pathname, relative to the current directory
323 # - DIR2            relative pathname, relative to DIR1
324 # Output:
325 # - relconcat       DIR1/DIR2, relative to the current directory
326 func_relconcat ()
327 {
328   dir1="$1"
329   dir2="$2"
330   sed_first='s,^\([^/]*\)/.*$,\1,'
331   sed_rest='s,^[^/]*/*,,'
332   sed_last='s,^.*/\([^/]*\)$,\1,'
333   sed_butlast='s,/*[^/]*$,,'
334   while true; do
335     first=`echo "$dir2" | sed -e "$sed_first"`
336     if test "$first" = "."; then
337       dir2=`echo "$dir2" | sed -e "$sed_rest"`
338       if test -z "$dir2"; then
339         relconcat="$dir1"
340         break
341       fi
342     else
343       last=`echo "$dir1" | sed -e "$sed_last"`
344       while test "$last" = "."; do
345         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
346         last=`echo "$dir1" | sed -e "$sed_last"`
347       done
348       if test -z "$dir1"; then
349         relconcat="$dir2"
350         break
351       fi
352       if test "$first" = ".."; then
353         if test "$last" = ".."; then
354           relconcat="$dir1/$dir2"
355           break
356         fi
357         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
358         dir2=`echo "$dir2" | sed -e "$sed_rest"`
359         if test -z "$dir1"; then
360           relconcat="$dir2"
361           break
362         fi
363         if test -z "$dir2"; then
364           relconcat="$dir1"
365           break
366         fi
367       else
368         relconcat="$dir1/$dir2"
369         break
370       fi
371     fi
372   done
373 }
374
375 # func_ln SRC DEST
376 # Like ln -s, except that SRC is given relative to the current directory (or
377 # absolute), not given relative to the directory of DEST.
378 func_ln ()
379 {
380   case "$1" in
381     /*)
382       ln -s "$1" "$2" ;;
383     *) # SRC is relative.
384       case "$2" in
385         /*)
386           ln -s "`pwd`/$1" "$2" ;;
387         *) # DEST is relative too.
388           ln_destdir=`echo "$2" | sed -e 's,[^/]*$,,'`
389           test -n "$ln_destdir" || ln_destdir="."
390           func_relativize "$ln_destdir" "$1"
391           ln -s "$reldir" "$2"
392           ;;
393       esac
394       ;;
395   esac
396 }
397
398 # func_ln_if_changed SRC DEST
399 # Like func_ln, but avoids munging timestamps if the link is correct.
400 func_ln_if_changed ()
401 {
402   if test $# -ne 2; then
403     echo "usage: func_ln_if_changed SRC DEST" >&2
404   fi
405   ln_target=`func_readlink "$2"`
406   if test -L "$2" && test "$1" = "$ln_target"; then
407     :
408   else
409     rm -f "$2"
410     func_ln "$1" "$2"
411   fi
412 }
413
414 # Ensure an 'echo' command that does not interpret backslashes.
415 # Test cases:
416 #   echo '\n' | wc -l                 prints 1 when OK, 2 when KO
417 #   echo '\t' | grep t > /dev/null    has return code 0 when OK, 1 when KO
418 # This problem is a weird heritage from SVR4. BSD got it right.
419 # Nowadays the problem occurs in 4 situations:
420 # - in bash, when the shell option xpg_echo is set,
421 # - in zsh, when sh-emulation is not set,
422 # - in ksh (e.g. AIX /bin/sh and Solaris /usr/xpg4/bin/sh are ksh instances,
423 #           and HP-UX /bin/sh and IRIX /bin/sh behave similarly),
424 # - in Solaris /bin/sh and OSF/1 /bin/sh.
425 # We try the following workarounds:
426 # - for all: respawn using $CONFIG_SHELL if that is set and works.
427 # - for bash: unset the shell option xpg_echo.
428 # - for zsh: turn sh-emulation on.
429 # - for ksh: alias echo to a function that uses cat of a here document.
430 # - for Solaris /bin/sh: respawn using /bin/ksh and rely on the ksh workaround.
431 # - otherwise: respawn using /bin/sh and rely on the workarounds.
432 # When respawning, we pass --no-reexec as first argument, so as to avoid
433 # turning this script into a fork bomb in unlucky situations.
434 have_echo=
435 if echo '\t' | grep t > /dev/null; then
436   have_echo=yes # Lucky!
437 fi
438 # Try the workarounds.
439 # Respawn using $CONFIG_SHELL if that is set and works.
440 if test -z "$have_echo" \
441    && test "X$1" != "X--no-reexec" \
442    && test -n "$CONFIG_SHELL" \
443    && test -f "$CONFIG_SHELL" \
444    && $CONFIG_SHELL -c 'echo '\t' | grep t > /dev/null'; then
445   exec $CONFIG_SHELL "$0" --no-reexec "$@"
446   exit 127
447 fi
448 # For bash: unset the shell option xpg_echo.
449 if test -z "$have_echo" \
450    && test -n "$BASH_VERSION" \
451    && (shopt -o xpg_echo; echo '\t' | grep t > /dev/null) 2>/dev/null; then
452   shopt -o xpg_echo
453   have_echo=yes
454 fi
455 # For zsh: turn sh-emulation on.
456 if test -z "$have_echo" \
457    && test -n "$ZSH_VERSION" \
458    && (emulate sh) >/dev/null 2>&1; then
459   emulate sh
460 fi
461 # For ksh: alias echo to a function that uses cat of a here document.
462 # The ksh manual page says:
463 #   "Aliasing is performed when scripts are read, not while they are executed.
464 #    Therefore, for an alias to take effect, the alias definition command has
465 #    to be executed before the command which references the alias is read."
466 # Because of this, we have to play strange tricks with have_echo, to ensure
467 # that the top-level statement containing the test start after the 'alias'
468 # command.
469 if test -z "$have_echo"; then
470 bsd_echo ()
471 {
472 cat <<EOF
473 $*
474 EOF
475 }
476 alias echo=bsd_echo 2>/dev/null
477 fi
478 if test -z "$have_echo" \
479    && echo '\t' | grep t > /dev/null; then
480   have_echo=yes
481 fi
482 if test -z "$have_echo"; then
483   unalias echo 2>/dev/null
484 fi
485 # For Solaris /bin/sh and OSF/1 /bin/sh: respawn using /bin/ksh.
486 if test -z "$have_echo" \
487    && test "X$1" != "X--no-reexec" \
488    && test -f /bin/ksh; then
489   exec /bin/ksh "$0" --no-reexec "$@"
490   exit 127
491 fi
492 # Otherwise: respawn using /bin/sh.
493 if test -z "$have_echo" \
494    && test "X$1" != "X--no-reexec" \
495    && test -f /bin/sh; then
496   exec /bin/sh "$0" --no-reexec "$@"
497   exit 127
498 fi
499 if test -z "$have_echo"; then
500   func_fatal_error "Shell does not support 'echo' correctly. Please install GNU bash and set the environment variable CONFIG_SHELL to point to it."
501 fi
502 if echo '\t' | grep t > /dev/null; then
503   : # Works fine now.
504 else
505   func_fatal_error "Shell does not support 'echo' correctly. Workaround does not work. Please report this as a bug to bug-gnulib@gnu.org."
506 fi
507 if test "X$1" = "X--no-reexec"; then
508   shift
509 fi
510
511 # Command-line option processing.
512 # Removes the OPTIONS from the arguments. Sets the variables:
513 # - mode            list or import or create-testdir or create-megatestdir
514 # - destdir         from --dir
515 # - local_gnulib_dir  from --local-dir
516 # - libname, supplied_libname  from --lib
517 # - sourcebase      from --source-base
518 # - m4base          from --m4-base
519 # - docbase         from --doc-base
520 # - testsbase       from --tests-base
521 # - auxdir          from --aux-dir
522 # - inctests        true if --with-tests was given, blank otherwise
523 # - avoidlist       list of modules to avoid, from --avoid
524 # - lgpl            true if --lgpl was given, blank otherwise
525 # - makefile_name   from --makefile-name
526 # - libtool         true if --libtool was given, false if --no-libtool was
527 #                   given, blank otherwise
528 # - macro_prefix    from --macro-prefix
529 # - autoconf_minversion  minimum supported autoconf version
530 # - do_changelog    false if --no-changelog was given, : otherwise
531 # - doit            : if actions shall be executed, false if only to be printed
532 # - symbolic        true if --symlink was given, blank otherwise
533 # - lsymbolic       true if --local-symlink was given, blank otherwise
534 {
535   mode=
536   destdir=
537   local_gnulib_dir=
538   libname=libgnu
539   supplied_libname=
540   sourcebase=
541   m4base=
542   docbase=
543   testsbase=
544   auxdir=
545   inctests=
546   avoidlist=
547   lgpl=
548   makefile_name=
549   libtool=
550   macro_prefix=
551   do_changelog=:
552   doit=:
553   symbolic=
554   lsymbolic=
555
556   supplied_opts="$@"
557
558   while test $# -gt 0; do
559     case "$1" in
560       --list | --lis )
561         mode=list
562         shift ;;
563       --import | --impor | --impo | --imp | --im | --i )
564         mode=import
565         shift ;;
566       --update | --updat | --upda | --upd | --up | --u )
567         mode=update
568         shift ;;
569       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
570         mode=create-testdir
571         shift ;;
572       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
573         mode=create-megatestdir
574         shift ;;
575       --test | --tes | --te | --t )
576         mode=test
577         shift ;;
578       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
579         mode=megatest
580         shift ;;
581       --extract-* )
582         mode=`echo "X$1" | sed -e 's/^X--//'`
583         shift ;;
584       --dir )
585         shift
586         if test $# = 0; then
587           func_fatal_error "missing argument for --dir"
588         fi
589         destdir=$1
590         shift ;;
591       --dir=* )
592         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
593         shift ;;
594       --local-dir )
595         shift
596         if test $# = 0; then
597           func_fatal_error "missing argument for --local-dir"
598         fi
599         local_gnulib_dir=$1
600         shift ;;
601       --local-dir=* )
602         local_gnulib_dir=`echo "X$1" | sed -e 's/^X--local-dir=//'`
603         shift ;;
604       --lib )
605         shift
606         if test $# = 0; then
607           func_fatal_error "missing argument for --lib"
608         fi
609         libname=$1
610         supplied_libname=true
611         shift ;;
612       --lib=* )
613         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
614         supplied_libname=true
615         shift ;;
616       --source-base )
617         shift
618         if test $# = 0; then
619           func_fatal_error "missing argument for --source-base"
620         fi
621         sourcebase=$1
622         shift ;;
623       --source-base=* )
624         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
625         shift ;;
626       --m4-base )
627         shift
628         if test $# = 0; then
629           func_fatal_error "missing argument for --m4-base"
630         fi
631         m4base=$1
632         shift ;;
633       --m4-base=* )
634         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
635         shift ;;
636       --doc-base )
637         shift
638         if test $# = 0; then
639           func_fatal_error "missing argument for --doc-base"
640         fi
641         docbase=$1
642         shift ;;
643       --doc-base=* )
644         docbase=`echo "X$1" | sed -e 's/^X--doc-base=//'`
645         shift ;;
646       --tests-base )
647         shift
648         if test $# = 0; then
649           func_fatal_error "missing argument for --tests-base"
650         fi
651         testsbase=$1
652         shift ;;
653       --tests-base=* )
654         testsbase=`echo "X$1" | sed -e 's/^X--tests-base=//'`
655         shift ;;
656       --aux-dir )
657         shift
658         if test $# = 0; then
659           func_fatal_error "missing argument for --aux-dir"
660         fi
661         auxdir=$1
662         shift ;;
663       --aux-dir=* )
664         auxdir=`echo "X$1" | sed -e 's/^X--aux-dir=//'`
665         shift ;;
666       --with-tests )
667         inctests=true
668         shift ;;
669       --avoid )
670         shift
671         if test $# = 0; then
672           func_fatal_error "missing argument for --avoid"
673         fi
674         func_append avoidlist " $1"
675         shift ;;
676       --avoid=* )
677         arg=`echo "X$1" | sed -e 's/^X--avoid=//'`
678         func_append avoidlist " $arg"
679         shift ;;
680       --lgpl )
681         lgpl=true
682         shift ;;
683       --makefile-name )
684         shift
685         if test $# = 0; then
686           func_fatal_error "missing argument for --makefile-name"
687         fi
688         makefile_name="$1"
689         shift ;;
690       --makefile-name=* )
691         makefile_name=`echo "X$1" | sed -e 's/^X--makefile-name=//'`
692         shift ;;
693       --libtool )
694         libtool=true
695         shift ;;
696       --no-libtool )
697         libtool=false
698         shift ;;
699       --macro-prefix )
700         shift
701         if test $# = 0; then
702           func_fatal_error "missing argument for --macro-prefix"
703         fi
704         macro_prefix="$1"
705         shift ;;
706       --macro-prefix=* )
707         macro_prefix=`echo "X$1" | sed -e 's/^X--macro-prefix=//'`
708         shift ;;
709       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
710         do_changelog=false
711         shift ;;
712       --dry-run )
713         doit=false
714         shift ;;
715       -s | --symbolic | --symboli | --symbol | --symbo | --symb | --symlink | --symlin | --symli | --syml | --sym | --sy )
716         symbolic=true
717         shift ;;
718       --local-symlink | --local-symlin | --local-symli | --local-syml | --local-sym | --local-sy | --local-s )
719         lsymbolic=true
720         shift ;;
721       --help | --hel | --he | --h )
722         func_usage
723         func_exit $? ;;
724       --version | --versio | --versi | --vers | --ver | --ve | --v )
725         func_version
726         func_exit $? ;;
727       -- )
728         # Stop option processing
729         shift
730         break ;;
731       -* )
732         echo "gnulib-tool: unknown option $1" 1>&2
733         echo "Try 'gnulib-tool --help' for more information." 1>&2
734         func_exit 1 ;;
735       * )
736         break ;;
737     esac
738   done
739
740   if test "$mode" = update; then
741     if test $# != 0; then
742       echo "gnulib-tool: too many arguments in 'update' mode" 1>&2
743       echo "Try 'gnulib-tool --help' for more information." 1>&2
744       echo "If you really want to modify the gnulib configuration of your project," 1>&2
745       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
746       func_exit 1
747     fi
748     if test -n "$local_gnulib_dir" || test -n "$supplied_libname" \
749        || test -n "$sourcebase" || test -n "$m4base" \
750        || test -n "$docbase" || test -n "$testsbase" || test -n "$auxdir" \
751        || test -n "$inctests" || test -n "$avoidlist" || test -n "$lgpl" \
752        || test -n "$makefile_name" || test -n "$macro_prefix"; then
753       echo "gnulib-tool: invalid options for 'update' mode" 1>&2
754       echo "Try 'gnulib-tool --help' for more information." 1>&2
755       echo "If you really want to modify the gnulib configuration of your project," 1>&2
756       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
757       func_exit 1
758     fi
759     do_changelog=false
760   fi
761
762   # Determine the minimum supported autoconf version from the project's
763   # configure.ac.
764   DEFAULT_AUTOCONF_MINVERSION="2.59"
765   autoconf_minversion=
766   configure_ac=
767   if { test "$mode" = import || test "$mode" = update; } && test -n "$destdir"; then
768     if test -f "$destdir"/configure.ac; then
769       configure_ac="$destdir/configure.ac"
770     else
771       if test -f "$destdir"/configure.in; then
772         configure_ac="$destdir/configure.in"
773       fi
774     fi
775   else
776     if test -f configure.ac; then
777       configure_ac="configure.ac"
778     else
779       if test -f configure.in; then
780         configure_ac="configure.in"
781       fi
782     fi
783   fi
784   if test -n "$configure_ac"; then
785     # Use sed, not autoconf --trace, to look for the AC_PREREQ invocation,
786     # because when some m4 files are omitted from a CVS repository,
787     # "autoconf --trace=AC_PREREQ" fails with an error message like this:
788     #   m4: aclocal.m4:851: Cannot open m4/absolute-header.m4: No such file or directory
789     #   autom4te: m4 failed with exit status: 1
790     prereqs=
791     my_sed_traces='
792       s,#.*$,,
793       s,^dnl .*$,,
794       s, dnl .*$,,
795       /AC_PREREQ/ {
796         s,^.*AC_PREREQ([[ ]*\([^])]*\).*$,\1,p
797       }'
798     prereqs=`sed -n -e "$my_sed_traces" < "$configure_ac"`
799     if test -n "$prereqs"; then
800       autoconf_minversion=`
801         for version in $prereqs; do echo $version; done |
802         LC_ALL=C sort -nru | sed 1q
803       `
804     fi
805   fi
806   if test -z "$autoconf_minversion"; then
807     autoconf_minversion=$DEFAULT_AUTOCONF_MINVERSION
808   fi
809   case "$autoconf_minversion" in
810     1.* | 2.[0-4]* | 2.5[0-8]*)
811       func_fatal_error "minimum supported autoconf version is 2.59. Try adding AC_PREREQ([$DEFAULT_AUTOCONF_MINVERSION]) to your configure.ac." ;;
812   esac
813
814   # Remove trailing slashes from the directory names. This is necessary for
815   # m4base (to avoid an error in func_import) and optional for the others.
816   sed_trimtrailingslashes='s,\([^/]\)//*$,\1,'
817   case "$local_gnulib_dir" in
818     */ ) local_gnulib_dir=`echo "$local_gnulib_dir" | sed -e "$sed_trimtrailingslashes"` ;;
819   esac
820   case "$sourcebase" in
821     */ ) sourcebase=`echo "$sourcebase" | sed -e "$sed_trimtrailingslashes"` ;;
822   esac
823   case "$m4base" in
824     */ ) m4base=`echo "$m4base" | sed -e "$sed_trimtrailingslashes"` ;;
825   esac
826   case "$docbase" in
827     */ ) docbase=`echo "$docbase" | sed -e "$sed_trimtrailingslashes"` ;;
828   esac
829   case "$testsbase" in
830     */ ) testsbase=`echo "$testsbase" | sed -e "$sed_trimtrailingslashes"` ;;
831   esac
832   case "$auxdir" in
833     */ ) auxdir=`echo "$auxdir" | sed -e "$sed_trimtrailingslashes"` ;;
834   esac
835 }
836
837 case "$0" in
838   /*) self_abspathname="$0" ;;
839   */*) self_abspathname=`pwd`/"$0" ;;
840   *)
841   
842   self_abspathname=
843   save_IFS=$IFS
844   IFS=:
845   for d in $PATH; do
846     IFS=$save_IFS
847     test -z "$d" && d=.
848     if test -x "$d/$0" && test ! -d "$d/$0"; then
849       self_abspathname=$d/$0
850       break
851     fi
852   done
853   IFS=$save_IFS
854   if test -z "$self_abspathname"; then
855     func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
856   fi
857   ;;
858 esac
859 while test -h "$self_abspathname"; do
860   # Resolve symbolic link.
861   linkval=`func_readlink "$self_abspathname"`
862   test -n "$linkval" || break
863   case "$linkval" in
864     /* ) self_abspathname="$linkval" ;;
865     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
866   esac
867 done
868 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
869
870 func_tmpdir
871 trap 'exit_status=$?
872       if test "$signal" != 0; then
873         echo "caught signal $signal" >&2
874       fi
875       rm -rf "$tmp"
876       exit $exit_status' 0
877 for signal in 1 2 3 13 15; do
878   trap '{ signal='$signal'; func_exit 1; }' $signal
879 done
880 signal=0
881
882 # func_lookup_file file
883 # looks up a file in $local_gnulib_dir or $gnulib_dir, or combines it through
884 # 'patch'.
885 # Input:
886 # - local_gnulib_dir  from --local-dir
887 # Output:
888 # - lookedup_file   name of the merged (combined) file
889 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
890 func_lookup_file ()
891 {
892   lkfile="$1"
893   if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile"; then
894     lookedup_file="$local_gnulib_dir/$lkfile"
895     lookedup_tmp=
896   else
897     if test -f "$gnulib_dir/$lkfile"; then
898       if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile.diff"; then
899         lkbase=`echo "$lkfile" | sed -e 's,^.*/,,'`
900         rm -f "$tmp/$lkbase"
901         cp "$gnulib_dir/$lkfile" "$tmp/$lkbase"
902         patch -s "$tmp/$lkbase" < "$local_gnulib_dir/$lkfile.diff" \
903           || func_fatal_error "patch file $local_gnulib_dir/$lkfile.diff didn't apply cleanly"
904         lookedup_file="$tmp/$lkbase"
905         lookedup_tmp=true
906       else
907         lookedup_file="$gnulib_dir/$lkfile"
908         lookedup_tmp=
909       fi
910     else
911       func_fatal_error "file $gnulib_dir/$lkfile not found"
912     fi
913   fi
914 }
915
916 # func_all_modules
917 # Input:
918 # - local_gnulib_dir  from --local-dir
919 func_all_modules ()
920 {
921   # Filter out metainformation files like README, which are not modules.
922   # Filter out unit test modules; they can be retrieved through
923   # --extract-tests-module if desired.
924   {
925     (cd "$gnulib_dir" && find modules -type f -print | sed -e 's,^modules/,,')
926     if test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules"; then
927       (cd "$local_gnulib_dir" && find modules -type f -print | sed -e 's,^modules/,,' -e 's,\.diff$,,')
928     fi
929   } \
930       | sed -e '/^CVS\//d' -e '/\/CVS\//d' \
931             -e '/^ChangeLog$/d' -e '/\/ChangeLog$/d' \
932             -e '/^COPYING$/d' -e '/\/COPYING$/d' \
933             -e '/^README$/d' -e '/\/README$/d' \
934             -e '/^TEMPLATE$/d' \
935             -e '/^TEMPLATE-EXTENDED$/d' \
936             -e '/^TEMPLATE-TESTS$/d' \
937             -e '/^\..*/d' \
938             -e '/~$/d' \
939       | sed -e '/-tests$/d' \
940       | LC_ALL=C sort -u
941 }
942
943 # func_verify_module
944 # verifies a module name
945 # Input:
946 # - local_gnulib_dir  from --local-dir
947 # - module          module name argument
948 func_verify_module ()
949 {
950   if { test -f "$gnulib_dir/modules/$module" \
951        || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
952             && test -f "$local_gnulib_dir/modules/$module"; }; } \
953      && test "CVS" != "$module" \
954      && test "ChangeLog" != "$module" \
955      && test "COPYING" != "$module" \
956      && test "README" != "$module" \
957      && test "TEMPLATE" != "$module" \
958      && test "TEMPLATE-EXTENDED" != "$module" \
959      && test "TEMPLATE-TESTS" != "$module"; then
960     # OK, $module is a correct module name.
961     # Verify that building the module description with 'patch' succeeds.
962     func_lookup_file "modules/$module"
963   else
964     echo "gnulib-tool: module $module doesn't exist" 1>&2
965     module=
966   fi
967 }
968
969 # func_verify_nontests_module
970 # verifies a module name, excluding tests modules
971 # Input:
972 # - local_gnulib_dir  from --local-dir
973 # - module          module name argument
974 func_verify_nontests_module ()
975 {
976   case "$module" in
977     *-tests ) module= ;;
978     * ) func_verify_module ;;
979   esac
980 }
981
982 # func_verify_tests_module
983 # verifies a module name, considering only tests modules
984 # Input:
985 # - local_gnulib_dir  from --local-dir
986 # - module          module name argument
987 func_verify_tests_module ()
988 {
989   case "$module" in
990     *-tests ) func_verify_module ;;
991     * ) module= ;;
992   esac
993 }
994
995 sed_extract_prog=':[     ]*$/ {
996   :a
997     n
998     s/^Description:[     ]*$//
999     s/^Files:[   ]*$//
1000     s/^Depends-on:[      ]*$//
1001     s/^configure\.ac-early:[     ]*$//
1002     s/^configure\.ac:[   ]*$//
1003     s/^Makefile\.am:[    ]*$//
1004     s/^Include:[         ]*$//
1005     s/^Link:[    ]*$//
1006     s/^License:[         ]*$//
1007     s/^Maintainer:[      ]*$//
1008     tb
1009     p
1010     ba
1011   :b
1012 }'
1013
1014 # func_get_description module
1015 # Input:
1016 # - local_gnulib_dir  from --local-dir
1017 func_get_description ()
1018 {
1019   func_lookup_file "modules/$1"
1020   sed -n -e "/^Description$sed_extract_prog" < "$lookedup_file"
1021 }
1022
1023 # func_get_filelist module
1024 # Input:
1025 # - local_gnulib_dir  from --local-dir
1026 func_get_filelist ()
1027 {
1028   func_lookup_file "modules/$1"
1029   sed -n -e "/^Files$sed_extract_prog" < "$lookedup_file"
1030   echo m4/gnulib-common.m4
1031   case "$autoconf_minversion" in
1032     2.59)
1033       #echo m4/onceonly.m4
1034       echo m4/onceonly_2_57.m4
1035       ;;
1036   esac
1037 }
1038
1039 # func_get_dependencies module
1040 # Input:
1041 # - local_gnulib_dir  from --local-dir
1042 func_get_dependencies ()
1043 {
1044   # ${module}-tests always implicitly depends on ${module}.
1045   echo "$1" | sed -n -e 's/-tests//p'
1046   # Then the explicit dependencies listed in the module description.
1047   func_lookup_file "modules/$1"
1048   sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
1049 }
1050
1051 # func_get_autoconf_early_snippet module
1052 # Input:
1053 # - local_gnulib_dir  from --local-dir
1054 func_get_autoconf_early_snippet ()
1055 {
1056   func_lookup_file "modules/$1"
1057   sed -n -e "/^configure\.ac-early$sed_extract_prog" < "$lookedup_file"
1058 }
1059
1060 # func_get_autoconf_snippet module
1061 # Input:
1062 # - local_gnulib_dir  from --local-dir
1063 func_get_autoconf_snippet ()
1064 {
1065   func_lookup_file "modules/$1"
1066   sed -n -e "/^configure\.ac$sed_extract_prog" < "$lookedup_file"
1067 }
1068
1069 # func_get_automake_snippet module
1070 # Input:
1071 # - local_gnulib_dir  from --local-dir
1072 func_get_automake_snippet ()
1073 {
1074   func_lookup_file "modules/$1"
1075   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file"
1076   case "$1" in
1077     *-tests)
1078       # *-tests module live in tests/, not lib/.
1079       ;;
1080     *)
1081       # Synthesize an EXTRA_DIST augmentation.
1082       sed_combine_lines='/\\$/{
1083         :a
1084         N
1085         s/\\\n/ /
1086         s/\\$/\\/
1087         ta
1088       }'
1089       sed_extract_mentioned_files='s/^lib_SOURCES[       ]*+=[   ]*//p'
1090       already_mentioned_files=` \
1091         sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file" \
1092         | sed -e "$sed_combine_lines" \
1093         | sed -n -e "$sed_extract_mentioned_files" | sed -e 's/#.*//'`
1094       all_files=`func_get_filelist $1`
1095       lib_files=`for f in $all_files; do \
1096                    case $f in \
1097                      lib/*) echo $f ;; \
1098                    esac; \
1099                  done | sed -e 's,^lib/,,'`
1100       # Remove $already_mentioned_files from $lib_files.
1101       echo "$lib_files" | LC_ALL=C sort -u > "$tmp"/lib-files
1102       extra_files=`for f in $already_mentioned_files; do echo $f; done \
1103                    | LC_ALL=C sort -u | LC_ALL=C join -v 2 - "$tmp"/lib-files`
1104       if test -n "$extra_files"; then
1105         echo "EXTRA_DIST +=" $extra_files
1106         echo
1107       fi
1108       # Synthesize also an EXTRA_lib_SOURCES augmentation.
1109       # This is necessary so that automake can generate the right list of
1110       # dependency rules.
1111       # A possible approach would be to use autom4te --trace of the redefined
1112       # AC_LIBOBJ and AC_REPLACE_FUNCS macros when creating the Makefile.am
1113       # (use autom4te --trace, not just grep, so that AC_LIBOBJ invocations
1114       # inside autoconf's built-in macros are not missed).
1115       # But it's simpler and more robust to do it here, based on the file list.
1116       # If some .c file exists and is not used with AC_LIBOBJ - for example,
1117       # a .c file is preprocessed into another .c file for BUILT_SOURCES -,
1118       # automake will generate a useless dependency; this is harmless.
1119       case "$1" in
1120         relocatable-prog-wrapper) ;;
1121         *)
1122           sed_extract_c_files='/\.c$/p'
1123           extra_files=`echo "$extra_files" | sed -n -e "$sed_extract_c_files"`
1124           if test -n "$extra_files"; then
1125             echo "EXTRA_lib_SOURCES +=" $extra_files
1126             echo
1127           fi
1128           ;;
1129       esac
1130       ;;
1131   esac
1132 }
1133
1134 # func_get_include_directive module
1135 # Input:
1136 # - local_gnulib_dir  from --local-dir
1137 func_get_include_directive ()
1138 {
1139   func_lookup_file "modules/$1"
1140   sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
1141   sed -e 's/^\(["<]\)/#include \1/'
1142 }
1143
1144 # func_get_link_directive module
1145 # Input:
1146 # - local_gnulib_dir  from --local-dir
1147 func_get_link_directive ()
1148 {
1149   func_lookup_file "modules/$1"
1150   sed -n -e "/^Link$sed_extract_prog" < "$lookedup_file"
1151 }
1152
1153 # func_get_license module
1154 # Input:
1155 # - local_gnulib_dir  from --local-dir
1156 func_get_license ()
1157 {
1158   func_lookup_file "modules/$1"
1159   { sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
1160     # The default is GPL.
1161     echo "GPL"
1162   } | sed -e 's,^ *$,,' | sed -e 1q
1163 }
1164
1165 # func_get_maintainer module
1166 # Input:
1167 # - local_gnulib_dir  from --local-dir
1168 func_get_maintainer ()
1169 {
1170   func_lookup_file "modules/$1"
1171   sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
1172 }
1173
1174 # func_get_tests_module module
1175 # Input:
1176 # - local_gnulib_dir  from --local-dir
1177 func_get_tests_module ()
1178 {
1179   # The naming convention for tests modules is hardwired: ${module}-tests.
1180   if test -f "$gnulib_dir/modules/$1"-tests \
1181      || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
1182           && test -f "$local_gnulib_dir/modules/$1"-tests; }; then
1183     echo "$1"-tests
1184   fi
1185 }
1186
1187 # func_acceptable module
1188 # tests whether a module is acceptable.
1189 # Input:
1190 # - avoidlist       list of modules to avoid
1191 func_acceptable ()
1192 {
1193   for avoid in $avoidlist; do
1194     if test "$avoid" = "$1"; then
1195       return 1
1196     fi
1197   done
1198   return 0
1199 }
1200
1201 # func_modules_transitive_closure
1202 # Input:
1203 # - local_gnulib_dir  from --local-dir
1204 # - modules         list of specified modules
1205 # - inctests        true if tests should be included, blank otherwise
1206 # - avoidlist       list of modules to avoid
1207 # - tmp             pathname of a temporary directory
1208 # Output:
1209 # - modules         list of modules, including dependencies
1210 func_modules_transitive_closure ()
1211 {
1212   # In order to process every module only once (for speed), process an "input
1213   # list" of modules, producing an "output list" of modules. During each round,
1214   # more modules can be queued in the input list. Once a module on the input
1215   # list has been processed, it is added to the "handled list", so we can avoid
1216   # to process it again.
1217   handledmodules=
1218   inmodules="$modules"
1219   outmodules=
1220   while test -n "$inmodules"; do
1221     inmodules_this_round="$inmodules"
1222     inmodules=                    # Accumulator, queue for next round
1223     for module in $inmodules_this_round; do
1224       func_verify_module
1225       if test -n "$module"; then
1226         if func_acceptable $module; then
1227           func_append outmodules " $module"
1228           deps=`func_get_dependencies $module`
1229           # Duplicate dependencies are harmless, but Jim wants a warning.
1230           duplicated_deps=`echo "$deps" | LC_ALL=C sort | LC_ALL=C uniq -d`
1231           if test -n "$duplicated_deps"; then
1232             echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
1233           fi
1234           func_append inmodules " $deps"
1235           if test -n "$inctests"; then
1236             testsmodule=`func_get_tests_module $module`
1237             if test -n "$testsmodule"; then
1238               func_append inmodules " $testsmodule"
1239             fi
1240           fi
1241         fi
1242       fi
1243     done
1244     handledmodules=`for m in $handledmodules $inmodules_this_round; do echo $m; done | LC_ALL=C sort -u`
1245     # Remove $handledmodules from $inmodules.
1246     for m in $inmodules; do echo $m; done | LC_ALL=C sort -u > "$tmp"/queued-modules
1247     inmodules=`echo "$handledmodules" | LC_ALL=C join -v 2 - "$tmp"/queued-modules`
1248   done
1249   modules=`for m in $outmodules; do echo $m; done | LC_ALL=C sort -u`
1250   rm -f "$tmp"/queued-modules
1251 }
1252
1253 # func_modules_add_dummy
1254 # Input:
1255 # - local_gnulib_dir  from --local-dir
1256 # - modules         list of modules, including dependencies
1257 # Output:
1258 # - modules         list of modules, including 'dummy' if needed
1259 func_modules_add_dummy ()
1260 {
1261   have_lib_SOURCES=
1262   sed_remove_backslash_newline=':a
1263 /\\$/{
1264 s/\\$//
1265 N
1266 s/\n//
1267 ba
1268 }'
1269   for module in $modules; do
1270     func_verify_nontests_module
1271     if test -n "$module"; then
1272       # Extract the value of "lib_SOURCES += ...".
1273       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
1274         # Ignore .h files since they are not compiled.
1275         case "$file" in
1276           *.h) ;;
1277           *) have_lib_SOURCES=yes ;;
1278         esac
1279       done
1280     fi
1281   done
1282   # Add the dummy module, to make sure the library will be non-empty.
1283   if test -z "$have_lib_SOURCES"; then
1284     if func_acceptable "dummy"; then
1285       func_append modules " dummy"
1286     fi
1287   fi
1288 }
1289
1290 # func_modules_to_filelist
1291 # Input:
1292 # - local_gnulib_dir  from --local-dir
1293 # - modules         list of modules, including dependencies
1294 # Output:
1295 # - files           list of files
1296 func_modules_to_filelist ()
1297 {
1298   files=
1299   for module in $modules; do
1300     func_verify_module
1301     if test -n "$module"; then
1302       fs=`func_get_filelist $module`
1303       func_append files " $fs"
1304     fi
1305   done
1306   files=`for f in $files; do echo $f; done | LC_ALL=C sort -u`
1307 }
1308
1309 # func_emit_lib_Makefile_am
1310 # emits the contents of library makefile to standard output.
1311 # Input:
1312 # - local_gnulib_dir  from --local-dir
1313 # - modules         list of modules, including dependencies
1314 # - libname         library name
1315 # - auxdir          directory relative to destdir where to place build aux files
1316 # - makefile_name   from --makefile-name
1317 # - libtool         true if libtool will be used, false or blank otherwise
1318 # - macro_prefix    prefix of gl_LIBOBJS macros to use
1319 # - actioncmd       (optional) command that will reproduce this invocation
1320 # - for_test        true if creating a package for testing, false otherwise
1321 # Output:
1322 # - uses_subdirs    nonempty if object files in subdirs exist
1323 func_emit_lib_Makefile_am ()
1324 {
1325   # When creating an includable Makefile.am snippet, augment variables with
1326   # += instead of assigning them.
1327   if test -n "$makefile_name"; then
1328     assign='+='
1329   else
1330     assign='='
1331   fi
1332   if test "$libtool" = true; then
1333     libext=la
1334     perhapsLT=LT
1335     sed_eliminate_LDFLAGS=
1336   else
1337     libext=a
1338     perhapsLT=
1339     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
1340   fi
1341   if $for_test; then
1342     # When creating a package for testing: Attempt to provoke failures,
1343     # especially link errors, already during "make" rather than during
1344     # "make check", because "make check" is not possible in a cross-compiling
1345     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
1346     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
1347   else
1348     sed_transform_check_PROGRAMS=
1349   fi
1350   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
1351   echo "## Process this file with automake to produce Makefile.in."
1352   func_emit_copyright_notice
1353   if test -n "$actioncmd"; then
1354     echo "# Reproduce by: $actioncmd"
1355   fi
1356   echo
1357   uses_subdirs=
1358   {
1359     for module in $modules; do
1360       func_verify_nontests_module
1361       if test -n "$module"; then
1362         {
1363           func_get_automake_snippet "$module" |
1364             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1365                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' \
1366                 -e "$sed_eliminate_LDFLAGS" \
1367                 -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' \
1368                 -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1369                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' \
1370                 -e "$sed_transform_check_PROGRAMS"
1371           if test "$module" = 'alloca'; then
1372             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
1373             echo "${libname}_${libext}_DEPENDENCIES += @${perhapsLT}ALLOCA@"
1374           fi
1375         } > amsnippet.tmp
1376         # Skip the contents if its entirely empty.
1377         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1378           echo "## begin gnulib module $module"
1379           echo
1380           cat amsnippet.tmp
1381           echo "## end   gnulib module $module"
1382           echo
1383         fi
1384         rm -f amsnippet.tmp
1385         # Test whether there are some source files in subdirectories.
1386         for f in `func_get_filelist "$module"`; do
1387           case $f in
1388             lib/*/*.c) uses_subdirs=yes ;;
1389           esac
1390         done
1391       fi
1392     done
1393   } > allsnippets.tmp
1394   if test -z "$makefile_name"; then
1395     # If there are source files in subdirectories, prevent collision of the
1396     # object files (example: hash.c and libxml/hash.c).
1397     subdir_options=
1398     if test -n "$uses_subdirs"; then
1399       subdir_options=' subdir-objects'
1400     fi
1401     echo "AUTOMAKE_OPTIONS = 1.5 gnits${subdir_options}"
1402   fi
1403   echo
1404   if test -z "$makefile_name"; then
1405     echo "noinst_HEADERS ="
1406     echo "noinst_LIBRARIES ="
1407     echo "noinst_LTLIBRARIES ="
1408     # Automake versions < 1.9b create an empty pkgdatadir at installation time
1409     # if you specify pkgdata_DATA to empty. This is a workaround.
1410     if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1411       echo "pkgdata_DATA ="
1412     fi
1413     echo "EXTRA_DIST ="
1414     echo "BUILT_SOURCES ="
1415     echo "SUFFIXES ="
1416   fi
1417   echo "MOSTLYCLEANFILES $assign core *.stackdump"
1418   if test -z "$makefile_name"; then
1419     echo "MOSTLYCLEANDIRS ="
1420     echo "CLEANFILES ="
1421     echo "DISTCLEANFILES ="
1422     echo "MAINTAINERCLEANFILES ="
1423     echo
1424     echo "AM_CPPFLAGS ="
1425   fi
1426   echo
1427   if LC_ALL=C grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *= *$libname\\.$libext\$" allsnippets.tmp > /dev/null; then
1428     # One of the snippets already specifies an installation location for the
1429     # library. Don't confuse automake by saying it should not be installed.
1430     :
1431   else
1432     # By default, the generated library should not be installed.
1433     echo "noinst_${perhapsLT}LIBRARIES += $libname.$libext"
1434   fi
1435   echo
1436   echo "${libname}_${libext}_SOURCES ="
1437   # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
1438   # automake during its analyses looks for $(LIBOBJS), not for @LIBOBJS@.
1439   echo "${libname}_${libext}_LIBADD = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
1440   echo "${libname}_${libext}_DEPENDENCIES = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
1441   echo "EXTRA_${libname}_${libext}_SOURCES ="
1442   if test "$libtool" = true; then
1443     echo "${libname}_${libext}_LDFLAGS = \$(AM_LDFLAGS)"
1444   fi
1445   echo
1446   cat allsnippets.tmp \
1447     | sed -e 's|\$(top_srcdir)/build-aux/|$(top_srcdir)/'"$auxdir"'/|g'
1448   echo
1449   echo "mostlyclean-local: mostlyclean-generic"
1450   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
1451   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
1452   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1453   echo "          fi; \\"
1454   echo "        done"
1455   rm -f allsnippets.tmp
1456 }
1457
1458 # func_emit_tests_Makefile_am
1459 # emits the contents of tests makefile to standard output.
1460 # Input:
1461 # - local_gnulib_dir  from --local-dir
1462 # - modules         list of modules, including dependencies
1463 # - libname         library name
1464 # - makefile_name   from --makefile-name
1465 # - libtool         true if libtool will be used, false or blank otherwise
1466 # - sourcebase      relative directory containing lib source code
1467 # - m4base          relative directory containing autoconf macros
1468 # - testsbase       relative directory containing unit test code
1469 # - for_test        true if creating a package for testing, false otherwise
1470 func_emit_tests_Makefile_am ()
1471 {
1472   if test "$libtool" = true; then
1473     libext=la
1474   else
1475     libext=a
1476   fi
1477   if test "$libtool" = true; then
1478     libext=la
1479     sed_eliminate_LDFLAGS=
1480   else
1481     libext=a
1482     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
1483   fi
1484   if $for_test; then
1485     # When creating a package for testing: Attempt to provoke failures,
1486     # especially link errors, already during "make" rather than during
1487     # "make check", because "make check" is not possible in a cross-compiling
1488     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
1489     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
1490   else
1491     sed_transform_check_PROGRAMS=
1492   fi
1493   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
1494   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
1495   echo "## Process this file with automake to produce Makefile.in."
1496   func_emit_copyright_notice
1497   echo
1498   # Generate dependencies here, since it eases the debugging of test failures.
1499   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1500   echo
1501   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
1502   echo
1503   (
1504     for module in $modules; do
1505       func_verify_tests_module
1506       if test -n "$module"; then
1507         {
1508           func_get_automake_snippet "$module" |
1509             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1510                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' \
1511                 -e "$sed_eliminate_LDFLAGS" \
1512                 -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' \
1513                 -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1514                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' \
1515                 -e "$sed_transform_check_PROGRAMS"
1516         } > amsnippet.tmp
1517         # Skip the contents if its entirely empty.
1518         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1519           echo "## begin gnulib module $module"
1520           echo
1521           cat amsnippet.tmp
1522           echo "## end   gnulib module $module"
1523           echo
1524         fi
1525         rm -f amsnippet.tmp
1526       fi
1527     done
1528   ) > allsnippets.tmp
1529   # Nothing is being added to SUBDIRS; nevertheless the existence of this
1530   # variable is needed to avoid an error from automake:
1531   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
1532   echo "SUBDIRS ="
1533   echo "TESTS ="
1534   echo "TESTS_ENVIRONMENT ="
1535   echo "noinst_PROGRAMS ="
1536   if ! $for_test; then
1537     echo "check_PROGRAMS ="
1538   fi
1539   echo "noinst_HEADERS ="
1540   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1541     echo "pkgdata_DATA ="
1542   fi
1543   echo "EXTRA_DIST ="
1544   echo "BUILT_SOURCES ="
1545   echo "SUFFIXES ="
1546   echo "MOSTLYCLEANFILES = core *.stackdump"
1547   echo "MOSTLYCLEANDIRS ="
1548   echo "CLEANFILES ="
1549   echo "DISTCLEANFILES ="
1550   echo "MAINTAINERCLEANFILES ="
1551   echo
1552   echo "AM_CPPFLAGS = \\"
1553   echo "  -I. -I\$(srcdir) \\"
1554   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
1555   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
1556   echo
1557   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
1558   echo
1559   cat allsnippets.tmp
1560   echo "# Clean up after Solaris cc."
1561   echo "clean-local:"
1562   echo "        rm -rf SunWS_cache"
1563   echo
1564   echo "mostlyclean-local: mostlyclean-generic"
1565   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
1566   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
1567   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1568   echo "          fi; \\"
1569   echo "        done"
1570   rm -f allsnippets.tmp
1571 }
1572
1573 # func_emit_initmacro_start
1574 # emits the first few statements of the gl_INIT macro to standard output.
1575 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1576 func_emit_initmacro_start ()
1577 {
1578   # Overriding AC_LIBOBJ and AC_REPLACE_FUNCS has the effect of storing
1579   # platform-dependent object files in ${macro_prefix}_LIBOBJS instead of
1580   # LIBOBJS.  The purpose is to allow several gnulib instantiations under
1581   # a single configure.ac file.  (AC_CONFIG_LIBOBJ_DIR does not allow this
1582   # flexibility.)
1583   # Furthermore it avoids an automake error like this when a Makefile.am
1584   # that uses pieces of gnulib also uses $(LIBOBJ):
1585   #   automatically discovered file `error.c' should not be explicitly mentioned
1586   echo "  m4_pushdef([AC_LIBOBJ], m4_defn([${macro_prefix}_LIBOBJ]))"
1587   echo "  m4_pushdef([AC_REPLACE_FUNCS], m4_defn([${macro_prefix}_REPLACE_FUNCS]))"
1588   # Overriding AC_LIBSOURCES has the same purpose of avoiding the automake
1589   # error when a Makefile.am that uses pieces of gnulib also uses $(LIBOBJ):
1590   #   automatically discovered file `error.c' should not be explicitly mentioned
1591   # We let automake know about the files to be distributed through the
1592   # EXTRA_lib_SOURCES variable.
1593   echo "  m4_pushdef([AC_LIBSOURCES], m4_defn([${macro_prefix}_LIBSOURCES]))"
1594 }
1595
1596 # func_emit_initmacro_end
1597 # emits the last few statements of the gl_INIT macro to standard output.
1598 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1599 func_emit_initmacro_end ()
1600 {
1601   echo "  m4_popdef([AC_LIBSOURCES])"
1602   echo "  m4_popdef([AC_REPLACE_FUNCS])"
1603   echo "  m4_popdef([AC_LIBOBJ])"
1604   echo "  AC_CONFIG_COMMANDS_PRE(["
1605   echo "    ${macro_prefix}_libobjs="
1606   echo "    ${macro_prefix}_ltlibobjs="
1607   echo "    if test -n \"\$${macro_prefix}_LIBOBJS\"; then"
1608   echo "      # Remove the extension."
1609   echo "      sed_drop_objext='s/\\.o\$//;s/\\.obj\$//'"
1610   echo "      for i in \`for i in \$${macro_prefix}_LIBOBJS; do echo \"\$i\"; done | sed \"\$sed_drop_objext\" | sort | uniq\`; do"
1611   echo "        ${macro_prefix}_libobjs=\"\$${macro_prefix}_libobjs \$i.\$ac_objext\""
1612   echo "        ${macro_prefix}_ltlibobjs=\"\$${macro_prefix}_ltlibobjs \$i.lo\""
1613   echo "      done"
1614   echo "    fi"
1615   echo "    AC_SUBST([${macro_prefix}_LIBOBJS], [\$${macro_prefix}_libobjs])"
1616   echo "    AC_SUBST([${macro_prefix}_LTLIBOBJS], [\$${macro_prefix}_ltlibobjs])"
1617   echo "  ])"
1618 }
1619
1620 # func_emit_initmacro_done
1621 # emits a few statements after the gl_INIT macro to standard output.
1622 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1623 func_emit_initmacro_done ()
1624 {
1625   echo
1626   echo "# Like AC_LIBOBJ, except that the module name goes"
1627   echo "# into ${macro_prefix}_LIBOBJS instead of into LIBOBJS."
1628   echo "AC_DEFUN([${macro_prefix}_LIBOBJ],"
1629   echo "  [${macro_prefix}_LIBOBJS=\"\$${macro_prefix}_LIBOBJS \$1.\$ac_objext\"])"
1630   echo
1631   echo "# Like AC_REPLACE_FUNCS, except that the module name goes"
1632   echo "# into ${macro_prefix}_LIBOBJS instead of into LIBOBJS."
1633   echo "AC_DEFUN([${macro_prefix}_REPLACE_FUNCS],"
1634   echo "  [AC_CHECK_FUNCS([\$1], , [${macro_prefix}_LIBOBJ(\$ac_func)])])"
1635   echo
1636   echo "# Like AC_LIBSOURCES, except that it does nothing."
1637   echo "# We rely on EXTRA_lib..._SOURCES instead."
1638   echo "AC_DEFUN([${macro_prefix}_LIBSOURCES],"
1639   echo "  [])"
1640 }
1641
1642 # func_import modules
1643 # Uses also the variables
1644 # - destdir         target directory
1645 # - local_gnulib_dir  from --local-dir
1646 # - libname         library name
1647 # - sourcebase      directory relative to destdir where to place source code
1648 # - m4base          directory relative to destdir where to place *.m4 macros
1649 # - docbase         directory relative to destdir where to place doc files
1650 # - testsbase       directory relative to destdir where to place unit test code
1651 # - auxdir          directory relative to destdir where to place build aux files
1652 # - inctests        true if --with-tests was given, blank otherwise
1653 # - avoidlist       list of modules to avoid, from --avoid
1654 # - lgpl            true if library's license shall be LGPL, blank otherwise
1655 # - makefile_name   from --makefile-name
1656 # - libtool         true if --libtool was given, false if --no-libtool was
1657 #                   given, blank otherwise
1658 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
1659 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1660 # - autoconf_minversion  minimum supported autoconf version
1661 # - doit            : if actions shall be executed, false if only to be printed
1662 # - symbolic        true if files should be symlinked, copied otherwise
1663 # - lsymbolic       true if files from local_gnulib_dir should be symlinked,
1664 #                   copied otherwise
1665 func_import ()
1666 {
1667   # Get the cached settings.
1668   cached_local_gnulib_dir=
1669   cached_specified_modules=
1670   cached_avoidlist=
1671   cached_sourcebase=
1672   cached_m4base=
1673   cached_docbase=
1674   cached_testsbase=
1675   cached_inctests=
1676   cached_libname=
1677   cached_lgpl=
1678   cached_makefile_name=
1679   cached_libtool=
1680   cached_macro_prefix=
1681   cached_files=
1682   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1683     cached_libtool=false
1684     my_sed_traces='
1685       s,#.*$,,
1686       s,^dnl .*$,,
1687       s, dnl .*$,,
1688       /gl_LOCAL_DIR(/ {
1689         s,^.*gl_LOCAL_DIR([[ ]*\([^])]*\).*$,cached_local_gnulib_dir="\1",p
1690       }
1691       /gl_MODULES(/ {
1692         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
1693       }
1694       /gl_AVOID(/ {
1695         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
1696       }
1697       /gl_SOURCE_BASE(/ {
1698         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
1699       }
1700       /gl_M4_BASE(/ {
1701         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
1702       }
1703       /gl_DOC_BASE(/ {
1704         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
1705       }
1706       /gl_TESTS_BASE(/ {
1707         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
1708       }
1709       /gl_WITH_TESTS/ {
1710         s,^.*$,cached_inctests=true,p
1711       }
1712       /gl_LIB(/ {
1713         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
1714       }
1715       /gl_LGPL/ {
1716         s,^.*$,cached_lgpl=true,p
1717       }
1718       /gl_MAKEFILE_NAME(/ {
1719         s,^.*gl_MAKEFILE_NAME([[ ]*\([^])]*\).*$,cached_makefile_name="\1",p
1720       }
1721       /gl_LIBTOOL/ {
1722         s,^.*$,cached_libtool=true,p
1723       }
1724       /gl_MACRO_PREFIX(/ {
1725         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
1726       }'
1727     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
1728     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1729       my_sed_traces='
1730         s,#.*$,,
1731         s,^dnl .*$,,
1732         s, dnl .*$,,
1733         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
1734           s,^.*$,cached_files=",p
1735           n
1736           ta
1737           :a
1738           s,^\]).*$,",
1739           tb
1740           p
1741           n
1742           ba
1743           :b
1744           p
1745         }'
1746       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1747     fi
1748   fi
1749
1750   # Merge the cached settings with the specified ones.
1751   # The m4base must be the same as expected from the pathname.
1752   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1753     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1754   fi
1755   # The local_gnulib_dir defaults to the cached one. Recall that the cached one
1756   # is relative to $destdir, whereas the one we use is relative to . or absolute.
1757   if test -z "$local_gnulib_dir"; then
1758     if test -n "$cached_local_gnulib_dir"; then
1759       case "$destdir" in
1760         /*)
1761           local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1762         *)
1763           case "$cached_local_gnulib_dir" in
1764             /*)
1765               local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1766             *)
1767               func_relconcat "$destdir" "$cached_local_gnulib_dir"
1768               local_gnulib_dir="$relconcat" ;;
1769           esac ;;
1770       esac
1771     fi
1772   fi
1773   # Append the cached and the specified module names. So that
1774   # "gnulib-tool --import foo" means to add the module foo.
1775   specified_modules="$cached_specified_modules $1"
1776   # Append the cached and the specified avoidlist. This is probably better
1777   # than dropping the cached one when --avoid is specified at least once.
1778   avoidlist=`echo $cached_avoidlist $avoidlist`
1779   # The sourcebase defaults to the cached one.
1780   if test -z "$sourcebase"; then
1781     sourcebase="$cached_sourcebase"
1782     if test -z "$sourcebase"; then
1783       func_fatal_error "missing --source-base option"
1784     fi
1785   fi
1786   # The docbase defaults to the cached one.
1787   if test -z "$docbase"; then
1788     docbase="$cached_docbase"
1789     if test -z "$docbase"; then
1790       func_fatal_error "missing --doc-base option. --doc-base has been introduced on 2006-07-11; if your last invocation of 'gnulib-tool --import' is before that date, you need to run 'gnulib-tool --import' once, with a --doc-base option."
1791     fi
1792   fi
1793   # The testsbase defaults to the cached one.
1794   if test -z "$testsbase"; then
1795     testsbase="$cached_testsbase"
1796     if test -z "$testsbase"; then
1797       func_fatal_error "missing --tests-base option"
1798     fi
1799   fi
1800   # Require the tests if specified either way.
1801   if test -z "$inctests"; then
1802     inctests="$cached_inctests"
1803   fi
1804   # The libname defaults to the cached one.
1805   if test -z "$supplied_libname"; then
1806     libname="$cached_libname"
1807     if test -z "$libname"; then
1808       func_fatal_error "missing --lib option"
1809     fi
1810   fi
1811   # Require LGPL if specified either way.
1812   if test -z "$lgpl"; then
1813     lgpl="$cached_lgpl"
1814   fi
1815   # The makefile_name defaults to the cached one.
1816   if test -z "$makefile_name"; then
1817     makefile_name="$cached_makefile_name"
1818   fi
1819   # Use libtool if specified either way, or if guessed.
1820   if test -z "$libtool"; then
1821     if test -n "$cached_m4base"; then
1822       libtool="$cached_libtool"
1823     else
1824       libtool="$guessed_libtool"
1825     fi
1826   fi
1827   # The macro_prefix defaults to the cached one.
1828   if test -z "$macro_prefix"; then
1829     macro_prefix="$cached_macro_prefix"
1830     if test -z "$macro_prefix"; then
1831       func_fatal_error "missing --macro-prefix option"
1832     fi
1833   fi
1834
1835   # Canonicalize the list of specified modules.
1836   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort -u`
1837
1838   # Determine final module list.
1839   modules="$specified_modules"
1840   func_modules_transitive_closure
1841   echo "Module list with included dependencies:"
1842   echo "$modules" | sed -e 's/^/  /'
1843
1844   # Add the dummy module if needed.
1845   func_modules_add_dummy
1846
1847   # If --lgpl, verify that the licenses of modules are compatible.
1848   if test -n "$lgpl"; then
1849     for module in $modules; do
1850       license=`func_get_license $module`
1851       case $license in
1852         LGPL | 'GPLed build tool') ;;
1853         'public domain' | 'unlimited' | 'unmodifiable license text') ;;
1854         *) func_fatal_error "incompatible license on module $module: $license" ;;
1855       esac
1856     done
1857   fi
1858
1859   # Determine script to apply to imported library files.
1860   sed_transform_lib_file=
1861   for module in $modules; do
1862     if test $module = config-h; then
1863       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
1864       sed_transform_lib_file=$sed_transform_lib_file'
1865         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
1866       '
1867       break
1868     fi
1869   done
1870   if test -n "$lgpl"; then
1871     # Update license.
1872     sed_transform_lib_file=$sed_transform_lib_file'
1873       s/GNU General/GNU Lesser General/g
1874       s/version 2\([ ,]\)/version 2.1\1/g
1875     '
1876   fi
1877
1878   # Determine final file list.
1879   func_modules_to_filelist
1880   echo "File list:"
1881   echo "$files" | sed -e 's/^/  /'
1882
1883   test -n "$files" \
1884     || func_fatal_error "refusing to do nothing"
1885
1886   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1887   new_files="$files m4/gnulib-tool.m4"
1888   old_files="$cached_files"
1889   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1890     func_append old_files " m4/gnulib-tool.m4"
1891   fi
1892
1893   sed_rewrite_old_files="\
1894     s,^build-aux/,$auxdir/,
1895     s,^doc/,$cached_docbase/,
1896     s,^lib/,$cached_sourcebase/,
1897     s,^m4/,$cached_m4base/,
1898     s,^tests/,$cached_testsbase/,"
1899   sed_rewrite_new_files="\
1900     s,^build-aux/,$auxdir/,
1901     s,^doc/,$docbase/,
1902     s,^lib/,$sourcebase/,
1903     s,^m4/,$m4base/,
1904     s,^tests/,$testsbase/,"
1905
1906   # Create directories.
1907   { echo "$sourcebase"
1908     echo "$m4base"
1909     docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1910     if test -n "$docfiles"; then
1911       echo "$docbase"
1912     fi
1913     if test -n "$inctests"; then
1914       echo "$testsbase"
1915     fi
1916     echo "$auxdir"
1917     for f in $files; do echo $f; done \
1918       | sed -e "$sed_rewrite_new_files" \
1919       | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
1920       | LC_ALL=C sort -u
1921   } > "$tmp"/dirs
1922   { # Rearrange file descriptors. Needed because "while ... done < ..."
1923     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1924     exec 5<&0 < "$tmp"/dirs
1925     while read d; do
1926       if test ! -d "$destdir/$d"; then
1927         if $doit; then
1928           echo "Creating directory $destdir/$d"
1929           mkdir -p "$destdir/$d" || func_fatal_error "failed"
1930         else
1931           echo "Create directory $destdir/$d"
1932         fi
1933       fi
1934     done
1935     exec 0<&5 5<&-
1936   }
1937
1938   # func_dest_tmpfilename file
1939   # determines the name of a temporary file (file is relative to destdir).
1940   # Sets variable:
1941   #   - tmpfile       absolute filename of the temporary file
1942   func_dest_tmpfilename ()
1943   {
1944     if $doit; then
1945       # Put the new contents of $file in a file in the same directory (needed
1946       # to guarantee that an 'mv' to "$destdir/$file" works).
1947       tmpfile="$destdir/$1.tmp"
1948     else
1949       # Put the new contents of $file in a file in a temporary directory
1950       # (because the directory of "$file" might not exist).
1951       tmpfile="$tmp"/`basename "$1"`.tmp
1952     fi
1953   }
1954
1955   # Copy files or make symbolic links. Remove obsolete files.
1956   added_files=''
1957   removed_files=''
1958   delimiter='   '
1959   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1960   # representing the files according to the last gnulib-tool invocation.
1961   for f in $old_files; do echo $f; done \
1962     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_old_files" \
1963     | LC_ALL=C sort \
1964     > "$tmp"/old-files
1965   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1966   # representing the files after this gnulib-tool invocation.
1967   for f in $new_files; do echo $f; done \
1968     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" \
1969     | LC_ALL=C sort \
1970     > "$tmp"/new-files
1971   # First the files that are in old-files, but not in new-files:
1972   sed_take_first_column='s,'"$delimiter"'.*,,'
1973   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1974     # Remove the file. Do nothing if the user already removed it.
1975     if test -f "$destdir/$g"; then
1976       if $doit; then
1977         echo "Removing file $g (backup in ${g}~)"
1978         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1979       else
1980         echo "Remove file $g (backup in ${g}~)"
1981       fi
1982       func_append removed_files "$g$nl"
1983     fi
1984   done
1985   # func_add_or_update handles a file that ought to be present afterwards.
1986   # Uses parameters
1987   # - f             the original file name
1988   # - g             the rewritten file name
1989   # - already_present  nonempty if the file already exists, empty otherwise
1990   func_add_or_update ()
1991   {
1992     func_dest_tmpfilename "$g"
1993     func_lookup_file "$f"
1994     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
1995     if test -n "$sed_transform_lib_file"; then
1996       case "$f" in
1997         lib/*)
1998           sed -e "$sed_transform_lib_file" \
1999             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
2000           ;;
2001       esac
2002     fi
2003     if test -f "$destdir/$g"; then
2004       # The file already exists.
2005       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
2006         : # The file has not changed.
2007       else
2008         # Replace the file.
2009         if $doit; then
2010           if test -n "$already_present"; then
2011             echo "Updating file $g (backup in ${g}~)"
2012           else
2013             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
2014           fi
2015           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
2016           if { test -n "$symbolic" \
2017                || { test -n "$lsymbolic" \
2018                     && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
2019              && test -z "$lookedup_tmp" \
2020              && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
2021             func_ln_if_changed "$lookedup_file" "$destdir/$g"
2022           else
2023             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
2024           fi
2025         else
2026           if test -n "$already_present"; then
2027             echo "Update file $g (backup in ${g}~)"
2028           else
2029             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
2030           fi
2031         fi
2032       fi
2033     else
2034       # Install the file.
2035       # Don't protest if the file should be there but isn't: it happens
2036       # frequently that developers don't put autogenerated files into CVS.
2037       if $doit; then
2038         echo "Copying file $g"
2039         if { test -n "$symbolic" \
2040              || { test -n "$lsymbolic" \
2041                   && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
2042            && test -z "$lookedup_tmp" \
2043            && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
2044           func_ln_if_changed "$lookedup_file" "$destdir/$g"
2045         else
2046           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
2047         fi
2048       else
2049         echo "Copy file $g"
2050       fi
2051       func_append added_files "$g$nl"
2052     fi
2053     rm -f "$tmpfile"
2054   }
2055   # Then the files that are in new-files, but not in old-files:
2056   sed_take_last_column='s,^.*'"$delimiter"',,'
2057   already_present=
2058   LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files \
2059     | sed -e "$sed_take_last_column" \
2060     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/added-files
2061   { # Rearrange file descriptors. Needed because "while ... done < ..."
2062     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2063     exec 5<&0 < "$tmp"/added-files
2064     while read g f; do
2065       func_add_or_update
2066     done
2067     exec 0<&5 5<&-
2068   }
2069   # Then the files that are in new-files and in old-files:
2070   already_present=true
2071   LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files \
2072     | sed -e "$sed_take_last_column" \
2073     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/kept-files
2074   { # Rearrange file descriptors. Needed because "while ... done < ..."
2075     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2076     exec 5<&0 < "$tmp"/kept-files
2077     while read g f; do
2078       func_add_or_update
2079     done
2080     exec 0<&5 5<&-
2081   }
2082
2083   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
2084   actioncmd="gnulib-tool --import"
2085   func_append actioncmd " --dir=$destdir"
2086   if test -n "$local_gnulib_dir"; then
2087     func_append actioncmd " --local-dir=$local_gnulib_dir"
2088   fi
2089   func_append actioncmd " --lib=$libname"
2090   func_append actioncmd " --source-base=$sourcebase"
2091   func_append actioncmd " --m4-base=$m4base"
2092   func_append actioncmd " --doc-base=$docbase"
2093   func_append actioncmd " --aux-dir=$auxdir"
2094   if test -n "$inctests"; then
2095     func_append actioncmd " --with-tests"
2096   fi
2097   for module in $avoidlist; do
2098     func_append actioncmd " --avoid=$module"
2099   done
2100   if test -n "$lgpl"; then
2101     func_append actioncmd " --lgpl"
2102   fi
2103   if test -n "$makefile_name"; then
2104     func_append actioncmd " --makefile-name=$makefile_name"
2105   fi
2106   if test "$libtool" = true; then
2107     func_append actioncmd " --libtool"
2108   else
2109     func_append actioncmd " --no-libtool"
2110   fi
2111   func_append actioncmd " --macro-prefix=$macro_prefix"
2112   func_append actioncmd " `echo $specified_modules`"
2113
2114   # Default the makefile name to Makefile.am.
2115   if test -n "$makefile_name"; then
2116     makefile_am=$makefile_name
2117   else
2118     makefile_am=Makefile.am
2119   fi
2120
2121   # Create normal Makefile.ams.
2122   for_test=false
2123
2124   # Create library makefile.
2125   func_dest_tmpfilename $sourcebase/$makefile_am
2126   func_emit_lib_Makefile_am > "$tmpfile"
2127   if test -f "$destdir"/$sourcebase/$makefile_am; then
2128     if cmp "$destdir"/$sourcebase/$makefile_am "$tmpfile" > /dev/null; then
2129       rm -f "$tmpfile"
2130     else
2131       if $doit; then
2132         echo "Updating $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
2133         mv -f "$destdir"/$sourcebase/$makefile_am "$destdir"/$sourcebase/$makefile_am~
2134         mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
2135       else
2136         echo "Update $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
2137         rm -f "$tmpfile"
2138       fi
2139     fi
2140   else
2141     if $doit; then
2142       echo "Creating $sourcebase/$makefile_am"
2143       mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
2144     else
2145       echo "Create $sourcebase/$makefile_am"
2146       rm -f "$tmpfile"
2147     fi
2148     func_append added_files "$sourcebase/$makefile_am$nl"
2149   fi
2150
2151   # Create m4/gnulib-cache.m4.
2152   func_dest_tmpfilename $m4base/gnulib-cache.m4
2153   (
2154     func_emit_copyright_notice
2155     echo "#"
2156     echo "# This file represents the specification of how gnulib-tool is used."
2157     echo "# It acts as a cache: It is written and read by gnulib-tool."
2158     echo "# In projects using CVS, this file is meant to be stored in CVS,"
2159     echo "# like the configure.ac and various Makefile.am files."
2160     echo
2161     echo
2162     echo "# Specification in the form of a command-line invocation:"
2163     echo "#   $actioncmd"
2164     echo
2165     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
2166     # Store the local_gnulib_dir relative to destdir.
2167     case "$local_gnulib_dir" in
2168       "" | /*)
2169         relative_local_gnulib_dir="$local_gnulib_dir" ;;
2170       * )
2171         case "$destdir" in
2172           /*) relative_local_gnulib_dir="$local_gnulib_dir" ;;
2173           *)
2174             # destdir, local_gnulib_dir are both relative.
2175             func_relativize "$destdir" "$local_gnulib_dir"
2176             relative_local_gnulib_dir="$reldir" ;;
2177         esac ;;
2178     esac
2179     echo "gl_LOCAL_DIR([$relative_local_gnulib_dir])"
2180     echo "gl_MODULES(["`echo $specified_modules`"])"
2181     echo "gl_AVOID([$avoidlist])"
2182     echo "gl_SOURCE_BASE([$sourcebase])"
2183     echo "gl_M4_BASE([$m4base])"
2184     echo "gl_DOC_BASE([$docbase])"
2185     echo "gl_TESTS_BASE([$testsbase])"
2186     test -z "$inctests" || echo "gl_WITH_TESTS"
2187     echo "gl_LIB([$libname])"
2188     test -z "$lgpl" || echo "gl_LGPL"
2189     echo "gl_MAKEFILE_NAME([$makefile_name])"
2190     if test "$libtool" = true; then
2191       echo "gl_LIBTOOL"
2192     fi
2193     echo "gl_MACRO_PREFIX([$macro_prefix])"
2194   ) > "$tmpfile"
2195   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
2196     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
2197       rm -f "$tmpfile"
2198     else
2199       if $doit; then
2200         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
2201         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
2202         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
2203       else
2204         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
2205         if false; then
2206           cat "$tmpfile"
2207           echo
2208           echo "# gnulib-cache.m4 ends here"
2209         fi
2210         rm -f "$tmpfile"
2211       fi
2212     fi
2213   else
2214     if $doit; then
2215       echo "Creating $m4base/gnulib-cache.m4"
2216       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
2217     else
2218       echo "Create $m4base/gnulib-cache.m4"
2219       cat "$tmpfile"
2220       rm -f "$tmpfile"
2221     fi
2222   fi
2223
2224   # Create m4/gnulib-comp.m4.
2225   func_dest_tmpfilename $m4base/gnulib-comp.m4
2226   (
2227     echo "# DO NOT EDIT! GENERATED AUTOMATICALLY!"
2228     func_emit_copyright_notice
2229     echo "#"
2230     echo "# This file represents the compiled summary of the specification in"
2231     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
2232     echo "# to be invoked from configure.ac."
2233     echo "# In projects using CVS, this file can be treated like other built files."
2234     echo
2235     echo
2236     echo "# This macro should be invoked from $configure_ac, in the section"
2237     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
2238     echo "# any checks for libraries, header files, types and library functions."
2239     echo "AC_DEFUN([${macro_prefix}_EARLY],"
2240     echo "["
2241     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
2242     echo "  m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
2243     echo "  m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
2244     echo "  m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
2245     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
2246     if test -n "$uses_subdirs"; then
2247       echo "  AC_REQUIRE([AM_PROG_CC_C_O])"
2248     fi
2249     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
2250       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
2251     fi
2252     for module in $modules; do
2253       func_verify_module
2254       if test -n "$module"; then
2255         func_get_autoconf_early_snippet "$module"
2256       fi
2257     done \
2258       | sed -e '/^$/d;' -e 's/^/  /'
2259     echo "])"
2260     echo
2261     echo "# This macro should be invoked from $configure_ac, in the section"
2262     echo "# \"Check for header files, types and library functions\"."
2263     echo "AC_DEFUN([${macro_prefix}_INIT],"
2264     echo "["
2265     func_emit_initmacro_start
2266     if test "$libtool" = true; then
2267       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2268       echo "  gl_cond_libtool=true"
2269     else
2270       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2271       echo "  gl_cond_libtool=false"
2272       echo "  gl_libdeps="
2273       echo "  gl_ltlibdeps="
2274     fi
2275     echo "  gl_source_base='$sourcebase'"
2276     if test "$auxdir" != "build-aux"; then
2277       sed_replace_build_aux='
2278         :a
2279         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2280           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2281           ba
2282         }'
2283     else
2284       sed_replace_build_aux=
2285     fi
2286     for module in $modules; do
2287       func_verify_module
2288       if test -n "$module"; then
2289         func_get_autoconf_snippet "$module" \
2290           | sed -e '/^$/d;' -e 's/^/  /' \
2291                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
2292                 -e "$sed_replace_build_aux"
2293         if test "$module" = 'alloca' && test "$libtool" = true; then
2294           echo 'changequote(,)dnl'
2295           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
2296           echo 'changequote([, ])dnl'
2297           echo 'AC_SUBST([LTALLOCA])'
2298         fi
2299       fi
2300     done
2301     # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2302     # created using libtool, because libtool already handles the dependencies.
2303     if test "$libtool" != true; then
2304       libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z' 'A-Z'`
2305       echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2306       echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2307       echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2308       echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2309     fi
2310     func_emit_initmacro_end
2311     echo "])"
2312     func_emit_initmacro_done
2313     echo
2314     echo "# This macro records the list of files which have been installed by"
2315     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
2316     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
2317     echo "$files" | sed -e 's,^,  ,'
2318     echo "])"
2319   ) > "$tmpfile"
2320   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
2321     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
2322       rm -f "$tmpfile"
2323     else
2324       if $doit; then
2325         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
2326         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
2327         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
2328       else
2329         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
2330         if false; then
2331           cat "$tmpfile"
2332           echo
2333           echo "# gnulib-comp.m4 ends here"
2334         fi
2335         rm -f "$tmpfile"
2336       fi
2337     fi
2338   else
2339     if $doit; then
2340       echo "Creating $m4base/gnulib-comp.m4"
2341       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
2342     else
2343       echo "Create $m4base/gnulib-comp.m4"
2344       cat "$tmpfile"
2345       rm -f "$tmpfile"
2346     fi
2347   fi
2348
2349   if test -n "$inctests"; then
2350     # Create tests makefile.
2351     func_dest_tmpfilename $testsbase/$makefile_am
2352     func_emit_tests_Makefile_am > "$tmpfile"
2353     if test -f "$destdir"/$testsbase/$makefile_am; then
2354       if cmp "$destdir"/$testsbase/$makefile_am "$tmpfile" > /dev/null; then
2355         rm -f "$tmpfile"
2356       else
2357         if $doit; then
2358           echo "Updating $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
2359           mv -f "$destdir"/$testsbase/$makefile_am "$destdir"/$testsbase/$makefile_am~
2360           mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
2361         else
2362           echo "Update $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
2363           rm -f "$tmpfile"
2364         fi
2365       fi
2366     else
2367       if $doit; then
2368         echo "Creating $testsbase/$makefile_am"
2369         mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
2370       else
2371         echo "Create $testsbase/$makefile_am"
2372         rm -f "$tmpfile"
2373       fi
2374     func_append added_files "$testsbase/$makefile_am$nl"
2375     fi
2376   fi
2377
2378   # Update the .cvsignore and .gitignore files.
2379   { echo "$added_files" | sed -e '/^$/d' -e 's,\([^/]*\)$,|A|\1,'
2380     echo "$removed_files" | sed -e '/^$/d' -e 's,\([^/]*\)$,|R|\1,'
2381   } | LC_ALL=C sort -t'|' -k1,1 > "$tmp"/fileset-changes
2382   { # Rearrange file descriptors. Needed because "while ... done < ..."
2383     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2384     exec 5<&0 < "$tmp"/fileset-changes
2385     func_update_ignorelist ()
2386     {
2387       ignore="$1"
2388       if test -f "$destdir/$dir$ignore"; then
2389         if test -n "$dir_added" || test -n "$dir_removed"; then
2390           LC_ALL=C sort "$destdir/$dir$ignore" > "$tmp"/ignore
2391           echo "$dir_added" | sed -e '/^$/d' | LC_ALL=C sort -u \
2392             | LC_ALL=C join -v 2 "$tmp"/ignore - > "$tmp"/ignore-added
2393           echo "$dir_removed" | sed -e '/^$/d' | LC_ALL=C sort -u \
2394             | LC_ALL=C join -v 2 "$tmp"/ignore - > "$tmp"/ignore-removed
2395           if test -s "$tmp"/ignore-added || test -s "$tmp"/ignore-removed; then
2396             if $doit; then
2397               echo "Updating $destdir/$dir$ignore (backup in $destdir/$dir${ignore}~)"
2398               mv -f "$destdir/$dir$ignore" "$destdir/$dir$ignore"~
2399               sed -e 's,^,/^,' -e 's,$,\$/d,' < "$tmp"/ignore-removed > "$tmp"/sed-ignore-removed
2400               cat "$destdir/$dir$ignore"~ "$tmp"/ignore-added \
2401                 | sed -f "$tmp"/sed-ignore-removed \
2402                 > "$destdir/$dir$ignore"
2403             else
2404               echo "Update $destdir/$dir$ignore (backup in $destdir/$dir${ignore}~)"
2405             fi
2406           fi
2407         fi
2408       else
2409         if test -n "$dir_added"; then
2410           if $doit; then
2411             echo "Creating $destdir/$dir$ignore"
2412             echo "$dir_added" | sed -e '/^$/d' | LC_ALL=C sort -u > "$destdir/$dir$ignore"
2413           else
2414             echo "Create $destdir/$dir$ignore"
2415           fi
2416         fi
2417       fi
2418     }
2419     func_done_dir ()
2420     {
2421       dir="$1"
2422       dir_added="$2"
2423       dir_removed="$3"
2424       if test -d "$destdir/${dir}CVS" || test -f "$destdir/${dir}.cvsignore"; then
2425         func_update_ignorelist .cvsignore
2426       fi
2427       if test -d "$destdir/.git" || test -f "$destdir/${dir}.gitignore"; then
2428         func_update_ignorelist .gitignore
2429       fi
2430     }
2431     last_dir=
2432     last_dir_added=
2433     last_dir_removed=
2434     while read line; do
2435       # Why not ''read next_dir op file'' ? Because the dir column can be empty.
2436       next_dir=`echo "$line" | sed -e 's,|.*,,'`
2437       op=`echo "$line" | sed -e 's,^[^|]*|\([^|]*\)|.*$,\1,'`
2438       file=`echo "$line" | sed -e 's,^[^|]*|[^|]*|,,'`
2439       if test "$next_dir" != "$last_dir"; then
2440         func_done_dir "$last_dir" "$last_dir_added" "$last_dir_removed"
2441         last_dir="$next_dir"
2442         last_dir_added=
2443         last_dir_removed=
2444       fi
2445       case $op in
2446         A) func_append last_dir_added "$file$nl";;
2447         R) func_append last_dir_removed "$file$nl";;
2448       esac
2449     done
2450     func_done_dir "$last_dir" "$last_dir_added" "$last_dir_removed"
2451     exec 0<&5 5<&-
2452   }
2453
2454   echo "Finished."
2455   echo
2456   echo "You may need to add #include directives for the following .h files."
2457   # Intersect $specified_modules and $modules
2458   # (since $specified_modules is not necessarily of subset of $modules - some
2459   # may have been skipped through --avoid, and since the elements of $modules
2460   # but not in $specified_modules can go away without explicit notice - through
2461   # changes in the module dependencies).
2462   echo "$specified_modules" > "$tmp"/modules1 # a sorted list, one module per line
2463   echo "$modules" > "$tmp"/modules2 # also a sorted list, one module per line
2464   # First the #include <...> directives without #ifs, sorted for convenience,
2465   # then the #include "..." directives without #ifs, sorted for convenience,
2466   # then the #include directives that are surrounded by #ifs. Not sorted.
2467   for module in `LC_ALL=C join "$tmp"/modules1 "$tmp"/modules2`; do
2468     include_directive=`func_get_include_directive "$module"`
2469     case "$nl$include_directive" in
2470       *"$nl#if"*)
2471         echo "$include_directive" 1>&5
2472         ;;
2473       *)
2474         echo "$include_directive" | grep -v 'include "' 1>&6
2475         echo "$include_directive" | grep 'include "' 1>&7
2476         ;;
2477     esac
2478   done 5> "$tmp"/include-if 6> "$tmp"/include-angles 7> "$tmp"/include-quotes
2479   (
2480    LC_ALL=C sort -u "$tmp"/include-angles
2481    LC_ALL=C sort -u "$tmp"/include-quotes
2482    cat "$tmp"/include-if
2483   ) | sed -e '/^$/d' -e 's/^/  /'
2484   rm -f "$tmp"/include-angles "$tmp"/include-quotes "$tmp"/include-if
2485
2486   for module in $modules; do
2487     func_get_link_directive "$module"
2488   done \
2489     | LC_ALL=C sort -u | sed -e '/^$/d' -e 's/^/  /' > "$tmp"/link
2490   if test `wc -l < "$tmp"/link` != 0; then
2491     echo
2492     echo "You may need to use the following Makefile variables when linking."
2493     echo "Use them in <program>_LDADD when linking a program, or"
2494     echo "in <library>_a_LDFLAGS or <library>_la_LDFLAGS when linking a library."
2495     cat "$tmp"/link
2496   fi
2497   rm -f "$tmp"/link
2498
2499   echo
2500   echo "Don't forget to"
2501   if test "$makefile_am" = Makefile.am; then
2502     echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
2503   else
2504     echo "  - \"include $makefile_name\" from within \"$sourcebase/Makefile.am\","
2505   fi
2506   if test -n "$inctests"; then
2507     if test "$makefile_am" = Makefile.am; then
2508       echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
2509     else
2510       echo "  - \"include $makefile_name\" from within \"$testsbase/Makefile.am\","
2511     fi
2512   fi
2513   if test "$makefile_am" = Makefile.am; then
2514     sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
2515     sourcebase_base=`basename "$sourcebase"`
2516     echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
2517   fi
2518   if test -n "$inctests"; then
2519     if test "$makefile_am" = Makefile.am; then
2520       testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
2521       testsbase_base=`basename "$testsbase"`
2522       echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
2523     fi
2524   fi
2525   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
2526   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
2527   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
2528 }
2529
2530 # func_create_testdir testdir modules
2531 # Input:
2532 # - local_gnulib_dir  from --local-dir
2533 # - auxdir          directory relative to destdir where to place build aux files
2534 func_create_testdir ()
2535 {
2536   testdir="$1"
2537   modules="$2"
2538   if test -z "$modules"; then
2539     # All modules together.
2540     # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
2541     # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2542     # Except ftruncate, mountlist, which abort the configuration on mingw. FIXME.
2543     modules=`func_all_modules`
2544     modules=`for m in $modules; do case $m in config-h | fnmatch-posix | ftruncate | mountlist) ;; *) echo $m;; esac; done`
2545   fi
2546   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort -u`
2547
2548   # Check that the license of every module is consistent with the license of
2549   # its dependencies.
2550   saved_modules="$modules"
2551   saved_inctests="$inctests"
2552   # When computing transitive closures, don't consider $module to depend on
2553   # $module-tests. Need this becauses tests are implicitly GPL and may depend
2554   # on GPL modules - therefore we don't want a warning in this case.
2555   inctests=""
2556   for requested_module in $saved_modules; do
2557     requested_license=`func_get_license "$requested_module"`
2558     if test "$requested_license" != GPL; then
2559       # Here we use func_modules_transitive_closure, not just
2560       # func_get_dependencies, so that we also detect weird situations like
2561       # an LGPL module which depends on a GPLed build tool module which depends
2562       # on a GPL module.
2563       modules="$requested_module"
2564       func_modules_transitive_closure
2565       for module in $modules; do
2566         license=`func_get_license "$module"`
2567         case $license in
2568           LGPL | 'GPLed build tool') ;;
2569           'public domain' | 'unlimited' | 'unmodifiable license text') ;;
2570           *) echo "warning: module $requested_module depends on a module with an incompatible license: $module" 1>&2 ;;
2571         esac
2572       done
2573     fi
2574   done
2575   modules="$saved_modules"
2576   inctests="$saved_inctests"
2577
2578   # Subdirectory names.
2579   sourcebase=gllib
2580   m4base=glm4
2581   docbase=gldoc
2582   testsbase=gltests
2583   macro_prefix=gl
2584
2585   # Determine final module list.
2586   func_modules_transitive_closure
2587   echo "Module list with included dependencies:"
2588   echo "$modules" | sed -e 's/^/  /'
2589
2590   # Add the dummy module if needed.
2591   func_modules_add_dummy
2592
2593   # Determine final file list.
2594   func_modules_to_filelist
2595   echo "File list:"
2596   echo "$files" | sed -e 's/^/  /'
2597
2598   sed_rewrite_files="\
2599     s,^build-aux/,$auxdir/,
2600     s,^doc/,$docbase/,
2601     s,^lib/,$sourcebase/,
2602     s,^m4/,$m4base/,
2603     s,^tests/,$testsbase/,"
2604
2605   # Create directories.
2606   for f in $files; do echo $f; done \
2607     | sed -e "$sed_rewrite_files" \
2608     | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
2609     | LC_ALL=C sort -u \
2610     > "$tmp"/dirs
2611   { # Rearrange file descriptors. Needed because "while ... done < ..."
2612     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2613     exec 5<&0 < "$tmp"/dirs
2614     while read d; do
2615       mkdir -p "$testdir/$d"
2616     done
2617     exec 0<&5 5<&-
2618   }
2619
2620   # Copy files or make symbolic links.
2621   delimiter='   '
2622   for f in $files; do echo $f; done \
2623     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_files" \
2624     | LC_ALL=C sort \
2625     > "$tmp"/files
2626   { # Rearrange file descriptors. Needed because "while ... done < ..."
2627     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2628     exec 5<&0 < "$tmp"/files
2629     while read g f; do
2630       func_lookup_file "$f"
2631       if test -n "$lookedup_tmp"; then
2632         cp -p "$lookedup_file" "$testdir/$g"
2633       else
2634         ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
2635         if { test -n "$symbolic" \
2636              || { test -n "$lsymbolic" \
2637                   && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; }; then
2638           func_ln "$lookedup_file" "$testdir/$g"
2639         else
2640           cp -p "$lookedup_file" "$testdir/$g"
2641         fi
2642       fi
2643     done
2644     exec 0<&5 5<&-
2645   }
2646
2647   # Create Makefile.ams that are for testing.
2648   for_test=true
2649
2650   # Create $sourcebase/Makefile.am.
2651   mkdir -p "$testdir/$sourcebase"
2652   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
2653
2654   # Create $m4base/Makefile.am.
2655   mkdir -p "$testdir/$m4base"
2656   (echo "## Process this file with automake to produce Makefile.in."
2657    echo
2658    echo "EXTRA_DIST ="
2659    for f in $files; do
2660      case "$f" in
2661        m4/* )
2662          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
2663      esac
2664    done
2665   ) > "$testdir/$m4base/Makefile.am"
2666
2667   subdirs="$sourcebase $m4base"
2668   subdirs_with_configure_ac=""
2669
2670   if false && test -f "$testdir"/$m4base/gettext.m4; then
2671     # Avoid stupid error message from automake:
2672     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
2673     mkdir -p "$testdir/po"
2674     (echo "## Process this file with automake to produce Makefile.in."
2675     ) > "$testdir/po/Makefile.am"
2676     func_append subdirs " po"
2677   fi
2678
2679   if test -n "$inctests"; then
2680     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
2681     # Create $testsbase/Makefile.am.
2682     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
2683     # Create $testsbase/configure.ac.
2684     (echo "# Process this file with autoconf to produce a configure script."
2685      echo "AC_INIT([dummy], [0])"
2686      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
2687      echo "AM_INIT_AUTOMAKE"
2688      echo
2689      echo "AM_CONFIG_HEADER([config.h])"
2690      echo
2691      echo "AC_PROG_CC"
2692      echo "AC_PROG_INSTALL"
2693      echo "AC_PROG_MAKE_SET"
2694      echo "AC_PROG_RANLIB"
2695      echo
2696      if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2697        echo "AC_GNU_SOURCE"
2698        echo
2699      fi
2700      for module in $modules; do
2701        func_verify_module
2702        if test -n "$module"; then
2703          func_get_autoconf_early_snippet "$module"
2704        fi
2705      done \
2706        | sed -e '/^$/d;' -e 's/AC_REQUIRE(\[\([^()]*\)\])/\1/'
2707      if test "$libtool" = true; then
2708        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2709        echo "gl_cond_libtool=true"
2710      else
2711        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2712        echo "gl_cond_libtool=false"
2713        echo "gl_libdeps="
2714        echo "gl_ltlibdeps="
2715      fi
2716      # Wrap the set of autoconf snippets into an autoconf macro that is then
2717      # invoked. This is needed because autoconf does not support AC_REQUIRE
2718      # at the top level:
2719      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2720      # but we want the AC_REQUIRE to have its normal meaning (provide one
2721      # expansion of the required macro before the current point, and only one
2722      # expansion total).
2723      echo "AC_DEFUN([gl_INIT], ["
2724      func_emit_initmacro_start
2725      sed_replace_build_aux='
2726        :a
2727        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2728          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
2729          ba
2730        }'
2731      # We don't have explicit ordering constraints between the various
2732      # autoconf snippets. It's cleanest to put those of the library before
2733      # those of the tests.
2734      echo "gl_source_base='../$sourcebase'"
2735      for module in $modules; do
2736        func_verify_nontests_module
2737        if test -n "$module"; then
2738          func_get_autoconf_snippet "$module" \
2739            | sed -e "$sed_replace_build_aux"
2740        fi
2741      done
2742      echo "gl_source_base='.'"
2743      for module in $modules; do
2744        func_verify_tests_module
2745        if test -n "$module"; then
2746          func_get_autoconf_snippet "$module" \
2747            | sed -e "$sed_replace_build_aux"
2748        fi
2749      done
2750      # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2751      # created using libtool, because libtool already handles the dependencies.
2752      if test "$libtool" != true; then
2753        libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z' 'A-Z'`
2754        echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2755        echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2756        echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2757        echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2758      fi
2759      func_emit_initmacro_end
2760      echo "])"
2761      func_emit_initmacro_done
2762      echo
2763      echo "gl_INIT"
2764      echo
2765      # Usually $testsbase/config.h will be a superset of config.h. Verify this
2766      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
2767      echo "AH_TOP([#include \"../config.h\"])"
2768      echo
2769      echo "AC_OUTPUT([Makefile])"
2770     ) > "$testdir/$testsbase/configure.ac"
2771     func_append subdirs " $testsbase"
2772     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
2773   fi
2774
2775   # Create Makefile.am.
2776   (echo "## Process this file with automake to produce Makefile.in."
2777    echo
2778    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2779    echo
2780    echo "SUBDIRS = $subdirs"
2781    echo
2782    echo "ACLOCAL_AMFLAGS = -I $m4base"
2783   ) > "$testdir/Makefile.am"
2784
2785   # Create configure.ac.
2786   (echo "# Process this file with autoconf to produce a configure script."
2787    echo "AC_INIT([dummy], [0])"
2788    if test "$auxdir" != "."; then
2789      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2790    fi
2791    echo "AM_INIT_AUTOMAKE"
2792    echo
2793    echo "AM_CONFIG_HEADER([config.h])"
2794    echo
2795    echo "AC_PROG_CC"
2796    echo "AC_PROG_INSTALL"
2797    echo "AC_PROG_MAKE_SET"
2798    echo
2799    echo "# For autobuild."
2800    echo "AC_CANONICAL_BUILD"
2801    echo "AC_CANONICAL_HOST"
2802    echo
2803    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
2804    echo "m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
2805    echo "m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
2806    echo "m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
2807    echo
2808    echo "AC_PROG_RANLIB"
2809    echo
2810    if test -n "$uses_subdirs"; then
2811      echo "AM_PROG_CC_C_O"
2812      echo
2813    fi
2814    if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2815      echo "AC_GNU_SOURCE"
2816      echo
2817    fi
2818    for module in $modules; do
2819      func_verify_nontests_module
2820      if test -n "$module"; then
2821        func_get_autoconf_early_snippet "$module"
2822      fi
2823    done \
2824      | sed -e '/^$/d;' -e 's/AC_REQUIRE(\[\([^()]*\)\])/\1/'
2825    if test "$libtool" = true; then
2826      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2827      echo "gl_cond_libtool=true"
2828    else
2829      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2830      echo "gl_cond_libtool=false"
2831      echo "gl_libdeps="
2832      echo "gl_ltlibdeps="
2833    fi
2834    echo "gl_source_base='$sourcebase'"
2835    # Wrap the set of autoconf snippets into an autoconf macro that is then
2836    # invoked. This is needed because autoconf does not support AC_REQUIRE
2837    # at the top level:
2838    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2839    # but we want the AC_REQUIRE to have its normal meaning (provide one
2840    # expansion of the required macro before the current point, and only one
2841    # expansion total).
2842    echo "AC_DEFUN([gl_INIT], ["
2843    func_emit_initmacro_start
2844    if test "$auxdir" != "build-aux"; then
2845      sed_replace_build_aux='
2846        :a
2847        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2848          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2849          ba
2850        }'
2851    else
2852      sed_replace_build_aux=
2853    fi
2854    for module in $modules; do
2855      func_verify_nontests_module
2856      if test -n "$module"; then
2857        func_get_autoconf_snippet "$module" \
2858          | sed -e "$sed_replace_build_aux"
2859      fi
2860    done
2861    # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2862    # created using libtool, because libtool already handles the dependencies.
2863    if test "$libtool" != true; then
2864      libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z' 'A-Z'`
2865      echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2866      echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2867      echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2868      echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2869    fi
2870    func_emit_initmacro_end
2871    echo "])"
2872    func_emit_initmacro_done
2873    echo
2874    echo "gl_INIT"
2875    echo
2876    if test -n "$subdirs_with_configure_ac"; then
2877      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
2878    fi
2879    makefiles="Makefile"
2880    for d in $subdirs; do
2881      # For subdirs that have a configure.ac by their own, it's the subdir's
2882      # configure.ac which creates the subdir's Makefile.am, not this one.
2883      case " $subdirs_with_configure_ac " in
2884        *" $d "*) ;;
2885        *) func_append makefiles " $d/Makefile" ;;
2886      esac
2887    done
2888    echo "AC_OUTPUT([$makefiles])"
2889   ) > "$testdir/configure.ac"
2890
2891   # Create autogenerated files.
2892   (cd "$testdir"
2893    # Do not use "${AUTORECONF} --force --install", because it may invoke
2894    # autopoint, which brings in older versions of some of our .m4 files.
2895    if test -f $m4base/gettext.m4; then
2896      echo "executing ${AUTOPOINT} --force"
2897      ${AUTOPOINT} --force || func_exit 1
2898      for f in $m4base/*.m4~; do
2899        mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
2900      done
2901    fi
2902    echo "executing ${ACLOCAL} -I $m4base"
2903    ${ACLOCAL} -I $m4base || func_exit 1
2904    if ! test -d build-aux; then
2905      echo "executing mkdir build-aux"
2906      mkdir build-aux || func_exit 1
2907    fi
2908    echo "executing ${AUTOCONF}"
2909    ${AUTOCONF} || func_exit 1
2910    echo "executing ${AUTOHEADER}"
2911    ${AUTOHEADER} || func_exit 1
2912    echo "executing ${AUTOMAKE} --add-missing --copy"
2913    ${AUTOMAKE} --add-missing --copy || func_exit 1
2914   ) || func_exit 1
2915   if test -n "$inctests"; then
2916     # Create autogenerated files.
2917     (cd "$testdir/$testsbase" || func_exit 1
2918      # Do not use "${AUTORECONF} --force --install", because it may invoke
2919      # autopoint, which brings in older versions of some of our .m4 files.
2920      if test -f ../$m4base/gettext.m4; then
2921        echo "executing ${AUTOPOINT} --force"
2922        ${AUTOPOINT} --force || func_exit 1
2923        for f in ../$m4base/*.m4~; do
2924          mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
2925        done
2926      fi
2927      echo "executing ${ACLOCAL} -I ../$m4base"
2928      ${ACLOCAL} -I ../$m4base || func_exit 1
2929      if ! test -d ../build-aux; then
2930        echo "executing mkdir ../build-aux"
2931        mkdir ../build-aux
2932      fi
2933      echo "executing ${AUTOCONF}"
2934      ${AUTOCONF} || func_exit 1
2935      echo "executing ${AUTOHEADER}"
2936      ${AUTOHEADER} || func_exit 1
2937      echo "executing ${AUTOMAKE} --add-missing --copy"
2938      ${AUTOMAKE} --add-missing --copy || func_exit 1
2939     ) || func_exit 1
2940   fi
2941   # Need to run configure and make once, to create built files that are to be
2942   # distributed (such as getdate.c).
2943   # Extract the value of "CLEANFILES += ..." and "MOSTLYCLEANFILES += ...".
2944   cleaned_files=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
2945                  | sed -n -e 's,^CLEANFILES[     ]*+=\([^#]*\).*$,\1,p' -e 's,^MOSTLYCLEANFILES[         ]*+=\([^#]*\).*$,\1,p'`
2946   cleaned_files=`for file in $cleaned_files; do echo " $file "; done`
2947   # Extract the value of "BUILT_SOURCES += ...". Remove variable references
2948   # such $(FOO_H) because they don't refer to distributed files.
2949   sed_remove_make_variables='s,[$]([A-Za-z0-9_]*),,g'
2950   built_sources=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
2951                  | sed -n -e 's,^BUILT_SOURCES[  ]*+=\([^#]*\).*$,\1,p' \
2952                  | sed -e "$sed_remove_make_variables"`
2953   distributed_built_sources=`for file in $built_sources; do
2954                                case "$cleaned_files" in
2955                                  *" "$file" "*) ;;
2956                                  *) echo $file ;;
2957                                esac;
2958                              done`
2959   if test -n "$distributed_built_sources"; then
2960     (cd "$testdir"
2961      ./configure || func_exit 1
2962        cd "$sourcebase"
2963        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
2964        make built_sources || func_exit 1
2965        cd ..
2966      make distclean || func_exit 1
2967     ) || func_exit 1
2968   fi
2969 }
2970
2971 # func_create_megatestdir megatestdir allmodules
2972 # Input:
2973 # - local_gnulib_dir  from --local-dir
2974 # - auxdir          directory relative to destdir where to place build aux files
2975 func_create_megatestdir ()
2976 {
2977   megatestdir="$1"
2978   allmodules="$2"
2979   if test -z "$allmodules"; then
2980     allmodules=`func_all_modules`
2981   fi
2982
2983   megasubdirs=
2984   # First, all modules one by one.
2985   for onemodule in $allmodules; do
2986     func_create_testdir "$megatestdir/$onemodule" $onemodule
2987     func_append megasubdirs "$onemodule "
2988   done
2989   # Then, all modules all together.
2990   # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
2991   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2992   allmodules=`for m in $allmodules; do if test $m != config-h && test $m != fnmatch-posix; then echo $m; fi; done`
2993   func_create_testdir "$megatestdir/ALL" "$allmodules"
2994   func_append megasubdirs "ALL"
2995
2996   # Create autobuild.
2997   cvsdate=`sh "$gnulib_dir/build-aux/mdate-sh" "$gnulib_dir/CVS/Entries" \
2998              | sed -e 's,January,01,'   -e 's,Jan,01,' \
2999                    -e 's,February,02,'  -e 's,Feb,02,' \
3000                    -e 's,March,03,'     -e 's,Mar,03,' \
3001                    -e 's,April,04,'     -e 's,Apr,04,' \
3002                    -e 's,May,05,'                      \
3003                    -e 's,June,06,'      -e 's,Jun,06,' \
3004                    -e 's,July,07,'      -e 's,Jul,07,' \
3005                    -e 's,August,08,'    -e 's,Aug,08,' \
3006                    -e 's,September,09,' -e 's,Sep,09,' \
3007                    -e 's,October,10,'   -e 's,Oct,10,' \
3008                    -e 's,November,11,'  -e 's,Nov,11,' \
3009                    -e 's,December,12,'  -e 's,Dec,12,' \
3010                    -e 's,^,00,'         -e 's,^[0-9]*\([0-9][0-9] \),\1,' \
3011                    -e 's,^\([0-9]*\) \([0-9]*\) \([0-9]*\),\3\2\1,'`
3012   (echo '#!/bin/sh'
3013    echo "CVSDATE=$cvsdate"
3014    echo "test -d logs || mkdir logs"
3015    echo "for module in $megasubdirs; do"
3016    echo "  echo \"Working on module \$module...\""
3017    echo "  safemodule=\`echo \$module | sed -e 's|/|-|g'\`"
3018    echo "  (echo \"To: gnulib@autobuild.josefsson.org\""
3019    echo "   echo"
3020    echo "   set -x"
3021    echo "   : autobuild project... \$module"
3022    echo "   : autobuild revision... cvs-\$CVSDATE-000000"
3023    echo "   : autobuild timestamp... \`date \"+%Y%m%d-%H%M%S\"\`"
3024    echo "   : autobuild hostname... \`hostname\`"
3025    echo "   cd \$module && ./configure \$CONFIGURE_OPTIONS && make && make check && make distclean"
3026    echo "   echo rc=\$?"
3027    echo "  ) 2>&1 | { if test -n \"\$AUTOBUILD_SUBST\"; then sed -e \"\$AUTOBUILD_SUBST\"; else cat; fi; } > logs/\$safemodule"
3028    echo "done"
3029   ) > "$megatestdir/do-autobuild"
3030   chmod a+x "$megatestdir/do-autobuild"
3031
3032   # Create Makefile.am.
3033   (echo "## Process this file with automake to produce Makefile.in."
3034    echo
3035    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
3036    echo
3037    echo "SUBDIRS = $megasubdirs"
3038    echo
3039    echo "EXTRA_DIST = do-autobuild"
3040   ) > "$megatestdir/Makefile.am"
3041
3042   # Create configure.ac.
3043   (echo "# Process this file with autoconf to produce a configure script."
3044    echo "AC_INIT([dummy], [0])"
3045    if test "$auxdir" != "."; then
3046      echo "AC_CONFIG_AUX_DIR([$auxdir])"
3047    fi
3048    echo "AM_INIT_AUTOMAKE"
3049    echo
3050    echo "AC_PROG_MAKE_SET"
3051    echo
3052    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
3053    echo "AC_OUTPUT([Makefile])"
3054   ) > "$megatestdir/configure.ac"
3055
3056   # Create autogenerated files.
3057   (cd "$megatestdir"
3058    # Do not use "${AUTORECONF} --install", because autoreconf operates
3059    # recursively, but the subdirectories are already finished, therefore
3060    # calling autoreconf here would only waste lots of CPU time.
3061    echo "executing ${ACLOCAL}"
3062    ${ACLOCAL} || func_exit 1
3063    echo "executing mkdir build-aux"
3064    mkdir build-aux
3065    echo "executing ${AUTOCONF}"
3066    ${AUTOCONF} || func_exit 1
3067    echo "executing ${AUTOMAKE} --add-missing --copy"
3068    ${AUTOMAKE} --add-missing --copy || func_exit 1
3069   ) || func_exit 1
3070 }
3071
3072 case $mode in
3073   "" )
3074     func_fatal_error "no mode specified" ;;
3075
3076   list )
3077     func_all_modules
3078     ;;
3079
3080   import | update )
3081
3082     # Where to import.
3083     if test -z "$destdir"; then
3084       destdir=.
3085     fi
3086     test -d "$destdir" \
3087       || func_fatal_error "destination directory does not exist: $destdir"
3088
3089     # Prefer configure.ac to configure.in.
3090     if test -f "$destdir"/configure.ac; then
3091       configure_ac="$destdir/configure.ac"
3092     else
3093       if test -f "$destdir"/configure.in; then
3094         configure_ac="$destdir/configure.in"
3095       else
3096         func_fatal_error "cannot find $destdir/configure.ac"
3097       fi
3098     fi
3099
3100     # Analyze configure.ac.
3101     guessed_auxdir="."
3102     guessed_libtool=false
3103     my_sed_traces='
3104       s,#.*$,,
3105       s,^dnl .*$,,
3106       s, dnl .*$,,
3107       /AC_CONFIG_AUX_DIR/ {
3108         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
3109       }
3110       /A[CM]_PROG_LIBTOOL/ {
3111         s,^.*$,guessed_libtool=true,p
3112       }'
3113     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
3114
3115     if test -z "$auxdir"; then
3116       auxdir="$guessed_auxdir"
3117     fi
3118
3119     # Determine where to apply func_import.
3120     if test -n "$m4base"; then
3121       # Apply func_import to a particular gnulib directory.
3122       # Any number of additional modules can be given.
3123       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
3124         # First use of gnulib in the given m4base.
3125         test -n "$supplied_libname" || supplied_libname=true
3126         test -n "$sourcebase" || sourcebase="lib"
3127         test -n "$docbase" || docbase="doc"
3128         test -n "$testsbase" || testsbase="tests"
3129         test -n "$macro_prefix" || macro_prefix="gl"
3130       fi
3131       func_import "$*"
3132     else
3133       # Apply func_import to all gnulib directories.
3134       # To get this list of directories, look at Makefile.am. (Not at
3135       # configure, because it may be omitted from CVS. Also, don't run
3136       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
3137       m4dirs=
3138       m4dirs_count=0
3139       if test -f "$destdir"/Makefile.am; then
3140         aclocal_amflags=`sed -n 's/^ACLOCAL_AMFLAGS[     ]*=\(.*\)$/\1/p' "$destdir"/Makefile.am`
3141         m4dir_is_next=
3142         for arg in $aclocal_amflags; do
3143           if test -n "$m4dir_is_next"; then
3144             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
3145             case "$arg" in
3146               /*) ;;
3147               *)
3148                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
3149                   func_append m4dirs " $arg"
3150                   m4dirs_count=`expr $m4dirs_count + 1`
3151                 fi
3152                 ;;
3153             esac
3154             m4dir_is_next=
3155           else
3156             if test "X$arg" = "X-I"; then
3157               m4dir_is_next=yes
3158             else
3159               m4dir_is_next=
3160             fi
3161           fi
3162         done
3163       else
3164         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
3165         if test -f "$destdir"/aclocal.m4; then
3166           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
3167           sedexpr2='s,^[^/]*$,.,'
3168           sedexpr3='s,/[^/]*$,,'
3169           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort -u`
3170           m4dirs_count=`echo "$m4dirs" | wc -l`
3171         fi
3172       fi
3173       if test $m4dirs_count = 0; then
3174         # First use of gnulib in a package.
3175         # Any number of additional modules can be given.
3176         test -n "$supplied_libname" || supplied_libname=true
3177         test -n "$sourcebase" || sourcebase="lib"
3178         m4base="m4"
3179         test -n "$docbase" || docbase="doc"
3180         test -n "$testsbase" || testsbase="tests"
3181         test -n "$macro_prefix" || macro_prefix="gl"
3182         func_import "$*"
3183       else
3184         if test $m4dirs_count = 1; then
3185           # There's only one use of gnulib here. Assume the user means it.
3186           # Any number of additional modules can be given.
3187           for m4base in $m4dirs; do
3188             func_import "$*"
3189           done
3190         else
3191           # Ambiguous - guess what the user meant.
3192           if test $# = 0; then
3193             # No further arguments. Guess the user wants to update all of them.
3194             for m4base in $m4dirs; do
3195               # Perform func_import in a subshell, so that variable values
3196               # such as
3197               #   local_gnulib_dir, avoidlist, sourcebase, m4base, docbase,
3198               #   testsbase, inctests, libname, lgpl, makefile_name, libtool,
3199               #   macro_prefix
3200               # don't propagate from one directory to another.
3201               (func_import) || func_exit 1
3202             done
3203           else
3204             # Really ambiguous.
3205             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
3206           fi
3207         fi
3208       fi
3209     fi
3210     ;;
3211
3212   create-testdir )
3213     if test -z "$destdir"; then
3214       func_fatal_error "please specify --dir option"
3215     fi
3216     mkdir "$destdir"
3217     test -d "$destdir" \
3218       || func_fatal_error "could not create destination directory"
3219     test -n "$auxdir" || auxdir="build-aux"
3220     func_create_testdir "$destdir" "$*"
3221     ;;
3222
3223   create-megatestdir )
3224     if test -z "$destdir"; then
3225       func_fatal_error "please specify --dir option"
3226     fi
3227     mkdir "$destdir" || func_fatal_error "could not create destination directory"
3228     test -n "$auxdir" || auxdir="build-aux"
3229     func_create_megatestdir "$destdir" "$*"
3230     ;;
3231
3232   test )
3233     test -n "$destdir" || destdir=testdir$$
3234     mkdir "$destdir" || func_fatal_error "could not create destination directory"
3235     test -n "$auxdir" || auxdir="build-aux"
3236     func_create_testdir "$destdir" "$*"
3237     cd "$destdir"
3238       mkdir build
3239       cd build
3240         ../configure || func_exit 1
3241         make || func_exit 1
3242         make check || func_exit 1
3243         make distclean || func_exit 1
3244         remaining=`find . -type f -print`
3245         if test -n "$remaining"; then
3246           echo "Remaining files:" $remaining 1>&2
3247           echo "gnulib-tool: *** Stop." 1>&2
3248           func_exit 1
3249         fi
3250       cd ..
3251     cd ..
3252     rm -rf "$destdir"
3253     ;;
3254
3255   megatest )
3256     test -n "$destdir" || destdir=testdir$$
3257     mkdir "$destdir" || func_fatal_error "could not create destination directory"
3258     test -n "$auxdir" || auxdir="build-aux"
3259     func_create_megatestdir "$destdir" "$*"
3260     cd "$destdir"
3261       mkdir build
3262       cd build
3263         ../configure
3264         make
3265         make check
3266         make distclean
3267         remaining=`find . -type f -print`
3268         if test -n "$remaining"; then
3269           echo "Remaining files:" $remaining 1>&2
3270           echo "gnulib-tool: *** Stop." 1>&2
3271           func_exit 1
3272         fi
3273       cd ..
3274     cd ..
3275     rm -rf "$destdir"
3276     ;;
3277
3278   extract-description )
3279     for module
3280     do
3281       func_verify_module
3282       if test -n "$module"; then
3283         func_get_description "$module"
3284       fi
3285     done
3286     ;;
3287
3288   extract-filelist )
3289     for module
3290     do
3291       func_verify_module
3292       if test -n "$module"; then
3293         func_get_filelist "$module"
3294       fi
3295     done
3296     ;;
3297
3298   extract-dependencies )
3299     for module
3300     do
3301       func_verify_module
3302       if test -n "$module"; then
3303         func_get_dependencies "$module"
3304       fi
3305     done
3306     ;;
3307
3308   extract-autoconf-snippet )
3309     for module
3310     do
3311       func_verify_module
3312       if test -n "$module"; then
3313         func_get_autoconf_snippet "$module"
3314       fi
3315     done
3316     ;;
3317
3318   extract-automake-snippet )
3319     for module
3320     do
3321       func_verify_module
3322       if test -n "$module"; then
3323         func_get_automake_snippet "$module"
3324       fi
3325     done
3326     ;;
3327
3328   extract-include-directive )
3329     for module
3330     do
3331       func_verify_module
3332       if test -n "$module"; then
3333         func_get_include_directive "$module"
3334       fi
3335     done
3336     ;;
3337
3338   extract-link-directive )
3339     for module
3340     do
3341       func_verify_module
3342       if test -n "$module"; then
3343         func_get_link_directive "$module"
3344       fi
3345     done
3346     ;;
3347
3348   extract-license )
3349     for module
3350     do
3351       func_verify_module
3352       if test -n "$module"; then
3353         func_get_license "$module"
3354       fi
3355     done
3356     ;;
3357
3358   extract-maintainer )
3359     for module
3360     do
3361       func_verify_module
3362       if test -n "$module"; then
3363         func_get_maintainer "$module"
3364       fi
3365     done
3366     ;;
3367
3368   extract-tests-module )
3369     for module
3370     do
3371       func_verify_module
3372       if test -n "$module"; then
3373         func_get_tests_module "$module"
3374       fi
3375     done
3376     ;;
3377
3378   * )
3379     func_fatal_error "unknown operation mode --$mode" ;;
3380 esac
3381
3382 rm -rf "$tmp"
3383 # Undo the effect of the previous 'trap' command. Some shellology:
3384 # We cannot use "trap - 0 1 2 3 13 15", because Solaris sh would attempt to
3385 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
3386 # exit); for the others we need to call 'exit' explicitly. The value of $? is
3387 # 128 + signal number and is set before the trap-registered command is run.
3388 trap '' 0
3389 trap 'func_exit $?' 1 2 3 13 15
3390
3391 exit 0
3392
3393 # Local Variables:
3394 # indent-tabs-mode: nil
3395 # whitespace-check-buffer-indent: nil
3396 # End: