* openat.c (openat): Use ?:, not if, to work around GCC bug 4210
[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-05-09 02:38:16 $'
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 "CLEANFILES ="
753   echo "DISTCLEANFILES ="
754   echo "MAINTAINERCLEANFILES ="
755   echo
756   echo "AM_CPPFLAGS ="
757   echo
758   for module in $modules; do
759     func_verify_nontests_module
760     if test -n "$module"; then
761       {
762         func_get_automake_snippet "$module" |
763           sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g'
764         if test "$module" = 'alloca'; then
765           echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
766         fi
767       } > amsnippet.tmp
768       # Skip the contents if its entirely empty.
769       if grep '[^       ]' amsnippet.tmp > /dev/null ; then
770         echo "## begin gnulib module $module"
771         echo
772         cat amsnippet.tmp
773         echo "## end   gnulib module $module"
774         echo
775       fi
776       rm -f amsnippet.tmp
777     fi
778   done
779   echo
780   echo "# Makefile.am ends here"
781 }
782
783 # func_emit_tests_Makefile_am
784 # emits the contents of tests/Makefile.am to standard output.
785 # Input:
786 # - modules         list of modules, including dependencies
787 # - libname         library name
788 # - libtool         true if libtool will be used, blank otherwise
789 # - sourcebase      relative directory containing lib source code
790 # - m4base          relative directory containing autoconf macros
791 # - testsbase       relative directory containing unit test code
792 func_emit_tests_Makefile_am ()
793 {
794   if test -n "$libtool"; then
795     libext=la
796   else
797     libext=a
798   fi
799   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
800   echo "## Process this file with automake to produce Makefile.in."
801   func_emit_copyright_notice
802   echo
803   # Generate dependencies here, since it eases the debugging of test failures.
804   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
805   echo
806   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
807   echo
808   # Nothing is being added to SUBDIRS; nevertheless the existence of this
809   # variable is needed to avoid an error from automake:
810   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
811   echo "SUBDIRS ="
812   echo "TESTS ="
813   echo "TESTS_ENVIRONMENT ="
814   echo "noinst_PROGRAMS ="
815   echo "check_PROGRAMS ="
816   echo "noinst_HEADERS ="
817   echo "EXTRA_DIST ="
818   echo "BUILT_SOURCES ="
819   echo "SUFFIXES ="
820   echo "MOSTLYCLEANFILES ="
821   echo "CLEANFILES ="
822   echo "DISTCLEANFILES ="
823   echo "MAINTAINERCLEANFILES ="
824   echo
825   echo "AM_CPPFLAGS = \\"
826   echo "  -I. -I\$(srcdir) \\"
827   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
828   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
829   echo
830   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
831   echo
832   for module in $modules; do
833     func_verify_tests_module
834     if test -n "$module"; then
835       func_get_automake_snippet "$module" > amsnippet.tmp
836       # Skip the contents if its entirely empty.
837       if grep '[^       ]' amsnippet.tmp > /dev/null ; then
838         echo "## begin gnulib module $module"
839         echo
840         cat amsnippet.tmp
841         echo "## end   gnulib module $module"
842         echo
843       fi
844       rm -f amsnippet.tmp
845     fi
846   done
847   echo "# Clean up after Solaris cc."
848   echo "clean-local:"
849   echo "        rm -rf SunWS_cache"
850   echo
851   echo "# Makefile.am ends here"
852 }
853
854 # func_import modules
855 # Uses also the variables
856 # - destdir         target directory
857 # - libname         library name
858 # - sourcebase      directory relative to destdir where to place source code
859 # - m4base          directory relative to destdir where to place *.m4 macros
860 # - testsbase       directory relative to destdir where to place unit test code
861 # - auxdir          directory relative to destdir where to place build aux files
862 # - inctests        true if --with-tests was given, blank otherwise
863 # - avoidlist       list of modules to avoid, from --avoid
864 # - lgpl            true if library's license shall be LGPL, blank otherwise
865 # - libtool         true if libtool will be used, blank otherwise
866 # - guessed_libtool true if the configure.ac file uses libtool, blank otherwise
867 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
868 # - doit            : if actions shall be executed, false if only to be printed
869 # - symbolic        true if files should be symlinked, copied otherwise
870 func_import ()
871 {
872   # Get the cached settings.
873   cached_specified_modules=
874   cached_avoidlist=
875   cached_sourcebase=
876   cached_m4base=
877   cached_testsbase=
878   cached_libname=
879   cached_lgpl=
880   cached_libtool=
881   cached_macro_prefix=
882   cached_files=
883   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
884     my_sed_traces='
885       s,#.*$,,
886       s,^dnl .*$,,
887       s, dnl .*$,,
888       /gl_MODULES(/ {
889         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
890       }
891       /gl_AVOID(/ {
892         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
893       }
894       /gl_SOURCE_BASE(/ {
895         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
896       }
897       /gl_M4_BASE(/ {
898         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
899       }
900       /gl_TESTS_BASE(/ {
901         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
902       }
903       /gl_LIB(/ {
904         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
905       }
906       /gl_LGPL/ {
907         s,^.*$,cached_lgpl=true,p
908       }
909       /gl_LIBTOOL/ {
910         s,^.*$,cached_libtool=true,p
911       }
912       /gl_MACRO_PREFIX(/ {
913         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
914       }'
915     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
916     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
917       my_sed_traces='
918         s,#.*$,,
919         s,^dnl .*$,,
920         s, dnl .*$,,
921         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
922           s,^.*$,cached_files=",p
923           n
924           ta
925           :a
926           s,^\]).*$,",
927           tb
928           p
929           n
930           ba
931           :b
932           p
933         }'
934       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
935     fi
936   fi
937
938   # Merge the cached settings with the specified ones.
939   # The m4base must be the same as expected from the pathname.
940   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
941     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
942   fi
943   # Append the cached and the specified module names. So that
944   # "gnulib-tool --import foo" means to add the module foo.
945   specified_modules="$cached_specified_modules $1"
946   # Append the cached and the specified avoidlist. This is probably better
947   # than dropping the cached one when --avoid is specified at least once.
948   avoidlist=`echo $cached_avoidlist $avoidlist`
949   # The sourcebase defaults to the cached one.
950   if test -z "$sourcebase"; then
951     sourcebase="$cached_sourcebase"
952     if test -z "$sourcebase"; then
953       func_fatal_error "missing --source-base option"
954     fi
955   fi
956   # The testsbase defaults to the cached one.
957   if test -z "$testsbase"; then
958     testsbase="$cached_testsbase"
959     if test -z "$testsbase"; then
960       func_fatal_error "missing --tests-base option"
961     fi
962   fi
963   # The libname defaults to the cached one.
964   if test -z "$supplied_libname"; then
965     libname="$cached_libname"
966     if test -z "$libname"; then
967       func_fatal_error "missing --lib option"
968     fi
969   fi
970   # Require LGPL if specified either way.
971   if test -z "$lgpl"; then
972     lgpl="$cached_lgpl"
973   fi
974   # Use libtool if specified either way, or if guessed.
975   if test -z "$libtool"; then
976     if test -n "$cached_m4base"; then
977       libtool="$cached_libtool"
978     else
979       libtool="$guessed_libtool"
980     fi
981   fi
982   # The macro_prefix defaults to the cached one.
983   if test -z "$macro_prefix"; then
984     macro_prefix="$cached_macro_prefix"
985     if test -z "$macro_prefix"; then
986       func_fatal_error "missing --macro-prefix option"
987     fi
988   fi
989
990   # Canonicalize the list of specified modules.
991   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
992
993   # Determine final module list.
994   modules="$specified_modules"
995   func_modules_transitive_closure
996   echo "Module list with included dependencies:"
997   echo "$modules" | sed -e 's/^/  /'
998
999   # Add the dummy module if needed.
1000   func_modules_add_dummy
1001
1002   # If --lgpl, check the license of modules are compatible.
1003   if test -n "$lgpl"; then
1004     for module in $modules; do
1005       license=`func_get_license $module`
1006       case $license in
1007         LGPL | 'GPLed build tool' | 'public domain' | 'unlimited') ;;
1008         *) func_fatal_error "incompatible license on module $module: $license" ;;
1009       esac
1010     done
1011   fi
1012
1013   # Determine final file list.
1014   func_modules_to_filelist
1015   echo "File list:"
1016   echo "$files" | sed -e 's/^/  /'
1017
1018   test -n "$files" \
1019     || func_fatal_error "refusing to do nothing"
1020
1021   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1022   new_files="$files m4/gnulib-tool.m4"
1023   old_files="$cached_files"
1024   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1025     old_files="$old_files m4/gnulib-tool.m4"
1026   fi
1027
1028   # Create directories.
1029   if test ! -d "$destdir/$sourcebase"; then
1030     if $doit; then
1031       echo "Creating directory $destdir/$sourcebase"
1032       mkdir "$destdir/$sourcebase" || func_fatal_error "failed"
1033     else
1034       echo "Create directory $destdir/$sourcebase"
1035     fi
1036   fi
1037   if test ! -d "$destdir/$m4base"; then
1038     if $doit; then
1039       echo "Creating directory $destdir/$m4base"
1040       mkdir "$destdir/$m4base" || func_fatal_error "failed"
1041     else
1042       echo "Create directory $destdir/$m4base"
1043     fi
1044   fi
1045   if test -n "$inctests"; then
1046     if test ! -d "$destdir/$testsbase"; then
1047       if $doit; then
1048         echo "Creating directory $destdir/$testsbase"
1049         mkdir "$destdir/$testsbase" || func_fatal_error "failed"
1050       else
1051         echo "Create directory $destdir/$testsbase"
1052       fi
1053     fi
1054   fi
1055   if test ! -d "$destdir/$auxdir"; then
1056     if $doit; then
1057       echo "Creating directory $destdir/$auxdir"
1058       mkdir "$destdir/$auxdir" || func_fatal_error "failed"
1059     else
1060       echo "Create directory $destdir/$auxdir"
1061     fi
1062   fi
1063
1064   func_tmpdir
1065   trap 'rm -rf "$tmp"' 0 1 2 3 15
1066   # func_dest_tmpfilename file
1067   # determines the name of a temporary file (file is relative to destdir).
1068   # Sets variable:
1069   #   - tmpfile       absolute filename of the temporary file
1070   func_dest_tmpfilename ()
1071   {
1072     if $doit; then
1073       # Put the new contents of $file in a file in the same directory (needed
1074       # to guarantee that an 'mv' to "$destdir/$file" works).
1075       tmpfile="$destdir/$1.tmp"
1076     else
1077       # Put the new contents of $file in a file in a temporary directory
1078       # (because the directory of "$file" might not exist).
1079       tmpfile="$tmp"/`basename "$1"`.tmp
1080     fi
1081   }
1082
1083   # Copy files or make symbolic links. Remove obsolete files.
1084   delimiter='   '
1085   for f in $old_files; do
1086     case "$f" in
1087       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1088       lib/*) g=`echo "$f" | sed -e "s,^lib/,$cached_sourcebase/,"` ;;
1089       m4/*) g=`echo "$f" | sed -e "s,^m4/,$cached_m4base/,"` ;;
1090       tests/*) g=`echo "$f" | sed -e "s,^tests/,$cached_testsbase/,"` ;;
1091       *) g="$f" ;;
1092     esac
1093     echo "$g""$delimiter""$f"
1094   done | LC_ALL=C sort > "$tmp"/old-files
1095   for f in $new_files; do
1096     case "$f" in
1097       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1098       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1099       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1100       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1101       *) g="$f" ;;
1102     esac
1103     echo "$g""$delimiter""$f"
1104   done | LC_ALL=C sort > "$tmp"/new-files
1105   # First the files that are in old-files, but not in new-files:
1106   sed_take_first_column='s,'"$delimiter"'.*,,'
1107   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1108     # Remove the file. Do nothing if the user already removed it.
1109     if test -f "$destdir/$g"; then
1110       if $doit; then
1111         echo "Removing file $g (backup in ${g}~)"
1112         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1113       else
1114         echo "Remove file $g (backup in ${g}~)"
1115       fi
1116     fi
1117   done
1118   # func_add_or_update handles a file that ought to be present afterwards.
1119   # Uses parameters f, g, already_present.
1120   func_add_or_update ()
1121   {
1122     func_dest_tmpfilename "$g"
1123     cp "$gnulib_dir/$f" "$tmpfile" || func_fatal_error "failed"
1124     if test -n "$lgpl"; then
1125       # Update license.
1126       case "$f" in
1127         lib/*)
1128           sed -e 's/GNU General/GNU Lesser General/g' \
1129               -e 's/version 2\([ ,]\)/version 2.1\1/g' \
1130             < "$gnulib_dir/$f" > "$tmpfile" || func_fatal_error "failed"
1131           ;;
1132       esac
1133     fi
1134     if test -f "$destdir/$g"; then
1135       # The file already exists.
1136       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1137         : # The file has not changed.
1138       else
1139         # Replace the file.
1140         if $doit; then
1141           if test -n "$already_present"; then
1142             echo "Updating file $g (backup in ${g}~)"
1143           else
1144             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1145           fi
1146           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1147           if test -n "$symbolic" && cmp "$gnulib_dir/$f" "$tmpfile" > /dev/null; then
1148             func_ln_if_changed "$gnulib_dir/$f" "$destdir/$g"
1149           else
1150             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1151           fi
1152         else
1153           if test -n "$already_present"; then
1154             echo "Update file $g (backup in ${g}~)"
1155           else
1156             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1157           fi
1158         fi
1159       fi
1160     else
1161       # Install the file.
1162       # Don't protest if the file should be there but isn't: it happens
1163       # frequently that developers don't put autogenerated files into CVS.
1164       if $doit; then
1165         echo "Copying file $g"
1166         if test -n "$symbolic" && cmp "$gnulib_dir/$f" "$tmpfile" > /dev/null; then
1167           func_ln_if_changed "$gnulib_dir/$f" "$destdir/$g"
1168         else
1169           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1170         fi
1171       else
1172         echo "Copy file $g"
1173       fi
1174     fi
1175     rm -f "$tmpfile"
1176   }
1177   # Then the files that are in new-files, but not in old-files:
1178   sed_take_last_column='s,^.*'"$delimiter"',,'
1179   already_present=
1180   for f in `LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1181     case "$f" in
1182       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1183       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1184       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1185       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1186       *) g="$f" ;;
1187     esac
1188     func_add_or_update
1189   done
1190   # Then the files that are in new-files and in old-files:
1191   already_present=true
1192   for f in `LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1193     case "$f" in
1194       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1195       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1196       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1197       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1198       *) g="$f" ;;
1199     esac
1200     func_add_or_update
1201   done
1202
1203   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1204   actioncmd="gnulib-tool --import"
1205   actioncmd="$actioncmd --dir=$destdir"
1206   actioncmd="$actioncmd --lib=$libname"
1207   actioncmd="$actioncmd --source-base=$sourcebase"
1208   actioncmd="$actioncmd --m4-base=$m4base"
1209   actioncmd="$actioncmd --aux-dir=$auxdir"
1210   for module in $avoidlist; do
1211     actioncmd="$actioncmd --avoid=$module"
1212   done
1213   if test -n "$lgpl"; then
1214     actioncmd="$actioncmd --lgpl"
1215   fi
1216   if test -n "$libtool"; then
1217     actioncmd="$actioncmd --libtool"
1218   fi
1219   actioncmd="$actioncmd --macro-prefix=$macro_prefix"
1220   actioncmd="$actioncmd `echo $specified_modules`"
1221
1222   # Create lib/Makefile.am.
1223   func_dest_tmpfilename $sourcebase/Makefile.am
1224   func_emit_lib_Makefile_am > "$tmpfile"
1225   if test -f "$destdir"/$sourcebase/Makefile.am; then
1226     if cmp "$destdir"/$sourcebase/Makefile.am "$tmpfile" > /dev/null; then
1227       rm -f "$tmpfile"
1228     else
1229       if $doit; then
1230         echo "Updating $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1231         mv -f "$destdir"/$sourcebase/Makefile.am "$destdir"/$sourcebase/Makefile.am~
1232         mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1233       else
1234         echo "Update $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1235         rm -f "$tmpfile"
1236       fi
1237     fi
1238   else
1239     if $doit; then
1240       echo "Creating $sourcebase/Makefile.am"
1241       mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1242     else
1243       echo "Create $sourcebase/Makefile.am"
1244       rm -f "$tmpfile"
1245     fi
1246   fi
1247
1248   # Create m4/gnulib-cache.m4.
1249   func_dest_tmpfilename $m4base/gnulib-cache.m4
1250   (
1251     func_emit_copyright_notice
1252     echo "#"
1253     echo "# This file represents the specification of how gnulib-tool is used."
1254     echo "# It acts as a cache: It is written and read by gnulib-tool."
1255     echo "# In projects using CVS, this file is meant to be stored in CVS,"
1256     echo "# like the configure.ac and various Makefile.am files."
1257     echo
1258     echo
1259     echo "# Specification in the form of a command-line invocation:"
1260     echo "#   $actioncmd"
1261     echo
1262     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
1263     echo "gl_MODULES(["`echo $specified_modules`"])"
1264     echo "gl_AVOID([$avoidlist])"
1265     echo "gl_SOURCE_BASE([$sourcebase])"
1266     echo "gl_M4_BASE([$m4base])"
1267     echo "gl_TESTS_BASE([$testsbase])"
1268     echo "gl_LIB([$libname])"
1269     test -z "$lgpl" || echo "gl_LGPL"
1270     test -z "$libtool" || echo "gl_LIBTOOL"
1271     echo "gl_MACRO_PREFIX([$macro_prefix])"
1272   ) > "$tmpfile"
1273   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1274     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
1275       rm -f "$tmpfile"
1276     else
1277       if $doit; then
1278         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1279         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
1280         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1281       else
1282         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1283         if false; then
1284           cat "$tmpfile"
1285           echo
1286           echo "# gnulib-cache.m4 ends here"
1287         fi
1288         rm -f "$tmpfile"
1289       fi
1290     fi
1291   else
1292     if $doit; then
1293       echo "Creating $m4base/gnulib-cache.m4"
1294       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1295     else
1296       echo "Create $m4base/gnulib-cache.m4"
1297       cat "$tmpfile"
1298       rm -f "$tmpfile"
1299     fi
1300   fi
1301
1302   # Create m4/gnulib-comp.m4.
1303   func_dest_tmpfilename $m4base/gnulib-comp.m4
1304   (
1305     func_emit_copyright_notice
1306     echo "#"
1307     echo "# This file represents the compiled summary of the specification in"
1308     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
1309     echo "# to be invoked from configure.ac."
1310     echo "# In projects using CVS, this file can be treated like other built files."
1311     echo
1312     echo
1313     echo "# This macro should be invoked from $configure_ac, in the section"
1314     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
1315     echo "# any checks for libraries, header files, types and library functions."
1316     echo "AC_DEFUN([${macro_prefix}_EARLY],"
1317     echo "["
1318     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
1319     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1320       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
1321     fi
1322     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1323       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
1324     fi
1325     if grep gl_LOCK "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1326       echo "  AC_REQUIRE([gl_LOCK])"
1327     fi
1328     echo "])"
1329     echo
1330     echo "# This macro should be invoked from $configure_ac, in the section"
1331     echo "# \"Check for header files, types and library functions\"."
1332     echo "AC_DEFUN([${macro_prefix}_INIT],"
1333     echo "["
1334     if test -z "$libtool"; then
1335       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1336     else
1337       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1338     fi
1339     if test "$auxdir" != "build-aux"; then
1340       sed_replace_build_aux='
1341         :a
1342         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1343           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1344           ba
1345         }'
1346       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1347     else
1348       sed_replace_build_aux=
1349     fi
1350     for module in $modules; do
1351       func_verify_module
1352       if test -n "$module"; then
1353         func_get_autoconf_snippet "$module" \
1354           | sed -e '/^$/d;' -e 's/^/  /' \
1355                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
1356                 -e "$sed_replace_build_aux"
1357         if test "$module" = 'alloca' && test -n "$libtool"; then
1358           echo 'changequote(,)dnl'
1359           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
1360           echo 'changequote([, ])dnl'
1361           echo 'AC_SUBST([LTALLOCA])'
1362         fi
1363       fi
1364     done
1365     echo "])"
1366     echo
1367     echo "# This macro records the list of files which have been installed by"
1368     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
1369     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
1370     echo "$files" | sed -e 's,^,  ,'
1371     echo "])"
1372   ) > "$tmpfile"
1373   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1374     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
1375       rm -f "$tmpfile"
1376     else
1377       if $doit; then
1378         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1379         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
1380         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1381       else
1382         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1383         if false; then
1384           cat "$tmpfile"
1385           echo
1386           echo "# gnulib-comp.m4 ends here"
1387         fi
1388         rm -f "$tmpfile"
1389       fi
1390     fi
1391   else
1392     if $doit; then
1393       echo "Creating $m4base/gnulib-comp.m4"
1394       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1395     else
1396       echo "Create $m4base/gnulib-comp.m4"
1397       cat "$tmpfile"
1398       rm -f "$tmpfile"
1399     fi
1400   fi
1401
1402   if test -n "$inctests"; then
1403     # Create tests/Makefile.am.
1404     func_dest_tmpfilename $testsbase/Makefile.am
1405     func_emit_tests_Makefile_am > "$tmpfile"
1406     if test -f "$destdir"/$testsbase/Makefile.am; then
1407       if cmp "$destdir"/$testsbase/Makefile.am "$tmpfile" > /dev/null; then
1408         rm -f "$tmpfile"
1409       else
1410         if $doit; then
1411           echo "Updating $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1412           mv -f "$destdir"/$testsbase/Makefile.am "$destdir"/$testsbase/Makefile.am~
1413           mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1414         else
1415           echo "Update $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1416           rm -f "$tmpfile"
1417         fi
1418       fi
1419     else
1420       if $doit; then
1421         echo "Creating $testsbase/Makefile.am"
1422         mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1423       else
1424         echo "Create $testsbase/Makefile.am"
1425         rm -f "$tmpfile"
1426       fi
1427     fi
1428   fi
1429
1430   rm -rf "$tmp"
1431   # Undo the effect of the previous 'trap' command. Some shellology:
1432   # We cannot use "trap - 0 1 2 3 15", because Solaris sh would attempt to
1433   # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
1434   # exit); for the others we need to call 'exit' explicitly. The value of $? is
1435   # 128 + signal number and is set before the trap-registered command is run.
1436   trap '' 0
1437   trap 'exit $?' 1 2 3 15
1438
1439   echo "Finished."
1440   echo
1441   echo "You may need to add #include directives for the following .h files."
1442   (
1443    # First the #include <...> directives without #ifs, sorted for convenience.
1444    for module in $modules; do
1445      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1446        :
1447      else
1448        func_get_include_directive "$module" | grep -v 'include "'
1449      fi
1450    done | LC_ALL=C sort -u
1451    # Then the #include "..." directives without #ifs, sorted for convenience.
1452    for module in $modules; do
1453      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1454        :
1455      else
1456        func_get_include_directive "$module" | grep 'include "'
1457      fi
1458    done | LC_ALL=C sort -u
1459    # Then the #include directives that are surrounded by #ifs. Not sorted.
1460    for module in $modules; do
1461      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1462        func_get_include_directive "$module"
1463      fi
1464    done
1465   ) | sed -e '/^$/d;' -e 's/^/  /'
1466   echo
1467   echo "Don't forget to"
1468   echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1469   if test -n "$inctests"; then
1470     echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1471   fi
1472   sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
1473   sourcebase_base=`basename "$sourcebase"`
1474   echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
1475   if test -n "$inctests"; then
1476     testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
1477     testsbase_base=`basename "$testsbase"`
1478     echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
1479   fi
1480   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
1481   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
1482   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
1483 }
1484
1485 # func_create_testdir testdir modules
1486 # Input:
1487 # - auxdir          directory relative to destdir where to place build aux files
1488 func_create_testdir ()
1489 {
1490   testdir="$1"
1491   modules="$2"
1492   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
1493
1494   # Determine final module list.
1495   func_modules_transitive_closure
1496   echo "Module list with included dependencies:"
1497   echo "$modules" | sed -e 's/^/  /'
1498
1499   # Add the dummy module if needed.
1500   func_modules_add_dummy
1501
1502   # Determine final file list.
1503   func_modules_to_filelist
1504   echo "File list:"
1505   echo "$files" | sed -e 's/^/  /'
1506
1507   # Create directories.
1508   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
1509     if test "$d" = build-aux; then
1510       mkdir -p "$testdir/$auxdir"
1511     else
1512       mkdir -p "$testdir/$d"
1513     fi
1514   done
1515
1516   # Copy files or make symbolic links.
1517   for f in $files; do
1518     case "$f" in
1519       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1520       *) g="$f" ;;
1521     esac
1522     ln "$gnulib_dir/$f" "$testdir/$g" 2>/dev/null ||
1523     if test -z "$symbolic"; then
1524       cp -p "$gnulib_dir/$f" "$testdir/$g"
1525     else
1526       ln -s "$gnulib_dir/$f" "$testdir/$g"
1527     fi
1528   done
1529
1530   # Create lib/Makefile.am.
1531   mkdir -p "$testdir/lib"
1532   func_emit_lib_Makefile_am > "$testdir/lib/Makefile.am"
1533
1534   # Create m4/Makefile.am.
1535   mkdir -p "$testdir/m4"
1536   (echo "## Process this file with automake to produce Makefile.in."
1537    echo
1538    echo "EXTRA_DIST ="
1539    for f in $files; do
1540      case "$f" in
1541        m4/* )
1542          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
1543      esac
1544    done
1545   ) > "$testdir/m4/Makefile.am"
1546
1547   subdirs="lib m4"
1548   subdirs_with_configure_ac=""
1549
1550   if test -f "$testdir"/m4/gettext.m4; then
1551     # Avoid stupid error message from automake:
1552     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
1553     mkdir -p "$testdir/po"
1554     (echo "## Process this file with automake to produce Makefile.in."
1555     ) > "$testdir/po/Makefile.am"
1556     subdirs="$subdirs po"
1557   fi
1558
1559   if test -n "$inctests"; then
1560     test -d "$testdir/tests" || mkdir "$testdir/tests"
1561     # Create tests/Makefile.am.
1562     sourcebase=lib
1563     m4base=m4
1564     testsbase=tests
1565     func_emit_tests_Makefile_am > "$testdir/tests/Makefile.am"
1566     # Create tests/configure.ac.
1567     (echo "# Process this file with autoconf to produce a configure script."
1568      echo "AC_INIT([dummy], [0])"
1569      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
1570      echo "AM_INIT_AUTOMAKE"
1571      echo
1572      echo "AM_CONFIG_HEADER([config.h])"
1573      echo
1574      echo "AC_PROG_CC"
1575      echo "AC_PROG_INSTALL"
1576      echo "AC_PROG_MAKE_SET"
1577      echo "AC_PROG_RANLIB"
1578      echo
1579      if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1580        echo "AC_GNU_SOURCE"
1581        echo
1582      fi
1583      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1584        echo "gl_USE_SYSTEM_EXTENSIONS"
1585        echo
1586      fi
1587      if grep gl_LOCK "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1588        echo "gl_LOCK"
1589        echo
1590      fi
1591      if test -z "$libtool"; then
1592        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1593      else
1594        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1595      fi
1596      if test "$auxdir" != "build-aux"; then
1597        sed_replace_build_aux='
1598          :a
1599          /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1600            s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
1601            ba
1602          }'
1603        sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1604      else
1605        sed_replace_build_aux=
1606      fi
1607      # We don't have explicit ordering constraints between the various
1608      # autoconf snippets. It's cleanest to put those of the library before
1609      # those of the tests.
1610      for module in $modules; do
1611        func_verify_nontests_module
1612        if test -n "$module"; then
1613          func_get_autoconf_snippet "$module" \
1614            | sed -e "$sed_replace_build_aux"
1615        fi
1616      done
1617      for module in $modules; do
1618        func_verify_tests_module
1619        if test -n "$module"; then
1620          func_get_autoconf_snippet "$module" \
1621            | sed -e "$sed_replace_build_aux"
1622        fi
1623      done
1624      echo
1625      # Usually tests/config.h will be a superset of config.h. Verify this by
1626      # "merging" config.h into tests/config.h; look out for gcc warnings.
1627      echo "AH_TOP([#include \"../config.h\"])"
1628      echo
1629      echo "AC_OUTPUT([Makefile])"
1630     ) > "$testdir/tests/configure.ac"
1631     subdirs="$subdirs tests"
1632     subdirs_with_configure_ac="$subdirs_with_configure_ac tests"
1633   fi
1634
1635   # Create Makefile.am.
1636   (echo "## Process this file with automake to produce Makefile.in."
1637    echo
1638    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1639    echo
1640    echo "SUBDIRS = $subdirs"
1641    echo
1642    echo "ACLOCAL_AMFLAGS = -I m4"
1643   ) > "$testdir/Makefile.am"
1644
1645   # Create configure.ac.
1646   (echo "# Process this file with autoconf to produce a configure script."
1647    echo "AC_INIT([dummy], [0])"
1648    if test "$auxdir" != "."; then
1649      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1650    fi
1651    echo "AM_INIT_AUTOMAKE"
1652    echo
1653    echo "AM_CONFIG_HEADER([config.h])"
1654    echo
1655    echo "AC_PROG_CC"
1656    echo "AC_PROG_INSTALL"
1657    echo "AC_PROG_MAKE_SET"
1658    echo "AC_PROG_RANLIB"
1659    echo
1660    if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1661      echo "AC_GNU_SOURCE"
1662      echo
1663    fi
1664    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1665      echo "gl_USE_SYSTEM_EXTENSIONS"
1666      echo
1667    fi
1668    if grep gl_LOCK "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1669      echo "gl_LOCK"
1670      echo
1671    fi
1672    if test -z "$libtool"; then
1673      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1674    else
1675      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1676    fi
1677    if test "$auxdir" != "build-aux"; then
1678      sed_replace_build_aux='
1679        :a
1680        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1681          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1682          ba
1683        }'
1684      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1685    else
1686      sed_replace_build_aux=
1687    fi
1688    for module in $modules; do
1689      func_verify_nontests_module
1690      if test -n "$module"; then
1691        func_get_autoconf_snippet "$module" \
1692          | sed -e "$sed_replace_build_aux"
1693      fi
1694    done
1695    echo
1696    if test -n "$subdirs_with_configure_ac"; then
1697      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
1698    fi
1699    makefiles="Makefile"
1700    for d in $subdirs; do
1701      # For subdirs that have a configure.ac by their own, it's the subdir's
1702      # configure.ac which creates the subdir's Makefile.am, not this one.
1703      case " $subdirs_with_configure_ac " in
1704        *" $d "*) ;;
1705        *) makefiles="$makefiles $d/Makefile" ;;
1706      esac
1707    done
1708    echo "AC_OUTPUT([$makefiles])"
1709   ) > "$testdir/configure.ac"
1710
1711   # Create autogenerated files.
1712   (cd "$testdir"
1713    echo "executing ${AUTORECONF} --force --install"
1714    ${AUTORECONF} --force --install
1715   )
1716   if grep '^BUILT_SOURCES *+=' "$testdir/lib/Makefile.am" > /dev/null; then
1717     (cd "$testdir"
1718      ./configure
1719        cd lib
1720        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
1721        make built_sources
1722        cd ..
1723      make distclean
1724     )
1725   fi
1726 }
1727
1728 # func_create_megatestdir megatestdir allmodules
1729 # Input:
1730 # - auxdir          directory relative to destdir where to place build aux files
1731 func_create_megatestdir ()
1732 {
1733   megatestdir="$1"
1734   allmodules="$2"
1735   if test -z "$allmodules"; then
1736     allmodules=`func_all_modules`
1737   fi
1738
1739   megasubdirs=
1740   # First, all modules one by one.
1741   for onemodule in $allmodules; do
1742     func_create_testdir "$megatestdir/$onemodule" $onemodule
1743     megasubdirs="${megasubdirs}$onemodule "
1744   done
1745   # Then, all modules all together.
1746   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
1747   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
1748   func_create_testdir "$megatestdir/ALL" "$allmodules"
1749   megasubdirs="${megasubdirs}ALL"
1750
1751   # Create Makefile.am.
1752   (echo "## Process this file with automake to produce Makefile.in."
1753    echo
1754    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1755    echo
1756    echo "SUBDIRS = $megasubdirs"
1757   ) > "$megatestdir/Makefile.am"
1758
1759   # Create configure.ac.
1760   (echo "# Process this file with autoconf to produce a configure script."
1761    echo "AC_INIT([dummy], [0])"
1762    if test "$auxdir" != "."; then
1763      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1764    fi
1765    echo "AM_INIT_AUTOMAKE"
1766    echo
1767    echo "AC_PROG_MAKE_SET"
1768    echo
1769    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
1770    echo "AC_OUTPUT([Makefile])"
1771   ) > "$megatestdir/configure.ac"
1772
1773   # Create autogenerated files.
1774   (cd "$megatestdir"
1775    # Do not use "${AUTORECONF} --install", because autoreconf operates
1776    # recursively, but the subdirectories are already finished, therefore
1777    # calling autoreconf here would only waste lots of CPU time.
1778    echo "executing ${ACLOCAL}"
1779    ${ACLOCAL}
1780    echo "executing mkdir build-aux"
1781    mkdir build-aux
1782    echo "executing ${AUTOCONF}"
1783    ${AUTOCONF}
1784    echo "executing ${AUTOMAKE} --add-missing --copy"
1785    ${AUTOMAKE} --add-missing --copy
1786   )
1787 }
1788
1789 case $mode in
1790   "" )
1791     func_fatal_error "no mode specified" ;;
1792
1793   list )
1794     func_all_modules
1795     ;;
1796
1797   import | update )
1798     
1799     # Where to import.
1800     if test -z "$destdir"; then
1801       destdir=.
1802     fi
1803     test -d "$destdir" \
1804       || func_fatal_error "destination directory does not exist: $destdir"
1805
1806     # Prefer configure.ac to configure.in.
1807     if test -f "$destdir"/configure.ac; then
1808       configure_ac="$destdir/configure.ac"
1809     else
1810       if test -f "$destdir"/configure.in; then
1811         configure_ac="$destdir/configure.in"
1812       else
1813         func_fatal_error "cannot find $destdir/configure.ac"
1814       fi
1815     fi
1816
1817     test -f "$destdir"/Makefile.am \
1818       || func_fatal_error "cannot find $destdir/Makefile.am"
1819
1820     # Analyze configure.ac.
1821     guessed_auxdir="."
1822     guessed_libtool=
1823     my_sed_traces='
1824       s,#.*$,,
1825       s,^dnl .*$,,
1826       s, dnl .*$,,
1827       /AC_CONFIG_AUX_DIR/ {
1828         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
1829       }
1830       /A[CM]_PROG_LIBTOOL/ {
1831         s,^.*$,guessed_libtool=true,p
1832       }'
1833     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
1834
1835     if test -z "$auxdir"; then
1836       auxdir="$guessed_auxdir"
1837     fi
1838
1839     # Determine where to apply func_import.
1840     if test -n "$m4base"; then
1841       # Apply func_import to a particular gnulib directory.
1842       # Any number of additional modules can be given.
1843       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
1844         # First use of gnulib in the given m4base.
1845         test -n "$supplied_libname" || supplied_libname=true
1846         test -n "$sourcebase" || sourcebase="lib"
1847         test -n "$testsbase" || testsbase="tests"
1848         test -n "$macro_prefix" || macro_prefix="gl"
1849       fi
1850       func_import "$*"
1851     else
1852       # Apply func_import to all gnulib directories.
1853       # To get this list of directories, look at Makefile.am. (Not at
1854       # configure, because it may be omitted from CVS. Also, don't run
1855       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
1856       aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[  ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
1857       m4dirs=
1858       m4dirs_count=0
1859       m4dir_is_next=
1860       for arg in $aclocal_amflags; do
1861         if test -n "$m4dir_is_next"; then
1862           # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
1863           case "$arg" in
1864             /*) ;;
1865             *)
1866               if test -f "$destdir/$arg"/gnulib-cache.m4; then
1867                 m4dirs="$m4dirs $arg"
1868                 m4dirs_count=`expr $m4dirs_count + 1`
1869               fi
1870               ;;
1871           esac
1872         else
1873           if test "X$arg" = "X-I"; then
1874             m4dir_is_next=yes
1875           else
1876             m4dir_is_next=
1877           fi
1878         fi
1879       done
1880       if test $m4dirs_count = 0; then
1881         # First use of gnulib in a package.
1882         # Any number of additional modules can be given.
1883         test -n "$supplied_libname" || supplied_libname=true
1884         test -n "$sourcebase" || sourcebase="lib"
1885         m4base="m4"
1886         test -n "$testsbase" || testsbase="tests"
1887         test -n "$macro_prefix" || macro_prefix="gl"
1888         func_import "$*"
1889       else
1890         if test $m4dirs_count = 1; then
1891           # There's only one use of gnulib here. Assume the user means it.
1892           # Any number of additional modules can be given.
1893           for m4base in $m4dirs; do
1894             func_import "$*"
1895           done
1896         else
1897           # Ambiguous - guess what the user meant.
1898           if test $# = 0; then
1899             # No further arguments. Guess the user wants to update all of them.
1900             for m4base in $m4dirs; do
1901               func_import
1902             done
1903           else
1904             # Really ambiguous.
1905             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
1906           fi
1907         fi
1908       fi
1909     fi
1910     ;;
1911
1912   create-testdir )
1913     if test -z "$destdir"; then
1914       func_fatal_error "please specify --dir option"
1915     fi
1916     mkdir "$destdir"
1917     test -d "$destdir" \
1918       || func_fatal_error "could not create destination directory"
1919     test -n "$auxdir" || auxdir="build-aux"
1920     func_create_testdir "$destdir" "$*"
1921     ;;
1922
1923   create-megatestdir )
1924     if test -z "$destdir"; then
1925       func_fatal_error "please specify --dir option"
1926     fi
1927     mkdir "$destdir" || func_fatal_error "could not create destination directory"
1928     test -n "$auxdir" || auxdir="build-aux"
1929     func_create_megatestdir "$destdir" "$*"
1930     ;;
1931
1932   test )
1933     test -n "$destdir" || destdir=testdir$$
1934     mkdir "$destdir" || func_fatal_error "could not create destination directory"
1935     test -n "$auxdir" || auxdir="build-aux"
1936     func_create_testdir "$destdir" "$*"
1937     cd "$destdir"
1938       mkdir build
1939       cd build
1940         ../configure
1941         make
1942         make check
1943         make distclean
1944         remaining=`find . -type f -print`
1945         if test -n "$remaining"; then
1946           echo "Remaining files:" $remaining 1>&2
1947           echo "gnulib-tool: *** Stop." 1>&2
1948           exit 1
1949         fi
1950       cd ..
1951     cd ..
1952     rm -rf "$destdir"
1953     ;;
1954
1955   megatest )
1956     test -n "$destdir" || destdir=testdir$$
1957     mkdir "$destdir" || func_fatal_error "could not create destination directory"
1958     test -n "$auxdir" || auxdir="build-aux"
1959     func_create_megatestdir "$destdir" "$*"
1960     cd "$destdir"
1961       mkdir build
1962       cd build
1963         ../configure
1964         make
1965         make check
1966         make distclean
1967         remaining=`find . -type f -print`
1968         if test -n "$remaining"; then
1969           echo "Remaining files:" $remaining 1>&2
1970           echo "gnulib-tool: *** Stop." 1>&2
1971           exit 1
1972         fi
1973       cd ..
1974     cd ..
1975     rm -rf "$destdir"
1976     ;;
1977
1978   extract-description )
1979     for module
1980     do
1981       func_verify_module
1982       if test -n "$module"; then
1983         func_get_description "$module"
1984       fi
1985     done
1986     ;;
1987
1988   extract-filelist )
1989     for module
1990     do
1991       func_verify_module
1992       if test -n "$module"; then
1993         func_get_filelist "$module"
1994       fi
1995     done
1996     ;;
1997
1998   extract-dependencies )
1999     for module
2000     do
2001       func_verify_module
2002       if test -n "$module"; then
2003         func_get_dependencies "$module"
2004       fi
2005     done
2006     ;;
2007
2008   extract-autoconf-snippet )
2009     for module
2010     do
2011       func_verify_module
2012       if test -n "$module"; then
2013         func_get_autoconf_snippet "$module"
2014       fi
2015     done
2016     ;;
2017
2018   extract-automake-snippet )
2019     for module
2020     do
2021       func_verify_module
2022       if test -n "$module"; then
2023         func_get_automake_snippet "$module"
2024       fi
2025     done
2026     ;;
2027
2028   extract-include-directive )
2029     for module
2030     do
2031       func_verify_module
2032       if test -n "$module"; then
2033         func_get_include_directive "$module"
2034       fi
2035     done
2036     ;;
2037
2038   extract-license )
2039     for module
2040     do
2041       func_verify_module
2042       if test -n "$module"; then
2043         func_get_license "$module"
2044       fi
2045     done
2046     ;;
2047
2048   extract-maintainer )
2049     for module
2050     do
2051       func_verify_module
2052       if test -n "$module"; then
2053         func_get_maintainer "$module"
2054       fi
2055     done
2056     ;;
2057
2058   extract-tests-module )
2059     for module
2060     do
2061       func_verify_module
2062       if test -n "$module"; then
2063         func_get_tests_module "$module"
2064       fi
2065     done
2066     ;;
2067
2068   * )
2069     func_fatal_error "unknown operation mode --$mode" ;;
2070 esac
2071
2072 exit 0