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