Support projects without a top-level Makefile.am.
[gnulib.git] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2006 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: 2006-07-29 13:18:04 $'
26 last_checkin_date=`echo "$cvsdatestamp" | sed -e 's,^\$[D]ate: ,,'`
27 version=`echo "$last_checkin_date" | sed -e 's/ .*$//' -e 's,/,-,g'`
28
29 # You can set AUTOCONFPATH to empty if autoconf 2.57 is already in your PATH.
30 AUTOCONFPATH=
31 #case $USER in
32 #  bruno )
33 #    AUTOCONFBINDIR=/packages/gnu-inst-autoconf/2.57/bin
34 #    AUTOCONFPATH="eval env PATH=${AUTOCONFBINDIR}:\$PATH "
35 #    ;;
36 #esac
37
38 # You can set AUTOMAKEPATH to empty if automake 1.9.x is already in your PATH.
39 AUTOMAKEPATH=
40
41 # If you didn't set AUTOCONFPATH and AUTOMAKEPATH, you can also set the
42 # variables AUTOCONF, ACLOCAL, AUTOMAKE, AUTORECONF individually.
43 if test -z "${AUTOCONF}" || test -n "${AUTOCONFPATH}"; then
44   AUTOCONF="${AUTOCONFPATH}autoconf"
45 fi
46 if test -z "${ACLOCAL}" || test -n "${AUTOMAKEPATH}"; then
47   ACLOCAL="${AUTOMAKEPATH}aclocal"
48 fi
49 if test -z "${AUTOMAKE}" || test -n "${AUTOMAKEPATH}"; then
50   AUTOMAKE="${AUTOMAKEPATH}automake"
51 fi
52 if test -z "${AUTORECONF}" || test -n "${AUTOCONFPATH}"; then
53   AUTORECONF="${AUTOCONFPATH}autoreconf"
54 fi
55
56 # GNU sort is needed. Set SORT to its location (not needed if it's called
57 # 'sort' and already in the PATH).
58 if test -z "$SORT"; then
59   SORT=sort
60 fi
61
62 # func_usage
63 # outputs to stdout the --help usage message.
64 func_usage ()
65 {
66   echo "\
67 Usage: gnulib-tool --list
68        gnulib-tool --import [module1 ... moduleN]
69        gnulib-tool --update
70        gnulib-tool --create-testdir --dir=directory module1 ... moduleN
71        gnulib-tool --create-megatestdir --dir=directory [module1 ... moduleN]
72        gnulib-tool --test --dir=directory module1 ... moduleN
73        gnulib-tool --megatest --dir=directory [module1 ... moduleN]
74        gnulib-tool --extract-description module
75        gnulib-tool --extract-filelist module
76        gnulib-tool --extract-dependencies module
77        gnulib-tool --extract-autoconf-snippet module
78        gnulib-tool --extract-automake-snippet module
79        gnulib-tool --extract-include-directive module
80        gnulib-tool --extract-license module
81        gnulib-tool --extract-maintainer module
82        gnulib-tool --extract-tests-module module
83
84 Operation modes:
85       --list                print the available module names
86       --import              import the given modules into the current package;
87                             if no modules are specified, update the current
88                             package from the current gnulib
89       --update              update the current package, restore files omitted
90                             from CVS
91       --create-testdir      create a scratch package with the given modules
92       --create-megatestdir  create a mega scratch package with the given modules
93                             one by one and all together
94       --test                test the combination of the given modules
95                             (recommended to use CC=\"gcc -Wall\" here)
96       --megatest            test the given modules one by one and all together
97                             (recommended to use CC=\"gcc -Wall\" here)
98       --extract-description        extract the description
99       --extract-filelist           extract the list of files
100       --extract-dependencies       extract the dependencies
101       --extract-autoconf-snippet   extract the snippet for configure.ac
102       --extract-automake-snippet   extract the snippet for lib/Makefile.am
103       --extract-include-directive  extract the #include directive
104       --extract-license            report the license terms of the source files
105                                    under lib/
106       --extract-maintainer         report the maintainer(s) inside gnulib
107       --extract-tests-module       report the unit test module, if it exists
108
109 General options:
110       --dir=DIRECTORY       Specify the target directory.
111                             For --import, this specifies where your
112                             configure.ac can be found.  Defaults to current
113                             directory.
114
115 Options for --import:
116       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
117       --source-base=DIRECTORY
118                             Directory relative to --dir where source code is
119                             placed (default \"lib\").
120       --m4-base=DIRECTORY   Directory relative to --dir where *.m4 macros are
121                             placed (default \"m4\").
122       --doc-base=DIRECTORY  Directory relative to --dir where doc files are
123                             placed (default \"doc\").
124       --tests-base=DIRECTORY
125                             Directory relative to --dir where unit tests are
126                             placed (default \"tests\").
127       --aux-dir=DIRECTORY   Directory relative to --dir where auxiliary build
128                             tools are placed (default \"build-aux\").
129       --with-tests          Include unit tests for the included modules.
130       --avoid=MODULE        Avoid including the given MODULE. Useful if you
131                             have code that provides equivalent functionality.
132                             This option can be repeated.
133       --lgpl                Abort if modules aren't available under the LGPL.
134                             Also modify license template from GPL to LGPL.
135       --libtool             Use libtool rules.
136       --macro-prefix=PREFIX  Specify the prefix of the macros 'gl_EARLY' and
137                             'gl_INIT'. Default is 'gl'.
138       --no-changelog        don't update or create ChangeLog files
139
140 Options for --import and --update:
141       --dry-run             For --import, only print what would have been done.
142   -s, --symbolic, --symlink Make symbolic links instead of copying files.
143
144 Report bugs to <bug-gnulib@gnu.org>."
145 }
146
147 # func_version
148 # outputs to stdout the --version message.
149 func_version ()
150 {
151   year=`echo "$last_checkin_date" | sed -e 's,/.*$,,'`
152   echo "$progname (GNU $package) $version"
153   echo "Copyright (C) $year Free Software Foundation, Inc.
154 This is free software; see the source for copying conditions.  There is NO
155 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
156   echo "Written by" "Bruno Haible" "and" "Simon Josefsson"
157 }
158
159 # func_emit_copyright_notice
160 # outputs to stdout a header for a generated file.
161 func_emit_copyright_notice ()
162 {
163   echo "# Copyright (C) 2004-2006 Free Software Foundation, Inc."
164   echo "#"
165   echo "# This file is free software, distributed under the terms of the GNU"
166   echo "# General Public License.  As a special exception to the GNU General"
167   echo "# Public License, this file may be distributed as part of a program"
168   echo "# that contains a configuration script generated by Autoconf, under"
169   echo "# the same distribution terms as the rest of that program."
170   echo "#"
171   echo "# Generated by gnulib-tool."
172 }
173
174 # func_tmpdir
175 # creates a temporary directory.
176 # Sets variable
177 # - tmp             pathname of freshly created temporary directory
178 func_tmpdir ()
179 {
180   # Use the environment variable TMPDIR, falling back to /tmp. This allows
181   # users to specify a different temporary directory, for example, if their
182   # /tmp is filled up or too small.
183   : ${TMPDIR=/tmp}
184   {
185     # Use the mktemp program if available. If not available, hide the error
186     # message.
187     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
188     test -n "$tmp" && test -d "$tmp"
189   } ||
190   {
191     # Use a simple mkdir command. It is guaranteed to fail if the directory
192     # already exists.  $RANDOM is bash specific and expands to empty in shells
193     # other than bash, ksh and zsh.  Its use does not increase security;
194     # rather, it minimizes the probability of failure in a very cluttered /tmp
195     # directory.
196     tmp=$TMPDIR/gl$$-$RANDOM
197     (umask 077 && mkdir "$tmp")
198   } ||
199   {
200     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
201     { (exit 1); exit 1; }
202   }
203 }
204
205 # func_fatal_error message
206 # outputs to stderr a fatal error message, and terminates the program.
207 func_fatal_error ()
208 {
209   echo "gnulib-tool: *** $1" 1>&2
210   echo "gnulib-tool: *** Stop." 1>&2
211   exit 1
212 }
213
214 # func_readlink SYMLINK
215 # outputs the target of the given symlink.
216 if (type -p readlink) > /dev/null 2>&1; then
217   func_readlink ()
218   {
219     # Use the readlink program from GNU coreutils.
220     readlink "$1"
221   }
222 else
223   func_readlink ()
224   {
225     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
226     # would do the wrong thing if the link target contains " -> ".
227     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
228   }
229 fi
230
231 # func_ln_if_changed SRC DEST
232 # Like ln -s, but avoids munging timestamps if the link is correct.
233 func_ln_if_changed ()
234 {
235   if test $# -ne 2; then
236     echo "usage: func_ln_if_changed SRC DEST" >&2
237   fi
238   if test -L "$2" && test "$1" = "`func_readlink "$2"`"; then
239     :
240   else
241     rm -f "$2"
242     ln -s "$1" "$2"
243   fi
244 }
245
246 # Command-line option processing.
247 # Removes the OPTIONS from the arguments. Sets the variables:
248 # - mode            list or import or create-testdir or create-megatestdir
249 # - destdir         from --dir
250 # - libname, supplied_libname  from --lib
251 # - sourcebase      from --source-base
252 # - m4base          from --m4-base
253 # - docbase         from --doc-base
254 # - testsbase       from --tests-base
255 # - auxdir          from --aux-dir
256 # - inctests        true if --with-tests was given, blank otherwise
257 # - avoidlist       list of modules to avoid, from --avoid
258 # - lgpl            true if --lgpl was given, blank otherwise
259 # - libtool         true if --libtool was given, blank otherwise
260 # - macro_prefix    from --macro-prefix
261 # - autoconf_minversion  minimum supported autoconf version
262 # - do_changelog    false if --no-changelog was given, : otherwise
263 # - doit            : if actions shall be executed, false if only to be printed
264 {
265   mode=
266   destdir=
267   libname=libgnu
268   supplied_libname=
269   sourcebase=
270   m4base=
271   docbase=
272   testsbase=
273   auxdir=
274   inctests=
275   avoidlist=
276   lgpl=
277   libtool=
278   macro_prefix=
279   autoconf_minversion=
280   do_changelog=:
281   doit=:
282   symbolic=
283
284   supplied_opts="$@"
285
286   while test $# -gt 0; do
287     case "$1" in
288       --list | --lis )
289         mode=list
290         shift ;;
291       --import | --impor | --impo | --imp | --im | --i )
292         mode=import
293         shift ;;
294       --update | --updat | --upda | --upd | --up | --u )
295         mode=update
296         shift ;;
297       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
298         mode=create-testdir
299         shift ;;
300       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
301         mode=create-megatestdir
302         shift ;;
303       --test | --tes | --te | --t )
304         mode=test
305         shift ;;
306       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
307         mode=megatest
308         shift ;;
309       --extract-* )
310         mode=`echo "X$1" | sed -e 's/^X--//'`
311         shift ;;
312       --dir )
313         shift
314         if test $# = 0; then
315           func_fatal_error "missing argument for --dir"
316         fi
317         destdir=$1
318         shift ;;
319       --dir=* )
320         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
321         shift ;;
322       --lib )
323         shift
324         if test $# = 0; then
325           func_fatal_error "missing argument for --lib"
326         fi
327         libname=$1
328         supplied_libname=true
329         shift ;;
330       --lib=* )
331         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
332         supplied_libname=true
333         shift ;;
334       --source-base )
335         shift
336         if test $# = 0; then
337           func_fatal_error "missing argument for --source-base"
338         fi
339         sourcebase=$1
340         shift ;;
341       --source-base=* )
342         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
343         shift ;;
344       --m4-base )
345         shift
346         if test $# = 0; then
347           func_fatal_error "missing argument for --m4-base"
348         fi
349         m4base=$1
350         shift ;;
351       --m4-base=* )
352         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
353         shift ;;
354       --doc-base )
355         shift
356         if test $# = 0; then
357           func_fatal_error "missing argument for --doc-base"
358         fi
359         docbase=$1
360         shift ;;
361       --doc-base=* )
362         docbase=`echo "X$1" | sed -e 's/^X--doc-base=//'`
363         shift ;;
364       --tests-base )
365         shift
366         if test $# = 0; then
367           func_fatal_error "missing argument for --tests-base"
368         fi
369         testsbase=$1
370         shift ;;
371       --tests-base=* )
372         testsbase=`echo "X$1" | sed -e 's/^X--tests-base=//'`
373         shift ;;
374       --aux-dir )
375         shift
376         if test $# = 0; then
377           func_fatal_error "missing argument for --aux-dir"
378         fi
379         auxdir=$1
380         shift ;;
381       --aux-dir=* )
382         auxdir=`echo "X$1" | sed -e 's/^X--aux-dir=//'`
383         shift ;;
384       --with-tests )
385         inctests=true
386         shift ;;
387       --avoid )
388         shift
389         if test $# = 0; then
390           func_fatal_error "missing argument for --avoid"
391         fi
392         avoidlist="$avoidlist $1"
393         shift ;;
394       --avoid=* )
395         avoidlist="$avoidlist "`echo "X$1" | sed -e 's/^X--avoid=//'`
396         shift ;;
397       --lgpl )
398         lgpl=true
399         shift ;;
400       --libtool )
401         libtool=true
402         shift ;;
403       --macro-prefix )
404         shift
405         if test $# = 0; then
406           func_fatal_error "missing argument for --macro-prefix"
407         fi
408         macro_prefix="$1"
409         shift ;;
410       --macro-prefix=* )
411         macro_prefix=`echo "X$1" | sed -e 's/^X--macro-prefix=//'`
412         shift ;;
413       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
414         do_changelog=false
415         shift ;;
416       --dry-run )
417         doit=false
418         shift ;;
419       -s | --symbolic | --symboli | --symbol | --symbo | --symb | --symlink | --symlin | --symli | --syml | --sym | --sy )
420         symbolic=true
421         shift ;;
422       --help | --hel | --he | --h )
423         func_usage
424         exit 0 ;;
425       --version | --versio | --versi | --vers | --ver | --ve | --v )
426         func_version
427         exit 0 ;;
428       -- )
429         # Stop option processing
430         shift
431         break ;;
432       -* )
433         echo "gnulib-tool: unknown option $1" 1>&2
434         echo "Try 'gnulib-tool --help' for more information." 1>&2
435         exit 1 ;;
436       * )
437         break ;;
438     esac
439   done
440
441   if test "$mode" = update; then
442     if test $# != 0; then
443       echo "gnulib-tool: too many arguments in 'update' mode" 1>&2
444       echo "Try 'gnulib-tool --help' for more information." 1>&2
445       echo "If you really want to modify the gnulib configuration of your project," 1>&2
446       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
447       exit 1
448     fi
449     if test -n "$supplied_libname" || test -n "$sourcebase" || test -n "$m4base" \
450        || test -n "$docbase" || test -n "$testsbase" || test -n "$auxdir" \
451        || test -n "$inctests" || test -n "$avoidlist" || test -n "$lgpl" \
452        || test -n "$macro_prefix"; then
453       echo "gnulib-tool: invalid options for 'update' mode" 1>&2
454       echo "Try 'gnulib-tool --help' for more information." 1>&2
455       echo "If you really want to modify the gnulib configuration of your project," 1>&2
456       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
457       exit 1
458     fi
459     do_changelog=false
460   fi
461
462   # Determine the minimum supported autoconf version from the project's
463   # configure.ac.
464   DEFAULT_AUTOCONF_MINVERSION="2.59"
465   if test -f configure.ac; then
466     autoconf_minversion=`$AUTOCONF --trace=AC_PREREQ:'$1' | $SORT -g | uniq | tail -1`
467   fi
468   if test -z "$autoconf_minversion"; then
469     autoconf_minversion=$DEFAULT_AUTOCONF_MINVERSION
470   fi
471   case "$autoconf_minversion" in
472     1.* | 2.[0-4]* | 2.5[0-8]*)
473       func_fatal_error "minimum supported autoconf version is 2.59. Try adding AC_PREREQ([$DEFAULT_AUTOCONF_MINVERSION]) to your configure.ac." ;;
474   esac
475
476   # Remove trailing slashes from the directory names. This is necessary for
477   # m4base (to avoid an error in func_import) and optional for the others.
478   sed_trimtrailingslashes='s,\([^/]\)//*$,\1,'
479   case "$sourcebase" in
480     */ ) sourcebase=`echo "$sourcebase" | sed -e "$sed_trimtrailingslashes"` ;;
481   esac
482   case "$m4base" in
483     */ ) m4base=`echo "$m4base" | sed -e "$sed_trimtrailingslashes"` ;;
484   esac
485   case "$docbase" in
486     */ ) docbase=`echo "$docbase" | sed -e "$sed_trimtrailingslashes"` ;;
487   esac
488   case "$testsbase" in
489     */ ) testsbase=`echo "$testsbase" | sed -e "$sed_trimtrailingslashes"` ;;
490   esac
491   case "$auxdir" in
492     */ ) auxdir=`echo "$auxdir" | sed -e "$sed_trimtrailingslashes"` ;;
493   esac
494 }
495
496 case "$0" in
497   /*) self_abspathname="$0" ;;
498   */*) self_abspathname=`pwd`/"$0" ;;
499   *) for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`; do
500        if test -x "$d/$0" && test ! -d "$d/$0"; then
501          self_abspathname="$d/$0"
502          break
503        fi
504      done
505      if test -z "$self_abspathname"; then
506        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
507      fi
508      ;;
509 esac
510 while test -h "$self_abspathname"; do
511   # Resolve symbolic link.
512   linkval=`func_readlink "$self_abspathname"`
513   test -n "$linkval" || break
514   case "$linkval" in
515     /* ) self_abspathname="$linkval" ;;
516     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
517   esac
518 done
519 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
520
521 # func_all_modules
522 func_all_modules ()
523 {
524   # Filter out metainformation files like README, which are not modules.
525   # Filter out unit test modules; they can be retrieved through
526   # --extract-tests-module if desired.
527   (cd "$gnulib_dir/modules" && ls -1) \
528       | sed -e '/^CVS$/d' -e '/^ChangeLog$/d' -e '/^README$/d' -e '/^TEMPLATE$/d' -e '/^TEMPLATE-TESTS$/d' -e '/~$/d' \
529       | sed -e '/-tests$/d' \
530       | LC_ALL=C sort
531 }
532
533 # func_verify_module
534 # verifies a module name
535 func_verify_module ()
536 {
537   if test ! -f "$gnulib_dir/modules/$module" \
538      || test "CVS" = "$module" \
539      || test "ChangeLog" = "$module" \
540      || test "README" = "$module" \
541      || test "TEMPLATE" = "$module" \
542      || test "TEMPLATE-TESTS" = "$module"; then
543     echo "gnulib-tool: module $module doesn't exist" 1>&2
544     module=
545   fi
546 }
547
548 # func_verify_nontests_module
549 # verifies a module name, excluding tests modules
550 func_verify_nontests_module ()
551 {
552   case "$module" in
553     *-tests ) module= ;;
554     * ) func_verify_module ;;
555   esac
556 }
557
558 # func_verify_tests_module
559 # verifies a module name, considering only tests modules
560 func_verify_tests_module ()
561 {
562   case "$module" in
563     *-tests ) func_verify_module ;;
564     * ) module= ;;
565   esac
566 }
567
568 sed_extract_prog=':[     ]*$/ {
569   :a
570     n
571     s/^Description:[     ]*$//
572     s/^Files:[   ]*$//
573     s/^Depends-on:[      ]*$//
574     s/^configure\.ac:[   ]*$//
575     s/^Makefile\.am:[    ]*$//
576     s/^Include:[         ]*$//
577     s/^License:[         ]*$//
578     s/^Maintainer:[      ]*$//
579     tb
580     p
581     ba
582   :b
583 }'
584
585 # func_get_description module
586 func_get_description ()
587 {
588   sed -n -e "/^Description$sed_extract_prog" < "$gnulib_dir/modules/$1"
589 }
590
591 # func_get_filelist module
592 func_get_filelist ()
593 {
594   sed -n -e "/^Files$sed_extract_prog" < "$gnulib_dir/modules/$1"
595   case "$autoconf_minversion" in
596     2.59)
597       #echo m4/onceonly.m4
598       echo m4/onceonly_2_57.m4
599       ;;
600   esac
601 }
602
603 # func_get_dependencies module
604 func_get_dependencies ()
605 {
606   # ${module}-tests always implicitly depends on ${module}.
607   echo "$1" | sed -n -e 's/-tests//p'
608   # Then the explicit dependencies listed in the module description.
609   sed -n -e "/^Depends-on$sed_extract_prog" < "$gnulib_dir/modules/$1"
610 }
611
612 # func_get_autoconf_snippet module
613 func_get_autoconf_snippet ()
614 {
615   sed -n -e "/^configure\.ac$sed_extract_prog" < "$gnulib_dir/modules/$1"
616 }
617
618 # func_get_automake_snippet module
619 func_get_automake_snippet ()
620 {
621   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$gnulib_dir/modules/$1"
622 }
623
624 # func_get_include_directive module
625 func_get_include_directive ()
626 {
627   sed -n -e "/^Include$sed_extract_prog" < "$gnulib_dir/modules/$1" | \
628   sed -e 's/^\(["<]\)/#include \1/'
629 }
630
631 # func_get_license module
632 func_get_license ()
633 {
634   sed -n -e "/^License$sed_extract_prog" < "$gnulib_dir/modules/$1"
635 }
636
637 # func_get_maintainer module
638 func_get_maintainer ()
639 {
640   sed -n -e "/^Maintainer$sed_extract_prog" < "$gnulib_dir/modules/$1"
641 }
642
643 # func_get_tests_module module
644 func_get_tests_module ()
645 {
646   # The naming convention for tests modules is hardwired: ${module}-tests.
647   if test -f modules/"$1"-tests; then
648     echo "$1"-tests
649   fi
650 }
651
652 # func_acceptable module
653 # tests whether a module is acceptable.
654 # Input:
655 # - avoidlist       list of modules to avoid
656 func_acceptable ()
657 {
658   for avoid in $avoidlist; do
659     if test "$avoid" = "$1"; then
660       return 1
661     fi
662   done
663   return 0
664 }
665
666 # func_modules_transitive_closure
667 # Input:
668 # - modules         list of specified modules
669 # - inctests        true if tests should be included, blank otherwise
670 # - avoidlist       list of modules to avoid
671 # Output:
672 # - modules         list of modules, including dependencies
673 func_modules_transitive_closure ()
674 {
675   while true; do
676     xmodules=
677     for module in $modules; do
678       func_verify_module
679       if test -n "$module"; then
680         # Duplicate dependencies are harmless, but Jim wants a warning.
681         duplicated_deps=`func_get_dependencies $module | LC_ALL=C sort | LC_ALL=C uniq -d`
682         if test -n "$duplicated_deps"; then
683           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
684         fi
685         if func_acceptable $module; then
686           xmodules="$xmodules $module"
687           for depmodule in `func_get_dependencies $module`; do
688             if func_acceptable $depmodule; then
689               xmodules="$xmodules $depmodule"
690             fi
691           done
692           if test -n "$inctests"; then
693             testsmodule=`func_get_tests_module $module`
694             if test -n "$testsmodule"; then
695               if func_acceptable $testsmodule; then
696                 xmodules="$xmodules $testsmodule"
697                 for depmodule in `func_get_dependencies $testsmodule`; do
698                   if func_acceptable $depmodule; then
699                     xmodules="$xmodules $depmodule"
700                   fi
701                 done
702               fi
703             fi
704           fi
705         fi
706       fi
707     done
708     xmodules=`for m in $xmodules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
709     if test "$xmodules" = "$modules"; then
710       break
711     fi
712     modules="$xmodules"
713   done
714 }
715
716 # func_modules_add_dummy
717 # Input:
718 # - modules         list of modules, including dependencies
719 # Output:
720 # - modules         list of modules, including 'dummy' if needed
721 func_modules_add_dummy ()
722 {
723   have_lib_SOURCES=
724   sed_remove_backslash_newline=':a
725 /\\$/{
726 s/\\$//
727 N
728 s/\n//
729 ba
730 }'
731   for module in $modules; do
732     func_verify_nontests_module
733     if test -n "$module"; then
734       # Extract the value of "lib_SOURCES += ...".
735       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
736         # Ignore .h files since they are not compiled.
737         case "$file" in
738           *.h) ;;
739           *) have_lib_SOURCES=yes ;;
740         esac
741       done
742     fi
743   done
744   # Add the dummy module, to make sure the library will be non-empty.
745   if test -z "$have_lib_SOURCES"; then
746     modules="$modules dummy"
747   fi
748 }
749
750 # func_modules_to_filelist
751 # Input:
752 # - modules         list of modules, including dependencies
753 # Output:
754 # - files           list of files
755 func_modules_to_filelist ()
756 {
757   files=
758   for module in $modules; do
759     func_verify_module
760     if test -n "$module"; then
761       files="$files "`func_get_filelist $module`
762     fi
763   done
764   files=`for f in $files; do echo $f; done | LC_ALL=C sort | LC_ALL=C uniq`
765 }
766
767 # func_emit_lib_Makefile_am
768 # emits the contents of lib/Makefile.am to standard output.
769 # Input:
770 # - modules         list of modules, including dependencies
771 # - libname         library name
772 # - libtool         true if libtool will be used, blank otherwise
773 # - actioncmd       (optional) command that will reproduce this invocation
774 func_emit_lib_Makefile_am ()
775 {
776   if test -n "$libtool"; then
777     libext=la
778     perhapsLT=LT
779   else
780     libext=a
781     perhapsLT=
782   fi
783   echo "## Process this file with automake to produce Makefile.in."
784   func_emit_copyright_notice
785   if test -n "$actioncmd"; then
786     echo "# Reproduce by: $actioncmd"
787   fi
788   echo
789   # No need to generate dependencies since the sources are in gnulib, not here.
790   echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
791   echo
792   echo "noinst_${perhapsLT}LIBRARIES = $libname.$libext"
793   echo
794   echo "${libname}_${libext}_SOURCES ="
795   echo "${libname}_${libext}_LIBADD = @${perhapsLT}LIBOBJS@"
796   echo "noinst_HEADERS ="
797   echo "EXTRA_DIST ="
798   echo "BUILT_SOURCES ="
799   echo "SUFFIXES ="
800   echo "MOSTLYCLEANFILES ="
801   echo "MOSTLYCLEANDIRS ="
802   echo "CLEANFILES ="
803   echo "DISTCLEANFILES ="
804   echo "MAINTAINERCLEANFILES ="
805   echo
806   echo "AM_CPPFLAGS ="
807   echo
808   for module in $modules; do
809     func_verify_nontests_module
810     if test -n "$module"; then
811       {
812         func_get_automake_snippet "$module" |
813           sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g'
814         if test "$module" = 'alloca'; then
815           echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
816         fi
817       } > amsnippet.tmp
818       # Skip the contents if its entirely empty.
819       if grep '[^        ]' amsnippet.tmp > /dev/null ; then
820         echo "## begin gnulib module $module"
821         echo
822         cat amsnippet.tmp
823         echo "## end   gnulib module $module"
824         echo
825       fi
826       rm -f amsnippet.tmp
827     fi
828   done
829   echo
830   echo "mostlyclean-local:"
831   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
832   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
833   echo "            if test -d \$\$dir; then \\"
834   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
835   echo "            fi; \\"
836   echo "          done"
837   echo
838   echo "# Makefile.am ends here"
839 }
840
841 # func_emit_tests_Makefile_am
842 # emits the contents of tests/Makefile.am to standard output.
843 # Input:
844 # - modules         list of modules, including dependencies
845 # - libname         library name
846 # - libtool         true if libtool will be used, blank otherwise
847 # - sourcebase      relative directory containing lib source code
848 # - m4base          relative directory containing autoconf macros
849 # - testsbase       relative directory containing unit test code
850 func_emit_tests_Makefile_am ()
851 {
852   if test -n "$libtool"; then
853     libext=la
854   else
855     libext=a
856   fi
857   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
858   echo "## Process this file with automake to produce Makefile.in."
859   func_emit_copyright_notice
860   echo
861   # Generate dependencies here, since it eases the debugging of test failures.
862   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
863   echo
864   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
865   echo
866   # Nothing is being added to SUBDIRS; nevertheless the existence of this
867   # variable is needed to avoid an error from automake:
868   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
869   echo "SUBDIRS ="
870   echo "TESTS ="
871   echo "TESTS_ENVIRONMENT ="
872   echo "noinst_PROGRAMS ="
873   echo "check_PROGRAMS ="
874   echo "noinst_HEADERS ="
875   echo "EXTRA_DIST ="
876   echo "BUILT_SOURCES ="
877   echo "SUFFIXES ="
878   echo "MOSTLYCLEANFILES ="
879   echo "MOSTLYCLEANDIRS ="
880   echo "CLEANFILES ="
881   echo "DISTCLEANFILES ="
882   echo "MAINTAINERCLEANFILES ="
883   echo
884   echo "AM_CPPFLAGS = \\"
885   echo "  -I. -I\$(srcdir) \\"
886   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
887   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
888   echo
889   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
890   echo
891   for module in $modules; do
892     func_verify_tests_module
893     if test -n "$module"; then
894       func_get_automake_snippet "$module" > amsnippet.tmp
895       # Skip the contents if its entirely empty.
896       if grep '[^        ]' amsnippet.tmp > /dev/null ; then
897         echo "## begin gnulib module $module"
898         echo
899         cat amsnippet.tmp
900         echo "## end   gnulib module $module"
901         echo
902       fi
903       rm -f amsnippet.tmp
904     fi
905   done
906   echo "# Clean up after Solaris cc."
907   echo "clean-local:"
908   echo "        rm -rf SunWS_cache"
909   echo
910   echo "mostlyclean-local:"
911   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
912   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
913   echo "            if test -d \$\$dir; then \\"
914   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
915   echo "            fi; \\"
916   echo "          done"
917   echo
918   echo "# Makefile.am ends here"
919 }
920
921 # func_import modules
922 # Uses also the variables
923 # - destdir         target directory
924 # - libname         library name
925 # - sourcebase      directory relative to destdir where to place source code
926 # - m4base          directory relative to destdir where to place *.m4 macros
927 # - docbase         directory relative to destdir where to place doc files
928 # - testsbase       directory relative to destdir where to place unit test code
929 # - auxdir          directory relative to destdir where to place build aux files
930 # - inctests        true if --with-tests was given, blank otherwise
931 # - avoidlist       list of modules to avoid, from --avoid
932 # - lgpl            true if library's license shall be LGPL, blank otherwise
933 # - libtool         true if libtool will be used, blank otherwise
934 # - guessed_libtool true if the configure.ac file uses libtool, blank otherwise
935 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
936 # - autoconf_minversion  minimum supported autoconf version
937 # - doit            : if actions shall be executed, false if only to be printed
938 # - symbolic        true if files should be symlinked, copied otherwise
939 func_import ()
940 {
941   # Get the cached settings.
942   cached_specified_modules=
943   cached_avoidlist=
944   cached_sourcebase=
945   cached_m4base=
946   cached_docbase=
947   cached_testsbase=
948   cached_libname=
949   cached_lgpl=
950   cached_libtool=
951   cached_macro_prefix=
952   cached_files=
953   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
954     my_sed_traces='
955       s,#.*$,,
956       s,^dnl .*$,,
957       s, dnl .*$,,
958       /gl_MODULES(/ {
959         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
960       }
961       /gl_AVOID(/ {
962         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
963       }
964       /gl_SOURCE_BASE(/ {
965         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
966       }
967       /gl_M4_BASE(/ {
968         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
969       }
970       /gl_DOC_BASE(/ {
971         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
972       }
973       /gl_TESTS_BASE(/ {
974         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
975       }
976       /gl_LIB(/ {
977         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
978       }
979       /gl_LGPL/ {
980         s,^.*$,cached_lgpl=true,p
981       }
982       /gl_LIBTOOL/ {
983         s,^.*$,cached_libtool=true,p
984       }
985       /gl_MACRO_PREFIX(/ {
986         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
987       }'
988     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
989     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
990       my_sed_traces='
991         s,#.*$,,
992         s,^dnl .*$,,
993         s, dnl .*$,,
994         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
995           s,^.*$,cached_files=",p
996           n
997           ta
998           :a
999           s,^\]).*$,",
1000           tb
1001           p
1002           n
1003           ba
1004           :b
1005           p
1006         }'
1007       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1008     fi
1009   fi
1010
1011   # Merge the cached settings with the specified ones.
1012   # The m4base must be the same as expected from the pathname.
1013   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1014     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1015   fi
1016   # Append the cached and the specified module names. So that
1017   # "gnulib-tool --import foo" means to add the module foo.
1018   specified_modules="$cached_specified_modules $1"
1019   # Append the cached and the specified avoidlist. This is probably better
1020   # than dropping the cached one when --avoid is specified at least once.
1021   avoidlist=`echo $cached_avoidlist $avoidlist`
1022   # The sourcebase defaults to the cached one.
1023   if test -z "$sourcebase"; then
1024     sourcebase="$cached_sourcebase"
1025     if test -z "$sourcebase"; then
1026       func_fatal_error "missing --source-base option"
1027     fi
1028   fi
1029   # The docbase defaults to the cached one.
1030   if test -z "$docbase"; then
1031     docbase="$cached_docbase"
1032     if test -z "$docbase"; then
1033       func_fatal_error "missing --doc-base option. --doc-base has been introduced on 2006-07-11; if your last invocation of 'gnulib-tool --update' is before that date, you need to run 'gnulib-tool --update' once, with a --doc-base option."
1034     fi
1035   fi
1036   # The testsbase defaults to the cached one.
1037   if test -z "$testsbase"; then
1038     testsbase="$cached_testsbase"
1039     if test -z "$testsbase"; then
1040       func_fatal_error "missing --tests-base option"
1041     fi
1042   fi
1043   # The libname defaults to the cached one.
1044   if test -z "$supplied_libname"; then
1045     libname="$cached_libname"
1046     if test -z "$libname"; then
1047       func_fatal_error "missing --lib option"
1048     fi
1049   fi
1050   # Require LGPL if specified either way.
1051   if test -z "$lgpl"; then
1052     lgpl="$cached_lgpl"
1053   fi
1054   # Use libtool if specified either way, or if guessed.
1055   if test -z "$libtool"; then
1056     if test -n "$cached_m4base"; then
1057       libtool="$cached_libtool"
1058     else
1059       libtool="$guessed_libtool"
1060     fi
1061   fi
1062   # The macro_prefix defaults to the cached one.
1063   if test -z "$macro_prefix"; then
1064     macro_prefix="$cached_macro_prefix"
1065     if test -z "$macro_prefix"; then
1066       func_fatal_error "missing --macro-prefix option"
1067     fi
1068   fi
1069
1070   # Canonicalize the list of specified modules.
1071   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
1072
1073   # Determine final module list.
1074   modules="$specified_modules"
1075   func_modules_transitive_closure
1076   echo "Module list with included dependencies:"
1077   echo "$modules" | sed -e 's/^/  /'
1078
1079   # Add the dummy module if needed.
1080   func_modules_add_dummy
1081
1082   # If --lgpl, check the license of modules are compatible.
1083   if test -n "$lgpl"; then
1084     for module in $modules; do
1085       license=`func_get_license $module`
1086       case $license in
1087         LGPL | 'GPLed build tool' | 'public domain' | 'unlimited') ;;
1088         *) func_fatal_error "incompatible license on module $module: $license" ;;
1089       esac
1090     done
1091   fi
1092
1093   # Determine final file list.
1094   func_modules_to_filelist
1095   echo "File list:"
1096   echo "$files" | sed -e 's/^/  /'
1097
1098   test -n "$files" \
1099     || func_fatal_error "refusing to do nothing"
1100
1101   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1102   new_files="$files m4/gnulib-tool.m4"
1103   old_files="$cached_files"
1104   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1105     old_files="$old_files m4/gnulib-tool.m4"
1106   fi
1107
1108   # Create directories.
1109   if test ! -d "$destdir/$sourcebase"; then
1110     if $doit; then
1111       echo "Creating directory $destdir/$sourcebase"
1112       mkdir "$destdir/$sourcebase" || func_fatal_error "failed"
1113     else
1114       echo "Create directory $destdir/$sourcebase"
1115     fi
1116   fi
1117   if test ! -d "$destdir/$m4base"; then
1118     if $doit; then
1119       echo "Creating directory $destdir/$m4base"
1120       mkdir "$destdir/$m4base" || func_fatal_error "failed"
1121     else
1122       echo "Create directory $destdir/$m4base"
1123     fi
1124   fi
1125   docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1126   if test -n "$docfiles"; then
1127     if test ! -d "$destdir/$docbase"; then
1128       if $doit; then
1129         echo "Creating directory $destdir/$docbase"
1130         mkdir "$destdir/$docbase" || func_fatal_error "failed"
1131       else
1132         echo "Create directory $destdir/$docbase"
1133       fi
1134     fi
1135   fi
1136   if test -n "$inctests"; then
1137     if test ! -d "$destdir/$testsbase"; then
1138       if $doit; then
1139         echo "Creating directory $destdir/$testsbase"
1140         mkdir "$destdir/$testsbase" || func_fatal_error "failed"
1141       else
1142         echo "Create directory $destdir/$testsbase"
1143       fi
1144     fi
1145   fi
1146   if test ! -d "$destdir/$auxdir"; then
1147     if $doit; then
1148       echo "Creating directory $destdir/$auxdir"
1149       mkdir "$destdir/$auxdir" || func_fatal_error "failed"
1150     else
1151       echo "Create directory $destdir/$auxdir"
1152     fi
1153   fi
1154
1155   func_tmpdir
1156   trap 'rm -rf "$tmp"' 0 1 2 3 15
1157   # func_dest_tmpfilename file
1158   # determines the name of a temporary file (file is relative to destdir).
1159   # Sets variable:
1160   #   - tmpfile       absolute filename of the temporary file
1161   func_dest_tmpfilename ()
1162   {
1163     if $doit; then
1164       # Put the new contents of $file in a file in the same directory (needed
1165       # to guarantee that an 'mv' to "$destdir/$file" works).
1166       tmpfile="$destdir/$1.tmp"
1167     else
1168       # Put the new contents of $file in a file in a temporary directory
1169       # (because the directory of "$file" might not exist).
1170       tmpfile="$tmp"/`basename "$1"`.tmp
1171     fi
1172   }
1173
1174   # Copy files or make symbolic links. Remove obsolete files.
1175   delimiter='   '
1176   for f in $old_files; do
1177     case "$f" in
1178       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1179       doc/*) g=`echo "$f" | sed -e "s,^doc/,$cached_docbase/,"` ;;
1180       lib/*) g=`echo "$f" | sed -e "s,^lib/,$cached_sourcebase/,"` ;;
1181       m4/*) g=`echo "$f" | sed -e "s,^m4/,$cached_m4base/,"` ;;
1182       tests/*) g=`echo "$f" | sed -e "s,^tests/,$cached_testsbase/,"` ;;
1183       *) g="$f" ;;
1184     esac
1185     echo "$g""$delimiter""$f"
1186   done | LC_ALL=C sort > "$tmp"/old-files
1187   for f in $new_files; do
1188     case "$f" in
1189       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1190       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1191       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1192       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1193       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1194       *) g="$f" ;;
1195     esac
1196     echo "$g""$delimiter""$f"
1197   done | LC_ALL=C sort > "$tmp"/new-files
1198   # First the files that are in old-files, but not in new-files:
1199   sed_take_first_column='s,'"$delimiter"'.*,,'
1200   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1201     # Remove the file. Do nothing if the user already removed it.
1202     if test -f "$destdir/$g"; then
1203       if $doit; then
1204         echo "Removing file $g (backup in ${g}~)"
1205         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1206       else
1207         echo "Remove file $g (backup in ${g}~)"
1208       fi
1209     fi
1210   done
1211   # func_add_or_update handles a file that ought to be present afterwards.
1212   # Uses parameters f, g, already_present.
1213   func_add_or_update ()
1214   {
1215     func_dest_tmpfilename "$g"
1216     cp "$gnulib_dir/$f" "$tmpfile" || func_fatal_error "failed"
1217     if test -n "$lgpl"; then
1218       # Update license.
1219       case "$f" in
1220         lib/*)
1221           sed -e 's/GNU General/GNU Lesser General/g' \
1222               -e 's/version 2\([ ,]\)/version 2.1\1/g' \
1223             < "$gnulib_dir/$f" > "$tmpfile" || func_fatal_error "failed"
1224           ;;
1225       esac
1226     fi
1227     if test -f "$destdir/$g"; then
1228       # The file already exists.
1229       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1230         : # The file has not changed.
1231       else
1232         # Replace the file.
1233         if $doit; then
1234           if test -n "$already_present"; then
1235             echo "Updating file $g (backup in ${g}~)"
1236           else
1237             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1238           fi
1239           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1240           if test -n "$symbolic" && cmp "$gnulib_dir/$f" "$tmpfile" > /dev/null; then
1241             func_ln_if_changed "$gnulib_dir/$f" "$destdir/$g"
1242           else
1243             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1244           fi
1245         else
1246           if test -n "$already_present"; then
1247             echo "Update file $g (backup in ${g}~)"
1248           else
1249             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1250           fi
1251         fi
1252       fi
1253     else
1254       # Install the file.
1255       # Don't protest if the file should be there but isn't: it happens
1256       # frequently that developers don't put autogenerated files into CVS.
1257       if $doit; then
1258         echo "Copying file $g"
1259         if test -n "$symbolic" && cmp "$gnulib_dir/$f" "$tmpfile" > /dev/null; then
1260           func_ln_if_changed "$gnulib_dir/$f" "$destdir/$g"
1261         else
1262           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1263         fi
1264       else
1265         echo "Copy file $g"
1266       fi
1267     fi
1268     rm -f "$tmpfile"
1269   }
1270   # Then the files that are in new-files, but not in old-files:
1271   sed_take_last_column='s,^.*'"$delimiter"',,'
1272   already_present=
1273   for f in `LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1274     case "$f" in
1275       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1276       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1277       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1278       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1279       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1280       *) g="$f" ;;
1281     esac
1282     func_add_or_update
1283   done
1284   # Then the files that are in new-files and in old-files:
1285   already_present=true
1286   for f in `LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1287     case "$f" in
1288       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1289       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1290       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1291       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1292       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1293       *) g="$f" ;;
1294     esac
1295     func_add_or_update
1296   done
1297
1298   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1299   actioncmd="gnulib-tool --import"
1300   actioncmd="$actioncmd --dir=$destdir"
1301   actioncmd="$actioncmd --lib=$libname"
1302   actioncmd="$actioncmd --source-base=$sourcebase"
1303   actioncmd="$actioncmd --m4-base=$m4base"
1304   actioncmd="$actioncmd --doc-base=$docbase"
1305   actioncmd="$actioncmd --aux-dir=$auxdir"
1306   for module in $avoidlist; do
1307     actioncmd="$actioncmd --avoid=$module"
1308   done
1309   if test -n "$lgpl"; then
1310     actioncmd="$actioncmd --lgpl"
1311   fi
1312   if test -n "$libtool"; then
1313     actioncmd="$actioncmd --libtool"
1314   fi
1315   actioncmd="$actioncmd --macro-prefix=$macro_prefix"
1316   actioncmd="$actioncmd `echo $specified_modules`"
1317
1318   # Create lib/Makefile.am.
1319   func_dest_tmpfilename $sourcebase/Makefile.am
1320   func_emit_lib_Makefile_am > "$tmpfile"
1321   if test -f "$destdir"/$sourcebase/Makefile.am; then
1322     if cmp "$destdir"/$sourcebase/Makefile.am "$tmpfile" > /dev/null; then
1323       rm -f "$tmpfile"
1324     else
1325       if $doit; then
1326         echo "Updating $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1327         mv -f "$destdir"/$sourcebase/Makefile.am "$destdir"/$sourcebase/Makefile.am~
1328         mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1329       else
1330         echo "Update $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1331         rm -f "$tmpfile"
1332       fi
1333     fi
1334   else
1335     if $doit; then
1336       echo "Creating $sourcebase/Makefile.am"
1337       mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1338     else
1339       echo "Create $sourcebase/Makefile.am"
1340       rm -f "$tmpfile"
1341     fi
1342   fi
1343
1344   # Create m4/gnulib-cache.m4.
1345   func_dest_tmpfilename $m4base/gnulib-cache.m4
1346   (
1347     func_emit_copyright_notice
1348     echo "#"
1349     echo "# This file represents the specification of how gnulib-tool is used."
1350     echo "# It acts as a cache: It is written and read by gnulib-tool."
1351     echo "# In projects using CVS, this file is meant to be stored in CVS,"
1352     echo "# like the configure.ac and various Makefile.am files."
1353     echo
1354     echo
1355     echo "# Specification in the form of a command-line invocation:"
1356     echo "#   $actioncmd"
1357     echo
1358     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
1359     echo "gl_MODULES(["`echo $specified_modules`"])"
1360     echo "gl_AVOID([$avoidlist])"
1361     echo "gl_SOURCE_BASE([$sourcebase])"
1362     echo "gl_M4_BASE([$m4base])"
1363     echo "gl_DOC_BASE([$docbase])"
1364     echo "gl_TESTS_BASE([$testsbase])"
1365     echo "gl_LIB([$libname])"
1366     test -z "$lgpl" || echo "gl_LGPL"
1367     test -z "$libtool" || echo "gl_LIBTOOL"
1368     echo "gl_MACRO_PREFIX([$macro_prefix])"
1369   ) > "$tmpfile"
1370   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1371     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
1372       rm -f "$tmpfile"
1373     else
1374       if $doit; then
1375         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1376         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
1377         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1378       else
1379         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1380         if false; then
1381           cat "$tmpfile"
1382           echo
1383           echo "# gnulib-cache.m4 ends here"
1384         fi
1385         rm -f "$tmpfile"
1386       fi
1387     fi
1388   else
1389     if $doit; then
1390       echo "Creating $m4base/gnulib-cache.m4"
1391       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1392     else
1393       echo "Create $m4base/gnulib-cache.m4"
1394       cat "$tmpfile"
1395       rm -f "$tmpfile"
1396     fi
1397   fi
1398
1399   # Create m4/gnulib-comp.m4.
1400   func_dest_tmpfilename $m4base/gnulib-comp.m4
1401   (
1402     func_emit_copyright_notice
1403     echo "#"
1404     echo "# This file represents the compiled summary of the specification in"
1405     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
1406     echo "# to be invoked from configure.ac."
1407     echo "# In projects using CVS, this file can be treated like other built files."
1408     echo
1409     echo
1410     echo "# This macro should be invoked from $configure_ac, in the section"
1411     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
1412     echo "# any checks for libraries, header files, types and library functions."
1413     echo "AC_DEFUN([${macro_prefix}_EARLY],"
1414     echo "["
1415     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
1416     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1417       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
1418     fi
1419     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1420       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
1421     fi
1422     if grep gl_LOCK "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1423       echo "  AC_REQUIRE([gl_LOCK])"
1424     fi
1425     echo "])"
1426     echo
1427     echo "# This macro should be invoked from $configure_ac, in the section"
1428     echo "# \"Check for header files, types and library functions\"."
1429     echo "AC_DEFUN([${macro_prefix}_INIT],"
1430     echo "["
1431     if test -z "$libtool"; then
1432       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1433     else
1434       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1435     fi
1436     if test "$auxdir" != "build-aux"; then
1437       sed_replace_build_aux='
1438         :a
1439         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1440           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1441           ba
1442         }'
1443       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1444     else
1445       sed_replace_build_aux=
1446     fi
1447     for module in $modules; do
1448       func_verify_module
1449       if test -n "$module"; then
1450         func_get_autoconf_snippet "$module" \
1451           | sed -e '/^$/d;' -e 's/^/  /' \
1452                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
1453                 -e "$sed_replace_build_aux"
1454         if test "$module" = 'alloca' && test -n "$libtool"; then
1455           echo 'changequote(,)dnl'
1456           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
1457           echo 'changequote([, ])dnl'
1458           echo 'AC_SUBST([LTALLOCA])'
1459         fi
1460       fi
1461     done
1462     echo "])"
1463     echo
1464     echo "# This macro records the list of files which have been installed by"
1465     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
1466     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
1467     echo "$files" | sed -e 's,^,  ,'
1468     echo "])"
1469   ) > "$tmpfile"
1470   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1471     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
1472       rm -f "$tmpfile"
1473     else
1474       if $doit; then
1475         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1476         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
1477         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1478       else
1479         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1480         if false; then
1481           cat "$tmpfile"
1482           echo
1483           echo "# gnulib-comp.m4 ends here"
1484         fi
1485         rm -f "$tmpfile"
1486       fi
1487     fi
1488   else
1489     if $doit; then
1490       echo "Creating $m4base/gnulib-comp.m4"
1491       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1492     else
1493       echo "Create $m4base/gnulib-comp.m4"
1494       cat "$tmpfile"
1495       rm -f "$tmpfile"
1496     fi
1497   fi
1498
1499   if test -n "$inctests"; then
1500     # Create tests/Makefile.am.
1501     func_dest_tmpfilename $testsbase/Makefile.am
1502     func_emit_tests_Makefile_am > "$tmpfile"
1503     if test -f "$destdir"/$testsbase/Makefile.am; then
1504       if cmp "$destdir"/$testsbase/Makefile.am "$tmpfile" > /dev/null; then
1505         rm -f "$tmpfile"
1506       else
1507         if $doit; then
1508           echo "Updating $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1509           mv -f "$destdir"/$testsbase/Makefile.am "$destdir"/$testsbase/Makefile.am~
1510           mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1511         else
1512           echo "Update $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1513           rm -f "$tmpfile"
1514         fi
1515       fi
1516     else
1517       if $doit; then
1518         echo "Creating $testsbase/Makefile.am"
1519         mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1520       else
1521         echo "Create $testsbase/Makefile.am"
1522         rm -f "$tmpfile"
1523       fi
1524     fi
1525   fi
1526
1527   rm -rf "$tmp"
1528   # Undo the effect of the previous 'trap' command. Some shellology:
1529   # We cannot use "trap - 0 1 2 3 15", because Solaris sh would attempt to
1530   # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
1531   # exit); for the others we need to call 'exit' explicitly. The value of $? is
1532   # 128 + signal number and is set before the trap-registered command is run.
1533   trap '' 0
1534   trap 'exit $?' 1 2 3 15
1535
1536   echo "Finished."
1537   echo
1538   echo "You may need to add #include directives for the following .h files."
1539   (
1540    # First the #include <...> directives without #ifs, sorted for convenience.
1541    for module in $modules; do
1542      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1543        :
1544      else
1545        func_get_include_directive "$module" | grep -v 'include "'
1546      fi
1547    done | LC_ALL=C sort -u
1548    # Then the #include "..." directives without #ifs, sorted for convenience.
1549    for module in $modules; do
1550      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1551        :
1552      else
1553        func_get_include_directive "$module" | grep 'include "'
1554      fi
1555    done | LC_ALL=C sort -u
1556    # Then the #include directives that are surrounded by #ifs. Not sorted.
1557    for module in $modules; do
1558      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1559        func_get_include_directive "$module"
1560      fi
1561    done
1562   ) | sed -e '/^$/d;' -e 's/^/  /'
1563   echo
1564   echo "Don't forget to"
1565   echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1566   if test -n "$inctests"; then
1567     echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1568   fi
1569   sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
1570   sourcebase_base=`basename "$sourcebase"`
1571   echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
1572   if test -n "$inctests"; then
1573     testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
1574     testsbase_base=`basename "$testsbase"`
1575     echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
1576   fi
1577   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
1578   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
1579   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
1580 }
1581
1582 # func_create_testdir testdir modules
1583 # Input:
1584 # - auxdir          directory relative to destdir where to place build aux files
1585 func_create_testdir ()
1586 {
1587   testdir="$1"
1588   modules="$2"
1589   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
1590
1591   # Determine final module list.
1592   func_modules_transitive_closure
1593   echo "Module list with included dependencies:"
1594   echo "$modules" | sed -e 's/^/  /'
1595
1596   # Add the dummy module if needed.
1597   func_modules_add_dummy
1598
1599   # Determine final file list.
1600   func_modules_to_filelist
1601   echo "File list:"
1602   echo "$files" | sed -e 's/^/  /'
1603
1604   # Create directories.
1605   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
1606     if test "$d" = build-aux; then
1607       mkdir -p "$testdir/$auxdir"
1608     else
1609       mkdir -p "$testdir/$d"
1610     fi
1611   done
1612
1613   # Copy files or make symbolic links.
1614   for f in $files; do
1615     case "$f" in
1616       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1617       *) g="$f" ;;
1618     esac
1619     ln "$gnulib_dir/$f" "$testdir/$g" 2>/dev/null ||
1620     if test -z "$symbolic"; then
1621       cp -p "$gnulib_dir/$f" "$testdir/$g"
1622     else
1623       ln -s "$gnulib_dir/$f" "$testdir/$g"
1624     fi
1625   done
1626
1627   # Create lib/Makefile.am.
1628   mkdir -p "$testdir/lib"
1629   func_emit_lib_Makefile_am > "$testdir/lib/Makefile.am"
1630
1631   # Create m4/Makefile.am.
1632   mkdir -p "$testdir/m4"
1633   (echo "## Process this file with automake to produce Makefile.in."
1634    echo
1635    echo "EXTRA_DIST ="
1636    for f in $files; do
1637      case "$f" in
1638        m4/* )
1639          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
1640      esac
1641    done
1642   ) > "$testdir/m4/Makefile.am"
1643
1644   subdirs="lib m4"
1645   subdirs_with_configure_ac=""
1646
1647   if test -f "$testdir"/m4/gettext.m4; then
1648     # Avoid stupid error message from automake:
1649     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
1650     mkdir -p "$testdir/po"
1651     (echo "## Process this file with automake to produce Makefile.in."
1652     ) > "$testdir/po/Makefile.am"
1653     subdirs="$subdirs po"
1654   fi
1655
1656   if test -n "$inctests"; then
1657     test -d "$testdir/tests" || mkdir "$testdir/tests"
1658     # Create tests/Makefile.am.
1659     sourcebase=lib
1660     m4base=m4
1661     testsbase=tests
1662     func_emit_tests_Makefile_am > "$testdir/tests/Makefile.am"
1663     # Create tests/configure.ac.
1664     (echo "# Process this file with autoconf to produce a configure script."
1665      echo "AC_INIT([dummy], [0])"
1666      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
1667      echo "AM_INIT_AUTOMAKE"
1668      echo
1669      echo "AM_CONFIG_HEADER([config.h])"
1670      echo
1671      echo "AC_PROG_CC"
1672      echo "AC_PROG_INSTALL"
1673      echo "AC_PROG_MAKE_SET"
1674      echo "AC_PROG_RANLIB"
1675      echo
1676      if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1677        echo "AC_GNU_SOURCE"
1678        echo
1679      fi
1680      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1681        echo "gl_USE_SYSTEM_EXTENSIONS"
1682        echo
1683      fi
1684      if grep gl_LOCK "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1685        echo "gl_LOCK"
1686        echo
1687      fi
1688      if test -z "$libtool"; then
1689        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1690      else
1691        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1692      fi
1693      if test "$auxdir" != "build-aux"; then
1694        sed_replace_build_aux='
1695          :a
1696          /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1697            s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
1698            ba
1699          }'
1700        sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1701      else
1702        sed_replace_build_aux=
1703      fi
1704      # We don't have explicit ordering constraints between the various
1705      # autoconf snippets. It's cleanest to put those of the library before
1706      # those of the tests.
1707      for module in $modules; do
1708        func_verify_nontests_module
1709        if test -n "$module"; then
1710          func_get_autoconf_snippet "$module" \
1711            | sed -e "$sed_replace_build_aux"
1712        fi
1713      done
1714      for module in $modules; do
1715        func_verify_tests_module
1716        if test -n "$module"; then
1717          func_get_autoconf_snippet "$module" \
1718            | sed -e "$sed_replace_build_aux"
1719        fi
1720      done
1721      echo
1722      # Usually tests/config.h will be a superset of config.h. Verify this by
1723      # "merging" config.h into tests/config.h; look out for gcc warnings.
1724      echo "AH_TOP([#include \"../config.h\"])"
1725      echo
1726      echo "AC_OUTPUT([Makefile])"
1727     ) > "$testdir/tests/configure.ac"
1728     subdirs="$subdirs tests"
1729     subdirs_with_configure_ac="$subdirs_with_configure_ac tests"
1730   fi
1731
1732   # Create Makefile.am.
1733   (echo "## Process this file with automake to produce Makefile.in."
1734    echo
1735    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1736    echo
1737    echo "SUBDIRS = $subdirs"
1738    echo
1739    echo "ACLOCAL_AMFLAGS = -I m4"
1740   ) > "$testdir/Makefile.am"
1741
1742   # Create configure.ac.
1743   (echo "# Process this file with autoconf to produce a configure script."
1744    echo "AC_INIT([dummy], [0])"
1745    if test "$auxdir" != "."; then
1746      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1747    fi
1748    echo "AM_INIT_AUTOMAKE"
1749    echo
1750    echo "AM_CONFIG_HEADER([config.h])"
1751    echo
1752    echo "AC_PROG_CC"
1753    echo "AC_PROG_INSTALL"
1754    echo "AC_PROG_MAKE_SET"
1755    echo "AC_PROG_RANLIB"
1756    echo
1757    if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1758      echo "AC_GNU_SOURCE"
1759      echo
1760    fi
1761    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1762      echo "gl_USE_SYSTEM_EXTENSIONS"
1763      echo
1764    fi
1765    if grep gl_LOCK "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1766      echo "gl_LOCK"
1767      echo
1768    fi
1769    if test -z "$libtool"; then
1770      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1771    else
1772      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1773    fi
1774    if test "$auxdir" != "build-aux"; then
1775      sed_replace_build_aux='
1776        :a
1777        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1778          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1779          ba
1780        }'
1781      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1782    else
1783      sed_replace_build_aux=
1784    fi
1785    for module in $modules; do
1786      func_verify_nontests_module
1787      if test -n "$module"; then
1788        func_get_autoconf_snippet "$module" \
1789          | sed -e "$sed_replace_build_aux"
1790      fi
1791    done
1792    echo
1793    if test -n "$subdirs_with_configure_ac"; then
1794      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
1795    fi
1796    makefiles="Makefile"
1797    for d in $subdirs; do
1798      # For subdirs that have a configure.ac by their own, it's the subdir's
1799      # configure.ac which creates the subdir's Makefile.am, not this one.
1800      case " $subdirs_with_configure_ac " in
1801        *" $d "*) ;;
1802        *) makefiles="$makefiles $d/Makefile" ;;
1803      esac
1804    done
1805    echo "AC_OUTPUT([$makefiles])"
1806   ) > "$testdir/configure.ac"
1807
1808   # Create autogenerated files.
1809   (cd "$testdir"
1810    echo "executing ${AUTORECONF} --force --install"
1811    ${AUTORECONF} --force --install
1812   )
1813   if grep '^BUILT_SOURCES *+=' "$testdir/lib/Makefile.am" > /dev/null; then
1814     (cd "$testdir"
1815      ./configure
1816        cd lib
1817        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
1818        make built_sources
1819        cd ..
1820      make distclean
1821     )
1822   fi
1823 }
1824
1825 # func_create_megatestdir megatestdir allmodules
1826 # Input:
1827 # - auxdir          directory relative to destdir where to place build aux files
1828 func_create_megatestdir ()
1829 {
1830   megatestdir="$1"
1831   allmodules="$2"
1832   if test -z "$allmodules"; then
1833     allmodules=`func_all_modules`
1834   fi
1835
1836   megasubdirs=
1837   # First, all modules one by one.
1838   for onemodule in $allmodules; do
1839     func_create_testdir "$megatestdir/$onemodule" $onemodule
1840     megasubdirs="${megasubdirs}$onemodule "
1841   done
1842   # Then, all modules all together.
1843   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
1844   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
1845   func_create_testdir "$megatestdir/ALL" "$allmodules"
1846   megasubdirs="${megasubdirs}ALL"
1847
1848   # Create Makefile.am.
1849   (echo "## Process this file with automake to produce Makefile.in."
1850    echo
1851    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1852    echo
1853    echo "SUBDIRS = $megasubdirs"
1854   ) > "$megatestdir/Makefile.am"
1855
1856   # Create configure.ac.
1857   (echo "# Process this file with autoconf to produce a configure script."
1858    echo "AC_INIT([dummy], [0])"
1859    if test "$auxdir" != "."; then
1860      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1861    fi
1862    echo "AM_INIT_AUTOMAKE"
1863    echo
1864    echo "AC_PROG_MAKE_SET"
1865    echo
1866    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
1867    echo "AC_OUTPUT([Makefile])"
1868   ) > "$megatestdir/configure.ac"
1869
1870   # Create autogenerated files.
1871   (cd "$megatestdir"
1872    # Do not use "${AUTORECONF} --install", because autoreconf operates
1873    # recursively, but the subdirectories are already finished, therefore
1874    # calling autoreconf here would only waste lots of CPU time.
1875    echo "executing ${ACLOCAL}"
1876    ${ACLOCAL}
1877    echo "executing mkdir build-aux"
1878    mkdir build-aux
1879    echo "executing ${AUTOCONF}"
1880    ${AUTOCONF}
1881    echo "executing ${AUTOMAKE} --add-missing --copy"
1882    ${AUTOMAKE} --add-missing --copy
1883   )
1884 }
1885
1886 case $mode in
1887   "" )
1888     func_fatal_error "no mode specified" ;;
1889
1890   list )
1891     func_all_modules
1892     ;;
1893
1894   import | update )
1895
1896     # Where to import.
1897     if test -z "$destdir"; then
1898       destdir=.
1899     fi
1900     test -d "$destdir" \
1901       || func_fatal_error "destination directory does not exist: $destdir"
1902
1903     # Prefer configure.ac to configure.in.
1904     if test -f "$destdir"/configure.ac; then
1905       configure_ac="$destdir/configure.ac"
1906     else
1907       if test -f "$destdir"/configure.in; then
1908         configure_ac="$destdir/configure.in"
1909       else
1910         func_fatal_error "cannot find $destdir/configure.ac"
1911       fi
1912     fi
1913
1914     # Analyze configure.ac.
1915     guessed_auxdir="."
1916     guessed_libtool=
1917     my_sed_traces='
1918       s,#.*$,,
1919       s,^dnl .*$,,
1920       s, dnl .*$,,
1921       /AC_CONFIG_AUX_DIR/ {
1922         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
1923       }
1924       /A[CM]_PROG_LIBTOOL/ {
1925         s,^.*$,guessed_libtool=true,p
1926       }'
1927     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
1928
1929     if test -z "$auxdir"; then
1930       auxdir="$guessed_auxdir"
1931     fi
1932
1933     # Determine where to apply func_import.
1934     if test -n "$m4base"; then
1935       # Apply func_import to a particular gnulib directory.
1936       # Any number of additional modules can be given.
1937       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
1938         # First use of gnulib in the given m4base.
1939         test -n "$supplied_libname" || supplied_libname=true
1940         test -n "$sourcebase" || sourcebase="lib"
1941         test -n "$docbase" || docbase="doc"
1942         test -n "$testsbase" || testsbase="tests"
1943         test -n "$macro_prefix" || macro_prefix="gl"
1944       fi
1945       func_import "$*"
1946     else
1947       # Apply func_import to all gnulib directories.
1948       # To get this list of directories, look at Makefile.am. (Not at
1949       # configure, because it may be omitted from CVS. Also, don't run
1950       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
1951       m4dirs=
1952       m4dirs_count=0
1953       if test -f "$destdir"/Makefile.am; then
1954         aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[         ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
1955         m4dir_is_next=
1956         for arg in $aclocal_amflags; do
1957           if test -n "$m4dir_is_next"; then
1958             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
1959             case "$arg" in
1960               /*) ;;
1961               *)
1962                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
1963                   m4dirs="$m4dirs $arg"
1964                   m4dirs_count=`expr $m4dirs_count + 1`
1965                 fi
1966                 ;;
1967             esac
1968           else
1969             if test "X$arg" = "X-I"; then
1970               m4dir_is_next=yes
1971             else
1972               m4dir_is_next=
1973             fi
1974           fi
1975         done
1976       else
1977         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
1978         if test -f "$destdir"/aclocal.m4; then
1979           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
1980           sedexpr2='s,^[^/]*$,.,'
1981           sedexpr3='s,/[^/]*$,,'
1982           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort | LC_ALL=C uniq`
1983           m4dirs_count=`echo "$m4dirs" | wc -l`
1984         fi
1985       fi
1986       if test $m4dirs_count = 0; then
1987         # First use of gnulib in a package.
1988         # Any number of additional modules can be given.
1989         test -n "$supplied_libname" || supplied_libname=true
1990         test -n "$sourcebase" || sourcebase="lib"
1991         m4base="m4"
1992         test -n "$docbase" || docbase="doc"
1993         test -n "$testsbase" || testsbase="tests"
1994         test -n "$macro_prefix" || macro_prefix="gl"
1995         func_import "$*"
1996       else
1997         if test $m4dirs_count = 1; then
1998           # There's only one use of gnulib here. Assume the user means it.
1999           # Any number of additional modules can be given.
2000           for m4base in $m4dirs; do
2001             func_import "$*"
2002           done
2003         else
2004           # Ambiguous - guess what the user meant.
2005           if test $# = 0; then
2006             # No further arguments. Guess the user wants to update all of them.
2007             for m4base in $m4dirs; do
2008               func_import
2009             done
2010           else
2011             # Really ambiguous.
2012             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
2013           fi
2014         fi
2015       fi
2016     fi
2017     ;;
2018
2019   create-testdir )
2020     if test -z "$destdir"; then
2021       func_fatal_error "please specify --dir option"
2022     fi
2023     mkdir "$destdir"
2024     test -d "$destdir" \
2025       || func_fatal_error "could not create destination directory"
2026     test -n "$auxdir" || auxdir="build-aux"
2027     func_create_testdir "$destdir" "$*"
2028     ;;
2029
2030   create-megatestdir )
2031     if test -z "$destdir"; then
2032       func_fatal_error "please specify --dir option"
2033     fi
2034     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2035     test -n "$auxdir" || auxdir="build-aux"
2036     func_create_megatestdir "$destdir" "$*"
2037     ;;
2038
2039   test )
2040     test -n "$destdir" || destdir=testdir$$
2041     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2042     test -n "$auxdir" || auxdir="build-aux"
2043     func_create_testdir "$destdir" "$*"
2044     cd "$destdir"
2045       mkdir build
2046       cd build
2047         ../configure
2048         make
2049         make check
2050         make distclean
2051         remaining=`find . -type f -print`
2052         if test -n "$remaining"; then
2053           echo "Remaining files:" $remaining 1>&2
2054           echo "gnulib-tool: *** Stop." 1>&2
2055           exit 1
2056         fi
2057       cd ..
2058     cd ..
2059     rm -rf "$destdir"
2060     ;;
2061
2062   megatest )
2063     test -n "$destdir" || destdir=testdir$$
2064     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2065     test -n "$auxdir" || auxdir="build-aux"
2066     func_create_megatestdir "$destdir" "$*"
2067     cd "$destdir"
2068       mkdir build
2069       cd build
2070         ../configure
2071         make
2072         make check
2073         make distclean
2074         remaining=`find . -type f -print`
2075         if test -n "$remaining"; then
2076           echo "Remaining files:" $remaining 1>&2
2077           echo "gnulib-tool: *** Stop." 1>&2
2078           exit 1
2079         fi
2080       cd ..
2081     cd ..
2082     rm -rf "$destdir"
2083     ;;
2084
2085   extract-description )
2086     for module
2087     do
2088       func_verify_module
2089       if test -n "$module"; then
2090         func_get_description "$module"
2091       fi
2092     done
2093     ;;
2094
2095   extract-filelist )
2096     for module
2097     do
2098       func_verify_module
2099       if test -n "$module"; then
2100         func_get_filelist "$module"
2101       fi
2102     done
2103     ;;
2104
2105   extract-dependencies )
2106     for module
2107     do
2108       func_verify_module
2109       if test -n "$module"; then
2110         func_get_dependencies "$module"
2111       fi
2112     done
2113     ;;
2114
2115   extract-autoconf-snippet )
2116     for module
2117     do
2118       func_verify_module
2119       if test -n "$module"; then
2120         func_get_autoconf_snippet "$module"
2121       fi
2122     done
2123     ;;
2124
2125   extract-automake-snippet )
2126     for module
2127     do
2128       func_verify_module
2129       if test -n "$module"; then
2130         func_get_automake_snippet "$module"
2131       fi
2132     done
2133     ;;
2134
2135   extract-include-directive )
2136     for module
2137     do
2138       func_verify_module
2139       if test -n "$module"; then
2140         func_get_include_directive "$module"
2141       fi
2142     done
2143     ;;
2144
2145   extract-license )
2146     for module
2147     do
2148       func_verify_module
2149       if test -n "$module"; then
2150         func_get_license "$module"
2151       fi
2152     done
2153     ;;
2154
2155   extract-maintainer )
2156     for module
2157     do
2158       func_verify_module
2159       if test -n "$module"; then
2160         func_get_maintainer "$module"
2161       fi
2162     done
2163     ;;
2164
2165   extract-tests-module )
2166     for module
2167     do
2168       func_verify_module
2169       if test -n "$module"; then
2170         func_get_tests_module "$module"
2171       fi
2172     done
2173     ;;
2174
2175   * )
2176     func_fatal_error "unknown operation mode --$mode" ;;
2177 esac
2178
2179 exit 0