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