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