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