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