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