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