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