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