Finish support for source files in subdirectories.
[pspp] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2006 Free Software Foundation, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19
20 # This program is meant for authors or maintainers which want to import
21 # modules from gnulib into their packages.
22
23 progname=$0
24 package=gnulib
25 cvsdatestamp='$Date: 2006-11-13 12:33:43 $'
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       # Synthesize also an EXTRA_lib_SOURCES augmentation.
943       # This is necessary so that automake can generate the right list of
944       # dependency rules.
945       # A possible approach would be to use autom4te --trace of the redefined
946       # AC_LIBOBJ and AC_REPLACE_FUNCS macros when creating the Makefile.am
947       # (use autom4te --trace, not just grep, so that AC_LIBOBJ invocations
948       # inside autoconf's built-in macros are not missed).
949       # But it's simpler and more robust to do it here, based on the file list.
950       # If some .c file exists and is not used with AC_LIBOBJ - for example,
951       # a .c file is preprocessed into another .c file for BUILT_SOURCES -,
952       # automake will generate a useless dependency; this is harmless.
953       sed_extract_c_files='/\.c$/p'
954       extra_files=`echo "$extra_files" | sed -n -e "$sed_extract_c_files"`
955       if test -n "$extra_files"; then
956         echo "EXTRA_lib_SOURCES +=" $extra_files
957         echo
958       fi
959       ;;
960   esac
961 }
962
963 # func_get_include_directive module
964 # Input:
965 # - local_gnulib_dir  from --local-dir
966 func_get_include_directive ()
967 {
968   func_lookup_file "modules/$1"
969   sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
970   sed -e 's/^\(["<]\)/#include \1/'
971 }
972
973 # func_get_license module
974 # Input:
975 # - local_gnulib_dir  from --local-dir
976 func_get_license ()
977 {
978   func_lookup_file "modules/$1"
979   sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
980 }
981
982 # func_get_maintainer module
983 # Input:
984 # - local_gnulib_dir  from --local-dir
985 func_get_maintainer ()
986 {
987   func_lookup_file "modules/$1"
988   sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
989 }
990
991 # func_get_tests_module module
992 # Input:
993 # - local_gnulib_dir  from --local-dir
994 func_get_tests_module ()
995 {
996   # The naming convention for tests modules is hardwired: ${module}-tests.
997   if test -f "$gnulib_dir/modules/$1"-tests \
998      || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
999           && test -f "$local_gnulib_dir/modules/$1"-tests; }; then
1000     echo "$1"-tests
1001   fi
1002 }
1003
1004 # func_acceptable module
1005 # tests whether a module is acceptable.
1006 # Input:
1007 # - avoidlist       list of modules to avoid
1008 func_acceptable ()
1009 {
1010   for avoid in $avoidlist; do
1011     if test "$avoid" = "$1"; then
1012       return 1
1013     fi
1014   done
1015   return 0
1016 }
1017
1018 # func_modules_transitive_closure
1019 # Input:
1020 # - local_gnulib_dir  from --local-dir
1021 # - modules         list of specified modules
1022 # - inctests        true if tests should be included, blank otherwise
1023 # - avoidlist       list of modules to avoid
1024 # - tmp             pathname of a temporary directory
1025 # Output:
1026 # - modules         list of modules, including dependencies
1027 func_modules_transitive_closure ()
1028 {
1029   # In order to process every module only once (for speed), process an "input
1030   # list" of modules, producing an "output list" of modules. During each round,
1031   # more modules can be queued in the input list. Once a module on the input
1032   # list has been processed, it is added to the "handled list", so we can avoid
1033   # to process it again.
1034   handledmodules=
1035   inmodules="$modules"
1036   outmodules=
1037   while test -n "$inmodules"; do
1038     inmodules_this_round="$inmodules"
1039     inmodules=                    # Accumulator, queue for next round
1040     for module in $inmodules_this_round; do
1041       func_verify_module
1042       if test -n "$module"; then
1043         if func_acceptable $module; then
1044           func_append outmodules " $module"
1045           deps=`func_get_dependencies $module`
1046           # Duplicate dependencies are harmless, but Jim wants a warning.
1047           duplicated_deps=`echo "$deps" | LC_ALL=C sort | LC_ALL=C uniq -d`
1048           if test -n "$duplicated_deps"; then
1049             echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
1050           fi
1051           func_append inmodules " $deps"
1052           if test -n "$inctests"; then
1053             testsmodule=`func_get_tests_module $module`
1054             if test -n "$testsmodule"; then
1055               func_append inmodules " $testsmodule"
1056             fi
1057           fi
1058         fi
1059       fi
1060     done
1061     handledmodules=`for m in $handledmodules $inmodules_this_round; do echo $m; done | LC_ALL=C sort -u`
1062     # Remove $handledmodules from $inmodules.
1063     for m in $inmodules; do echo $m; done | LC_ALL=C sort -u > "$tmp"/queued-modules
1064     inmodules=`echo "$handledmodules" | LC_ALL=C join -v 2 - "$tmp"/queued-modules`
1065   done
1066   modules=`for m in $outmodules; do echo $m; done | LC_ALL=C sort -u`
1067   rm -f "$tmp"/queued-modules
1068 }
1069
1070 # func_modules_add_dummy
1071 # Input:
1072 # - local_gnulib_dir  from --local-dir
1073 # - modules         list of modules, including dependencies
1074 # Output:
1075 # - modules         list of modules, including 'dummy' if needed
1076 func_modules_add_dummy ()
1077 {
1078   have_lib_SOURCES=
1079   sed_remove_backslash_newline=':a
1080 /\\$/{
1081 s/\\$//
1082 N
1083 s/\n//
1084 ba
1085 }'
1086   for module in $modules; do
1087     func_verify_nontests_module
1088     if test -n "$module"; then
1089       # Extract the value of "lib_SOURCES += ...".
1090       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
1091         # Ignore .h files since they are not compiled.
1092         case "$file" in
1093           *.h) ;;
1094           *) have_lib_SOURCES=yes ;;
1095         esac
1096       done
1097     fi
1098   done
1099   # Add the dummy module, to make sure the library will be non-empty.
1100   if test -z "$have_lib_SOURCES"; then
1101     func_append modules " dummy"
1102   fi
1103 }
1104
1105 # func_modules_to_filelist
1106 # Input:
1107 # - local_gnulib_dir  from --local-dir
1108 # - modules         list of modules, including dependencies
1109 # Output:
1110 # - files           list of files
1111 func_modules_to_filelist ()
1112 {
1113   files=
1114   for module in $modules; do
1115     func_verify_module
1116     if test -n "$module"; then
1117       fs=`func_get_filelist $module`
1118       func_append files " $fs"
1119     fi
1120   done
1121   files=`for f in $files; do echo $f; done | LC_ALL=C sort -u`
1122 }
1123
1124 # func_emit_lib_Makefile_am
1125 # emits the contents of library makefile to standard output.
1126 # Input:
1127 # - local_gnulib_dir  from --local-dir
1128 # - modules         list of modules, including dependencies
1129 # - libname         library name
1130 # - makefile_name   from --makefile-name
1131 # - libtool         true if libtool will be used, false or blank otherwise
1132 # - macro_prefix    prefix of gl_LIBOBJS macros to use
1133 # - actioncmd       (optional) command that will reproduce this invocation
1134 # Output:
1135 # - uses_subdirs    nonempty if object files in subdirs exist
1136 func_emit_lib_Makefile_am ()
1137 {
1138   # When creating an includable Makefile.am snippet, augment variables with
1139   # += instead of assigning them.
1140   if test -n "$makefile_name"; then
1141     assign='+='
1142   else
1143     assign='='
1144   fi
1145   if test "$libtool" = true; then
1146     libext=la
1147     perhapsLT=LT
1148     sed_eliminate_LDFLAGS=
1149   else
1150     libext=a
1151     perhapsLT=
1152     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
1153   fi
1154   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
1155   echo "## Process this file with automake to produce Makefile.in."
1156   func_emit_copyright_notice
1157   if test -n "$actioncmd"; then
1158     echo "# Reproduce by: $actioncmd"
1159   fi
1160   echo
1161   uses_subdirs=
1162   {
1163     for module in $modules; do
1164       func_verify_nontests_module
1165       if test -n "$module"; then
1166         {
1167           func_get_automake_snippet "$module" |
1168             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1169                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1170             sed -e "$sed_eliminate_LDFLAGS" |
1171             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1172             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1173                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g'
1174           if test "$module" = 'alloca'; then
1175             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
1176             echo "${libname}_${libext}_DEPENDENCIES += @${perhapsLT}ALLOCA@"
1177           fi
1178         } > amsnippet.tmp
1179         # Skip the contents if its entirely empty.
1180         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1181           echo "## begin gnulib module $module"
1182           echo
1183           cat amsnippet.tmp
1184           echo "## end   gnulib module $module"
1185           echo
1186         fi
1187         rm -f amsnippet.tmp
1188         # Test whether there are some source files in subdirectories.
1189         for f in `func_get_filelist "$module"`; do
1190           case $f in
1191             lib/*/*.c) uses_subdirs=yes ;;
1192           esac
1193         done
1194       fi
1195     done
1196   } > allsnippets.tmp
1197   if test -z "$makefile_name"; then
1198     # If there are source files in subdirectories, prevent collision of the
1199     # object files (example: hash.c and libxml/hash.c).
1200     subdir_options=
1201     if test -n "$uses_subdirs"; then
1202       subdir_options=' subdir-objects'
1203     fi
1204     echo "AUTOMAKE_OPTIONS = 1.5 gnits${subdir_options}"
1205   fi
1206   echo
1207   if grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *= *$libname\\.$libext\$" allsnippets.tmp > /dev/null; then
1208     # One of the snippets already specifies an installation location for the
1209     # library. Don't confuse automake by saying it should not be installed.
1210     :
1211   else
1212     # By default, the generated library should not be installed.
1213     echo "noinst_${perhapsLT}LIBRARIES $assign $libname.$libext"
1214   fi
1215   echo
1216   echo "${libname}_${libext}_SOURCES ="
1217   # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
1218   # automake during its analyses looks for $(LIBOBJS), not for @LIBOBJS@.
1219   echo "${libname}_${libext}_LIBADD = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
1220   echo "${libname}_${libext}_DEPENDENCIES = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
1221   echo "EXTRA_${libname}_${libext}_SOURCES ="
1222   if test "$libtool" = true; then
1223     echo "${libname}_${libext}_LDFLAGS = \$(AM_LDFLAGS)"
1224   fi
1225   if test -z "$makefile_name"; then
1226     echo "noinst_HEADERS ="
1227     # Automake versions < 1.9b create an empty pkgdatadir at installation time
1228     # if you specify pkgdata_DATA to empty. This is a workaround.
1229     if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1230       echo "pkgdata_DATA ="
1231     fi
1232     echo "EXTRA_DIST ="
1233     echo "BUILT_SOURCES ="
1234     echo "SUFFIXES ="
1235   fi
1236   echo "MOSTLYCLEANFILES $assign core *.stackdump"
1237   if test -z "$makefile_name"; then
1238     echo "MOSTLYCLEANDIRS ="
1239     echo "CLEANFILES ="
1240     echo "DISTCLEANFILES ="
1241     echo "MAINTAINERCLEANFILES ="
1242     echo
1243     echo "AM_CPPFLAGS ="
1244   fi
1245   echo
1246   cat allsnippets.tmp
1247   echo
1248   echo "mostlyclean-local: mostlyclean-generic"
1249   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
1250   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
1251   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1252   echo "          fi; \\"
1253   echo "        done"
1254   rm -f allsnippets.tmp
1255 }
1256
1257 # func_emit_tests_Makefile_am
1258 # emits the contents of tests makefile to standard output.
1259 # Input:
1260 # - local_gnulib_dir  from --local-dir
1261 # - modules         list of modules, including dependencies
1262 # - libname         library name
1263 # - makefile_name   from --makefile-name
1264 # - libtool         true if libtool will be used, false or blank otherwise
1265 # - sourcebase      relative directory containing lib source code
1266 # - m4base          relative directory containing autoconf macros
1267 # - testsbase       relative directory containing unit test code
1268 func_emit_tests_Makefile_am ()
1269 {
1270   if test "$libtool" = true; then
1271     libext=la
1272   else
1273     libext=a
1274   fi
1275   if test "$libtool" = true; then
1276     libext=la
1277     sed_eliminate_LDFLAGS=
1278   else
1279     libext=a
1280     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
1281   fi
1282   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
1283   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
1284   echo "## Process this file with automake to produce Makefile.in."
1285   func_emit_copyright_notice
1286   echo
1287   # Generate dependencies here, since it eases the debugging of test failures.
1288   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1289   echo
1290   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
1291   echo
1292   (
1293     for module in $modules; do
1294       func_verify_tests_module
1295       if test -n "$module"; then
1296         {
1297           func_get_automake_snippet "$module" |
1298             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1299                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1300             sed -e "$sed_eliminate_LDFLAGS" |
1301             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1302             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1303                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g'
1304         } > amsnippet.tmp
1305         # Skip the contents if its entirely empty.
1306         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1307           echo "## begin gnulib module $module"
1308           echo
1309           cat amsnippet.tmp
1310           echo "## end   gnulib module $module"
1311           echo
1312         fi
1313         rm -f amsnippet.tmp
1314       fi
1315     done
1316   ) > allsnippets.tmp
1317   # Nothing is being added to SUBDIRS; nevertheless the existence of this
1318   # variable is needed to avoid an error from automake:
1319   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
1320   echo "SUBDIRS ="
1321   echo "TESTS ="
1322   echo "TESTS_ENVIRONMENT ="
1323   echo "noinst_PROGRAMS ="
1324   echo "check_PROGRAMS ="
1325   echo "noinst_HEADERS ="
1326   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1327     echo "pkgdata_DATA ="
1328   fi
1329   echo "EXTRA_DIST ="
1330   echo "BUILT_SOURCES ="
1331   echo "SUFFIXES ="
1332   echo "MOSTLYCLEANFILES = core *.stackdump"
1333   echo "MOSTLYCLEANDIRS ="
1334   echo "CLEANFILES ="
1335   echo "DISTCLEANFILES ="
1336   echo "MAINTAINERCLEANFILES ="
1337   echo
1338   echo "AM_CPPFLAGS = \\"
1339   echo "  -I. -I\$(srcdir) \\"
1340   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
1341   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
1342   echo
1343   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
1344   echo
1345   cat allsnippets.tmp
1346   echo "# Clean up after Solaris cc."
1347   echo "clean-local:"
1348   echo "        rm -rf SunWS_cache"
1349   echo
1350   echo "mostlyclean-local: mostlyclean-generic"
1351   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
1352   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
1353   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1354   echo "          fi; \\"
1355   echo "        done"
1356   rm -f allsnippets.tmp
1357 }
1358
1359 # func_emit_initmacro_start
1360 # emits the first few statements of the gl_INIT macro to standard output.
1361 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1362 func_emit_initmacro_start ()
1363 {
1364   echo "  m4_pushdef([AC_LIBOBJ], m4_defn([${macro_prefix}_LIBOBJ]))"
1365   echo "  m4_pushdef([AC_REPLACE_FUNCS], m4_defn([${macro_prefix}_REPLACE_FUNCS]))"
1366 }
1367
1368 # func_emit_initmacro_end
1369 # emits the last few statements of the gl_INIT macro to standard output.
1370 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1371 func_emit_initmacro_end ()
1372 {
1373   echo "  m4_popdef([AC_REPLACE_FUNCS])"
1374   echo "  m4_popdef([AC_LIBOBJ])"
1375   echo "  AC_CONFIG_COMMANDS_PRE(["
1376   echo "    ${macro_prefix}_libobjs="
1377   echo "    ${macro_prefix}_ltlibobjs="
1378   echo "    if test -n \"\$${macro_prefix}_LIBOBJS\"; then"
1379   echo "      # Remove the extension."
1380   echo "      sed_drop_objext='s/\\.o\$//;s/\\.obj\$//'"
1381   echo "      for i in \`for i in \$${macro_prefix}_LIBOBJS; do echo \"\$i\"; done | sed \"\$sed_drop_objext\" | sort | uniq\`; do"
1382   echo "        ${macro_prefix}_libobjs=\"\$${macro_prefix}_libobjs \$i.\$ac_objext\""
1383   echo "        ${macro_prefix}_ltlibobjs=\"\$${macro_prefix}_ltlibobjs \$i.lo\""
1384   echo "      done"
1385   echo "    fi"
1386   echo "    AC_SUBST([${macro_prefix}_LIBOBJS], [\$${macro_prefix}_libobjs])"
1387   echo "    AC_SUBST([${macro_prefix}_LTLIBOBJS], [\$${macro_prefix}_ltlibobjs])"
1388   echo "  ])"
1389 }
1390
1391 # func_emit_initmacro_done
1392 # emits a few statements after the gl_INIT macro to standard output.
1393 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1394 func_emit_initmacro_done ()
1395 {
1396   echo
1397   echo "# Like AC_LIBOBJ, except that the module name goes"
1398   echo "# into ${macro_prefix}_LIBOBJS instead of into LIBOBJS."
1399   echo "AC_DEFUN([${macro_prefix}_LIBOBJ],"
1400   echo "  [${macro_prefix}_LIBOBJS=\"\$${macro_prefix}_LIBOBJS \$1.\$ac_objext\"])"
1401   echo
1402   echo "# Like AC_REPLACE_FUNCS, except that the module name goes"
1403   echo "# into ${macro_prefix}_LIBOBJS instead of into LIBOBJS."
1404   echo "AC_DEFUN([${macro_prefix}_REPLACE_FUNCS],"
1405   echo "  [AC_CHECK_FUNCS([\$1], , [${macro_prefix}_LIBOBJ(\$ac_func)])])"
1406 }
1407
1408 # func_import modules
1409 # Uses also the variables
1410 # - destdir         target directory
1411 # - local_gnulib_dir  from --local-dir
1412 # - libname         library name
1413 # - sourcebase      directory relative to destdir where to place source code
1414 # - m4base          directory relative to destdir where to place *.m4 macros
1415 # - docbase         directory relative to destdir where to place doc files
1416 # - testsbase       directory relative to destdir where to place unit test code
1417 # - auxdir          directory relative to destdir where to place build aux files
1418 # - inctests        true if --with-tests was given, blank otherwise
1419 # - avoidlist       list of modules to avoid, from --avoid
1420 # - lgpl            true if library's license shall be LGPL, blank otherwise
1421 # - makefile_name   from --makefile-name
1422 # - libtool         true if --libtool was given, false if --no-libtool was
1423 #                   given, blank otherwise
1424 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
1425 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1426 # - autoconf_minversion  minimum supported autoconf version
1427 # - doit            : if actions shall be executed, false if only to be printed
1428 # - symbolic        true if files should be symlinked, copied otherwise
1429 func_import ()
1430 {
1431   # Get the cached settings.
1432   cached_local_gnulib_dir=
1433   cached_specified_modules=
1434   cached_avoidlist=
1435   cached_sourcebase=
1436   cached_m4base=
1437   cached_docbase=
1438   cached_testsbase=
1439   cached_libname=
1440   cached_lgpl=
1441   cached_makefile_name=
1442   cached_libtool=
1443   cached_macro_prefix=
1444   cached_files=
1445   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1446     cached_libtool=false
1447     my_sed_traces='
1448       s,#.*$,,
1449       s,^dnl .*$,,
1450       s, dnl .*$,,
1451       /gl_LOCAL_DIR(/ {
1452         s,^.*gl_LOCAL_DIR([[ ]*\([^])]*\).*$,cached_local_gnulib_dir="\1",p
1453       }
1454       /gl_MODULES(/ {
1455         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
1456       }
1457       /gl_AVOID(/ {
1458         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
1459       }
1460       /gl_SOURCE_BASE(/ {
1461         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
1462       }
1463       /gl_M4_BASE(/ {
1464         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
1465       }
1466       /gl_DOC_BASE(/ {
1467         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
1468       }
1469       /gl_TESTS_BASE(/ {
1470         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
1471       }
1472       /gl_LIB(/ {
1473         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
1474       }
1475       /gl_LGPL/ {
1476         s,^.*$,cached_lgpl=true,p
1477       }
1478       /gl_MAKEFILE_NAME(/ {
1479         s,^.*gl_MAKEFILE_NAME([[ ]*\([^])]*\).*$,cached_makefile_name="\1",p
1480       }
1481       /gl_LIBTOOL/ {
1482         s,^.*$,cached_libtool=true,p
1483       }
1484       /gl_MACRO_PREFIX(/ {
1485         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
1486       }'
1487     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
1488     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1489       my_sed_traces='
1490         s,#.*$,,
1491         s,^dnl .*$,,
1492         s, dnl .*$,,
1493         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
1494           s,^.*$,cached_files=",p
1495           n
1496           ta
1497           :a
1498           s,^\]).*$,",
1499           tb
1500           p
1501           n
1502           ba
1503           :b
1504           p
1505         }'
1506       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1507     fi
1508   fi
1509
1510   # Merge the cached settings with the specified ones.
1511   # The m4base must be the same as expected from the pathname.
1512   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1513     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1514   fi
1515   # The local_gnulib_dir defaults to the cached one. Recall that the cached one
1516   # is relative to $destdir, whereas the one we use is relative to . or absolute.
1517   if test -z "$local_gnulib_dir"; then
1518     if test -n "$cached_local_gnulib_dir"; then
1519       case "$destdir" in
1520         /*)
1521           local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1522         *)
1523           case "$cached_local_gnulib_dir" in
1524             /*)
1525               local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1526             *)
1527               func_relconcat "$destdir" "$cached_local_gnulib_dir"
1528               local_gnulib_dir="$relconcat" ;;
1529           esac ;;
1530       esac
1531     fi
1532   fi
1533   # Append the cached and the specified module names. So that
1534   # "gnulib-tool --import foo" means to add the module foo.
1535   specified_modules="$cached_specified_modules $1"
1536   # Append the cached and the specified avoidlist. This is probably better
1537   # than dropping the cached one when --avoid is specified at least once.
1538   avoidlist=`echo $cached_avoidlist $avoidlist`
1539   # The sourcebase defaults to the cached one.
1540   if test -z "$sourcebase"; then
1541     sourcebase="$cached_sourcebase"
1542     if test -z "$sourcebase"; then
1543       func_fatal_error "missing --source-base option"
1544     fi
1545   fi
1546   # The docbase defaults to the cached one.
1547   if test -z "$docbase"; then
1548     docbase="$cached_docbase"
1549     if test -z "$docbase"; then
1550       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."
1551     fi
1552   fi
1553   # The testsbase defaults to the cached one.
1554   if test -z "$testsbase"; then
1555     testsbase="$cached_testsbase"
1556     if test -z "$testsbase"; then
1557       func_fatal_error "missing --tests-base option"
1558     fi
1559   fi
1560   # The libname defaults to the cached one.
1561   if test -z "$supplied_libname"; then
1562     libname="$cached_libname"
1563     if test -z "$libname"; then
1564       func_fatal_error "missing --lib option"
1565     fi
1566   fi
1567   # Require LGPL if specified either way.
1568   if test -z "$lgpl"; then
1569     lgpl="$cached_lgpl"
1570   fi
1571   # The makefile_name defaults to the cached one.
1572   if test -z "$makefile_name"; then
1573     makefile_name="$cached_makefile_name"
1574   fi
1575   # Use libtool if specified either way, or if guessed.
1576   if test -z "$libtool"; then
1577     if test -n "$cached_m4base"; then
1578       libtool="$cached_libtool"
1579     else
1580       libtool="$guessed_libtool"
1581     fi
1582   fi
1583   # The macro_prefix defaults to the cached one.
1584   if test -z "$macro_prefix"; then
1585     macro_prefix="$cached_macro_prefix"
1586     if test -z "$macro_prefix"; then
1587       func_fatal_error "missing --macro-prefix option"
1588     fi
1589   fi
1590
1591   # Canonicalize the list of specified modules.
1592   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort -u`
1593
1594   # Determine final module list.
1595   modules="$specified_modules"
1596   func_modules_transitive_closure
1597   echo "Module list with included dependencies:"
1598   echo "$modules" | sed -e 's/^/  /'
1599
1600   # Add the dummy module if needed.
1601   func_modules_add_dummy
1602
1603   # If --lgpl, verify that the licenses of modules are compatible.
1604   if test -n "$lgpl"; then
1605     for module in $modules; do
1606       license=`func_get_license $module`
1607       case $license in
1608         LGPL | 'GPLed build tool') ;;
1609         'public domain' | 'unlimited' | 'unmodifiable license text') ;;
1610         *) func_fatal_error "incompatible license on module $module: $license" ;;
1611       esac
1612     done
1613   fi
1614
1615   # Determine script to apply to imported library files.
1616   sed_transform_lib_file=
1617   for module in $modules; do
1618     if test $module = config-h; then
1619       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
1620       sed_transform_lib_file=$sed_transform_lib_file'
1621         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
1622       '
1623       break
1624     fi
1625   done
1626   if test -n "$lgpl"; then
1627     # Update license.
1628     sed_transform_lib_file=$sed_transform_lib_file'
1629       s/GNU General/GNU Lesser General/g
1630       s/version 2\([ ,]\)/version 2.1\1/g
1631     '
1632   fi
1633
1634   # Determine final file list.
1635   func_modules_to_filelist
1636   echo "File list:"
1637   echo "$files" | sed -e 's/^/  /'
1638
1639   test -n "$files" \
1640     || func_fatal_error "refusing to do nothing"
1641
1642   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1643   new_files="$files m4/gnulib-tool.m4"
1644   old_files="$cached_files"
1645   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1646     func_append old_files " m4/gnulib-tool.m4"
1647   fi
1648
1649   sed_rewrite_old_files="\
1650     s,^build-aux/,$auxdir/,
1651     s,^doc/,$cached_docbase/,
1652     s,^lib/,$cached_sourcebase/,
1653     s,^m4/,$cached_m4base/,
1654     s,^tests/,$cached_testsbase/,"
1655   sed_rewrite_new_files="\
1656     s,^build-aux/,$auxdir/,
1657     s,^doc/,$docbase/,
1658     s,^lib/,$sourcebase/,
1659     s,^m4/,$m4base/,
1660     s,^tests/,$testsbase/,"
1661
1662   # Create directories.
1663   { echo "$sourcebase"
1664     echo "$m4base"
1665     docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1666     if test -n "$docfiles"; then
1667       echo "$docbase"
1668     fi
1669     if test -n "$inctests"; then
1670       echo "$testsbase"
1671     fi
1672     echo "$auxdir"
1673     for f in $files; do echo $f; done \
1674       | sed -e "$sed_rewrite_new_files" \
1675       | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
1676       | LC_ALL=C sort -u
1677   } > "$tmp"/dirs
1678   { # Rearrange file descriptors. Needed because "while ... done < ..."
1679     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1680     exec 5<&0 < "$tmp"/dirs
1681     while read d; do
1682       if test ! -d "$destdir/$d"; then
1683         if $doit; then
1684           echo "Creating directory $destdir/$d"
1685           mkdir -p "$destdir/$d" || func_fatal_error "failed"
1686         else
1687           echo "Create directory $destdir/$d"
1688         fi
1689       fi
1690     done
1691     exec 0<&5 5<&-
1692   }
1693
1694   # func_dest_tmpfilename file
1695   # determines the name of a temporary file (file is relative to destdir).
1696   # Sets variable:
1697   #   - tmpfile       absolute filename of the temporary file
1698   func_dest_tmpfilename ()
1699   {
1700     if $doit; then
1701       # Put the new contents of $file in a file in the same directory (needed
1702       # to guarantee that an 'mv' to "$destdir/$file" works).
1703       tmpfile="$destdir/$1.tmp"
1704     else
1705       # Put the new contents of $file in a file in a temporary directory
1706       # (because the directory of "$file" might not exist).
1707       tmpfile="$tmp"/`basename "$1"`.tmp
1708     fi
1709   }
1710
1711   # Copy files or make symbolic links. Remove obsolete files.
1712   delimiter='   '
1713   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1714   # representing the files according to the last gnulib-tool invocation.
1715   for f in $old_files; do echo $f; done \
1716     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_old_files" \
1717     | LC_ALL=C sort \
1718     > "$tmp"/old-files
1719   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1720   # representing the files after this gnulib-tool invocation.
1721   for f in $new_files; do echo $f; done \
1722     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" \
1723     | LC_ALL=C sort \
1724     > "$tmp"/new-files
1725   # First the files that are in old-files, but not in new-files:
1726   sed_take_first_column='s,'"$delimiter"'.*,,'
1727   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1728     # Remove the file. Do nothing if the user already removed it.
1729     if test -f "$destdir/$g"; then
1730       if $doit; then
1731         echo "Removing file $g (backup in ${g}~)"
1732         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1733       else
1734         echo "Remove file $g (backup in ${g}~)"
1735       fi
1736     fi
1737   done
1738   # func_add_or_update handles a file that ought to be present afterwards.
1739   # Uses parameters
1740   # - f             the original file name
1741   # - g             the rewritten file name
1742   # - already_present  nonempty if the file already exists, empty otherwise
1743   func_add_or_update ()
1744   {
1745     func_dest_tmpfilename "$g"
1746     func_lookup_file "$f"
1747     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
1748     if test -n "$sed_transform_lib_file"; then
1749       case "$f" in
1750         lib/*)
1751           sed -e "$sed_transform_lib_file" \
1752             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
1753           ;;
1754       esac
1755     fi
1756     if test -f "$destdir/$g"; then
1757       # The file already exists.
1758       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1759         : # The file has not changed.
1760       else
1761         # Replace the file.
1762         if $doit; then
1763           if test -n "$already_present"; then
1764             echo "Updating file $g (backup in ${g}~)"
1765           else
1766             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1767           fi
1768           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1769           if test -n "$symbolic" && test -z "$lookedup_tmp" \
1770              && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1771             func_ln_if_changed "$lookedup_file" "$destdir/$g"
1772           else
1773             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1774           fi
1775         else
1776           if test -n "$already_present"; then
1777             echo "Update file $g (backup in ${g}~)"
1778           else
1779             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1780           fi
1781         fi
1782       fi
1783     else
1784       # Install the file.
1785       # Don't protest if the file should be there but isn't: it happens
1786       # frequently that developers don't put autogenerated files into CVS.
1787       if $doit; then
1788         echo "Copying file $g"
1789         if test -n "$symbolic" && test -z "$lookedup_tmp" \
1790            && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1791           func_ln_if_changed "$lookedup_file" "$destdir/$g"
1792         else
1793           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1794         fi
1795       else
1796         echo "Copy file $g"
1797       fi
1798     fi
1799     rm -f "$tmpfile"
1800   }
1801   # Then the files that are in new-files, but not in old-files:
1802   sed_take_last_column='s,^.*'"$delimiter"',,'
1803   already_present=
1804   LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files \
1805     | sed -e "$sed_take_last_column" \
1806     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/added-files
1807   { # Rearrange file descriptors. Needed because "while ... done < ..."
1808     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1809     exec 5<&0 < "$tmp"/added-files
1810     while read g f; do
1811       func_add_or_update
1812     done
1813     exec 0<&5 5<&-
1814   }
1815   # Then the files that are in new-files and in old-files:
1816   already_present=true
1817   LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files \
1818     | sed -e "$sed_take_last_column" \
1819     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/kept-files
1820   { # Rearrange file descriptors. Needed because "while ... done < ..."
1821     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1822     exec 5<&0 < "$tmp"/kept-files
1823     while read g f; do
1824       func_add_or_update
1825     done
1826     exec 0<&5 5<&-
1827   }
1828
1829   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1830   actioncmd="gnulib-tool --import"
1831   func_append actioncmd " --dir=$destdir"
1832   if test -n "$local_gnulib_dir"; then
1833     func_append actioncmd " --local-dir=$local_gnulib_dir"
1834   fi
1835   func_append actioncmd " --lib=$libname"
1836   func_append actioncmd " --source-base=$sourcebase"
1837   func_append actioncmd " --m4-base=$m4base"
1838   func_append actioncmd " --doc-base=$docbase"
1839   func_append actioncmd " --aux-dir=$auxdir"
1840   for module in $avoidlist; do
1841     func_append actioncmd " --avoid=$module"
1842   done
1843   if test -n "$lgpl"; then
1844     func_append actioncmd " --lgpl"
1845   fi
1846   if test -n "$makefile_name"; then
1847     func_append actioncmd " --makefile-name=$makefile_name"
1848   fi
1849   if test "$libtool" = true; then
1850     func_append actioncmd " --libtool"
1851   else
1852     func_append actioncmd " --no-libtool"
1853   fi
1854   func_append actioncmd " --macro-prefix=$macro_prefix"
1855   func_append actioncmd " `echo $specified_modules`"
1856
1857   # Default the makefile name to Makefile.am.
1858   if test -n "$makefile_name"; then
1859     makefile_am=$makefile_name
1860   else
1861     makefile_am=Makefile.am
1862   fi
1863
1864   # Create library makefile.
1865   func_dest_tmpfilename $sourcebase/$makefile_am
1866   func_emit_lib_Makefile_am > "$tmpfile"
1867   if test -f "$destdir"/$sourcebase/$makefile_am; then
1868     if cmp "$destdir"/$sourcebase/$makefile_am "$tmpfile" > /dev/null; then
1869       rm -f "$tmpfile"
1870     else
1871       if $doit; then
1872         echo "Updating $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
1873         mv -f "$destdir"/$sourcebase/$makefile_am "$destdir"/$sourcebase/$makefile_am~
1874         mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
1875       else
1876         echo "Update $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
1877         rm -f "$tmpfile"
1878       fi
1879     fi
1880   else
1881     if $doit; then
1882       echo "Creating $sourcebase/$makefile_am"
1883       mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
1884     else
1885       echo "Create $sourcebase/$makefile_am"
1886       rm -f "$tmpfile"
1887     fi
1888   fi
1889
1890   # Create m4/gnulib-cache.m4.
1891   func_dest_tmpfilename $m4base/gnulib-cache.m4
1892   (
1893     func_emit_copyright_notice
1894     echo "#"
1895     echo "# This file represents the specification of how gnulib-tool is used."
1896     echo "# It acts as a cache: It is written and read by gnulib-tool."
1897     echo "# In projects using CVS, this file is meant to be stored in CVS,"
1898     echo "# like the configure.ac and various Makefile.am files."
1899     echo
1900     echo
1901     echo "# Specification in the form of a command-line invocation:"
1902     echo "#   $actioncmd"
1903     echo
1904     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
1905     # Store the local_gnulib_dir relative to destdir.
1906     case "$local_gnulib_dir" in
1907       "" | /*)
1908         relative_local_gnulib_dir="$local_gnulib_dir" ;;
1909       * )
1910         case "$destdir" in
1911           /*) relative_local_gnulib_dir="$local_gnulib_dir" ;;
1912           *)
1913             # destdir, local_gnulib_dir are both relative.
1914             func_relativize "$destdir" "$local_gnulib_dir"
1915             relative_local_gnulib_dir="$reldir" ;;
1916         esac ;;
1917     esac
1918     echo "gl_LOCAL_DIR([$relative_local_gnulib_dir])"
1919     echo "gl_MODULES(["`echo $specified_modules`"])"
1920     echo "gl_AVOID([$avoidlist])"
1921     echo "gl_SOURCE_BASE([$sourcebase])"
1922     echo "gl_M4_BASE([$m4base])"
1923     echo "gl_DOC_BASE([$docbase])"
1924     echo "gl_TESTS_BASE([$testsbase])"
1925     echo "gl_LIB([$libname])"
1926     test -z "$lgpl" || echo "gl_LGPL"
1927     echo "gl_MAKEFILE_NAME([$makefile_name])"
1928     if test "$libtool" = true; then
1929       echo "gl_LIBTOOL"
1930     fi
1931     echo "gl_MACRO_PREFIX([$macro_prefix])"
1932   ) > "$tmpfile"
1933   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1934     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
1935       rm -f "$tmpfile"
1936     else
1937       if $doit; then
1938         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1939         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
1940         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1941       else
1942         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1943         if false; then
1944           cat "$tmpfile"
1945           echo
1946           echo "# gnulib-cache.m4 ends here"
1947         fi
1948         rm -f "$tmpfile"
1949       fi
1950     fi
1951   else
1952     if $doit; then
1953       echo "Creating $m4base/gnulib-cache.m4"
1954       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1955     else
1956       echo "Create $m4base/gnulib-cache.m4"
1957       cat "$tmpfile"
1958       rm -f "$tmpfile"
1959     fi
1960   fi
1961
1962   # Create m4/gnulib-comp.m4.
1963   func_dest_tmpfilename $m4base/gnulib-comp.m4
1964   (
1965     echo "# DO NOT EDIT! GENERATED AUTOMATICALLY!"
1966     func_emit_copyright_notice
1967     echo "#"
1968     echo "# This file represents the compiled summary of the specification in"
1969     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
1970     echo "# to be invoked from configure.ac."
1971     echo "# In projects using CVS, this file can be treated like other built files."
1972     echo
1973     echo
1974     echo "# This macro should be invoked from $configure_ac, in the section"
1975     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
1976     echo "# any checks for libraries, header files, types and library functions."
1977     echo "AC_DEFUN([${macro_prefix}_EARLY],"
1978     echo "["
1979     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
1980     echo "  m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
1981     echo "  m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
1982     echo "  m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
1983     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
1984     if test -n "$uses_subdirs"; then
1985       echo "  AC_REQUIRE([AM_PROG_CC_C_O])"
1986     fi
1987     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1988       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
1989     fi
1990     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1991       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
1992     fi
1993     if grep gl_LOCK "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1994       echo "  AC_REQUIRE([gl_LOCK_EARLY])"
1995     fi
1996     echo "])"
1997     echo
1998     echo "# This macro should be invoked from $configure_ac, in the section"
1999     echo "# \"Check for header files, types and library functions\"."
2000     echo "AC_DEFUN([${macro_prefix}_INIT],"
2001     echo "["
2002     func_emit_initmacro_start
2003     if test "$libtool" = true; then
2004       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2005       echo "  gl_cond_libtool=true"
2006     else
2007       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2008       echo "  gl_cond_libtool=false"
2009       echo "  gl_libdeps="
2010       echo "  gl_ltlibdeps="
2011     fi
2012     echo "  gl_source_base='$sourcebase'"
2013     if test "$auxdir" != "build-aux"; then
2014       sed_replace_build_aux='
2015         :a
2016         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2017           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2018           ba
2019         }'
2020       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2021     else
2022       sed_replace_build_aux=
2023     fi
2024     for module in $modules; do
2025       func_verify_module
2026       if test -n "$module"; then
2027         func_get_autoconf_snippet "$module" \
2028           | sed -e '/^$/d;' -e 's/^/  /' \
2029                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
2030                 -e "$sed_replace_build_aux"
2031         if test "$module" = 'alloca' && test "$libtool" = true; then
2032           echo 'changequote(,)dnl'
2033           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
2034           echo 'changequote([, ])dnl'
2035           echo 'AC_SUBST([LTALLOCA])'
2036         fi
2037       fi
2038     done
2039     # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2040     # created using libtool, because libtool already handles the dependencies.
2041     if test "$libtool" != true; then
2042       libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2043       echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2044       echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2045       echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2046       echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2047     fi
2048     func_emit_initmacro_end
2049     echo "])"
2050     func_emit_initmacro_done
2051     echo
2052     echo "# This macro records the list of files which have been installed by"
2053     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
2054     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
2055     echo "$files" | sed -e 's,^,  ,'
2056     echo "])"
2057   ) > "$tmpfile"
2058   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
2059     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
2060       rm -f "$tmpfile"
2061     else
2062       if $doit; then
2063         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
2064         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
2065         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
2066       else
2067         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
2068         if false; then
2069           cat "$tmpfile"
2070           echo
2071           echo "# gnulib-comp.m4 ends here"
2072         fi
2073         rm -f "$tmpfile"
2074       fi
2075     fi
2076   else
2077     if $doit; then
2078       echo "Creating $m4base/gnulib-comp.m4"
2079       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
2080     else
2081       echo "Create $m4base/gnulib-comp.m4"
2082       cat "$tmpfile"
2083       rm -f "$tmpfile"
2084     fi
2085   fi
2086
2087   if test -n "$inctests"; then
2088     # Create tests makefile.
2089     func_dest_tmpfilename $testsbase/$makefile_am
2090     func_emit_tests_Makefile_am > "$tmpfile"
2091     if test -f "$destdir"/$testsbase/$makefile_am; then
2092       if cmp "$destdir"/$testsbase/$makefile_am "$tmpfile" > /dev/null; then
2093         rm -f "$tmpfile"
2094       else
2095         if $doit; then
2096           echo "Updating $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
2097           mv -f "$destdir"/$testsbase/$makefile_am "$destdir"/$testsbase/$makefile_am~
2098           mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
2099         else
2100           echo "Update $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
2101           rm -f "$tmpfile"
2102         fi
2103       fi
2104     else
2105       if $doit; then
2106         echo "Creating $testsbase/$makefile_am"
2107         mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
2108       else
2109         echo "Create $testsbase/$makefile_am"
2110         rm -f "$tmpfile"
2111       fi
2112     fi
2113   fi
2114
2115   echo "Finished."
2116   echo
2117   echo "You may need to add #include directives for the following .h files."
2118   # First the #include <...> directives without #ifs, sorted for convenience,
2119   # then the #include "..." directives without #ifs, sorted for convenience,
2120   # then the #include directives that are surrounded by #ifs. Not sorted.
2121   for module in $modules; do
2122     include_directive=`func_get_include_directive "$module"`
2123     case "$nl$include_directive" in
2124       *"$nl#if"*)
2125         echo "$include_directive" 1>&5
2126         ;;
2127       *)
2128         echo "$include_directive" | grep -v 'include "' 1>&6
2129         echo "$include_directive" | grep 'include "' 1>&7
2130         ;;
2131     esac
2132   done 5> "$tmp"/include-if 6> "$tmp"/include-angles 7> "$tmp"/include-quotes
2133   (
2134    LC_ALL=C sort -u "$tmp"/include-angles
2135    LC_ALL=C sort -u "$tmp"/include-quotes
2136    cat "$tmp"/include-if
2137   ) | sed -e '/^$/d' -e 's/^/  /'
2138   rm -f "$tmp"/include-angles "$tmp"/include-quotes "$tmp"/include-if
2139
2140   echo
2141   echo "Don't forget to"
2142   if test "$makefile_am" = Makefile.am; then
2143     echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
2144   else
2145     echo "  - \"include $makefile_name\" from within \"$sourcebase/Makefile.am\","
2146   fi
2147   if test -n "$inctests"; then
2148     if test "$makefile_am" = Makefile.am; then
2149       echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
2150     else
2151       echo "  - \"include $makefile_name\" from within \"$testsbase/Makefile.am\","
2152     fi
2153   fi
2154   if test "$makefile_am" = Makefile.am; then
2155     sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
2156     sourcebase_base=`basename "$sourcebase"`
2157     echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
2158   fi
2159   if test -n "$inctests"; then
2160     if test "$makefile_am" = Makefile.am; then
2161       testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
2162       testsbase_base=`basename "$testsbase"`
2163       echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
2164     fi
2165   fi
2166   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
2167   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
2168   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
2169 }
2170
2171 # func_create_testdir testdir modules
2172 # Input:
2173 # - local_gnulib_dir  from --local-dir
2174 # - auxdir          directory relative to destdir where to place build aux files
2175 func_create_testdir ()
2176 {
2177   testdir="$1"
2178   modules="$2"
2179   if test -z "$modules"; then
2180     # All modules together.
2181     # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
2182     # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2183     # Except ftruncate, mountlist, which abort the configuration on mingw. FIXME.
2184     modules=`func_all_modules`
2185     modules=`for m in $modules; do case $m in config-h | fnmatch-posix | ftruncate | mountlist) ;; *) echo $m;; esac; done`
2186   fi
2187   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort -u`
2188
2189   # Subdirectory names.
2190   sourcebase=gllib
2191   m4base=glm4
2192   docbase=gldoc
2193   testsbase=gltests
2194   macro_prefix=gl
2195
2196   # Determine final module list.
2197   func_modules_transitive_closure
2198   echo "Module list with included dependencies:"
2199   echo "$modules" | sed -e 's/^/  /'
2200
2201   # Add the dummy module if needed.
2202   func_modules_add_dummy
2203
2204   # Determine final file list.
2205   func_modules_to_filelist
2206   echo "File list:"
2207   echo "$files" | sed -e 's/^/  /'
2208
2209   sed_rewrite_files="\
2210     s,^build-aux/,$auxdir/,
2211     s,^doc/,$docbase/,
2212     s,^lib/,$sourcebase/,
2213     s,^m4/,$m4base/,
2214     s,^tests/,$testsbase/,"
2215
2216   # Create directories.
2217   for f in $files; do echo $f; done \
2218     | sed -e "$sed_rewrite_files" \
2219     | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
2220     | LC_ALL=C sort -u \
2221     > "$tmp"/dirs
2222   { # Rearrange file descriptors. Needed because "while ... done < ..."
2223     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2224     exec 5<&0 < "$tmp"/dirs
2225     while read d; do
2226       mkdir -p "$testdir/$d"
2227     done
2228     exec 0<&5 5<&-
2229   }
2230
2231   # Copy files or make symbolic links.
2232   delimiter='   '
2233   for f in $files; do echo $f; done \
2234     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_files" \
2235     | LC_ALL=C sort \
2236     > "$tmp"/files
2237   { # Rearrange file descriptors. Needed because "while ... done < ..."
2238     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2239     exec 5<&0 < "$tmp"/files
2240     while read g f; do
2241       func_lookup_file "$f"
2242       if test -n "$lookedup_tmp"; then
2243         cp -p "$lookedup_file" "$testdir/$g"
2244       else
2245         ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
2246         if test -z "$symbolic"; then
2247           cp -p "$lookedup_file" "$testdir/$g"
2248         else
2249           ln -s "$lookedup_file" "$testdir/$g"
2250         fi
2251       fi
2252     done
2253     exec 0<&5 5<&-
2254   }
2255
2256   # Create $sourcebase/Makefile.am.
2257   mkdir -p "$testdir/$sourcebase"
2258   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
2259
2260   # Create $m4base/Makefile.am.
2261   mkdir -p "$testdir/$m4base"
2262   (echo "## Process this file with automake to produce Makefile.in."
2263    echo
2264    echo "EXTRA_DIST ="
2265    for f in $files; do
2266      case "$f" in
2267        m4/* )
2268          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
2269      esac
2270    done
2271   ) > "$testdir/$m4base/Makefile.am"
2272
2273   subdirs="$sourcebase $m4base"
2274   subdirs_with_configure_ac=""
2275
2276   if false && test -f "$testdir"/$m4base/gettext.m4; then
2277     # Avoid stupid error message from automake:
2278     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
2279     mkdir -p "$testdir/po"
2280     (echo "## Process this file with automake to produce Makefile.in."
2281     ) > "$testdir/po/Makefile.am"
2282     func_append subdirs " po"
2283   fi
2284
2285   if test -n "$inctests"; then
2286     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
2287     # Create $testsbase/Makefile.am.
2288     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
2289     # Create $testsbase/configure.ac.
2290     (echo "# Process this file with autoconf to produce a configure script."
2291      echo "AC_INIT([dummy], [0])"
2292      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
2293      echo "AM_INIT_AUTOMAKE"
2294      echo
2295      echo "AM_CONFIG_HEADER([config.h])"
2296      echo
2297      echo "AC_PROG_CC"
2298      echo "AC_PROG_INSTALL"
2299      echo "AC_PROG_MAKE_SET"
2300      echo "AC_PROG_RANLIB"
2301      echo
2302      if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2303        echo "AC_GNU_SOURCE"
2304        echo
2305      fi
2306      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2307        echo "gl_USE_SYSTEM_EXTENSIONS"
2308        echo
2309      fi
2310      if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2311        echo "gl_LOCK_EARLY"
2312        echo
2313      fi
2314      if test "$libtool" = true; then
2315        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2316        echo "gl_cond_libtool=true"
2317      else
2318        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2319        echo "gl_cond_libtool=false"
2320        echo "gl_libdeps="
2321        echo "gl_ltlibdeps="
2322      fi
2323      echo "gl_source_base='$testsbase'"
2324      # Wrap the set of autoconf snippets into an autoconf macro that is then
2325      # invoked. This is needed because autoconf does not support AC_REQUIRE
2326      # at the top level:
2327      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2328      # but we want the AC_REQUIRE to have its normal meaning (provide one
2329      # expansion of the required macro before the current point, and only one
2330      # expansion total).
2331      echo "AC_DEFUN([gl_INIT], ["
2332      func_emit_initmacro_start
2333      sed_replace_build_aux='
2334        :a
2335        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2336          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
2337          ba
2338        }'
2339      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2340      # We don't have explicit ordering constraints between the various
2341      # autoconf snippets. It's cleanest to put those of the library before
2342      # those of the tests.
2343      for module in $modules; do
2344        func_verify_nontests_module
2345        if test -n "$module"; then
2346          func_get_autoconf_snippet "$module" \
2347            | sed -e "$sed_replace_build_aux"
2348        fi
2349      done
2350      for module in $modules; do
2351        func_verify_tests_module
2352        if test -n "$module"; then
2353          func_get_autoconf_snippet "$module" \
2354            | sed -e "$sed_replace_build_aux"
2355        fi
2356      done
2357      # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2358      # created using libtool, because libtool already handles the dependencies.
2359      if test "$libtool" != true; then
2360        libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2361        echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2362        echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2363        echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2364        echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2365      fi
2366      func_emit_initmacro_end
2367      echo "])"
2368      func_emit_initmacro_done
2369      echo
2370      echo "gl_INIT"
2371      echo
2372      # Usually $testsbase/config.h will be a superset of config.h. Verify this
2373      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
2374      echo "AH_TOP([#include \"../config.h\"])"
2375      echo
2376      echo "AC_OUTPUT([Makefile])"
2377     ) > "$testdir/$testsbase/configure.ac"
2378     func_append subdirs " $testsbase"
2379     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
2380   fi
2381
2382   # Create Makefile.am.
2383   (echo "## Process this file with automake to produce Makefile.in."
2384    echo
2385    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2386    echo
2387    echo "SUBDIRS = $subdirs"
2388    echo
2389    echo "ACLOCAL_AMFLAGS = -I $m4base"
2390   ) > "$testdir/Makefile.am"
2391
2392   # Create configure.ac.
2393   (echo "# Process this file with autoconf to produce a configure script."
2394    echo "AC_INIT([dummy], [0])"
2395    if test "$auxdir" != "."; then
2396      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2397    fi
2398    echo "AM_INIT_AUTOMAKE"
2399    echo
2400    echo "AM_CONFIG_HEADER([config.h])"
2401    echo
2402    echo "AC_PROG_CC"
2403    echo "AC_PROG_INSTALL"
2404    echo "AC_PROG_MAKE_SET"
2405    echo
2406    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
2407    echo "m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
2408    echo "m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
2409    echo "m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
2410    echo
2411    echo "AC_PROG_RANLIB"
2412    echo
2413    if test -n "$uses_subdirs"; then
2414      echo "AM_PROG_CC_C_O"
2415      echo
2416    fi
2417    if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2418      echo "AC_GNU_SOURCE"
2419      echo
2420    fi
2421    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2422      echo "gl_USE_SYSTEM_EXTENSIONS"
2423      echo
2424    fi
2425    if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2426      echo "gl_LOCK_EARLY"
2427      echo
2428    fi
2429    if test "$libtool" = true; then
2430      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2431      echo "gl_cond_libtool=true"
2432    else
2433      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2434      echo "gl_cond_libtool=false"
2435      echo "gl_libdeps="
2436      echo "gl_ltlibdeps="
2437    fi
2438    echo "gl_source_base='$sourcebase'"
2439    # Wrap the set of autoconf snippets into an autoconf macro that is then
2440    # invoked. This is needed because autoconf does not support AC_REQUIRE
2441    # at the top level:
2442    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2443    # but we want the AC_REQUIRE to have its normal meaning (provide one
2444    # expansion of the required macro before the current point, and only one
2445    # expansion total).
2446    echo "AC_DEFUN([gl_INIT], ["
2447    func_emit_initmacro_start
2448    if test "$auxdir" != "build-aux"; then
2449      sed_replace_build_aux='
2450        :a
2451        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2452          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2453          ba
2454        }'
2455      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2456    else
2457      sed_replace_build_aux=
2458    fi
2459    for module in $modules; do
2460      func_verify_nontests_module
2461      if test -n "$module"; then
2462        func_get_autoconf_snippet "$module" \
2463          | sed -e "$sed_replace_build_aux"
2464      fi
2465    done
2466    # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2467    # created using libtool, because libtool already handles the dependencies.
2468    if test "$libtool" != true; then
2469      libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2470      echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2471      echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2472      echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2473      echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2474    fi
2475    func_emit_initmacro_end
2476    echo "])"
2477    func_emit_initmacro_done
2478    echo
2479    echo "gl_INIT"
2480    echo
2481    if test -n "$subdirs_with_configure_ac"; then
2482      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
2483    fi
2484    makefiles="Makefile"
2485    for d in $subdirs; do
2486      # For subdirs that have a configure.ac by their own, it's the subdir's
2487      # configure.ac which creates the subdir's Makefile.am, not this one.
2488      case " $subdirs_with_configure_ac " in
2489        *" $d "*) ;;
2490        *) func_append makefiles " $d/Makefile" ;;
2491      esac
2492    done
2493    echo "AC_OUTPUT([$makefiles])"
2494   ) > "$testdir/configure.ac"
2495
2496   # Create autogenerated files.
2497   (cd "$testdir"
2498    # Do not use "${AUTORECONF} --force --install", because it may invoke
2499    # autopoint, which brings in older versions of some of our .m4 files.
2500    if test -f $m4base/gettext.m4; then
2501      echo "executing ${AUTOPOINT} --force"
2502      ${AUTOPOINT} --force || func_exit 1
2503      for f in $m4base/*.m4~; do
2504        mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
2505      done
2506    fi
2507    echo "executing ${ACLOCAL} -I $m4base"
2508    ${ACLOCAL} -I $m4base || func_exit 1
2509    if ! test -d build-aux; then
2510      echo "executing mkdir build-aux"
2511      mkdir build-aux || func_exit 1
2512    fi
2513    echo "executing ${AUTOCONF}"
2514    ${AUTOCONF} || func_exit 1
2515    echo "executing ${AUTOHEADER}"
2516    ${AUTOHEADER} || func_exit 1
2517    echo "executing ${AUTOMAKE} --add-missing --copy"
2518    ${AUTOMAKE} --add-missing --copy || func_exit 1
2519   ) || func_exit 1
2520   if test -n "$inctests"; then
2521     # Create autogenerated files.
2522     (cd "$testdir/$testsbase" || func_exit 1
2523      # Do not use "${AUTORECONF} --force --install", because it may invoke
2524      # autopoint, which brings in older versions of some of our .m4 files.
2525      if test -f ../$m4base/gettext.m4; then
2526        echo "executing ${AUTOPOINT} --force"
2527        ${AUTOPOINT} --force || func_exit 1
2528        for f in ../$m4base/*.m4~; do
2529          mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
2530        done
2531      fi
2532      echo "executing ${ACLOCAL} -I ../$m4base"
2533      ${ACLOCAL} -I ../$m4base || func_exit 1
2534      if ! test -d ../build-aux; then
2535        echo "executing mkdir ../build-aux"
2536        mkdir ../build-aux
2537      fi
2538      echo "executing ${AUTOCONF}"
2539      ${AUTOCONF} || func_exit 1
2540      echo "executing ${AUTOHEADER}"
2541      ${AUTOHEADER} || func_exit 1
2542      echo "executing ${AUTOMAKE} --add-missing --copy"
2543      ${AUTOMAKE} --add-missing --copy || func_exit 1
2544     ) || func_exit 1
2545   fi
2546   if grep '^BUILT_SOURCES *+=' "$testdir/$sourcebase/Makefile.am" > /dev/null; then
2547     (cd "$testdir"
2548      ./configure || func_exit 1
2549        cd "$sourcebase"
2550        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
2551        make built_sources || func_exit 1
2552        cd ..
2553      make distclean || func_exit 1
2554     ) || func_exit 1
2555   fi
2556 }
2557
2558 # func_create_megatestdir megatestdir allmodules
2559 # Input:
2560 # - local_gnulib_dir  from --local-dir
2561 # - auxdir          directory relative to destdir where to place build aux files
2562 func_create_megatestdir ()
2563 {
2564   megatestdir="$1"
2565   allmodules="$2"
2566   if test -z "$allmodules"; then
2567     allmodules=`func_all_modules`
2568   fi
2569
2570   megasubdirs=
2571   # First, all modules one by one.
2572   for onemodule in $allmodules; do
2573     func_create_testdir "$megatestdir/$onemodule" $onemodule
2574     func_append megasubdirs "$onemodule "
2575   done
2576   # Then, all modules all together.
2577   # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
2578   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2579   allmodules=`for m in $allmodules; do if test $m != config-h && test $m != fnmatch-posix; then echo $m; fi; done`
2580   func_create_testdir "$megatestdir/ALL" "$allmodules"
2581   func_append megasubdirs "ALL"
2582
2583   # Create Makefile.am.
2584   (echo "## Process this file with automake to produce Makefile.in."
2585    echo
2586    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2587    echo
2588    echo "SUBDIRS = $megasubdirs"
2589   ) > "$megatestdir/Makefile.am"
2590
2591   # Create configure.ac.
2592   (echo "# Process this file with autoconf to produce a configure script."
2593    echo "AC_INIT([dummy], [0])"
2594    if test "$auxdir" != "."; then
2595      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2596    fi
2597    echo "AM_INIT_AUTOMAKE"
2598    echo
2599    echo "AC_PROG_MAKE_SET"
2600    echo
2601    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
2602    echo "AC_OUTPUT([Makefile])"
2603   ) > "$megatestdir/configure.ac"
2604
2605   # Create autogenerated files.
2606   (cd "$megatestdir"
2607    # Do not use "${AUTORECONF} --install", because autoreconf operates
2608    # recursively, but the subdirectories are already finished, therefore
2609    # calling autoreconf here would only waste lots of CPU time.
2610    echo "executing ${ACLOCAL}"
2611    ${ACLOCAL} || func_exit 1
2612    echo "executing mkdir build-aux"
2613    mkdir build-aux
2614    echo "executing ${AUTOCONF}"
2615    ${AUTOCONF} || func_exit 1
2616    echo "executing ${AUTOMAKE} --add-missing --copy"
2617    ${AUTOMAKE} --add-missing --copy || func_exit 1
2618   ) || func_exit 1
2619 }
2620
2621 case $mode in
2622   "" )
2623     func_fatal_error "no mode specified" ;;
2624
2625   list )
2626     func_all_modules
2627     ;;
2628
2629   import | update )
2630
2631     # Where to import.
2632     if test -z "$destdir"; then
2633       destdir=.
2634     fi
2635     test -d "$destdir" \
2636       || func_fatal_error "destination directory does not exist: $destdir"
2637
2638     # Prefer configure.ac to configure.in.
2639     if test -f "$destdir"/configure.ac; then
2640       configure_ac="$destdir/configure.ac"
2641     else
2642       if test -f "$destdir"/configure.in; then
2643         configure_ac="$destdir/configure.in"
2644       else
2645         func_fatal_error "cannot find $destdir/configure.ac"
2646       fi
2647     fi
2648
2649     # Analyze configure.ac.
2650     guessed_auxdir="."
2651     guessed_libtool=false
2652     my_sed_traces='
2653       s,#.*$,,
2654       s,^dnl .*$,,
2655       s, dnl .*$,,
2656       /AC_CONFIG_AUX_DIR/ {
2657         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
2658       }
2659       /A[CM]_PROG_LIBTOOL/ {
2660         s,^.*$,guessed_libtool=true,p
2661       }'
2662     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
2663
2664     if test -z "$auxdir"; then
2665       auxdir="$guessed_auxdir"
2666     fi
2667
2668     # Determine where to apply func_import.
2669     if test -n "$m4base"; then
2670       # Apply func_import to a particular gnulib directory.
2671       # Any number of additional modules can be given.
2672       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
2673         # First use of gnulib in the given m4base.
2674         test -n "$supplied_libname" || supplied_libname=true
2675         test -n "$sourcebase" || sourcebase="lib"
2676         test -n "$docbase" || docbase="doc"
2677         test -n "$testsbase" || testsbase="tests"
2678         test -n "$macro_prefix" || macro_prefix="gl"
2679       fi
2680       func_import "$*"
2681     else
2682       # Apply func_import to all gnulib directories.
2683       # To get this list of directories, look at Makefile.am. (Not at
2684       # configure, because it may be omitted from CVS. Also, don't run
2685       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
2686       m4dirs=
2687       m4dirs_count=0
2688       if test -f "$destdir"/Makefile.am; then
2689         aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[         ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
2690         m4dir_is_next=
2691         for arg in $aclocal_amflags; do
2692           if test -n "$m4dir_is_next"; then
2693             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
2694             case "$arg" in
2695               /*) ;;
2696               *)
2697                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
2698                   func_append m4dirs " $arg"
2699                   m4dirs_count=`expr $m4dirs_count + 1`
2700                 fi
2701                 ;;
2702             esac
2703           else
2704             if test "X$arg" = "X-I"; then
2705               m4dir_is_next=yes
2706             else
2707               m4dir_is_next=
2708             fi
2709           fi
2710         done
2711       else
2712         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
2713         if test -f "$destdir"/aclocal.m4; then
2714           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
2715           sedexpr2='s,^[^/]*$,.,'
2716           sedexpr3='s,/[^/]*$,,'
2717           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort -u`
2718           m4dirs_count=`echo "$m4dirs" | wc -l`
2719         fi
2720       fi
2721       if test $m4dirs_count = 0; then
2722         # First use of gnulib in a package.
2723         # Any number of additional modules can be given.
2724         test -n "$supplied_libname" || supplied_libname=true
2725         test -n "$sourcebase" || sourcebase="lib"
2726         m4base="m4"
2727         test -n "$docbase" || docbase="doc"
2728         test -n "$testsbase" || testsbase="tests"
2729         test -n "$macro_prefix" || macro_prefix="gl"
2730         func_import "$*"
2731       else
2732         if test $m4dirs_count = 1; then
2733           # There's only one use of gnulib here. Assume the user means it.
2734           # Any number of additional modules can be given.
2735           for m4base in $m4dirs; do
2736             func_import "$*"
2737           done
2738         else
2739           # Ambiguous - guess what the user meant.
2740           if test $# = 0; then
2741             # No further arguments. Guess the user wants to update all of them.
2742             for m4base in $m4dirs; do
2743               # Perform func_import in a subshell, so that variable values
2744               # such as
2745               #   local_gnulib_dir, avoidlist, sourcebase, m4base, docbase,
2746               #   testsbase, libname, lgpl, makefile_name, libtool, macro_prefix
2747               # don't propagate from one directory to another.
2748               (func_import) || func_exit 1
2749             done
2750           else
2751             # Really ambiguous.
2752             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
2753           fi
2754         fi
2755       fi
2756     fi
2757     ;;
2758
2759   create-testdir )
2760     if test -z "$destdir"; then
2761       func_fatal_error "please specify --dir option"
2762     fi
2763     mkdir "$destdir"
2764     test -d "$destdir" \
2765       || func_fatal_error "could not create destination directory"
2766     test -n "$auxdir" || auxdir="build-aux"
2767     func_create_testdir "$destdir" "$*"
2768     ;;
2769
2770   create-megatestdir )
2771     if test -z "$destdir"; then
2772       func_fatal_error "please specify --dir option"
2773     fi
2774     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2775     test -n "$auxdir" || auxdir="build-aux"
2776     func_create_megatestdir "$destdir" "$*"
2777     ;;
2778
2779   test )
2780     test -n "$destdir" || destdir=testdir$$
2781     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2782     test -n "$auxdir" || auxdir="build-aux"
2783     func_create_testdir "$destdir" "$*"
2784     cd "$destdir"
2785       mkdir build
2786       cd build
2787         ../configure || func_exit 1
2788         make || func_exit 1
2789         make check || func_exit 1
2790         make distclean || func_exit 1
2791         remaining=`find . -type f -print`
2792         if test -n "$remaining"; then
2793           echo "Remaining files:" $remaining 1>&2
2794           echo "gnulib-tool: *** Stop." 1>&2
2795           func_exit 1
2796         fi
2797       cd ..
2798     cd ..
2799     rm -rf "$destdir"
2800     ;;
2801
2802   megatest )
2803     test -n "$destdir" || destdir=testdir$$
2804     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2805     test -n "$auxdir" || auxdir="build-aux"
2806     func_create_megatestdir "$destdir" "$*"
2807     cd "$destdir"
2808       mkdir build
2809       cd build
2810         ../configure
2811         make
2812         make check
2813         make distclean
2814         remaining=`find . -type f -print`
2815         if test -n "$remaining"; then
2816           echo "Remaining files:" $remaining 1>&2
2817           echo "gnulib-tool: *** Stop." 1>&2
2818           func_exit 1
2819         fi
2820       cd ..
2821     cd ..
2822     rm -rf "$destdir"
2823     ;;
2824
2825   extract-description )
2826     for module
2827     do
2828       func_verify_module
2829       if test -n "$module"; then
2830         func_get_description "$module"
2831       fi
2832     done
2833     ;;
2834
2835   extract-filelist )
2836     for module
2837     do
2838       func_verify_module
2839       if test -n "$module"; then
2840         func_get_filelist "$module"
2841       fi
2842     done
2843     ;;
2844
2845   extract-dependencies )
2846     for module
2847     do
2848       func_verify_module
2849       if test -n "$module"; then
2850         func_get_dependencies "$module"
2851       fi
2852     done
2853     ;;
2854
2855   extract-autoconf-snippet )
2856     for module
2857     do
2858       func_verify_module
2859       if test -n "$module"; then
2860         func_get_autoconf_snippet "$module"
2861       fi
2862     done
2863     ;;
2864
2865   extract-automake-snippet )
2866     for module
2867     do
2868       func_verify_module
2869       if test -n "$module"; then
2870         func_get_automake_snippet "$module"
2871       fi
2872     done
2873     ;;
2874
2875   extract-include-directive )
2876     for module
2877     do
2878       func_verify_module
2879       if test -n "$module"; then
2880         func_get_include_directive "$module"
2881       fi
2882     done
2883     ;;
2884
2885   extract-license )
2886     for module
2887     do
2888       func_verify_module
2889       if test -n "$module"; then
2890         func_get_license "$module"
2891       fi
2892     done
2893     ;;
2894
2895   extract-maintainer )
2896     for module
2897     do
2898       func_verify_module
2899       if test -n "$module"; then
2900         func_get_maintainer "$module"
2901       fi
2902     done
2903     ;;
2904
2905   extract-tests-module )
2906     for module
2907     do
2908       func_verify_module
2909       if test -n "$module"; then
2910         func_get_tests_module "$module"
2911       fi
2912     done
2913     ;;
2914
2915   * )
2916     func_fatal_error "unknown operation mode --$mode" ;;
2917 esac
2918
2919 rm -rf "$tmp"
2920 # Undo the effect of the previous 'trap' command. Some shellology:
2921 # We cannot use "trap - 0 1 2 3 13 15", because Solaris sh would attempt to
2922 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
2923 # exit); for the others we need to call 'exit' explicitly. The value of $? is
2924 # 128 + signal number and is set before the trap-registered command is run.
2925 trap '' 0
2926 trap 'func_exit $?' 1 2 3 13 15
2927
2928 exit 0
2929
2930 # Local Variables:
2931 # indent-tabs-mode: nil
2932 # whitespace-check-buffer-indent: nil
2933 # End: