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