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