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