In testing directories, link the test programs already during "make", not
[gnulib.git] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2007 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: 2007-01-27 13:53:54 $'
26 last_checkin_date=`echo "$cvsdatestamp" | sed -e 's,^\$[D]ate: ,,'`
27 version=`echo "$last_checkin_date" | sed -e 's/ .*$//' -e 's,/,-,g'`
28 nl='
29 '
30
31 # You can set AUTOCONFPATH to empty if autoconf 2.57 is already in your PATH.
32 AUTOCONFPATH=
33 #case $USER in
34 #  bruno )
35 #    AUTOCONFBINDIR=/packages/gnu-inst-autoconf/2.57/bin
36 #    AUTOCONFPATH="eval env PATH=${AUTOCONFBINDIR}:\$PATH "
37 #    ;;
38 #esac
39
40 # You can set AUTOMAKEPATH to empty if automake 1.9.x is already in your PATH.
41 AUTOMAKEPATH=
42
43 # You can set GETTEXTPATH to empty if autopoint 0.15 is already in your PATH.
44 GETTEXTPATH=
45
46 # If you didn't set AUTOCONFPATH and AUTOMAKEPATH, you can also set the
47 # variables AUTOCONF, AUTOHEADER, ACLOCAL, AUTOMAKE, AUTORECONF individually.
48 if test -z "${AUTOCONF}" || test -n "${AUTOCONFPATH}"; then
49   AUTOCONF="${AUTOCONFPATH}autoconf"
50 fi
51 if test -z "${AUTOHEADER}" || test -n "${AUTOCONFPATH}"; then
52   AUTOHEADER="${AUTOCONFPATH}autoheader"
53 fi
54 if test -z "${ACLOCAL}" || test -n "${AUTOMAKEPATH}"; then
55   ACLOCAL="${AUTOMAKEPATH}aclocal"
56 fi
57 if test -z "${AUTOMAKE}" || test -n "${AUTOMAKEPATH}"; then
58   AUTOMAKE="${AUTOMAKEPATH}automake"
59 fi
60 if test -z "${AUTORECONF}" || test -n "${AUTOCONFPATH}"; then
61   AUTORECONF="${AUTOCONFPATH}autoreconf"
62 fi
63
64 # If you didn't set GETTEXTPATH, you can also set the variable AUTOPOINT.
65 if test -z "${AUTOPOINT}" || test -n "${GETTEXTPATH}"; then
66   AUTOPOINT="${GETTEXTPATH}autopoint"
67 fi
68
69 # When using GNU sed, turn off as many GNU extensions as possible,
70 # to minimize the risk of accidentally using non-portable features.
71 # However, do this only for gnulib-tool itself, not for the code that
72 # gnulib-tool generates, since we don't want "sed --posix" to leak
73 # into makefiles.
74 if (alias) > /dev/null 2>&1 && echo | sed --posix -e d >/dev/null 2>&1; then
75   alias sed='sed --posix'
76 fi
77
78 # func_usage
79 # outputs to stdout the --help usage message.
80 func_usage ()
81 {
82   echo "\
83 Usage: gnulib-tool --list
84        gnulib-tool --import [module1 ... moduleN]
85        gnulib-tool --update
86        gnulib-tool --create-testdir --dir=directory [module1 ... moduleN]
87        gnulib-tool --create-megatestdir --dir=directory [module1 ... moduleN]
88        gnulib-tool --test --dir=directory module1 ... moduleN
89        gnulib-tool --megatest --dir=directory [module1 ... moduleN]
90        gnulib-tool --extract-description module
91        gnulib-tool --extract-filelist module
92        gnulib-tool --extract-dependencies module
93        gnulib-tool --extract-autoconf-snippet module
94        gnulib-tool --extract-automake-snippet module
95        gnulib-tool --extract-include-directive module
96        gnulib-tool --extract-license module
97        gnulib-tool --extract-maintainer module
98        gnulib-tool --extract-tests-module module
99
100 Operation modes:
101       --list                print the available module names
102       --import              import the given modules into the current package;
103                             if no modules are specified, update the current
104                             package from the current gnulib
105       --update              update the current package, restore files omitted
106                             from CVS
107       --create-testdir      create a scratch package with the given modules
108       --create-megatestdir  create a mega scratch package with the given modules
109                             one by one and all together
110       --test                test the combination of the given modules
111                             (recommended to use CC=\"gcc -Wall\" here)
112       --megatest            test the given modules one by one and all together
113                             (recommended to use CC=\"gcc -Wall\" here)
114       --extract-description        extract the description
115       --extract-filelist           extract the list of files
116       --extract-dependencies       extract the dependencies
117       --extract-autoconf-snippet   extract the snippet for configure.ac
118       --extract-automake-snippet   extract the snippet for library makefile
119       --extract-include-directive  extract the #include directive
120       --extract-license            report the license terms of the source files
121                                    under lib/
122       --extract-maintainer         report the maintainer(s) inside gnulib
123       --extract-tests-module       report the unit test module, if it exists
124
125 General options:
126       --dir=DIRECTORY       Specify the target directory.
127                             For --import, this specifies where your
128                             configure.ac can be found.  Defaults to current
129                             directory.
130       --local-dir=DIRECTORY  Specify a local override directory where to look
131                             up files before looking in gnulib's directory.
132
133 Options for --import:
134       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
135       --source-base=DIRECTORY
136                             Directory relative to --dir where source code is
137                             placed (default \"lib\").
138       --m4-base=DIRECTORY   Directory relative to --dir where *.m4 macros are
139                             placed (default \"m4\").
140       --doc-base=DIRECTORY  Directory relative to --dir where doc files are
141                             placed (default \"doc\").
142       --tests-base=DIRECTORY
143                             Directory relative to --dir where unit tests are
144                             placed (default \"tests\").
145       --aux-dir=DIRECTORY   Directory relative to --dir where auxiliary build
146                             tools are placed (default \"build-aux\").
147       --with-tests          Include unit tests for the included modules.
148       --avoid=MODULE        Avoid including the given MODULE. Useful if you
149                             have code that provides equivalent functionality.
150                             This option can be repeated.
151       --lgpl                Abort if modules aren't available under the LGPL.
152                             Also modify license template from GPL to LGPL.
153       --makefile-name=NAME  Name of makefile in automake syntax in the
154                             source-base and tests-base directories
155                             (default \"Makefile.am\").
156       --libtool             Use libtool rules.
157       --no-libtool          Don't use libtool rules.
158       --macro-prefix=PREFIX  Specify the prefix of the macros 'gl_EARLY' and
159                             'gl_INIT'. Default is 'gl'.
160       --no-changelog        don't update or create ChangeLog files
161
162 Options for --import and --update:
163       --dry-run             For --import, only print what would have been done.
164   -s, --symbolic, --symlink Make symbolic links instead of copying files.
165       --local-symlink       Make symbolic links instead of copying files, only
166                             for files from the local override directory.
167
168 Report bugs to <bug-gnulib@gnu.org>."
169 }
170
171 # func_version
172 # outputs to stdout the --version message.
173 func_version ()
174 {
175   year=`echo "$last_checkin_date" | sed -e 's,/.*$,,'`
176   echo "\
177 $progname (GNU $package) $version
178 Copyright (C) $year Free Software Foundation, Inc.
179 This is free software; see the source for copying conditions.  There is NO
180 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
181 Written by" "Bruno Haible" "and" "Simon Josefsson"
182 }
183
184 # func_emit_copyright_notice
185 # outputs to stdout a header for a generated file.
186 func_emit_copyright_notice ()
187 {
188   echo "# Copyright (C) 2004-2007 Free Software Foundation, Inc."
189   echo "#"
190   echo "# This file is free software, distributed under the terms of the GNU"
191   echo "# General Public License.  As a special exception to the GNU General"
192   echo "# Public License, this file may be distributed as part of a program"
193   echo "# that contains a configuration script generated by Autoconf, under"
194   echo "# the same distribution terms as the rest of that program."
195   echo "#"
196   echo "# Generated by gnulib-tool."
197 }
198
199 # func_exit STATUS
200 # exit with status
201 func_exit ()
202 {
203   (exit $1); exit $1
204 }
205
206 # func_tmpdir
207 # creates a temporary directory.
208 # Sets variable
209 # - tmp             pathname of freshly created temporary directory
210 func_tmpdir ()
211 {
212   # Use the environment variable TMPDIR, falling back to /tmp. This allows
213   # users to specify a different temporary directory, for example, if their
214   # /tmp is filled up or too small.
215   : ${TMPDIR=/tmp}
216   {
217     # Use the mktemp program if available. If not available, hide the error
218     # message.
219     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
220     test -n "$tmp" && test -d "$tmp"
221   } ||
222   {
223     # Use a simple mkdir command. It is guaranteed to fail if the directory
224     # already exists.  $RANDOM is bash specific and expands to empty in shells
225     # other than bash, ksh and zsh.  Its use does not increase security;
226     # rather, it minimizes the probability of failure in a very cluttered /tmp
227     # directory.
228     tmp=$TMPDIR/gl$$-$RANDOM
229     (umask 077 && mkdir "$tmp")
230   } ||
231   {
232     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
233     func_exit 1
234   }
235 }
236
237 # func_append var value
238 # appends the given value to the shell variable var.
239 if ( foo=bar; foo+=baz && test "$foo" = barbaz ) >/dev/null 2>&1; then
240   # Use bash's += operator. It reduces complexity of appending repeatedly to
241   # a single variable from O(n^2) to O(n).
242   func_append ()
243   {
244     eval "$1+=\"\$2\""
245   }
246 else
247   func_append ()
248   {
249     eval "$1=\"\$$1\$2\""
250   }
251 fi
252
253 # func_fatal_error message
254 # outputs to stderr a fatal error message, and terminates the program.
255 func_fatal_error ()
256 {
257   echo "gnulib-tool: *** $1" 1>&2
258   echo "gnulib-tool: *** Stop." 1>&2
259   func_exit 1
260 }
261
262 # func_readlink SYMLINK
263 # outputs the target of the given symlink.
264 if (type -p readlink) > /dev/null 2>&1; then
265   func_readlink ()
266   {
267     # Use the readlink program from GNU coreutils.
268     readlink "$1"
269   }
270 else
271   func_readlink ()
272   {
273     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
274     # would do the wrong thing if the link target contains " -> ".
275     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
276   }
277 fi
278
279 # func_relativize DIR1 DIR2
280 # computes a relative pathname RELDIR such that DIR1/RELDIR = DIR2.
281 # Input:
282 # - DIR1            relative pathname, relative to the current directory
283 # - DIR2            relative pathname, relative to the current directory
284 # Output:
285 # - reldir          relative pathname of DIR2, relative to DIR1
286 func_relativize ()
287 {
288   dir0=`pwd`
289   dir1="$1"
290   dir2="$2"
291   sed_first='s,^\([^/]*\)/.*$,\1,'
292   sed_rest='s,^[^/]*/*,,'
293   sed_last='s,^.*/\([^/]*\)$,\1,'
294   sed_butlast='s,/*[^/]*$,,'
295   while test -n "$dir1"; do
296     first=`echo "$dir1" | sed -e "$sed_first"`
297     if test "$first" != "."; then
298       if test "$first" = ".."; then
299         dir2=`echo "$dir0" | sed -e "$sed_last"`/"$dir2"
300         dir0=`echo "$dir0" | sed -e "$sed_butlast"`
301       else
302         first2=`echo "$dir2" | sed -e "$sed_first"`
303         if test "$first2" = "$first"; then
304           dir2=`echo "$dir2" | sed -e "$sed_rest"`
305         else
306           dir2="../$dir2"
307         fi
308         dir0="$dir0"/"$first"
309       fi
310     fi
311     dir1=`echo "$dir1" | sed -e "$sed_rest"`
312   done
313   reldir="$dir2"
314 }
315
316 # func_relconcat DIR1 DIR2
317 # computes a relative pathname DIR1/DIR2, with obvious simplifications.
318 # Input:
319 # - DIR1            relative pathname, relative to the current directory
320 # - DIR2            relative pathname, relative to DIR1
321 # Output:
322 # - relconcat       DIR1/DIR2, relative to the current directory
323 func_relconcat ()
324 {
325   dir1="$1"
326   dir2="$2"
327   sed_first='s,^\([^/]*\)/.*$,\1,'
328   sed_rest='s,^[^/]*/*,,'
329   sed_last='s,^.*/\([^/]*\)$,\1,'
330   sed_butlast='s,/*[^/]*$,,'
331   while true; do
332     first=`echo "$dir2" | sed -e "$sed_first"`
333     if test "$first" = "."; then
334       dir2=`echo "$dir2" | sed -e "$sed_rest"`
335       if test -z "$dir2"; then
336         relconcat="$dir1"
337         break
338       fi
339     else
340       last=`echo "$dir1" | sed -e "$sed_last"`
341       while test "$last" = "."; do
342         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
343         last=`echo "$dir1" | sed -e "$sed_last"`
344       done
345       if test -z "$dir1"; then
346         relconcat="$dir2"
347         break
348       fi
349       if test "$first" = ".."; then
350         if test "$last" = ".."; then
351           relconcat="$dir1/$dir2"
352           break
353         fi
354         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
355         dir2=`echo "$dir2" | sed -e "$sed_rest"`
356         if test -z "$dir1"; then
357           relconcat="$dir2"
358           break
359         fi
360         if test -z "$dir2"; then
361           relconcat="$dir1"
362           break
363         fi
364       else
365         relconcat="$dir1/$dir2"
366         break
367       fi
368     fi
369   done
370 }
371
372 # func_ln SRC DEST
373 # Like ln -s, except that SRC is given relative to the current directory (or
374 # absolute), not given relative to the directory of DEST.
375 func_ln ()
376 {
377   case "$1" in
378     /*)
379       ln -s "$1" "$2" ;;
380     *) # SRC is relative.
381       case "$2" in
382         /*)
383           ln -s "`pwd`/$1" "$2" ;;
384         *) # DEST is relative too.
385           ln_destdir=`echo "$2" | sed -e 's,[^/]*$,,'`
386           test -n "$ln_destdir" || ln_destdir="."
387           func_relativize "$ln_destdir" "$1"
388           ln -s "$reldir" "$2"
389           ;;
390       esac
391       ;;
392   esac
393 }
394
395 # func_ln_if_changed SRC DEST
396 # Like func_ln, but avoids munging timestamps if the link is correct.
397 func_ln_if_changed ()
398 {
399   if test $# -ne 2; then
400     echo "usage: func_ln_if_changed SRC DEST" >&2
401   fi
402   ln_target=`func_readlink "$2"`
403   if test -L "$2" && test "$1" = "$ln_target"; then
404     :
405   else
406     rm -f "$2"
407     func_ln "$1" "$2"
408   fi
409 }
410
411 # Command-line option processing.
412 # Removes the OPTIONS from the arguments. Sets the variables:
413 # - mode            list or import or create-testdir or create-megatestdir
414 # - destdir         from --dir
415 # - local_gnulib_dir  from --local-dir
416 # - libname, supplied_libname  from --lib
417 # - sourcebase      from --source-base
418 # - m4base          from --m4-base
419 # - docbase         from --doc-base
420 # - testsbase       from --tests-base
421 # - auxdir          from --aux-dir
422 # - inctests        true if --with-tests was given, blank otherwise
423 # - avoidlist       list of modules to avoid, from --avoid
424 # - lgpl            true if --lgpl was given, blank otherwise
425 # - makefile_name   from --makefile-name
426 # - libtool         true if --libtool was given, false if --no-libtool was
427 #                   given, blank otherwise
428 # - macro_prefix    from --macro-prefix
429 # - autoconf_minversion  minimum supported autoconf version
430 # - do_changelog    false if --no-changelog was given, : otherwise
431 # - doit            : if actions shall be executed, false if only to be printed
432 # - symbolic        true if --symlink was given, blank otherwise
433 # - lsymbolic       true if --local-symlink was given, blank otherwise
434 {
435   mode=
436   destdir=
437   local_gnulib_dir=
438   libname=libgnu
439   supplied_libname=
440   sourcebase=
441   m4base=
442   docbase=
443   testsbase=
444   auxdir=
445   inctests=
446   avoidlist=
447   lgpl=
448   makefile_name=
449   libtool=
450   macro_prefix=
451   do_changelog=:
452   doit=:
453   symbolic=
454   lsymbolic=
455
456   supplied_opts="$@"
457
458   while test $# -gt 0; do
459     case "$1" in
460       --list | --lis )
461         mode=list
462         shift ;;
463       --import | --impor | --impo | --imp | --im | --i )
464         mode=import
465         shift ;;
466       --update | --updat | --upda | --upd | --up | --u )
467         mode=update
468         shift ;;
469       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
470         mode=create-testdir
471         shift ;;
472       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
473         mode=create-megatestdir
474         shift ;;
475       --test | --tes | --te | --t )
476         mode=test
477         shift ;;
478       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
479         mode=megatest
480         shift ;;
481       --extract-* )
482         mode=`echo "X$1" | sed -e 's/^X--//'`
483         shift ;;
484       --dir )
485         shift
486         if test $# = 0; then
487           func_fatal_error "missing argument for --dir"
488         fi
489         destdir=$1
490         shift ;;
491       --dir=* )
492         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
493         shift ;;
494       --local-dir )
495         shift
496         if test $# = 0; then
497           func_fatal_error "missing argument for --local-dir"
498         fi
499         local_gnulib_dir=$1
500         shift ;;
501       --local-dir=* )
502         local_gnulib_dir=`echo "X$1" | sed -e 's/^X--local-dir=//'`
503         shift ;;
504       --lib )
505         shift
506         if test $# = 0; then
507           func_fatal_error "missing argument for --lib"
508         fi
509         libname=$1
510         supplied_libname=true
511         shift ;;
512       --lib=* )
513         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
514         supplied_libname=true
515         shift ;;
516       --source-base )
517         shift
518         if test $# = 0; then
519           func_fatal_error "missing argument for --source-base"
520         fi
521         sourcebase=$1
522         shift ;;
523       --source-base=* )
524         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
525         shift ;;
526       --m4-base )
527         shift
528         if test $# = 0; then
529           func_fatal_error "missing argument for --m4-base"
530         fi
531         m4base=$1
532         shift ;;
533       --m4-base=* )
534         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
535         shift ;;
536       --doc-base )
537         shift
538         if test $# = 0; then
539           func_fatal_error "missing argument for --doc-base"
540         fi
541         docbase=$1
542         shift ;;
543       --doc-base=* )
544         docbase=`echo "X$1" | sed -e 's/^X--doc-base=//'`
545         shift ;;
546       --tests-base )
547         shift
548         if test $# = 0; then
549           func_fatal_error "missing argument for --tests-base"
550         fi
551         testsbase=$1
552         shift ;;
553       --tests-base=* )
554         testsbase=`echo "X$1" | sed -e 's/^X--tests-base=//'`
555         shift ;;
556       --aux-dir )
557         shift
558         if test $# = 0; then
559           func_fatal_error "missing argument for --aux-dir"
560         fi
561         auxdir=$1
562         shift ;;
563       --aux-dir=* )
564         auxdir=`echo "X$1" | sed -e 's/^X--aux-dir=//'`
565         shift ;;
566       --with-tests )
567         inctests=true
568         shift ;;
569       --avoid )
570         shift
571         if test $# = 0; then
572           func_fatal_error "missing argument for --avoid"
573         fi
574         func_append avoidlist " $1"
575         shift ;;
576       --avoid=* )
577         arg=`echo "X$1" | sed -e 's/^X--avoid=//'`
578         func_append avoidlist " $arg"
579         shift ;;
580       --lgpl )
581         lgpl=true
582         shift ;;
583       --makefile-name )
584         shift
585         if test $# = 0; then
586           func_fatal_error "missing argument for --makefile-name"
587         fi
588         makefile_name="$1"
589         shift ;;
590       --makefile-name=* )
591         makefile_name=`echo "X$1" | sed -e 's/^X--makefile-name=//'`
592         shift ;;
593       --libtool )
594         libtool=true
595         shift ;;
596       --no-libtool )
597         libtool=false
598         shift ;;
599       --macro-prefix )
600         shift
601         if test $# = 0; then
602           func_fatal_error "missing argument for --macro-prefix"
603         fi
604         macro_prefix="$1"
605         shift ;;
606       --macro-prefix=* )
607         macro_prefix=`echo "X$1" | sed -e 's/^X--macro-prefix=//'`
608         shift ;;
609       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
610         do_changelog=false
611         shift ;;
612       --dry-run )
613         doit=false
614         shift ;;
615       -s | --symbolic | --symboli | --symbol | --symbo | --symb | --symlink | --symlin | --symli | --syml | --sym | --sy )
616         symbolic=true
617         shift ;;
618       --local-symlink | --local-symlin | --local-symli | --local-syml | --local-sym | --local-sy | --local-s )
619         lsymbolic=true
620         shift ;;
621       --help | --hel | --he | --h )
622         func_usage
623         func_exit $? ;;
624       --version | --versio | --versi | --vers | --ver | --ve | --v )
625         func_version
626         func_exit $? ;;
627       -- )
628         # Stop option processing
629         shift
630         break ;;
631       -* )
632         echo "gnulib-tool: unknown option $1" 1>&2
633         echo "Try 'gnulib-tool --help' for more information." 1>&2
634         func_exit 1 ;;
635       * )
636         break ;;
637     esac
638   done
639
640   if test "$mode" = update; then
641     if test $# != 0; then
642       echo "gnulib-tool: too many arguments in 'update' mode" 1>&2
643       echo "Try 'gnulib-tool --help' for more information." 1>&2
644       echo "If you really want to modify the gnulib configuration of your project," 1>&2
645       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
646       func_exit 1
647     fi
648     if test -n "$local_gnulib_dir" || test -n "$supplied_libname" \
649        || test -n "$sourcebase" || test -n "$m4base" \
650        || test -n "$docbase" || test -n "$testsbase" || test -n "$auxdir" \
651        || test -n "$inctests" || test -n "$avoidlist" || test -n "$lgpl" \
652        || test -n "$makefile_name" || test -n "$macro_prefix"; then
653       echo "gnulib-tool: invalid options for 'update' mode" 1>&2
654       echo "Try 'gnulib-tool --help' for more information." 1>&2
655       echo "If you really want to modify the gnulib configuration of your project," 1>&2
656       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
657       func_exit 1
658     fi
659     do_changelog=false
660   fi
661
662   # Determine the minimum supported autoconf version from the project's
663   # configure.ac.
664   DEFAULT_AUTOCONF_MINVERSION="2.59"
665   autoconf_minversion=
666   configure_ac=
667   if { test "$mode" = import || test "$mode" = update; } && test -n "$destdir"; then
668     if test -f "$destdir"/configure.ac; then
669       configure_ac="$destdir/configure.ac"
670     else
671       if test -f "$destdir"/configure.in; then
672         configure_ac="$destdir/configure.in"
673       fi
674     fi
675   else
676     if test -f configure.ac; then
677       configure_ac="configure.ac"
678     else
679       if test -f configure.in; then
680         configure_ac="configure.in"
681       fi
682     fi
683   fi
684   if test -n "$configure_ac"; then
685     # Use sed, not autoconf --trace, to look for the AC_PREREQ invocation,
686     # because when some m4 files are omitted from a CVS repository,
687     # "autoconf --trace=AC_PREREQ" fails with an error message like this:
688     #   m4: aclocal.m4:851: Cannot open m4/absolute-header.m4: No such file or directory
689     #   autom4te: m4 failed with exit status: 1
690     prereqs=
691     my_sed_traces='
692       s,#.*$,,
693       s,^dnl .*$,,
694       s, dnl .*$,,
695       /AC_PREREQ/ {
696         s,^.*AC_PREREQ([[ ]*\([^])]*\).*$,\1,p
697       }'
698     prereqs=`sed -n -e "$my_sed_traces" < "$configure_ac"`
699     if test -n "$prereqs"; then
700       autoconf_minversion=`
701         for version in $prereqs; do echo $version; done |
702         LC_ALL=C sort -nru | sed 1q
703       `
704     fi
705   fi
706   if test -z "$autoconf_minversion"; then
707     autoconf_minversion=$DEFAULT_AUTOCONF_MINVERSION
708   fi
709   case "$autoconf_minversion" in
710     1.* | 2.[0-4]* | 2.5[0-8]*)
711       func_fatal_error "minimum supported autoconf version is 2.59. Try adding AC_PREREQ([$DEFAULT_AUTOCONF_MINVERSION]) to your configure.ac." ;;
712   esac
713
714   # Remove trailing slashes from the directory names. This is necessary for
715   # m4base (to avoid an error in func_import) and optional for the others.
716   sed_trimtrailingslashes='s,\([^/]\)//*$,\1,'
717   case "$local_gnulib_dir" in
718     */ ) local_gnulib_dir=`echo "$local_gnulib_dir" | sed -e "$sed_trimtrailingslashes"` ;;
719   esac
720   case "$sourcebase" in
721     */ ) sourcebase=`echo "$sourcebase" | sed -e "$sed_trimtrailingslashes"` ;;
722   esac
723   case "$m4base" in
724     */ ) m4base=`echo "$m4base" | sed -e "$sed_trimtrailingslashes"` ;;
725   esac
726   case "$docbase" in
727     */ ) docbase=`echo "$docbase" | sed -e "$sed_trimtrailingslashes"` ;;
728   esac
729   case "$testsbase" in
730     */ ) testsbase=`echo "$testsbase" | sed -e "$sed_trimtrailingslashes"` ;;
731   esac
732   case "$auxdir" in
733     */ ) auxdir=`echo "$auxdir" | sed -e "$sed_trimtrailingslashes"` ;;
734   esac
735 }
736
737 case "$0" in
738   /*) self_abspathname="$0" ;;
739   */*) self_abspathname=`pwd`/"$0" ;;
740   *) for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`; do
741        if test -x "$d/$0" && test ! -d "$d/$0"; then
742          self_abspathname="$d/$0"
743          break
744        fi
745      done
746      if test -z "$self_abspathname"; then
747        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
748      fi
749      ;;
750 esac
751 while test -h "$self_abspathname"; do
752   # Resolve symbolic link.
753   linkval=`func_readlink "$self_abspathname"`
754   test -n "$linkval" || break
755   case "$linkval" in
756     /* ) self_abspathname="$linkval" ;;
757     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
758   esac
759 done
760 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
761
762 func_tmpdir
763 trap 'exit_status=$?
764       if test "$signal" != 0; then
765         echo "caught signal $signal" >&2
766       fi
767       rm -rf "$tmp"
768       exit $exit_status' 0
769 for signal in 1 2 3 13 15; do
770   trap '{ signal='$signal'; func_exit 1; }' $signal
771 done
772 signal=0
773
774 # func_lookup_file file
775 # looks up a file in $local_gnulib_dir or $gnulib_dir, or combines it through
776 # 'patch'.
777 # Input:
778 # - local_gnulib_dir  from --local-dir
779 # Output:
780 # - lookedup_file   name of the merged (combined) file
781 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
782 func_lookup_file ()
783 {
784   lkfile="$1"
785   if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile"; then
786     lookedup_file="$local_gnulib_dir/$lkfile"
787     lookedup_tmp=
788   else
789     if test -f "$gnulib_dir/$lkfile"; then
790       if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile.diff"; then
791         lkbase=`echo "$lkfile" | sed -e 's,^.*/,,'`
792         rm -f "$tmp/$lkbase"
793         cp "$gnulib_dir/$lkfile" "$tmp/$lkbase"
794         patch -s "$tmp/$lkbase" < "$local_gnulib_dir/$lkfile.diff" \
795           || func_fatal_error "patch file $local_gnulib_dir/$lkfile.diff didn't apply cleanly"
796         lookedup_file="$tmp/$lkbase"
797         lookedup_tmp=true
798       else
799         lookedup_file="$gnulib_dir/$lkfile"
800         lookedup_tmp=
801       fi
802     else
803       func_fatal_error "file $gnulib_dir/$lkfile not found"
804     fi
805   fi
806 }
807
808 # func_all_modules
809 # Input:
810 # - local_gnulib_dir  from --local-dir
811 func_all_modules ()
812 {
813   # Filter out metainformation files like README, which are not modules.
814   # Filter out unit test modules; they can be retrieved through
815   # --extract-tests-module if desired.
816   {
817     (cd "$gnulib_dir" && find modules -type f -print | sed -e 's,^modules/,,')
818     if test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules"; then
819       (cd "$local_gnulib_dir" && find modules -type f -print | sed -e 's,^modules/,,' -e 's,\.diff$,,')
820     fi
821   } \
822       | sed -e '/^CVS\//d' -e '/\/CVS\//d' \
823             -e '/^ChangeLog$/d' -e '/\/ChangeLog$/d' \
824             -e '/^COPYING$/d' -e '/\/COPYING$/d' \
825             -e '/^README$/d' -e '/\/README$/d' \
826             -e '/^TEMPLATE$/d' \
827             -e '/^TEMPLATE-TESTS$/d' \
828             -e '/^\..*/d' \
829             -e '/~$/d' \
830       | sed -e '/-tests$/d' \
831       | LC_ALL=C sort -u
832 }
833
834 # func_verify_module
835 # verifies a module name
836 # Input:
837 # - local_gnulib_dir  from --local-dir
838 # - module          module name argument
839 func_verify_module ()
840 {
841   if { test -f "$gnulib_dir/modules/$module" \
842        || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
843             && test -f "$local_gnulib_dir/modules/$module"; }; } \
844      && test "CVS" != "$module" \
845      && test "ChangeLog" != "$module" \
846      && test "COPYING" != "$module" \
847      && test "README" != "$module" \
848      && test "TEMPLATE" != "$module" \
849      && test "TEMPLATE-TESTS" != "$module"; then
850     # OK, $module is a correct module name.
851     # Verify that building the module description with 'patch' succeeds.
852     func_lookup_file "modules/$module"
853   else
854     echo "gnulib-tool: module $module doesn't exist" 1>&2
855     module=
856   fi
857 }
858
859 # func_verify_nontests_module
860 # verifies a module name, excluding tests modules
861 # Input:
862 # - local_gnulib_dir  from --local-dir
863 # - module          module name argument
864 func_verify_nontests_module ()
865 {
866   case "$module" in
867     *-tests ) module= ;;
868     * ) func_verify_module ;;
869   esac
870 }
871
872 # func_verify_tests_module
873 # verifies a module name, considering only tests modules
874 # Input:
875 # - local_gnulib_dir  from --local-dir
876 # - module          module name argument
877 func_verify_tests_module ()
878 {
879   case "$module" in
880     *-tests ) func_verify_module ;;
881     * ) module= ;;
882   esac
883 }
884
885 sed_extract_prog=':[     ]*$/ {
886   :a
887     n
888     s/^Description:[     ]*$//
889     s/^Files:[   ]*$//
890     s/^Depends-on:[      ]*$//
891     s/^configure\.ac:[   ]*$//
892     s/^Makefile\.am:[    ]*$//
893     s/^Include:[         ]*$//
894     s/^License:[         ]*$//
895     s/^Maintainer:[      ]*$//
896     tb
897     p
898     ba
899   :b
900 }'
901
902 # func_get_description module
903 # Input:
904 # - local_gnulib_dir  from --local-dir
905 func_get_description ()
906 {
907   func_lookup_file "modules/$1"
908   sed -n -e "/^Description$sed_extract_prog" < "$lookedup_file"
909 }
910
911 # func_get_filelist module
912 # Input:
913 # - local_gnulib_dir  from --local-dir
914 func_get_filelist ()
915 {
916   func_lookup_file "modules/$1"
917   sed -n -e "/^Files$sed_extract_prog" < "$lookedup_file"
918   echo m4/gnulib-common.m4
919   case "$autoconf_minversion" in
920     2.59)
921       #echo m4/onceonly.m4
922       echo m4/onceonly_2_57.m4
923       ;;
924   esac
925 }
926
927 # func_get_dependencies module
928 # Input:
929 # - local_gnulib_dir  from --local-dir
930 func_get_dependencies ()
931 {
932   # ${module}-tests always implicitly depends on ${module}.
933   echo "$1" | sed -n -e 's/-tests//p'
934   # Then the explicit dependencies listed in the module description.
935   func_lookup_file "modules/$1"
936   sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
937 }
938
939 # func_get_autoconf_snippet module
940 # Input:
941 # - local_gnulib_dir  from --local-dir
942 func_get_autoconf_snippet ()
943 {
944   func_lookup_file "modules/$1"
945   sed -n -e "/^configure\.ac$sed_extract_prog" < "$lookedup_file"
946 }
947
948 # func_get_automake_snippet module
949 # Input:
950 # - local_gnulib_dir  from --local-dir
951 func_get_automake_snippet ()
952 {
953   func_lookup_file "modules/$1"
954   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file"
955   case "$1" in
956     *-tests)
957       # *-tests module live in tests/, not lib/.
958       ;;
959     *)
960       # Synthesize an EXTRA_DIST augmentation.
961       sed_combine_lines='/\\$/{
962         :a
963         N
964         s/\\\n/ /
965         s/\\$/\\/
966         ta
967       }'
968       sed_extract_mentioned_files='s/^lib_SOURCES[       ]*+=[   ]*//p'
969       already_mentioned_files=` \
970         sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file" \
971         | sed -e "$sed_combine_lines" \
972         | sed -n -e "$sed_extract_mentioned_files" | sed -e 's/#.*//'`
973       all_files=`func_get_filelist $1`
974       lib_files=`for f in $all_files; do \
975                    case $f in \
976                      lib/*) echo $f ;; \
977                    esac; \
978                  done | sed -e 's,^lib/,,'`
979       # Remove $already_mentioned_files from $lib_files.
980       echo "$lib_files" | LC_ALL=C sort -u > "$tmp"/lib-files
981       extra_files=`for f in $already_mentioned_files; do echo $f; done \
982                    | LC_ALL=C sort -u | LC_ALL=C join -v 2 - "$tmp"/lib-files`
983       if test -n "$extra_files"; then
984         echo "EXTRA_DIST +=" $extra_files
985         echo
986       fi
987       # Synthesize also an EXTRA_lib_SOURCES augmentation.
988       # This is necessary so that automake can generate the right list of
989       # dependency rules.
990       # A possible approach would be to use autom4te --trace of the redefined
991       # AC_LIBOBJ and AC_REPLACE_FUNCS macros when creating the Makefile.am
992       # (use autom4te --trace, not just grep, so that AC_LIBOBJ invocations
993       # inside autoconf's built-in macros are not missed).
994       # But it's simpler and more robust to do it here, based on the file list.
995       # If some .c file exists and is not used with AC_LIBOBJ - for example,
996       # a .c file is preprocessed into another .c file for BUILT_SOURCES -,
997       # automake will generate a useless dependency; this is harmless.
998       sed_extract_c_files='/\.c$/p'
999       extra_files=`echo "$extra_files" | sed -n -e "$sed_extract_c_files"`
1000       if test -n "$extra_files"; then
1001         echo "EXTRA_lib_SOURCES +=" $extra_files
1002         echo
1003       fi
1004       ;;
1005   esac
1006 }
1007
1008 # func_get_include_directive module
1009 # Input:
1010 # - local_gnulib_dir  from --local-dir
1011 func_get_include_directive ()
1012 {
1013   func_lookup_file "modules/$1"
1014   sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
1015   sed -e 's/^\(["<]\)/#include \1/'
1016 }
1017
1018 # func_get_license module
1019 # Input:
1020 # - local_gnulib_dir  from --local-dir
1021 func_get_license ()
1022 {
1023   func_lookup_file "modules/$1"
1024   { sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
1025     # The default is GPL.
1026     echo "GPL"
1027   } | sed -e 's,^ *$,,' | sed -e 1q
1028 }
1029
1030 # func_get_maintainer module
1031 # Input:
1032 # - local_gnulib_dir  from --local-dir
1033 func_get_maintainer ()
1034 {
1035   func_lookup_file "modules/$1"
1036   sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
1037 }
1038
1039 # func_get_tests_module module
1040 # Input:
1041 # - local_gnulib_dir  from --local-dir
1042 func_get_tests_module ()
1043 {
1044   # The naming convention for tests modules is hardwired: ${module}-tests.
1045   if test -f "$gnulib_dir/modules/$1"-tests \
1046      || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
1047           && test -f "$local_gnulib_dir/modules/$1"-tests; }; then
1048     echo "$1"-tests
1049   fi
1050 }
1051
1052 # func_acceptable module
1053 # tests whether a module is acceptable.
1054 # Input:
1055 # - avoidlist       list of modules to avoid
1056 func_acceptable ()
1057 {
1058   for avoid in $avoidlist; do
1059     if test "$avoid" = "$1"; then
1060       return 1
1061     fi
1062   done
1063   return 0
1064 }
1065
1066 # func_modules_transitive_closure
1067 # Input:
1068 # - local_gnulib_dir  from --local-dir
1069 # - modules         list of specified modules
1070 # - inctests        true if tests should be included, blank otherwise
1071 # - avoidlist       list of modules to avoid
1072 # - tmp             pathname of a temporary directory
1073 # Output:
1074 # - modules         list of modules, including dependencies
1075 func_modules_transitive_closure ()
1076 {
1077   # In order to process every module only once (for speed), process an "input
1078   # list" of modules, producing an "output list" of modules. During each round,
1079   # more modules can be queued in the input list. Once a module on the input
1080   # list has been processed, it is added to the "handled list", so we can avoid
1081   # to process it again.
1082   handledmodules=
1083   inmodules="$modules"
1084   outmodules=
1085   while test -n "$inmodules"; do
1086     inmodules_this_round="$inmodules"
1087     inmodules=                    # Accumulator, queue for next round
1088     for module in $inmodules_this_round; do
1089       func_verify_module
1090       if test -n "$module"; then
1091         if func_acceptable $module; then
1092           func_append outmodules " $module"
1093           deps=`func_get_dependencies $module`
1094           # Duplicate dependencies are harmless, but Jim wants a warning.
1095           duplicated_deps=`echo "$deps" | LC_ALL=C sort | LC_ALL=C uniq -d`
1096           if test -n "$duplicated_deps"; then
1097             echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
1098           fi
1099           func_append inmodules " $deps"
1100           if test -n "$inctests"; then
1101             testsmodule=`func_get_tests_module $module`
1102             if test -n "$testsmodule"; then
1103               func_append inmodules " $testsmodule"
1104             fi
1105           fi
1106         fi
1107       fi
1108     done
1109     handledmodules=`for m in $handledmodules $inmodules_this_round; do echo $m; done | LC_ALL=C sort -u`
1110     # Remove $handledmodules from $inmodules.
1111     for m in $inmodules; do echo $m; done | LC_ALL=C sort -u > "$tmp"/queued-modules
1112     inmodules=`echo "$handledmodules" | LC_ALL=C join -v 2 - "$tmp"/queued-modules`
1113   done
1114   modules=`for m in $outmodules; do echo $m; done | LC_ALL=C sort -u`
1115   rm -f "$tmp"/queued-modules
1116 }
1117
1118 # func_modules_add_dummy
1119 # Input:
1120 # - local_gnulib_dir  from --local-dir
1121 # - modules         list of modules, including dependencies
1122 # Output:
1123 # - modules         list of modules, including 'dummy' if needed
1124 func_modules_add_dummy ()
1125 {
1126   have_lib_SOURCES=
1127   sed_remove_backslash_newline=':a
1128 /\\$/{
1129 s/\\$//
1130 N
1131 s/\n//
1132 ba
1133 }'
1134   for module in $modules; do
1135     func_verify_nontests_module
1136     if test -n "$module"; then
1137       # Extract the value of "lib_SOURCES += ...".
1138       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
1139         # Ignore .h files since they are not compiled.
1140         case "$file" in
1141           *.h) ;;
1142           *) have_lib_SOURCES=yes ;;
1143         esac
1144       done
1145     fi
1146   done
1147   # Add the dummy module, to make sure the library will be non-empty.
1148   if test -z "$have_lib_SOURCES"; then
1149     func_append modules " dummy"
1150   fi
1151 }
1152
1153 # func_modules_to_filelist
1154 # Input:
1155 # - local_gnulib_dir  from --local-dir
1156 # - modules         list of modules, including dependencies
1157 # Output:
1158 # - files           list of files
1159 func_modules_to_filelist ()
1160 {
1161   files=
1162   for module in $modules; do
1163     func_verify_module
1164     if test -n "$module"; then
1165       fs=`func_get_filelist $module`
1166       func_append files " $fs"
1167     fi
1168   done
1169   files=`for f in $files; do echo $f; done | LC_ALL=C sort -u`
1170 }
1171
1172 # func_emit_lib_Makefile_am
1173 # emits the contents of library makefile to standard output.
1174 # Input:
1175 # - local_gnulib_dir  from --local-dir
1176 # - modules         list of modules, including dependencies
1177 # - libname         library name
1178 # - auxdir          directory relative to destdir where to place build aux files
1179 # - makefile_name   from --makefile-name
1180 # - libtool         true if libtool will be used, false or blank otherwise
1181 # - macro_prefix    prefix of gl_LIBOBJS macros to use
1182 # - actioncmd       (optional) command that will reproduce this invocation
1183 # - for_test        true if creating a package for testing, false otherwise
1184 # Output:
1185 # - uses_subdirs    nonempty if object files in subdirs exist
1186 func_emit_lib_Makefile_am ()
1187 {
1188   # When creating an includable Makefile.am snippet, augment variables with
1189   # += instead of assigning them.
1190   if test -n "$makefile_name"; then
1191     assign='+='
1192   else
1193     assign='='
1194   fi
1195   if test "$libtool" = true; then
1196     libext=la
1197     perhapsLT=LT
1198     sed_eliminate_LDFLAGS=
1199   else
1200     libext=a
1201     perhapsLT=
1202     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
1203   fi
1204   if $for_test; then
1205     # When creating a package for testing: Attempt to provoke failures,
1206     # especially link errors, already during "make" rather than during
1207     # "make check", because "make check" is not possible in a cross-compiling
1208     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
1209     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
1210   else
1211     sed_transform_check_PROGRAMS=
1212   fi
1213   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
1214   echo "## Process this file with automake to produce Makefile.in."
1215   func_emit_copyright_notice
1216   if test -n "$actioncmd"; then
1217     echo "# Reproduce by: $actioncmd"
1218   fi
1219   echo
1220   uses_subdirs=
1221   {
1222     for module in $modules; do
1223       func_verify_nontests_module
1224       if test -n "$module"; then
1225         {
1226           func_get_automake_snippet "$module" |
1227             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1228                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1229             sed -e "$sed_eliminate_LDFLAGS" |
1230             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1231             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1232                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' |
1233             sed -e "$sed_transform_check_PROGRAMS"
1234           if test "$module" = 'alloca'; then
1235             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
1236             echo "${libname}_${libext}_DEPENDENCIES += @${perhapsLT}ALLOCA@"
1237           fi
1238         } > amsnippet.tmp
1239         # Skip the contents if its entirely empty.
1240         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1241           echo "## begin gnulib module $module"
1242           echo
1243           cat amsnippet.tmp
1244           echo "## end   gnulib module $module"
1245           echo
1246         fi
1247         rm -f amsnippet.tmp
1248         # Test whether there are some source files in subdirectories.
1249         for f in `func_get_filelist "$module"`; do
1250           case $f in
1251             lib/*/*.c) uses_subdirs=yes ;;
1252           esac
1253         done
1254       fi
1255     done
1256   } > allsnippets.tmp
1257   if test -z "$makefile_name"; then
1258     # If there are source files in subdirectories, prevent collision of the
1259     # object files (example: hash.c and libxml/hash.c).
1260     subdir_options=
1261     if test -n "$uses_subdirs"; then
1262       subdir_options=' subdir-objects'
1263     fi
1264     echo "AUTOMAKE_OPTIONS = 1.5 gnits${subdir_options}"
1265   fi
1266   echo
1267   if test -z "$makefile_name"; then
1268     echo "noinst_HEADERS ="
1269     echo "noinst_LIBRARIES ="
1270     echo "noinst_LTLIBRARIES ="
1271     # Automake versions < 1.9b create an empty pkgdatadir at installation time
1272     # if you specify pkgdata_DATA to empty. This is a workaround.
1273     if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1274       echo "pkgdata_DATA ="
1275     fi
1276     echo "EXTRA_DIST ="
1277     echo "BUILT_SOURCES ="
1278     echo "SUFFIXES ="
1279   fi
1280   echo "MOSTLYCLEANFILES $assign core *.stackdump"
1281   if test -z "$makefile_name"; then
1282     echo "MOSTLYCLEANDIRS ="
1283     echo "CLEANFILES ="
1284     echo "DISTCLEANFILES ="
1285     echo "MAINTAINERCLEANFILES ="
1286     echo
1287     echo "AM_CPPFLAGS ="
1288   fi
1289   echo
1290   if grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *= *$libname\\.$libext\$" allsnippets.tmp > /dev/null; then
1291     # One of the snippets already specifies an installation location for the
1292     # library. Don't confuse automake by saying it should not be installed.
1293     :
1294   else
1295     # By default, the generated library should not be installed.
1296     echo "noinst_${perhapsLT}LIBRARIES += $libname.$libext"
1297   fi
1298   echo
1299   echo "${libname}_${libext}_SOURCES ="
1300   # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
1301   # automake during its analyses looks for $(LIBOBJS), not for @LIBOBJS@.
1302   echo "${libname}_${libext}_LIBADD = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
1303   echo "${libname}_${libext}_DEPENDENCIES = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
1304   echo "EXTRA_${libname}_${libext}_SOURCES ="
1305   if test "$libtool" = true; then
1306     echo "${libname}_${libext}_LDFLAGS = \$(AM_LDFLAGS)"
1307   fi
1308   echo
1309   cat allsnippets.tmp \
1310     | sed -e 's|\$(top_srcdir)/build-aux/|$(top_srcdir)/'"$auxdir"'/|g'
1311   echo
1312   echo "mostlyclean-local: mostlyclean-generic"
1313   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
1314   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
1315   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1316   echo "          fi; \\"
1317   echo "        done"
1318   rm -f allsnippets.tmp
1319 }
1320
1321 # func_emit_tests_Makefile_am
1322 # emits the contents of tests makefile to standard output.
1323 # Input:
1324 # - local_gnulib_dir  from --local-dir
1325 # - modules         list of modules, including dependencies
1326 # - libname         library name
1327 # - makefile_name   from --makefile-name
1328 # - libtool         true if libtool will be used, false or blank otherwise
1329 # - sourcebase      relative directory containing lib source code
1330 # - m4base          relative directory containing autoconf macros
1331 # - testsbase       relative directory containing unit test code
1332 # - for_test        true if creating a package for testing, false otherwise
1333 func_emit_tests_Makefile_am ()
1334 {
1335   if test "$libtool" = true; then
1336     libext=la
1337   else
1338     libext=a
1339   fi
1340   if test "$libtool" = true; then
1341     libext=la
1342     sed_eliminate_LDFLAGS=
1343   else
1344     libext=a
1345     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
1346   fi
1347   if $for_test; then
1348     # When creating a package for testing: Attempt to provoke failures,
1349     # especially link errors, already during "make" rather than during
1350     # "make check", because "make check" is not possible in a cross-compiling
1351     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
1352     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
1353   else
1354     sed_transform_check_PROGRAMS=
1355   fi
1356   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
1357   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
1358   echo "## Process this file with automake to produce Makefile.in."
1359   func_emit_copyright_notice
1360   echo
1361   # Generate dependencies here, since it eases the debugging of test failures.
1362   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1363   echo
1364   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
1365   echo
1366   (
1367     for module in $modules; do
1368       func_verify_tests_module
1369       if test -n "$module"; then
1370         {
1371           func_get_automake_snippet "$module" |
1372             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1373                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1374             sed -e "$sed_eliminate_LDFLAGS" |
1375             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1376             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1377                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' |
1378             sed -e "$sed_transform_check_PROGRAMS"
1379         } > amsnippet.tmp
1380         # Skip the contents if its entirely empty.
1381         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1382           echo "## begin gnulib module $module"
1383           echo
1384           cat amsnippet.tmp
1385           echo "## end   gnulib module $module"
1386           echo
1387         fi
1388         rm -f amsnippet.tmp
1389       fi
1390     done
1391   ) > allsnippets.tmp
1392   # Nothing is being added to SUBDIRS; nevertheless the existence of this
1393   # variable is needed to avoid an error from automake:
1394   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
1395   echo "SUBDIRS ="
1396   echo "TESTS ="
1397   echo "TESTS_ENVIRONMENT ="
1398   echo "noinst_PROGRAMS ="
1399   if ! $for_test; then
1400     echo "check_PROGRAMS ="
1401   fi
1402   echo "noinst_HEADERS ="
1403   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1404     echo "pkgdata_DATA ="
1405   fi
1406   echo "EXTRA_DIST ="
1407   echo "BUILT_SOURCES ="
1408   echo "SUFFIXES ="
1409   echo "MOSTLYCLEANFILES = core *.stackdump"
1410   echo "MOSTLYCLEANDIRS ="
1411   echo "CLEANFILES ="
1412   echo "DISTCLEANFILES ="
1413   echo "MAINTAINERCLEANFILES ="
1414   echo
1415   echo "AM_CPPFLAGS = \\"
1416   echo "  -I. -I\$(srcdir) \\"
1417   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
1418   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
1419   echo
1420   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
1421   echo
1422   cat allsnippets.tmp
1423   echo "# Clean up after Solaris cc."
1424   echo "clean-local:"
1425   echo "        rm -rf SunWS_cache"
1426   echo
1427   echo "mostlyclean-local: mostlyclean-generic"
1428   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
1429   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
1430   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1431   echo "          fi; \\"
1432   echo "        done"
1433   rm -f allsnippets.tmp
1434 }
1435
1436 # func_emit_initmacro_start
1437 # emits the first few statements of the gl_INIT macro to standard output.
1438 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1439 func_emit_initmacro_start ()
1440 {
1441   # Overriding AC_LIBOBJ and AC_REPLACE_FUNCS has the effect of storing
1442   # platform-dependent object files in ${macro_prefix}_LIBOBJS instead of
1443   # LIBOBJS.  The purpose is to allow several gnulib instantiations under
1444   # a single configure.ac file.  (AC_CONFIG_LIBOBJ_DIR does not allow this
1445   # flexibility.)
1446   # Furthermore it avoids an automake error like this when a Makefile.am
1447   # that uses pieces of gnulib also uses $(LIBOBJ):
1448   #   automatically discovered file `error.c' should not be explicitly mentioned
1449   echo "  m4_pushdef([AC_LIBOBJ], m4_defn([${macro_prefix}_LIBOBJ]))"
1450   echo "  m4_pushdef([AC_REPLACE_FUNCS], m4_defn([${macro_prefix}_REPLACE_FUNCS]))"
1451   # Overriding AC_LIBSOURCES has the same purpose of avoiding the automake
1452   # error when a Makefile.am that uses pieces of gnulib also uses $(LIBOBJ):
1453   #   automatically discovered file `error.c' should not be explicitly mentioned
1454   # We let automake know about the files to be distributed through the
1455   # EXTRA_lib_SOURCES variable.
1456   echo "  m4_pushdef([AC_LIBSOURCES], m4_defn([${macro_prefix}_LIBSOURCES]))"
1457 }
1458
1459 # func_emit_initmacro_end
1460 # emits the last few statements of the gl_INIT macro to standard output.
1461 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1462 func_emit_initmacro_end ()
1463 {
1464   echo "  m4_popdef([AC_LIBSOURCES])"
1465   echo "  m4_popdef([AC_REPLACE_FUNCS])"
1466   echo "  m4_popdef([AC_LIBOBJ])"
1467   echo "  AC_CONFIG_COMMANDS_PRE(["
1468   echo "    ${macro_prefix}_libobjs="
1469   echo "    ${macro_prefix}_ltlibobjs="
1470   echo "    if test -n \"\$${macro_prefix}_LIBOBJS\"; then"
1471   echo "      # Remove the extension."
1472   echo "      sed_drop_objext='s/\\.o\$//;s/\\.obj\$//'"
1473   echo "      for i in \`for i in \$${macro_prefix}_LIBOBJS; do echo \"\$i\"; done | sed \"\$sed_drop_objext\" | sort | uniq\`; do"
1474   echo "        ${macro_prefix}_libobjs=\"\$${macro_prefix}_libobjs \$i.\$ac_objext\""
1475   echo "        ${macro_prefix}_ltlibobjs=\"\$${macro_prefix}_ltlibobjs \$i.lo\""
1476   echo "      done"
1477   echo "    fi"
1478   echo "    AC_SUBST([${macro_prefix}_LIBOBJS], [\$${macro_prefix}_libobjs])"
1479   echo "    AC_SUBST([${macro_prefix}_LTLIBOBJS], [\$${macro_prefix}_ltlibobjs])"
1480   echo "  ])"
1481 }
1482
1483 # func_emit_initmacro_done
1484 # emits a few statements after the gl_INIT macro to standard output.
1485 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1486 func_emit_initmacro_done ()
1487 {
1488   echo
1489   echo "# Like AC_LIBOBJ, except that the module name goes"
1490   echo "# into ${macro_prefix}_LIBOBJS instead of into LIBOBJS."
1491   echo "AC_DEFUN([${macro_prefix}_LIBOBJ],"
1492   echo "  [${macro_prefix}_LIBOBJS=\"\$${macro_prefix}_LIBOBJS \$1.\$ac_objext\"])"
1493   echo
1494   echo "# Like AC_REPLACE_FUNCS, except that the module name goes"
1495   echo "# into ${macro_prefix}_LIBOBJS instead of into LIBOBJS."
1496   echo "AC_DEFUN([${macro_prefix}_REPLACE_FUNCS],"
1497   echo "  [AC_CHECK_FUNCS([\$1], , [${macro_prefix}_LIBOBJ(\$ac_func)])])"
1498   echo
1499   echo "# Like AC_LIBSOURCES, except that it does nothing."
1500   echo "# We rely on EXTRA_lib..._SOURCES instead."
1501   echo "AC_DEFUN([${macro_prefix}_LIBSOURCES],"
1502   echo "  [])"
1503 }
1504
1505 # func_import modules
1506 # Uses also the variables
1507 # - destdir         target directory
1508 # - local_gnulib_dir  from --local-dir
1509 # - libname         library name
1510 # - sourcebase      directory relative to destdir where to place source code
1511 # - m4base          directory relative to destdir where to place *.m4 macros
1512 # - docbase         directory relative to destdir where to place doc files
1513 # - testsbase       directory relative to destdir where to place unit test code
1514 # - auxdir          directory relative to destdir where to place build aux files
1515 # - inctests        true if --with-tests was given, blank otherwise
1516 # - avoidlist       list of modules to avoid, from --avoid
1517 # - lgpl            true if library's license shall be LGPL, blank otherwise
1518 # - makefile_name   from --makefile-name
1519 # - libtool         true if --libtool was given, false if --no-libtool was
1520 #                   given, blank otherwise
1521 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
1522 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1523 # - autoconf_minversion  minimum supported autoconf version
1524 # - doit            : if actions shall be executed, false if only to be printed
1525 # - symbolic        true if files should be symlinked, copied otherwise
1526 # - lsymbolic       true if files from local_gnulib_dir should be symlinked,
1527 #                   copied otherwise
1528 func_import ()
1529 {
1530   # Get the cached settings.
1531   cached_local_gnulib_dir=
1532   cached_specified_modules=
1533   cached_avoidlist=
1534   cached_sourcebase=
1535   cached_m4base=
1536   cached_docbase=
1537   cached_testsbase=
1538   cached_libname=
1539   cached_lgpl=
1540   cached_makefile_name=
1541   cached_libtool=
1542   cached_macro_prefix=
1543   cached_files=
1544   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1545     cached_libtool=false
1546     my_sed_traces='
1547       s,#.*$,,
1548       s,^dnl .*$,,
1549       s, dnl .*$,,
1550       /gl_LOCAL_DIR(/ {
1551         s,^.*gl_LOCAL_DIR([[ ]*\([^])]*\).*$,cached_local_gnulib_dir="\1",p
1552       }
1553       /gl_MODULES(/ {
1554         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
1555       }
1556       /gl_AVOID(/ {
1557         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
1558       }
1559       /gl_SOURCE_BASE(/ {
1560         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
1561       }
1562       /gl_M4_BASE(/ {
1563         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
1564       }
1565       /gl_DOC_BASE(/ {
1566         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
1567       }
1568       /gl_TESTS_BASE(/ {
1569         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
1570       }
1571       /gl_LIB(/ {
1572         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
1573       }
1574       /gl_LGPL/ {
1575         s,^.*$,cached_lgpl=true,p
1576       }
1577       /gl_MAKEFILE_NAME(/ {
1578         s,^.*gl_MAKEFILE_NAME([[ ]*\([^])]*\).*$,cached_makefile_name="\1",p
1579       }
1580       /gl_LIBTOOL/ {
1581         s,^.*$,cached_libtool=true,p
1582       }
1583       /gl_MACRO_PREFIX(/ {
1584         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
1585       }'
1586     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
1587     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1588       my_sed_traces='
1589         s,#.*$,,
1590         s,^dnl .*$,,
1591         s, dnl .*$,,
1592         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
1593           s,^.*$,cached_files=",p
1594           n
1595           ta
1596           :a
1597           s,^\]).*$,",
1598           tb
1599           p
1600           n
1601           ba
1602           :b
1603           p
1604         }'
1605       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1606     fi
1607   fi
1608
1609   # Merge the cached settings with the specified ones.
1610   # The m4base must be the same as expected from the pathname.
1611   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1612     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1613   fi
1614   # The local_gnulib_dir defaults to the cached one. Recall that the cached one
1615   # is relative to $destdir, whereas the one we use is relative to . or absolute.
1616   if test -z "$local_gnulib_dir"; then
1617     if test -n "$cached_local_gnulib_dir"; then
1618       case "$destdir" in
1619         /*)
1620           local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1621         *)
1622           case "$cached_local_gnulib_dir" in
1623             /*)
1624               local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1625             *)
1626               func_relconcat "$destdir" "$cached_local_gnulib_dir"
1627               local_gnulib_dir="$relconcat" ;;
1628           esac ;;
1629       esac
1630     fi
1631   fi
1632   # Append the cached and the specified module names. So that
1633   # "gnulib-tool --import foo" means to add the module foo.
1634   specified_modules="$cached_specified_modules $1"
1635   # Append the cached and the specified avoidlist. This is probably better
1636   # than dropping the cached one when --avoid is specified at least once.
1637   avoidlist=`echo $cached_avoidlist $avoidlist`
1638   # The sourcebase defaults to the cached one.
1639   if test -z "$sourcebase"; then
1640     sourcebase="$cached_sourcebase"
1641     if test -z "$sourcebase"; then
1642       func_fatal_error "missing --source-base option"
1643     fi
1644   fi
1645   # The docbase defaults to the cached one.
1646   if test -z "$docbase"; then
1647     docbase="$cached_docbase"
1648     if test -z "$docbase"; then
1649       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."
1650     fi
1651   fi
1652   # The testsbase defaults to the cached one.
1653   if test -z "$testsbase"; then
1654     testsbase="$cached_testsbase"
1655     if test -z "$testsbase"; then
1656       func_fatal_error "missing --tests-base option"
1657     fi
1658   fi
1659   # The libname defaults to the cached one.
1660   if test -z "$supplied_libname"; then
1661     libname="$cached_libname"
1662     if test -z "$libname"; then
1663       func_fatal_error "missing --lib option"
1664     fi
1665   fi
1666   # Require LGPL if specified either way.
1667   if test -z "$lgpl"; then
1668     lgpl="$cached_lgpl"
1669   fi
1670   # The makefile_name defaults to the cached one.
1671   if test -z "$makefile_name"; then
1672     makefile_name="$cached_makefile_name"
1673   fi
1674   # Use libtool if specified either way, or if guessed.
1675   if test -z "$libtool"; then
1676     if test -n "$cached_m4base"; then
1677       libtool="$cached_libtool"
1678     else
1679       libtool="$guessed_libtool"
1680     fi
1681   fi
1682   # The macro_prefix defaults to the cached one.
1683   if test -z "$macro_prefix"; then
1684     macro_prefix="$cached_macro_prefix"
1685     if test -z "$macro_prefix"; then
1686       func_fatal_error "missing --macro-prefix option"
1687     fi
1688   fi
1689
1690   # Canonicalize the list of specified modules.
1691   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort -u`
1692
1693   # Determine final module list.
1694   modules="$specified_modules"
1695   func_modules_transitive_closure
1696   echo "Module list with included dependencies:"
1697   echo "$modules" | sed -e 's/^/  /'
1698
1699   # Add the dummy module if needed.
1700   func_modules_add_dummy
1701
1702   # If --lgpl, verify that the licenses of modules are compatible.
1703   if test -n "$lgpl"; then
1704     for module in $modules; do
1705       license=`func_get_license $module`
1706       case $license in
1707         LGPL | 'GPLed build tool') ;;
1708         'public domain' | 'unlimited' | 'unmodifiable license text') ;;
1709         *) func_fatal_error "incompatible license on module $module: $license" ;;
1710       esac
1711     done
1712   fi
1713
1714   # Determine script to apply to imported library files.
1715   sed_transform_lib_file=
1716   for module in $modules; do
1717     if test $module = config-h; then
1718       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
1719       sed_transform_lib_file=$sed_transform_lib_file'
1720         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
1721       '
1722       break
1723     fi
1724   done
1725   if test -n "$lgpl"; then
1726     # Update license.
1727     sed_transform_lib_file=$sed_transform_lib_file'
1728       s/GNU General/GNU Lesser General/g
1729       s/version 2\([ ,]\)/version 2.1\1/g
1730     '
1731   fi
1732
1733   # Determine final file list.
1734   func_modules_to_filelist
1735   echo "File list:"
1736   echo "$files" | sed -e 's/^/  /'
1737
1738   test -n "$files" \
1739     || func_fatal_error "refusing to do nothing"
1740
1741   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1742   new_files="$files m4/gnulib-tool.m4"
1743   old_files="$cached_files"
1744   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1745     func_append old_files " m4/gnulib-tool.m4"
1746   fi
1747
1748   sed_rewrite_old_files="\
1749     s,^build-aux/,$auxdir/,
1750     s,^doc/,$cached_docbase/,
1751     s,^lib/,$cached_sourcebase/,
1752     s,^m4/,$cached_m4base/,
1753     s,^tests/,$cached_testsbase/,"
1754   sed_rewrite_new_files="\
1755     s,^build-aux/,$auxdir/,
1756     s,^doc/,$docbase/,
1757     s,^lib/,$sourcebase/,
1758     s,^m4/,$m4base/,
1759     s,^tests/,$testsbase/,"
1760
1761   # Create directories.
1762   { echo "$sourcebase"
1763     echo "$m4base"
1764     docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1765     if test -n "$docfiles"; then
1766       echo "$docbase"
1767     fi
1768     if test -n "$inctests"; then
1769       echo "$testsbase"
1770     fi
1771     echo "$auxdir"
1772     for f in $files; do echo $f; done \
1773       | sed -e "$sed_rewrite_new_files" \
1774       | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
1775       | LC_ALL=C sort -u
1776   } > "$tmp"/dirs
1777   { # Rearrange file descriptors. Needed because "while ... done < ..."
1778     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1779     exec 5<&0 < "$tmp"/dirs
1780     while read d; do
1781       if test ! -d "$destdir/$d"; then
1782         if $doit; then
1783           echo "Creating directory $destdir/$d"
1784           mkdir -p "$destdir/$d" || func_fatal_error "failed"
1785         else
1786           echo "Create directory $destdir/$d"
1787         fi
1788       fi
1789     done
1790     exec 0<&5 5<&-
1791   }
1792
1793   # func_dest_tmpfilename file
1794   # determines the name of a temporary file (file is relative to destdir).
1795   # Sets variable:
1796   #   - tmpfile       absolute filename of the temporary file
1797   func_dest_tmpfilename ()
1798   {
1799     if $doit; then
1800       # Put the new contents of $file in a file in the same directory (needed
1801       # to guarantee that an 'mv' to "$destdir/$file" works).
1802       tmpfile="$destdir/$1.tmp"
1803     else
1804       # Put the new contents of $file in a file in a temporary directory
1805       # (because the directory of "$file" might not exist).
1806       tmpfile="$tmp"/`basename "$1"`.tmp
1807     fi
1808   }
1809
1810   # Copy files or make symbolic links. Remove obsolete files.
1811   delimiter='   '
1812   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1813   # representing the files according to the last gnulib-tool invocation.
1814   for f in $old_files; do echo $f; done \
1815     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_old_files" \
1816     | LC_ALL=C sort \
1817     > "$tmp"/old-files
1818   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1819   # representing the files after this gnulib-tool invocation.
1820   for f in $new_files; do echo $f; done \
1821     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" \
1822     | LC_ALL=C sort \
1823     > "$tmp"/new-files
1824   # First the files that are in old-files, but not in new-files:
1825   sed_take_first_column='s,'"$delimiter"'.*,,'
1826   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1827     # Remove the file. Do nothing if the user already removed it.
1828     if test -f "$destdir/$g"; then
1829       if $doit; then
1830         echo "Removing file $g (backup in ${g}~)"
1831         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1832       else
1833         echo "Remove file $g (backup in ${g}~)"
1834       fi
1835     fi
1836   done
1837   # func_add_or_update handles a file that ought to be present afterwards.
1838   # Uses parameters
1839   # - f             the original file name
1840   # - g             the rewritten file name
1841   # - already_present  nonempty if the file already exists, empty otherwise
1842   func_add_or_update ()
1843   {
1844     func_dest_tmpfilename "$g"
1845     func_lookup_file "$f"
1846     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
1847     if test -n "$sed_transform_lib_file"; then
1848       case "$f" in
1849         lib/*)
1850           sed -e "$sed_transform_lib_file" \
1851             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
1852           ;;
1853       esac
1854     fi
1855     if test -f "$destdir/$g"; then
1856       # The file already exists.
1857       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1858         : # The file has not changed.
1859       else
1860         # Replace the file.
1861         if $doit; then
1862           if test -n "$already_present"; then
1863             echo "Updating file $g (backup in ${g}~)"
1864           else
1865             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1866           fi
1867           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1868           if { test -n "$symbolic" \
1869                || { test -n "$lsymbolic" \
1870                     && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
1871              && test -z "$lookedup_tmp" \
1872              && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1873             func_ln_if_changed "$lookedup_file" "$destdir/$g"
1874           else
1875             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1876           fi
1877         else
1878           if test -n "$already_present"; then
1879             echo "Update file $g (backup in ${g}~)"
1880           else
1881             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1882           fi
1883         fi
1884       fi
1885     else
1886       # Install the file.
1887       # Don't protest if the file should be there but isn't: it happens
1888       # frequently that developers don't put autogenerated files into CVS.
1889       if $doit; then
1890         echo "Copying file $g"
1891         if { test -n "$symbolic" \
1892              || { test -n "$lsymbolic" \
1893                   && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
1894            && test -z "$lookedup_tmp" \
1895            && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1896           func_ln_if_changed "$lookedup_file" "$destdir/$g"
1897         else
1898           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1899         fi
1900       else
1901         echo "Copy file $g"
1902       fi
1903     fi
1904     rm -f "$tmpfile"
1905   }
1906   # Then the files that are in new-files, but not in old-files:
1907   sed_take_last_column='s,^.*'"$delimiter"',,'
1908   already_present=
1909   LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files \
1910     | sed -e "$sed_take_last_column" \
1911     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/added-files
1912   { # Rearrange file descriptors. Needed because "while ... done < ..."
1913     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1914     exec 5<&0 < "$tmp"/added-files
1915     while read g f; do
1916       func_add_or_update
1917     done
1918     exec 0<&5 5<&-
1919   }
1920   # Then the files that are in new-files and in old-files:
1921   already_present=true
1922   LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files \
1923     | sed -e "$sed_take_last_column" \
1924     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/kept-files
1925   { # Rearrange file descriptors. Needed because "while ... done < ..."
1926     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1927     exec 5<&0 < "$tmp"/kept-files
1928     while read g f; do
1929       func_add_or_update
1930     done
1931     exec 0<&5 5<&-
1932   }
1933
1934   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1935   actioncmd="gnulib-tool --import"
1936   func_append actioncmd " --dir=$destdir"
1937   if test -n "$local_gnulib_dir"; then
1938     func_append actioncmd " --local-dir=$local_gnulib_dir"
1939   fi
1940   func_append actioncmd " --lib=$libname"
1941   func_append actioncmd " --source-base=$sourcebase"
1942   func_append actioncmd " --m4-base=$m4base"
1943   func_append actioncmd " --doc-base=$docbase"
1944   func_append actioncmd " --aux-dir=$auxdir"
1945   for module in $avoidlist; do
1946     func_append actioncmd " --avoid=$module"
1947   done
1948   if test -n "$lgpl"; then
1949     func_append actioncmd " --lgpl"
1950   fi
1951   if test -n "$makefile_name"; then
1952     func_append actioncmd " --makefile-name=$makefile_name"
1953   fi
1954   if test "$libtool" = true; then
1955     func_append actioncmd " --libtool"
1956   else
1957     func_append actioncmd " --no-libtool"
1958   fi
1959   func_append actioncmd " --macro-prefix=$macro_prefix"
1960   func_append actioncmd " `echo $specified_modules`"
1961
1962   # Default the makefile name to Makefile.am.
1963   if test -n "$makefile_name"; then
1964     makefile_am=$makefile_name
1965   else
1966     makefile_am=Makefile.am
1967   fi
1968
1969   # Create normal Makefile.ams.
1970   for_test=false
1971
1972   # Create library makefile.
1973   func_dest_tmpfilename $sourcebase/$makefile_am
1974   func_emit_lib_Makefile_am > "$tmpfile"
1975   if test -f "$destdir"/$sourcebase/$makefile_am; then
1976     if cmp "$destdir"/$sourcebase/$makefile_am "$tmpfile" > /dev/null; then
1977       rm -f "$tmpfile"
1978     else
1979       if $doit; then
1980         echo "Updating $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
1981         mv -f "$destdir"/$sourcebase/$makefile_am "$destdir"/$sourcebase/$makefile_am~
1982         mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
1983       else
1984         echo "Update $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
1985         rm -f "$tmpfile"
1986       fi
1987     fi
1988   else
1989     if $doit; then
1990       echo "Creating $sourcebase/$makefile_am"
1991       mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
1992     else
1993       echo "Create $sourcebase/$makefile_am"
1994       rm -f "$tmpfile"
1995     fi
1996   fi
1997
1998   # Create m4/gnulib-cache.m4.
1999   func_dest_tmpfilename $m4base/gnulib-cache.m4
2000   (
2001     func_emit_copyright_notice
2002     echo "#"
2003     echo "# This file represents the specification of how gnulib-tool is used."
2004     echo "# It acts as a cache: It is written and read by gnulib-tool."
2005     echo "# In projects using CVS, this file is meant to be stored in CVS,"
2006     echo "# like the configure.ac and various Makefile.am files."
2007     echo
2008     echo
2009     echo "# Specification in the form of a command-line invocation:"
2010     echo "#   $actioncmd"
2011     echo
2012     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
2013     # Store the local_gnulib_dir relative to destdir.
2014     case "$local_gnulib_dir" in
2015       "" | /*)
2016         relative_local_gnulib_dir="$local_gnulib_dir" ;;
2017       * )
2018         case "$destdir" in
2019           /*) relative_local_gnulib_dir="$local_gnulib_dir" ;;
2020           *)
2021             # destdir, local_gnulib_dir are both relative.
2022             func_relativize "$destdir" "$local_gnulib_dir"
2023             relative_local_gnulib_dir="$reldir" ;;
2024         esac ;;
2025     esac
2026     echo "gl_LOCAL_DIR([$relative_local_gnulib_dir])"
2027     echo "gl_MODULES(["`echo $specified_modules`"])"
2028     echo "gl_AVOID([$avoidlist])"
2029     echo "gl_SOURCE_BASE([$sourcebase])"
2030     echo "gl_M4_BASE([$m4base])"
2031     echo "gl_DOC_BASE([$docbase])"
2032     echo "gl_TESTS_BASE([$testsbase])"
2033     echo "gl_LIB([$libname])"
2034     test -z "$lgpl" || echo "gl_LGPL"
2035     echo "gl_MAKEFILE_NAME([$makefile_name])"
2036     if test "$libtool" = true; then
2037       echo "gl_LIBTOOL"
2038     fi
2039     echo "gl_MACRO_PREFIX([$macro_prefix])"
2040   ) > "$tmpfile"
2041   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
2042     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
2043       rm -f "$tmpfile"
2044     else
2045       if $doit; then
2046         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
2047         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
2048         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
2049       else
2050         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
2051         if false; then
2052           cat "$tmpfile"
2053           echo
2054           echo "# gnulib-cache.m4 ends here"
2055         fi
2056         rm -f "$tmpfile"
2057       fi
2058     fi
2059   else
2060     if $doit; then
2061       echo "Creating $m4base/gnulib-cache.m4"
2062       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
2063     else
2064       echo "Create $m4base/gnulib-cache.m4"
2065       cat "$tmpfile"
2066       rm -f "$tmpfile"
2067     fi
2068   fi
2069
2070   # Create m4/gnulib-comp.m4.
2071   func_dest_tmpfilename $m4base/gnulib-comp.m4
2072   (
2073     echo "# DO NOT EDIT! GENERATED AUTOMATICALLY!"
2074     func_emit_copyright_notice
2075     echo "#"
2076     echo "# This file represents the compiled summary of the specification in"
2077     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
2078     echo "# to be invoked from configure.ac."
2079     echo "# In projects using CVS, this file can be treated like other built files."
2080     echo
2081     echo
2082     echo "# This macro should be invoked from $configure_ac, in the section"
2083     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
2084     echo "# any checks for libraries, header files, types and library functions."
2085     echo "AC_DEFUN([${macro_prefix}_EARLY],"
2086     echo "["
2087     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
2088     echo "  m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
2089     echo "  m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
2090     echo "  m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
2091     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
2092     if test -n "$uses_subdirs"; then
2093       echo "  AC_REQUIRE([AM_PROG_CC_C_O])"
2094     fi
2095     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
2096       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
2097     fi
2098     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
2099       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
2100     fi
2101     if grep gl_LOCK "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
2102       echo "  AC_REQUIRE([gl_LOCK_EARLY])"
2103     fi
2104     echo "])"
2105     echo
2106     echo "# This macro should be invoked from $configure_ac, in the section"
2107     echo "# \"Check for header files, types and library functions\"."
2108     echo "AC_DEFUN([${macro_prefix}_INIT],"
2109     echo "["
2110     func_emit_initmacro_start
2111     if test "$libtool" = true; then
2112       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2113       echo "  gl_cond_libtool=true"
2114     else
2115       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2116       echo "  gl_cond_libtool=false"
2117       echo "  gl_libdeps="
2118       echo "  gl_ltlibdeps="
2119     fi
2120     echo "  gl_source_base='$sourcebase'"
2121     if test "$auxdir" != "build-aux"; then
2122       sed_replace_build_aux='
2123         :a
2124         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2125           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2126           ba
2127         }'
2128       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2129     else
2130       sed_replace_build_aux=
2131     fi
2132     for module in $modules; do
2133       func_verify_module
2134       if test -n "$module"; then
2135         func_get_autoconf_snippet "$module" \
2136           | sed -e '/^$/d;' -e 's/^/  /' \
2137                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
2138                 -e "$sed_replace_build_aux"
2139         if test "$module" = 'alloca' && test "$libtool" = true; then
2140           echo 'changequote(,)dnl'
2141           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
2142           echo 'changequote([, ])dnl'
2143           echo 'AC_SUBST([LTALLOCA])'
2144         fi
2145       fi
2146     done
2147     # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2148     # created using libtool, because libtool already handles the dependencies.
2149     if test "$libtool" != true; then
2150       libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2151       echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2152       echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2153       echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2154       echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2155     fi
2156     func_emit_initmacro_end
2157     echo "])"
2158     func_emit_initmacro_done
2159     echo
2160     echo "# This macro records the list of files which have been installed by"
2161     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
2162     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
2163     echo "$files" | sed -e 's,^,  ,'
2164     echo "])"
2165   ) > "$tmpfile"
2166   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
2167     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
2168       rm -f "$tmpfile"
2169     else
2170       if $doit; then
2171         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
2172         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
2173         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
2174       else
2175         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
2176         if false; then
2177           cat "$tmpfile"
2178           echo
2179           echo "# gnulib-comp.m4 ends here"
2180         fi
2181         rm -f "$tmpfile"
2182       fi
2183     fi
2184   else
2185     if $doit; then
2186       echo "Creating $m4base/gnulib-comp.m4"
2187       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
2188     else
2189       echo "Create $m4base/gnulib-comp.m4"
2190       cat "$tmpfile"
2191       rm -f "$tmpfile"
2192     fi
2193   fi
2194
2195   if test -n "$inctests"; then
2196     # Create tests makefile.
2197     func_dest_tmpfilename $testsbase/$makefile_am
2198     func_emit_tests_Makefile_am > "$tmpfile"
2199     if test -f "$destdir"/$testsbase/$makefile_am; then
2200       if cmp "$destdir"/$testsbase/$makefile_am "$tmpfile" > /dev/null; then
2201         rm -f "$tmpfile"
2202       else
2203         if $doit; then
2204           echo "Updating $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
2205           mv -f "$destdir"/$testsbase/$makefile_am "$destdir"/$testsbase/$makefile_am~
2206           mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
2207         else
2208           echo "Update $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
2209           rm -f "$tmpfile"
2210         fi
2211       fi
2212     else
2213       if $doit; then
2214         echo "Creating $testsbase/$makefile_am"
2215         mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
2216       else
2217         echo "Create $testsbase/$makefile_am"
2218         rm -f "$tmpfile"
2219       fi
2220     fi
2221   fi
2222
2223   echo "Finished."
2224   echo
2225   echo "You may need to add #include directives for the following .h files."
2226   # Intersect $specified_modules and $modules
2227   # (since $specified_modules is not necessarily of subset of $modules - some
2228   # may have been skipped through --avoid, and since the elements of $modules
2229   # but not in $specified_modules can go away without explicit notice - through
2230   # changes in the module dependencies).
2231   echo "$specified_modules" > "$tmp"/modules1 # a sorted list, one module per line
2232   echo "$modules" > "$tmp"/modules2 # also a sorted list, one module per line
2233   # First the #include <...> directives without #ifs, sorted for convenience,
2234   # then the #include "..." directives without #ifs, sorted for convenience,
2235   # then the #include directives that are surrounded by #ifs. Not sorted.
2236   for module in `LC_ALL=C join "$tmp"/modules1 "$tmp"/modules2`; do
2237     include_directive=`func_get_include_directive "$module"`
2238     case "$nl$include_directive" in
2239       *"$nl#if"*)
2240         echo "$include_directive" 1>&5
2241         ;;
2242       *)
2243         echo "$include_directive" | grep -v 'include "' 1>&6
2244         echo "$include_directive" | grep 'include "' 1>&7
2245         ;;
2246     esac
2247   done 5> "$tmp"/include-if 6> "$tmp"/include-angles 7> "$tmp"/include-quotes
2248   (
2249    LC_ALL=C sort -u "$tmp"/include-angles
2250    LC_ALL=C sort -u "$tmp"/include-quotes
2251    cat "$tmp"/include-if
2252   ) | sed -e '/^$/d' -e 's/^/  /'
2253   rm -f "$tmp"/include-angles "$tmp"/include-quotes "$tmp"/include-if
2254
2255   echo
2256   echo "Don't forget to"
2257   if test "$makefile_am" = Makefile.am; then
2258     echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
2259   else
2260     echo "  - \"include $makefile_name\" from within \"$sourcebase/Makefile.am\","
2261   fi
2262   if test -n "$inctests"; then
2263     if test "$makefile_am" = Makefile.am; then
2264       echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
2265     else
2266       echo "  - \"include $makefile_name\" from within \"$testsbase/Makefile.am\","
2267     fi
2268   fi
2269   if test "$makefile_am" = Makefile.am; then
2270     sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
2271     sourcebase_base=`basename "$sourcebase"`
2272     echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
2273   fi
2274   if test -n "$inctests"; then
2275     if test "$makefile_am" = Makefile.am; then
2276       testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
2277       testsbase_base=`basename "$testsbase"`
2278       echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
2279     fi
2280   fi
2281   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
2282   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
2283   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
2284 }
2285
2286 # func_create_testdir testdir modules
2287 # Input:
2288 # - local_gnulib_dir  from --local-dir
2289 # - auxdir          directory relative to destdir where to place build aux files
2290 func_create_testdir ()
2291 {
2292   testdir="$1"
2293   modules="$2"
2294   if test -z "$modules"; then
2295     # All modules together.
2296     # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
2297     # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2298     # Except ftruncate, mountlist, which abort the configuration on mingw. FIXME.
2299     modules=`func_all_modules`
2300     modules=`for m in $modules; do case $m in config-h | fnmatch-posix | ftruncate | mountlist) ;; *) echo $m;; esac; done`
2301   fi
2302   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort -u`
2303
2304   # Check that the license of every module is consistent with the license of
2305   # its dependencies.
2306   saved_modules="$modules"
2307   saved_inctests="$inctests"
2308   # When computing transitive closures, don't consider $module to depend on
2309   # $module-tests. Need this becauses tests are implicitly GPL and may depend
2310   # on GPL modules - therefore we don't want a warning in this case.
2311   inctests=""
2312   for requested_module in $saved_modules; do
2313     requested_license=`func_get_license "$requested_module"`
2314     if test "$requested_license" != GPL; then
2315       # Here we use func_modules_transitive_closure, not just
2316       # func_get_dependencies, so that we also detect weird situations like
2317       # an LGPL module which depends on a GPLed build tool module which depends
2318       # on a GPL module.
2319       modules="$requested_module"
2320       func_modules_transitive_closure
2321       for module in $modules; do
2322         license=`func_get_license "$module"`
2323         case $license in
2324           LGPL | 'GPLed build tool') ;;
2325           'public domain' | 'unlimited' | 'unmodifiable license text') ;;
2326           *) echo "warning: module $requested_module depends on a module with an incompatible license: $module" 1>&2 ;;
2327         esac
2328       done
2329     fi
2330   done
2331   modules="$saved_modules"
2332   inctests="$saved_inctests"
2333
2334   # Subdirectory names.
2335   sourcebase=gllib
2336   m4base=glm4
2337   docbase=gldoc
2338   testsbase=gltests
2339   macro_prefix=gl
2340
2341   # Determine final module list.
2342   func_modules_transitive_closure
2343   echo "Module list with included dependencies:"
2344   echo "$modules" | sed -e 's/^/  /'
2345
2346   # Add the dummy module if needed.
2347   func_modules_add_dummy
2348
2349   # Determine final file list.
2350   func_modules_to_filelist
2351   echo "File list:"
2352   echo "$files" | sed -e 's/^/  /'
2353
2354   sed_rewrite_files="\
2355     s,^build-aux/,$auxdir/,
2356     s,^doc/,$docbase/,
2357     s,^lib/,$sourcebase/,
2358     s,^m4/,$m4base/,
2359     s,^tests/,$testsbase/,"
2360
2361   # Create directories.
2362   for f in $files; do echo $f; done \
2363     | sed -e "$sed_rewrite_files" \
2364     | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
2365     | LC_ALL=C sort -u \
2366     > "$tmp"/dirs
2367   { # Rearrange file descriptors. Needed because "while ... done < ..."
2368     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2369     exec 5<&0 < "$tmp"/dirs
2370     while read d; do
2371       mkdir -p "$testdir/$d"
2372     done
2373     exec 0<&5 5<&-
2374   }
2375
2376   # Copy files or make symbolic links.
2377   delimiter='   '
2378   for f in $files; do echo $f; done \
2379     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_files" \
2380     | LC_ALL=C sort \
2381     > "$tmp"/files
2382   { # Rearrange file descriptors. Needed because "while ... done < ..."
2383     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2384     exec 5<&0 < "$tmp"/files
2385     while read g f; do
2386       func_lookup_file "$f"
2387       if test -n "$lookedup_tmp"; then
2388         cp -p "$lookedup_file" "$testdir/$g"
2389       else
2390         ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
2391         if { test -n "$symbolic" \
2392              || { test -n "$lsymbolic" \
2393                   && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; }; then
2394           func_ln "$lookedup_file" "$testdir/$g"
2395         else
2396           cp -p "$lookedup_file" "$testdir/$g"
2397         fi
2398       fi
2399     done
2400     exec 0<&5 5<&-
2401   }
2402
2403   # Create Makefile.ams that are for testing.
2404   for_test=true
2405
2406   # Create $sourcebase/Makefile.am.
2407   mkdir -p "$testdir/$sourcebase"
2408   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
2409
2410   # Create $m4base/Makefile.am.
2411   mkdir -p "$testdir/$m4base"
2412   (echo "## Process this file with automake to produce Makefile.in."
2413    echo
2414    echo "EXTRA_DIST ="
2415    for f in $files; do
2416      case "$f" in
2417        m4/* )
2418          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
2419      esac
2420    done
2421   ) > "$testdir/$m4base/Makefile.am"
2422
2423   subdirs="$sourcebase $m4base"
2424   subdirs_with_configure_ac=""
2425
2426   if false && test -f "$testdir"/$m4base/gettext.m4; then
2427     # Avoid stupid error message from automake:
2428     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
2429     mkdir -p "$testdir/po"
2430     (echo "## Process this file with automake to produce Makefile.in."
2431     ) > "$testdir/po/Makefile.am"
2432     func_append subdirs " po"
2433   fi
2434
2435   if test -n "$inctests"; then
2436     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
2437     # Create $testsbase/Makefile.am.
2438     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
2439     # Create $testsbase/configure.ac.
2440     (echo "# Process this file with autoconf to produce a configure script."
2441      echo "AC_INIT([dummy], [0])"
2442      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
2443      echo "AM_INIT_AUTOMAKE"
2444      echo
2445      echo "AM_CONFIG_HEADER([config.h])"
2446      echo
2447      echo "AC_PROG_CC"
2448      echo "AC_PROG_INSTALL"
2449      echo "AC_PROG_MAKE_SET"
2450      echo "AC_PROG_RANLIB"
2451      echo
2452      if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2453        echo "AC_GNU_SOURCE"
2454        echo
2455      fi
2456      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2457        echo "gl_USE_SYSTEM_EXTENSIONS"
2458        echo
2459      fi
2460      if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2461        echo "gl_LOCK_EARLY"
2462        echo
2463      fi
2464      if test "$libtool" = true; then
2465        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2466        echo "gl_cond_libtool=true"
2467      else
2468        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2469        echo "gl_cond_libtool=false"
2470        echo "gl_libdeps="
2471        echo "gl_ltlibdeps="
2472      fi
2473      # Wrap the set of autoconf snippets into an autoconf macro that is then
2474      # invoked. This is needed because autoconf does not support AC_REQUIRE
2475      # at the top level:
2476      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2477      # but we want the AC_REQUIRE to have its normal meaning (provide one
2478      # expansion of the required macro before the current point, and only one
2479      # expansion total).
2480      echo "AC_DEFUN([gl_INIT], ["
2481      func_emit_initmacro_start
2482      sed_replace_build_aux='
2483        :a
2484        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2485          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
2486          ba
2487        }'
2488      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2489      # We don't have explicit ordering constraints between the various
2490      # autoconf snippets. It's cleanest to put those of the library before
2491      # those of the tests.
2492      echo "gl_source_base='../$sourcebase'"
2493      for module in $modules; do
2494        func_verify_nontests_module
2495        if test -n "$module"; then
2496          func_get_autoconf_snippet "$module" \
2497            | sed -e "$sed_replace_build_aux"
2498        fi
2499      done
2500      echo "gl_source_base='.'"
2501      for module in $modules; do
2502        func_verify_tests_module
2503        if test -n "$module"; then
2504          func_get_autoconf_snippet "$module" \
2505            | sed -e "$sed_replace_build_aux"
2506        fi
2507      done
2508      # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2509      # created using libtool, because libtool already handles the dependencies.
2510      if test "$libtool" != true; then
2511        libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2512        echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2513        echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2514        echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2515        echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2516      fi
2517      func_emit_initmacro_end
2518      echo "])"
2519      func_emit_initmacro_done
2520      echo
2521      echo "gl_INIT"
2522      echo
2523      # Usually $testsbase/config.h will be a superset of config.h. Verify this
2524      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
2525      echo "AH_TOP([#include \"../config.h\"])"
2526      echo
2527      echo "AC_OUTPUT([Makefile])"
2528     ) > "$testdir/$testsbase/configure.ac"
2529     func_append subdirs " $testsbase"
2530     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
2531   fi
2532
2533   # Create Makefile.am.
2534   (echo "## Process this file with automake to produce Makefile.in."
2535    echo
2536    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2537    echo
2538    echo "SUBDIRS = $subdirs"
2539    echo
2540    echo "ACLOCAL_AMFLAGS = -I $m4base"
2541   ) > "$testdir/Makefile.am"
2542
2543   # Create configure.ac.
2544   (echo "# Process this file with autoconf to produce a configure script."
2545    echo "AC_INIT([dummy], [0])"
2546    if test "$auxdir" != "."; then
2547      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2548    fi
2549    echo "AM_INIT_AUTOMAKE"
2550    echo
2551    echo "AM_CONFIG_HEADER([config.h])"
2552    echo
2553    echo "AC_PROG_CC"
2554    echo "AC_PROG_INSTALL"
2555    echo "AC_PROG_MAKE_SET"
2556    echo
2557    echo "# For autobuild."
2558    echo "AC_CANONICAL_BUILD"
2559    echo "AC_CANONICAL_HOST"
2560    echo
2561    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
2562    echo "m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
2563    echo "m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
2564    echo "m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
2565    echo
2566    echo "AC_PROG_RANLIB"
2567    echo
2568    if test -n "$uses_subdirs"; then
2569      echo "AM_PROG_CC_C_O"
2570      echo
2571    fi
2572    if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2573      echo "AC_GNU_SOURCE"
2574      echo
2575    fi
2576    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2577      echo "gl_USE_SYSTEM_EXTENSIONS"
2578      echo
2579    fi
2580    if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2581      echo "gl_LOCK_EARLY"
2582      echo
2583    fi
2584    if test "$libtool" = true; then
2585      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2586      echo "gl_cond_libtool=true"
2587    else
2588      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2589      echo "gl_cond_libtool=false"
2590      echo "gl_libdeps="
2591      echo "gl_ltlibdeps="
2592    fi
2593    echo "gl_source_base='$sourcebase'"
2594    # Wrap the set of autoconf snippets into an autoconf macro that is then
2595    # invoked. This is needed because autoconf does not support AC_REQUIRE
2596    # at the top level:
2597    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2598    # but we want the AC_REQUIRE to have its normal meaning (provide one
2599    # expansion of the required macro before the current point, and only one
2600    # expansion total).
2601    echo "AC_DEFUN([gl_INIT], ["
2602    func_emit_initmacro_start
2603    if test "$auxdir" != "build-aux"; then
2604      sed_replace_build_aux='
2605        :a
2606        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2607          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2608          ba
2609        }'
2610      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2611    else
2612      sed_replace_build_aux=
2613    fi
2614    for module in $modules; do
2615      func_verify_nontests_module
2616      if test -n "$module"; then
2617        func_get_autoconf_snippet "$module" \
2618          | sed -e "$sed_replace_build_aux"
2619      fi
2620    done
2621    # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2622    # created using libtool, because libtool already handles the dependencies.
2623    if test "$libtool" != true; then
2624      libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2625      echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2626      echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2627      echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2628      echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2629    fi
2630    func_emit_initmacro_end
2631    echo "])"
2632    func_emit_initmacro_done
2633    echo
2634    echo "gl_INIT"
2635    echo
2636    if test -n "$subdirs_with_configure_ac"; then
2637      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
2638    fi
2639    makefiles="Makefile"
2640    for d in $subdirs; do
2641      # For subdirs that have a configure.ac by their own, it's the subdir's
2642      # configure.ac which creates the subdir's Makefile.am, not this one.
2643      case " $subdirs_with_configure_ac " in
2644        *" $d "*) ;;
2645        *) func_append makefiles " $d/Makefile" ;;
2646      esac
2647    done
2648    echo "AC_OUTPUT([$makefiles])"
2649   ) > "$testdir/configure.ac"
2650
2651   # Create autogenerated files.
2652   (cd "$testdir"
2653    # Do not use "${AUTORECONF} --force --install", because it may invoke
2654    # autopoint, which brings in older versions of some of our .m4 files.
2655    if test -f $m4base/gettext.m4; then
2656      echo "executing ${AUTOPOINT} --force"
2657      ${AUTOPOINT} --force || func_exit 1
2658      for f in $m4base/*.m4~; do
2659        mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
2660      done
2661    fi
2662    echo "executing ${ACLOCAL} -I $m4base"
2663    ${ACLOCAL} -I $m4base || func_exit 1
2664    if ! test -d build-aux; then
2665      echo "executing mkdir build-aux"
2666      mkdir build-aux || func_exit 1
2667    fi
2668    echo "executing ${AUTOCONF}"
2669    ${AUTOCONF} || func_exit 1
2670    echo "executing ${AUTOHEADER}"
2671    ${AUTOHEADER} || func_exit 1
2672    echo "executing ${AUTOMAKE} --add-missing --copy"
2673    ${AUTOMAKE} --add-missing --copy || func_exit 1
2674   ) || func_exit 1
2675   if test -n "$inctests"; then
2676     # Create autogenerated files.
2677     (cd "$testdir/$testsbase" || func_exit 1
2678      # Do not use "${AUTORECONF} --force --install", because it may invoke
2679      # autopoint, which brings in older versions of some of our .m4 files.
2680      if test -f ../$m4base/gettext.m4; then
2681        echo "executing ${AUTOPOINT} --force"
2682        ${AUTOPOINT} --force || func_exit 1
2683        for f in ../$m4base/*.m4~; do
2684          mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
2685        done
2686      fi
2687      echo "executing ${ACLOCAL} -I ../$m4base"
2688      ${ACLOCAL} -I ../$m4base || func_exit 1
2689      if ! test -d ../build-aux; then
2690        echo "executing mkdir ../build-aux"
2691        mkdir ../build-aux
2692      fi
2693      echo "executing ${AUTOCONF}"
2694      ${AUTOCONF} || func_exit 1
2695      echo "executing ${AUTOHEADER}"
2696      ${AUTOHEADER} || func_exit 1
2697      echo "executing ${AUTOMAKE} --add-missing --copy"
2698      ${AUTOMAKE} --add-missing --copy || func_exit 1
2699     ) || func_exit 1
2700   fi
2701   # Need to run configure and make once, to create built files that are to be
2702   # distributed (such as getdate.c).
2703   # Extract the value of "CLEANFILES += ...".
2704   cleaned_files=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
2705                  | sed -n -e 's,^CLEANFILES[    ]*+=\([^#]*\).*$,\1,p'`
2706   cleaned_files=" "`for file in $cleaned_files; do echo "$file"; done`" "
2707   # Extract the value of "BUILT_SOURCES += ...". Remove variable references
2708   # such $(FOO_H) because they don't refer to distributed files.
2709   sed_remove_make_variables='s,[$]([A-Za-z0-9_]*),,g'
2710   built_sources=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
2711                  | sed -n -e 's,^BUILT_SOURCES[         ]*+=\([^#]*\).*$,\1,p' \
2712                  | sed -e "$sed_remove_make_variables"`
2713   distributed_built_sources=`for file in $built_sources; do
2714                                case "$cleaned_files" in
2715                                  *" "$file" "*) ;;
2716                                  *) echo $file ;;
2717                                esac;
2718                              done`
2719   if test -n "$distributed_built_sources"; then
2720     (cd "$testdir"
2721      ./configure || func_exit 1
2722        cd "$sourcebase"
2723        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
2724        make built_sources || func_exit 1
2725        cd ..
2726      make distclean || func_exit 1
2727     ) || func_exit 1
2728   fi
2729 }
2730
2731 # func_create_megatestdir megatestdir allmodules
2732 # Input:
2733 # - local_gnulib_dir  from --local-dir
2734 # - auxdir          directory relative to destdir where to place build aux files
2735 func_create_megatestdir ()
2736 {
2737   megatestdir="$1"
2738   allmodules="$2"
2739   if test -z "$allmodules"; then
2740     allmodules=`func_all_modules`
2741   fi
2742
2743   megasubdirs=
2744   # First, all modules one by one.
2745   for onemodule in $allmodules; do
2746     func_create_testdir "$megatestdir/$onemodule" $onemodule
2747     func_append megasubdirs "$onemodule "
2748   done
2749   # Then, all modules all together.
2750   # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
2751   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2752   allmodules=`for m in $allmodules; do if test $m != config-h && test $m != fnmatch-posix; then echo $m; fi; done`
2753   func_create_testdir "$megatestdir/ALL" "$allmodules"
2754   func_append megasubdirs "ALL"
2755
2756   # Create Makefile.am.
2757   (echo "## Process this file with automake to produce Makefile.in."
2758    echo
2759    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2760    echo
2761    echo "SUBDIRS = $megasubdirs"
2762   ) > "$megatestdir/Makefile.am"
2763
2764   # Create configure.ac.
2765   (echo "# Process this file with autoconf to produce a configure script."
2766    echo "AC_INIT([dummy], [0])"
2767    if test "$auxdir" != "."; then
2768      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2769    fi
2770    echo "AM_INIT_AUTOMAKE"
2771    echo
2772    echo "AC_PROG_MAKE_SET"
2773    echo
2774    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
2775    echo "AC_OUTPUT([Makefile])"
2776   ) > "$megatestdir/configure.ac"
2777
2778   # Create autogenerated files.
2779   (cd "$megatestdir"
2780    # Do not use "${AUTORECONF} --install", because autoreconf operates
2781    # recursively, but the subdirectories are already finished, therefore
2782    # calling autoreconf here would only waste lots of CPU time.
2783    echo "executing ${ACLOCAL}"
2784    ${ACLOCAL} || func_exit 1
2785    echo "executing mkdir build-aux"
2786    mkdir build-aux
2787    echo "executing ${AUTOCONF}"
2788    ${AUTOCONF} || func_exit 1
2789    echo "executing ${AUTOMAKE} --add-missing --copy"
2790    ${AUTOMAKE} --add-missing --copy || func_exit 1
2791   ) || func_exit 1
2792 }
2793
2794 case $mode in
2795   "" )
2796     func_fatal_error "no mode specified" ;;
2797
2798   list )
2799     func_all_modules
2800     ;;
2801
2802   import | update )
2803
2804     # Where to import.
2805     if test -z "$destdir"; then
2806       destdir=.
2807     fi
2808     test -d "$destdir" \
2809       || func_fatal_error "destination directory does not exist: $destdir"
2810
2811     # Prefer configure.ac to configure.in.
2812     if test -f "$destdir"/configure.ac; then
2813       configure_ac="$destdir/configure.ac"
2814     else
2815       if test -f "$destdir"/configure.in; then
2816         configure_ac="$destdir/configure.in"
2817       else
2818         func_fatal_error "cannot find $destdir/configure.ac"
2819       fi
2820     fi
2821
2822     # Analyze configure.ac.
2823     guessed_auxdir="."
2824     guessed_libtool=false
2825     my_sed_traces='
2826       s,#.*$,,
2827       s,^dnl .*$,,
2828       s, dnl .*$,,
2829       /AC_CONFIG_AUX_DIR/ {
2830         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
2831       }
2832       /A[CM]_PROG_LIBTOOL/ {
2833         s,^.*$,guessed_libtool=true,p
2834       }'
2835     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
2836
2837     if test -z "$auxdir"; then
2838       auxdir="$guessed_auxdir"
2839     fi
2840
2841     # Determine where to apply func_import.
2842     if test -n "$m4base"; then
2843       # Apply func_import to a particular gnulib directory.
2844       # Any number of additional modules can be given.
2845       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
2846         # First use of gnulib in the given m4base.
2847         test -n "$supplied_libname" || supplied_libname=true
2848         test -n "$sourcebase" || sourcebase="lib"
2849         test -n "$docbase" || docbase="doc"
2850         test -n "$testsbase" || testsbase="tests"
2851         test -n "$macro_prefix" || macro_prefix="gl"
2852       fi
2853       func_import "$*"
2854     else
2855       # Apply func_import to all gnulib directories.
2856       # To get this list of directories, look at Makefile.am. (Not at
2857       # configure, because it may be omitted from CVS. Also, don't run
2858       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
2859       m4dirs=
2860       m4dirs_count=0
2861       if test -f "$destdir"/Makefile.am; then
2862         aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[         ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
2863         m4dir_is_next=
2864         for arg in $aclocal_amflags; do
2865           if test -n "$m4dir_is_next"; then
2866             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
2867             case "$arg" in
2868               /*) ;;
2869               *)
2870                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
2871                   func_append m4dirs " $arg"
2872                   m4dirs_count=`expr $m4dirs_count + 1`
2873                 fi
2874                 ;;
2875             esac
2876           else
2877             if test "X$arg" = "X-I"; then
2878               m4dir_is_next=yes
2879             else
2880               m4dir_is_next=
2881             fi
2882           fi
2883         done
2884       else
2885         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
2886         if test -f "$destdir"/aclocal.m4; then
2887           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
2888           sedexpr2='s,^[^/]*$,.,'
2889           sedexpr3='s,/[^/]*$,,'
2890           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort -u`
2891           m4dirs_count=`echo "$m4dirs" | wc -l`
2892         fi
2893       fi
2894       if test $m4dirs_count = 0; then
2895         # First use of gnulib in a package.
2896         # Any number of additional modules can be given.
2897         test -n "$supplied_libname" || supplied_libname=true
2898         test -n "$sourcebase" || sourcebase="lib"
2899         m4base="m4"
2900         test -n "$docbase" || docbase="doc"
2901         test -n "$testsbase" || testsbase="tests"
2902         test -n "$macro_prefix" || macro_prefix="gl"
2903         func_import "$*"
2904       else
2905         if test $m4dirs_count = 1; then
2906           # There's only one use of gnulib here. Assume the user means it.
2907           # Any number of additional modules can be given.
2908           for m4base in $m4dirs; do
2909             func_import "$*"
2910           done
2911         else
2912           # Ambiguous - guess what the user meant.
2913           if test $# = 0; then
2914             # No further arguments. Guess the user wants to update all of them.
2915             for m4base in $m4dirs; do
2916               # Perform func_import in a subshell, so that variable values
2917               # such as
2918               #   local_gnulib_dir, avoidlist, sourcebase, m4base, docbase,
2919               #   testsbase, libname, lgpl, makefile_name, libtool, macro_prefix
2920               # don't propagate from one directory to another.
2921               (func_import) || func_exit 1
2922             done
2923           else
2924             # Really ambiguous.
2925             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
2926           fi
2927         fi
2928       fi
2929     fi
2930     ;;
2931
2932   create-testdir )
2933     if test -z "$destdir"; then
2934       func_fatal_error "please specify --dir option"
2935     fi
2936     mkdir "$destdir"
2937     test -d "$destdir" \
2938       || func_fatal_error "could not create destination directory"
2939     test -n "$auxdir" || auxdir="build-aux"
2940     func_create_testdir "$destdir" "$*"
2941     ;;
2942
2943   create-megatestdir )
2944     if test -z "$destdir"; then
2945       func_fatal_error "please specify --dir option"
2946     fi
2947     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2948     test -n "$auxdir" || auxdir="build-aux"
2949     func_create_megatestdir "$destdir" "$*"
2950     ;;
2951
2952   test )
2953     test -n "$destdir" || destdir=testdir$$
2954     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2955     test -n "$auxdir" || auxdir="build-aux"
2956     func_create_testdir "$destdir" "$*"
2957     cd "$destdir"
2958       mkdir build
2959       cd build
2960         ../configure || func_exit 1
2961         make || func_exit 1
2962         make check || func_exit 1
2963         make distclean || func_exit 1
2964         remaining=`find . -type f -print`
2965         if test -n "$remaining"; then
2966           echo "Remaining files:" $remaining 1>&2
2967           echo "gnulib-tool: *** Stop." 1>&2
2968           func_exit 1
2969         fi
2970       cd ..
2971     cd ..
2972     rm -rf "$destdir"
2973     ;;
2974
2975   megatest )
2976     test -n "$destdir" || destdir=testdir$$
2977     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2978     test -n "$auxdir" || auxdir="build-aux"
2979     func_create_megatestdir "$destdir" "$*"
2980     cd "$destdir"
2981       mkdir build
2982       cd build
2983         ../configure
2984         make
2985         make check
2986         make distclean
2987         remaining=`find . -type f -print`
2988         if test -n "$remaining"; then
2989           echo "Remaining files:" $remaining 1>&2
2990           echo "gnulib-tool: *** Stop." 1>&2
2991           func_exit 1
2992         fi
2993       cd ..
2994     cd ..
2995     rm -rf "$destdir"
2996     ;;
2997
2998   extract-description )
2999     for module
3000     do
3001       func_verify_module
3002       if test -n "$module"; then
3003         func_get_description "$module"
3004       fi
3005     done
3006     ;;
3007
3008   extract-filelist )
3009     for module
3010     do
3011       func_verify_module
3012       if test -n "$module"; then
3013         func_get_filelist "$module"
3014       fi
3015     done
3016     ;;
3017
3018   extract-dependencies )
3019     for module
3020     do
3021       func_verify_module
3022       if test -n "$module"; then
3023         func_get_dependencies "$module"
3024       fi
3025     done
3026     ;;
3027
3028   extract-autoconf-snippet )
3029     for module
3030     do
3031       func_verify_module
3032       if test -n "$module"; then
3033         func_get_autoconf_snippet "$module"
3034       fi
3035     done
3036     ;;
3037
3038   extract-automake-snippet )
3039     for module
3040     do
3041       func_verify_module
3042       if test -n "$module"; then
3043         func_get_automake_snippet "$module"
3044       fi
3045     done
3046     ;;
3047
3048   extract-include-directive )
3049     for module
3050     do
3051       func_verify_module
3052       if test -n "$module"; then
3053         func_get_include_directive "$module"
3054       fi
3055     done
3056     ;;
3057
3058   extract-license )
3059     for module
3060     do
3061       func_verify_module
3062       if test -n "$module"; then
3063         func_get_license "$module"
3064       fi
3065     done
3066     ;;
3067
3068   extract-maintainer )
3069     for module
3070     do
3071       func_verify_module
3072       if test -n "$module"; then
3073         func_get_maintainer "$module"
3074       fi
3075     done
3076     ;;
3077
3078   extract-tests-module )
3079     for module
3080     do
3081       func_verify_module
3082       if test -n "$module"; then
3083         func_get_tests_module "$module"
3084       fi
3085     done
3086     ;;
3087
3088   * )
3089     func_fatal_error "unknown operation mode --$mode" ;;
3090 esac
3091
3092 rm -rf "$tmp"
3093 # Undo the effect of the previous 'trap' command. Some shellology:
3094 # We cannot use "trap - 0 1 2 3 13 15", because Solaris sh would attempt to
3095 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
3096 # exit); for the others we need to call 'exit' explicitly. The value of $? is
3097 # 128 + signal number and is set before the trap-registered command is run.
3098 trap '' 0
3099 trap 'func_exit $?' 1 2 3 13 15
3100
3101 exit 0
3102
3103 # Local Variables:
3104 # indent-tabs-mode: nil
3105 # whitespace-check-buffer-indent: nil
3106 # End: