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