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