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