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