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