gnulib-tool: Make caching optional.
[pspp] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2010 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 3 of the License, or
8 # (at your option) 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, see <http://www.gnu.org/licenses/>.
17 #
18
19 # This program is meant for authors or maintainers which want to import
20 # modules from gnulib into their packages.
21
22 progname=$0
23 package=gnulib
24 nl='
25 '
26 IFS=" ""        $nl"
27
28 # You can set AUTOCONFPATH to empty if autoconf 2.57 is already in your PATH.
29 AUTOCONFPATH=
30 #case $USER in
31 #  bruno )
32 #    AUTOCONFBINDIR=/packages/gnu-inst-autoconf/2.57/bin
33 #    AUTOCONFPATH="eval env PATH=${AUTOCONFBINDIR}:\$PATH "
34 #    ;;
35 #esac
36
37 # You can set AUTOMAKEPATH to empty if automake 1.9.x is already in your PATH.
38 AUTOMAKEPATH=
39
40 # You can set GETTEXTPATH to empty if autopoint 0.15 is already in your PATH.
41 GETTEXTPATH=
42
43 # If you didn't set AUTOCONFPATH and AUTOMAKEPATH, you can also set the
44 # variables AUTOCONF, AUTOHEADER, ACLOCAL, AUTOMAKE, AUTORECONF individually.
45 if test -z "${AUTOCONF}" || test -n "${AUTOCONFPATH}"; then
46   AUTOCONF="${AUTOCONFPATH}autoconf"
47 fi
48 if test -z "${AUTOHEADER}" || test -n "${AUTOCONFPATH}"; then
49   AUTOHEADER="${AUTOCONFPATH}autoheader"
50 fi
51 if test -z "${ACLOCAL}" || test -n "${AUTOMAKEPATH}"; then
52   ACLOCAL="${AUTOMAKEPATH}aclocal"
53 fi
54 if test -z "${AUTOMAKE}" || test -n "${AUTOMAKEPATH}"; then
55   AUTOMAKE="${AUTOMAKEPATH}automake"
56 fi
57 if test -z "${AUTORECONF}" || test -n "${AUTOCONFPATH}"; then
58   AUTORECONF="${AUTOCONFPATH}autoreconf"
59 fi
60
61 # If you didn't set GETTEXTPATH, you can also set the variable AUTOPOINT.
62 if test -z "${AUTOPOINT}" || test -n "${GETTEXTPATH}"; then
63   AUTOPOINT="${GETTEXTPATH}autopoint"
64 fi
65
66 # You can set MAKE.
67 if test -z "${MAKE}"; then
68   MAKE=make
69 fi
70
71 # When using GNU sed, turn off as many GNU extensions as possible,
72 # to minimize the risk of accidentally using non-portable features.
73 # However, do this only for gnulib-tool itself, not for the code that
74 # gnulib-tool generates, since we don't want "sed --posix" to leak
75 # into makefiles. And do it only for sed versions 4.2 or newer,
76 # because "sed --posix" is buggy in GNU sed 4.1.5, see
77 # <http://lists.gnu.org/archive/html/bug-gnulib/2009-02/msg00225.html>.
78 if (alias) > /dev/null 2>&1 \
79    && echo | sed --posix -e d >/dev/null 2>&1 \
80    && case `sed --version | sed -e 's/^[^0-9]*//' -e 1q` in \
81         [1-3]* | 4.[01]*) false;; \
82         *) true;; \
83       esac \
84    ; then
85   # Define sed as an alias.
86   # It is not always possible to use aliases. Aliases are guaranteed to work
87   # if the executing shell is bash and either it is invoked as /bin/sh or
88   # is a version >= 2.0, supporting shopt. This is the common case.
89   # Two other approaches (use of a variable $sed or of a function func_sed
90   # instead of an alias) require massive, fragile code changes.
91   # An other approach (use of function sed) requires `which sed` - but
92   # 'which' is hard to emulate, due to missing "test -x" on some platforms.
93   if test -n "$BASH_VERSION"; then
94     shopt -s expand_aliases >/dev/null 2>&1
95   fi
96   alias sed='sed --posix'
97 fi
98
99 # sed_noop is a sed expression that does nothing.
100 # An empty expression does not work with the native 'sed' on AIX 6.1.
101 sed_noop='s,x,x,'
102
103 # func_usage
104 # outputs to stdout the --help usage message.
105 func_usage ()
106 {
107   echo "\
108 Usage: gnulib-tool --list
109        gnulib-tool --import [module1 ... moduleN]
110        gnulib-tool --update
111        gnulib-tool --create-testdir --dir=directory [module1 ... moduleN]
112        gnulib-tool --create-megatestdir --dir=directory [module1 ... moduleN]
113        gnulib-tool --test --dir=directory module1 ... moduleN
114        gnulib-tool --megatest --dir=directory [module1 ... moduleN]
115        gnulib-tool --extract-description module
116        gnulib-tool --extract-status module
117        gnulib-tool --extract-notice module
118        gnulib-tool --extract-applicability module
119        gnulib-tool --extract-filelist module
120        gnulib-tool --extract-dependencies module
121        gnulib-tool --extract-autoconf-snippet module
122        gnulib-tool --extract-automake-snippet module
123        gnulib-tool --extract-include-directive module
124        gnulib-tool --extract-link-directive module
125        gnulib-tool --extract-license module
126        gnulib-tool --extract-maintainer module
127        gnulib-tool --extract-tests-module module
128        gnulib-tool --copy-file file [destination]
129
130 Operation modes:
131       --list                print the available module names
132       --import              import the given modules into the current package;
133                             if no modules are specified, update the current
134                             package from the current gnulib
135       --update              update the current package, restore files omitted
136                             from CVS
137       --create-testdir      create a scratch package with the given modules
138       --create-megatestdir  create a mega scratch package with the given modules
139                             one by one and all together
140       --test                test the combination of the given modules
141                             (recommended to use CC=\"gcc -Wall\" here)
142       --megatest            test the given modules one by one and all together
143                             (recommended to use CC=\"gcc -Wall\" here)
144       --extract-description        extract the description
145       --extract-status             extract the status (obsolete or not)
146       --extract-notice             extract the notice or banner
147       --extract-filelist           extract the list of files
148       --extract-dependencies       extract the dependencies
149       --extract-autoconf-snippet   extract the snippet for configure.ac
150       --extract-automake-snippet   extract the snippet for library makefile
151       --extract-include-directive  extract the #include directive
152       --extract-link-directive     extract the linker directive
153       --extract-license            report the license terms of the source files
154                                    under lib/
155       --extract-maintainer         report the maintainer(s) inside gnulib
156       --extract-tests-module       report the unit test module, if it exists
157       --copy-file                  copy a file that is not part of any module
158
159 General options:
160       --dir=DIRECTORY       Specify the target directory.
161                             For --import, this specifies where your
162                             configure.ac can be found.  Defaults to current
163                             directory.
164       --local-dir=DIRECTORY  Specify a local override directory where to look
165                             up files before looking in gnulib's directory.
166       --verbose             Increase verbosity. May be repeated.
167       --quiet               Decrease verbosity. May be repeated.
168       --no-cache            Disable module caching optimization.
169
170 Options for --import:
171       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
172       --source-base=DIRECTORY
173                             Directory relative to --dir where source code is
174                             placed (default \"lib\").
175       --m4-base=DIRECTORY   Directory relative to --dir where *.m4 macros are
176                             placed (default \"m4\").
177       --po-base=DIRECTORY   Directory relative to --dir where *.po files are
178                             placed (default \"po\").
179       --doc-base=DIRECTORY  Directory relative to --dir where doc files are
180                             placed (default \"doc\").
181       --tests-base=DIRECTORY
182                             Directory relative to --dir where unit tests are
183                             placed (default \"tests\").
184       --aux-dir=DIRECTORY   Directory relative to --dir where auxiliary build
185                             tools are placed (default \"build-aux\").
186       --with-tests          Include unit tests for the included modules.
187       --with-obsolete       Include obsolete modules when they occur among the
188                             dependencies. By default, dependencies to obsolete
189                             modules are ignored.
190       --avoid=MODULE        Avoid including the given MODULE. Useful if you
191                             have code that provides equivalent functionality.
192                             This option can be repeated.
193       --lgpl[=2|=3]         Abort if modules aren't available under the LGPL.
194                             Also modify license template from GPL to LGPL.
195                             The version number of the LGPL can be specified;
196                             the default is currently LGPLv3.
197       --makefile-name=NAME  Name of makefile in automake syntax in the
198                             source-base and tests-base directories
199                             (default \"Makefile.am\").
200       --libtool             Use libtool rules.
201       --no-libtool          Don't use libtool rules.
202       --macro-prefix=PREFIX  Specify the prefix of the macros 'gl_EARLY' and
203                             'gl_INIT'. Default is 'gl'.
204       --po-domain=NAME      Specify the prefix of the i18n domain. Usually use
205                             the package name. A suffix '-gnulib' is appended.
206       --vc-files            Update version control related files.
207       --no-vc-files         Don't update version control related files
208                             (.gitignore and/or .cvsignore).
209       --no-changelog        Don't update or create ChangeLog files.
210
211 Options for --import and --update:
212       --dry-run             For --import, only print what would have been done.
213   -s, --symbolic, --symlink Make symbolic links instead of copying files.
214       --local-symlink       Make symbolic links instead of copying files, only
215                             for files from the local override directory.
216   -S, --more-symlinks       Make symbolic links instead of copying files, and
217                             don't replace copyright notices.
218
219 Report bugs to <bug-gnulib@gnu.org>."
220 }
221
222 # func_version
223 # outputs to stdout the --version message.
224 func_version ()
225 {
226   func_gnulib_dir
227   if test -d "$gnulib_dir"/.git \
228      && (git --version) >/dev/null 2>/dev/null \
229      && (date --version) >/dev/null 2>/dev/null; then
230     # gnulib checked out from git.
231     sed_extract_first_date='/^Date/{
232 s/^Date:[        ]*//p
233 q
234 }'
235     date=`cd "$gnulib_dir" && git log ChangeLog | sed -n -e "$sed_extract_first_date"`
236     # Turn "Fri Mar 21 07:16:51 2008 -0600" into "Mar 21 2008 07:16:51 -0600".
237     sed_year_before_time='s/^[^ ]* \([^ ]*\) \([0-9]*\) \([0-9:]*\) \([0-9]*\) /\1 \2 \4 \3 /'
238     date=`echo "$date" | sed -e "$sed_year_before_time"`
239     # Use GNU date to compute the time in GMT.
240     date=`date -d "$date" -u +"%Y-%m-%d %H:%M:%S"`
241     version=' '`cd "$gnulib_dir" && ./build-aux/git-version-gen /dev/null | sed -e 's/-dirty/-modified/'`
242   else
243     if test -d "$gnulib_dir"/CVS \
244        && (cvs --version) >/dev/null 2>/dev/null; then
245       # gnulib checked out from CVS.
246       sed_extract_first_date='/^date: /{
247 s/^date: \([0-9][0-9][0-9][0-9]\).\([0-9][0-9]\).\([0-9][0-9]\) \([0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*/\1-\2-\3 \4/p
248 q
249 }'
250       date=`cd "$gnulib_dir" && cvs log -N ChangeLog 2>/dev/null | sed -n -e "$sed_extract_first_date"`
251     else
252       # gnulib copy without versioning information.
253       date=`sed -e 's/ .*//;q' "$gnulib_dir"/ChangeLog`
254     fi
255     version=
256   fi
257   year=`"$gnulib_dir"/build-aux/mdate-sh "$self_abspathname" | sed -e 's,^.* ,,'`
258   echo "\
259 gnulib-tool (GNU $package $date)$version
260 Copyright (C) $year Free Software Foundation, Inc.
261 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
262 This is free software: you are free to change and redistribute it.
263 There is NO WARRANTY, to the extent permitted by law.
264
265 Written by" "Bruno Haible" "and" "Simon Josefsson"
266 }
267
268 # func_emit_copyright_notice
269 # outputs to stdout a header for a generated file.
270 func_emit_copyright_notice ()
271 {
272   sed -n -e '/Copyright/ {
273                p
274                q
275              }' < "$self_abspathname"
276   echo "#"
277   echo "# This file is free software, distributed under the terms of the GNU"
278   echo "# General Public License.  As a special exception to the GNU General"
279   echo "# Public License, this file may be distributed as part of a program"
280   echo "# that contains a configuration script generated by Autoconf, under"
281   echo "# the same distribution terms as the rest of that program."
282   echo "#"
283   echo "# Generated by gnulib-tool."
284 }
285
286 # func_exit STATUS
287 # exits with a given status.
288 # This function needs to be used, rather than 'exit', when a 'trap' handler is
289 # in effect that refers to $?.
290 func_exit ()
291 {
292   (exit $1); exit $1
293 }
294
295 # func_gnulib_dir
296 # locates the directory where the gnulib repository lives
297 # Input:
298 # - progname                 name of this program
299 # Sets variables
300 # - self_abspathname         absolute pathname of gnulib-tool
301 # - gnulib_dir               absolute pathname of gnulib repository
302 func_gnulib_dir ()
303 {
304   case "$progname" in
305     /*) self_abspathname="$progname" ;;
306     */*) self_abspathname=`pwd`/"$progname" ;;
307     *)
308       # Look in $PATH.
309       # Iterate through the elements of $PATH.
310       # We use IFS=: instead of
311       #   for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`
312       # because the latter does not work when some PATH element contains spaces.
313       # We use a canonicalized $pathx instead of $PATH, because empty PATH
314       # elements are by definition equivalent to '.', however field splitting
315       # according to IFS=: loses empty fields in many shells:
316       #   - /bin/sh on OSF/1 and Solaris loses all empty fields (at the
317       #     beginning, at the end, and in the middle),
318       #   - /bin/sh on IRIX and /bin/ksh on IRIX and OSF/1 lose empty fields
319       #     at the beginning and at the end,
320       #   - GNU bash, /bin/sh on AIX and HP-UX, and /bin/ksh on AIX, HP-UX,
321       #     Solaris lose empty fields at the end.
322       # The 'case' statement is an optimization, to avoid evaluating the
323       # explicit canonicalization command when $PATH contains no empty fields.
324       self_abspathname=
325       if test "${PATH_SEPARATOR+set}" != set; then
326         func_tmpdir
327         { echo "#! /bin/sh"; echo "exit 0"; } > "$tmp"/conf.sh
328         chmod +x "$tmp"/conf.sh
329         if (PATH="/nonexistent;$tmp"; conf.sh) >/dev/null 2>&1; then
330           PATH_SEPARATOR=';'
331         else
332           PATH_SEPARATOR=:
333         fi
334         rm -rf "$tmp"
335       fi
336       if test "$PATH_SEPARATOR" = ";"; then
337         # On Windows, programs are searched in "." before $PATH.
338         pathx=".;$PATH"
339       else
340         # On Unix, we have to convert empty PATH elements to ".".
341         pathx="$PATH"
342         case :$PATH: in
343           *::*)
344             pathx=`echo ":$PATH:" | sed -e 's/:::*/:.:/g' -e 's/^://' -e 's/:\$//'`
345             ;;
346         esac
347       fi
348       save_IFS="$IFS"
349       IFS="$PATH_SEPARATOR"
350       for d in $pathx; do
351         IFS="$save_IFS"
352         test -z "$d" && d=.
353         if test -x "$d/$progname" && test ! -d "$d/$progname"; then
354           self_abspathname="$d/$progname"
355           break
356         fi
357       done
358       IFS="$save_IFS"
359       if test -z "$self_abspathname"; then
360         func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
361       fi
362       ;;
363   esac
364   while test -h "$self_abspathname"; do
365     # Resolve symbolic link.
366     linkval=`func_readlink "$self_abspathname"`
367     test -n "$linkval" || break
368     case "$linkval" in
369       /* ) self_abspathname="$linkval" ;;
370       * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
371     esac
372   done
373   gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
374 }
375
376 # func_tmpdir
377 # creates a temporary directory.
378 # Input:
379 # - progname                 name of this program
380 # Sets variable
381 # - tmp             pathname of freshly created temporary directory
382 func_tmpdir ()
383 {
384   # Use the environment variable TMPDIR, falling back to /tmp. This allows
385   # users to specify a different temporary directory, for example, if their
386   # /tmp is filled up or too small.
387   : ${TMPDIR=/tmp}
388   {
389     # Use the mktemp program if available. If not available, hide the error
390     # message.
391     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
392     test -n "$tmp" && test -d "$tmp"
393   } ||
394   {
395     # Use a simple mkdir command. It is guaranteed to fail if the directory
396     # already exists.  $RANDOM is bash specific and expands to empty in shells
397     # other than bash, ksh and zsh.  Its use does not increase security;
398     # rather, it minimizes the probability of failure in a very cluttered /tmp
399     # directory.
400     tmp=$TMPDIR/gl$$-$RANDOM
401     (umask 077 && mkdir "$tmp")
402   } ||
403   {
404     echo "$progname: cannot create a temporary directory in $TMPDIR" >&2
405     func_exit 1
406   }
407 }
408
409 # func_append var value
410 # appends the given value to the shell variable var.
411 if ( foo=bar; foo+=baz && test "$foo" = barbaz ) >/dev/null 2>&1; then
412   # Use bash's += operator. It reduces complexity of appending repeatedly to
413   # a single variable from O(n^2) to O(n).
414   func_append ()
415   {
416     eval "$1+=\"\$2\""
417   }
418   fast_func_append=true
419 else
420   func_append ()
421   {
422     eval "$1=\"\$$1\$2\""
423   }
424   fast_func_append=false
425 fi
426
427 # func_remove_prefix var prefix
428 # removes the given prefix from the value of the shell variable var.
429 # var should be the name of a shell variable.
430 # Its value should not contain a newline and not start or end with whitespace.
431 # prefix should not contain the characters "$`\{}[]^|.
432 if ( foo=bar; eval 'test "${foo#b}" = ar' ) >/dev/null 2>&1; then
433   func_remove_prefix ()
434   {
435     eval "$1=\${$1#\$2}"
436   }
437   fast_func_remove_prefix=true
438 else
439   func_remove_prefix ()
440   {
441     eval "value=\"\$$1\""
442     prefix="$2"
443     case "$prefix" in
444       *.*)
445         sed_escape_dots='s/\([.]\)/\\\1/g'
446         prefix=`echo "$prefix" | sed -e "$sed_escape_dots"`
447         ;;
448     esac
449     value=`echo "$value" | sed -e "s|^${prefix}||"`
450     eval "$1=\"\$value\""
451   }
452   fast_func_remove_prefix=false
453 fi
454
455 # func_remove_suffix var suffix
456 # removes the given suffix from the value of the shell variable var.
457 # var should be the name of a shell variable.
458 # Its value should not contain a newline and not start or end with whitespace.
459 # suffix should not contain the characters "$`\{}[]^|.
460 if ( foo=bar; eval 'test "${foo%r}" = ba' ) >/dev/null 2>&1; then
461   func_remove_suffix ()
462   {
463     eval "$1=\${$1%\$2}"
464   }
465   fast_func_remove_suffix=true
466 else
467   func_remove_suffix ()
468   {
469     eval "value=\"\$$1\""
470     suffix="$2"
471     case "$suffix" in
472       *.*)
473         sed_escape_dots='s/\([.]\)/\\\1/g'
474         suffix=`echo "$suffix" | sed -e "$sed_escape_dots"`
475         ;;
476     esac
477     value=`echo "$value" | sed -e "s|${suffix}\$||"`
478     eval "$1=\"\$value\""
479   }
480   fast_func_remove_suffix=false
481 fi
482
483 # func_fatal_error message
484 # outputs to stderr a fatal error message, and terminates the program.
485 # Input:
486 # - progname                 name of this program
487 func_fatal_error ()
488 {
489   echo "$progname: *** $1" 1>&2
490   echo "$progname: *** Stop." 1>&2
491   func_exit 1
492 }
493
494 # func_warning message
495 # Outputs to stderr a warning message,
496 func_warning ()
497 {
498   echo "gnulib-tool: warning: $1" 1>&2
499 }
500
501 # func_readlink SYMLINK
502 # outputs the target of the given symlink.
503 if (type -p readlink) > /dev/null 2>&1; then
504   func_readlink ()
505   {
506     # Use the readlink program from GNU coreutils.
507     readlink "$1"
508   }
509 else
510   func_readlink ()
511   {
512     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
513     # would do the wrong thing if the link target contains " -> ".
514     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
515   }
516 fi
517
518 # func_relativize DIR1 DIR2
519 # computes a relative pathname RELDIR such that DIR1/RELDIR = DIR2.
520 # Input:
521 # - DIR1            relative pathname, relative to the current directory
522 # - DIR2            relative pathname, relative to the current directory
523 # Output:
524 # - reldir          relative pathname of DIR2, relative to DIR1
525 func_relativize ()
526 {
527   dir0=`pwd`
528   dir1="$1"
529   dir2="$2"
530   sed_first='s,^\([^/]*\)/.*$,\1,'
531   sed_rest='s,^[^/]*/*,,'
532   sed_last='s,^.*/\([^/]*\)$,\1,'
533   sed_butlast='s,/*[^/]*$,,'
534   while test -n "$dir1"; do
535     first=`echo "$dir1" | sed -e "$sed_first"`
536     if test "$first" != "."; then
537       if test "$first" = ".."; then
538         dir2=`echo "$dir0" | sed -e "$sed_last"`/"$dir2"
539         dir0=`echo "$dir0" | sed -e "$sed_butlast"`
540       else
541         first2=`echo "$dir2" | sed -e "$sed_first"`
542         if test "$first2" = "$first"; then
543           dir2=`echo "$dir2" | sed -e "$sed_rest"`
544         else
545           dir2="../$dir2"
546         fi
547         dir0="$dir0"/"$first"
548       fi
549     fi
550     dir1=`echo "$dir1" | sed -e "$sed_rest"`
551   done
552   reldir="$dir2"
553 }
554
555 # func_relconcat DIR1 DIR2
556 # computes a relative pathname DIR1/DIR2, with obvious simplifications.
557 # Input:
558 # - DIR1            relative pathname, relative to the current directory
559 # - DIR2            relative pathname, relative to DIR1
560 # Output:
561 # - relconcat       DIR1/DIR2, relative to the current directory
562 func_relconcat ()
563 {
564   dir1="$1"
565   dir2="$2"
566   sed_first='s,^\([^/]*\)/.*$,\1,'
567   sed_rest='s,^[^/]*/*,,'
568   sed_last='s,^.*/\([^/]*\)$,\1,'
569   sed_butlast='s,/*[^/]*$,,'
570   while true; do
571     first=`echo "$dir2" | sed -e "$sed_first"`
572     if test "$first" = "."; then
573       dir2=`echo "$dir2" | sed -e "$sed_rest"`
574       if test -z "$dir2"; then
575         relconcat="$dir1"
576         break
577       fi
578     else
579       last=`echo "$dir1" | sed -e "$sed_last"`
580       while test "$last" = "."; do
581         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
582         last=`echo "$dir1" | sed -e "$sed_last"`
583       done
584       if test -z "$dir1"; then
585         relconcat="$dir2"
586         break
587       fi
588       if test "$first" = ".."; then
589         if test "$last" = ".."; then
590           relconcat="$dir1/$dir2"
591           break
592         fi
593         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
594         dir2=`echo "$dir2" | sed -e "$sed_rest"`
595         if test -z "$dir1"; then
596           relconcat="$dir2"
597           break
598         fi
599         if test -z "$dir2"; then
600           relconcat="$dir1"
601           break
602         fi
603       else
604         relconcat="$dir1/$dir2"
605         break
606       fi
607     fi
608   done
609 }
610
611 # func_ln SRC DEST
612 # Like ln -s, except that SRC is given relative to the current directory (or
613 # absolute), not given relative to the directory of DEST.
614 func_ln ()
615 {
616   case "$1" in
617     /*)
618       ln -s "$1" "$2" ;;
619     *) # SRC is relative.
620       case "$2" in
621         /*)
622           ln -s "`pwd`/$1" "$2" ;;
623         *) # DEST is relative too.
624           ln_destdir=`echo "$2" | sed -e 's,[^/]*$,,'`
625           test -n "$ln_destdir" || ln_destdir="."
626           func_relativize "$ln_destdir" "$1"
627           ln -s "$reldir" "$2"
628           ;;
629       esac
630       ;;
631   esac
632 }
633
634 # func_ln_if_changed SRC DEST
635 # Like func_ln, but avoids munging timestamps if the link is correct.
636 func_ln_if_changed ()
637 {
638   if test $# -ne 2; then
639     echo "usage: func_ln_if_changed SRC DEST" >&2
640   fi
641   ln_target=`func_readlink "$2"`
642   if test -h "$2" && test "$1" = "$ln_target"; then
643     :
644   else
645     rm -f "$2"
646     func_ln "$1" "$2"
647   fi
648 }
649
650 # func_reset_sigpipe
651 # Resets SIGPIPE to its default behaviour. SIGPIPE is signalled when a process
652 # writes into a pipe with no readers, i.e. a pipe where all readers have
653 # already closed their file descriptor that read from it or exited entirely.
654 # The default behaviour is to terminate the current process without an error
655 # message.
656 # When "trap '' SIGPIPE" is in effect, the behaviour (at least with bash) is to
657 # terminate the current process with an error message.
658 # This function should be called at the beginning of a command that only
659 # produces output to stdout (i.e. no side effects!), when the command that
660 # will read from this pipe might prematurely exit or close its standard input
661 # descriptor.
662 if test -n "$BASH_VERSION"; then
663   # The problem has only been reported with bash. Probably it occurs only with
664   # bash-3.2. For the reasons, see
665   # <http://lists.gnu.org/archive/html/bug-bash/2008-12/msg00050.html>.
666   # Note that Solaris sh does not understand "trap - SIGPIPE".
667   func_reset_sigpipe ()
668   {
669     trap - SIGPIPE
670   }
671 else
672   func_reset_sigpipe ()
673   {
674     :
675   }
676 fi
677
678 # Ensure an 'echo' command that does not interpret backslashes.
679 # Test cases:
680 #   echo '\n' | wc -l                 prints 1 when OK, 2 when KO
681 #   echo '\t' | grep t > /dev/null    has return code 0 when OK, 1 when KO
682 # This problem is a weird heritage from SVR4. BSD got it right (except that
683 # BSD echo interprets '-n' as an option, which is also not desirable).
684 # Nowadays the problem occurs in 4 situations:
685 # - in bash, when the shell option xpg_echo is set (bash >= 2.04)
686 #            or when it was built with --enable-usg-echo-default (bash >= 2.0)
687 #            or when it was built with DEFAULT_ECHO_TO_USG (bash < 2.0),
688 # - in zsh, when sh-emulation is not set,
689 # - in ksh (e.g. AIX /bin/sh and Solaris /usr/xpg4/bin/sh are ksh instances,
690 #           and HP-UX /bin/sh and IRIX /bin/sh behave similarly),
691 # - in Solaris /bin/sh and OSF/1 /bin/sh.
692 # We try the following workarounds:
693 # - for all: respawn using $CONFIG_SHELL if that is set and works.
694 # - for bash >= 2.04: unset the shell option xpg_echo.
695 # - for bash >= 2.0: define echo to a function that uses the printf built-in.
696 # - for bash < 2.0: define echo to a function that uses cat of a here document.
697 # - for zsh: turn sh-emulation on.
698 # - for ksh: alias echo to 'print -r'.
699 # - for ksh: alias echo to a function that uses cat of a here document.
700 # - for Solaris /bin/sh and OSF/1 /bin/sh: respawn using /bin/ksh and rely on
701 #   the ksh workaround.
702 # - otherwise: respawn using /bin/sh and rely on the workarounds.
703 # When respawning, we pass --no-reexec as first argument, so as to avoid
704 # turning this script into a fork bomb in unlucky situations.
705 have_echo=
706 if echo '\t' | grep t > /dev/null; then
707   have_echo=yes # Lucky!
708 fi
709 # Try the workarounds.
710 # Respawn using $CONFIG_SHELL if that is set and works.
711 if test -z "$have_echo" \
712    && test "X$1" != "X--no-reexec" \
713    && test -n "$CONFIG_SHELL" \
714    && test -f "$CONFIG_SHELL" \
715    && $CONFIG_SHELL -c "echo '\\t' | grep t > /dev/null"; then
716   exec $CONFIG_SHELL "$0" --no-reexec "$@"
717   exit 127
718 fi
719 # For bash >= 2.04: unset the shell option xpg_echo.
720 if test -z "$have_echo" \
721    && test -n "$BASH_VERSION" \
722    && (shopt -o xpg_echo; echo '\t' | grep t > /dev/null) 2>/dev/null; then
723   shopt -o xpg_echo
724   have_echo=yes
725 fi
726 # For bash >= 2.0: define echo to a function that uses the printf built-in.
727 # For bash < 2.0: define echo to a function that uses cat of a here document.
728 # (There is no win in using 'printf' over 'cat' if it is not a shell built-in.)
729 if test -z "$have_echo" \
730    && test -n "$BASH_VERSION"; then \
731   if type printf 2>/dev/null | grep / > /dev/null; then
732     # 'printf' is not a shell built-in.
733 echo ()
734 {
735 cat <<EOF
736 $*
737 EOF
738 }
739   else
740     # 'printf' is a shell built-in.
741 echo ()
742 {
743   printf '%s\n' "$*"
744 }
745   fi
746   if echo '\t' | grep t > /dev/null; then
747     have_echo=yes
748   fi
749 fi
750 # For zsh: turn sh-emulation on.
751 if test -z "$have_echo" \
752    && test -n "$ZSH_VERSION" \
753    && (emulate sh) >/dev/null 2>&1; then
754   emulate sh
755 fi
756 # For ksh: alias echo to 'print -r'.
757 if test -z "$have_echo" \
758    && (type print) >/dev/null 2>&1; then
759   # A 'print' command exists.
760   if type print 2>/dev/null | grep / > /dev/null; then
761     :
762   else
763     # 'print' is a shell built-in.
764     if (print -r '\told' | grep told > /dev/null) 2>/dev/null; then
765       # 'print' is the ksh shell built-in.
766       alias echo='print -r'
767     fi
768   fi
769 fi
770 if test -z "$have_echo" \
771    && echo '\t' | grep t > /dev/null; then
772   have_echo=yes
773 fi
774 # For ksh: alias echo to a function that uses cat of a here document.
775 # The ksh manual page says:
776 #   "Aliasing is performed when scripts are read, not while they are executed.
777 #    Therefore, for an alias to take effect, the alias definition command has
778 #    to be executed before the command which references the alias is read."
779 # Because of this, we have to play strange tricks with have_echo, to ensure
780 # that the top-level statement containing the test starts after the 'alias'
781 # command.
782 if test -z "$have_echo"; then
783 bsd_echo ()
784 {
785 cat <<EOF
786 $*
787 EOF
788 }
789 alias echo=bsd_echo 2>/dev/null
790 fi
791 if test -z "$have_echo" \
792    && echo '\t' | grep t > /dev/null; then
793   have_echo=yes
794 fi
795 if test -z "$have_echo"; then
796   unalias echo 2>/dev/null
797 fi
798 # For Solaris /bin/sh and OSF/1 /bin/sh: respawn using /bin/ksh.
799 if test -z "$have_echo" \
800    && test "X$1" != "X--no-reexec" \
801    && test -f /bin/ksh; then
802   exec /bin/ksh "$0" --no-reexec "$@"
803   exit 127
804 fi
805 # Otherwise: respawn using /bin/sh.
806 if test -z "$have_echo" \
807    && test "X$1" != "X--no-reexec" \
808    && test -f /bin/sh; then
809   exec /bin/sh "$0" --no-reexec "$@"
810   exit 127
811 fi
812 if test -z "$have_echo"; then
813   func_fatal_error "Shell does not support 'echo' correctly. Please install GNU bash and set the environment variable CONFIG_SHELL to point to it."
814 fi
815 if echo '\t' | grep t > /dev/null; then
816   : # Works fine now.
817 else
818   func_fatal_error "Shell does not support 'echo' correctly. Workaround does not work. Please report this as a bug to bug-gnulib@gnu.org."
819 fi
820 if test "X$1" = "X--no-reexec"; then
821   shift
822 fi
823
824 # Command-line option processing.
825 # Removes the OPTIONS from the arguments. Sets the variables:
826 # - mode            list or import or create-testdir or create-megatestdir
827 # - destdir         from --dir
828 # - local_gnulib_dir  from --local-dir
829 # - verbose         integer, default 0, inc/decremented by --verbose/--quiet
830 # - libname, supplied_libname  from --lib
831 # - sourcebase      from --source-base
832 # - m4base          from --m4-base
833 # - pobase          from --po-base
834 # - docbase         from --doc-base
835 # - testsbase       from --tests-base
836 # - auxdir          from --aux-dir
837 # - inctests        true if --with-tests was given, blank otherwise
838 # - incobsolete     true if --with-obsolete was given, blank otherwise
839 # - avoidlist       list of modules to avoid, from --avoid
840 # - lgpl            yes or a number if --lgpl was given, blank otherwise
841 # - makefile_name   from --makefile-name
842 # - libtool         true if --libtool was given, false if --no-libtool was
843 #                   given, blank otherwise
844 # - macro_prefix    from --macro-prefix
845 # - po_domain       from --po-domain
846 # - vc_files        true if --vc-files was given, false if --no-vc-files was
847 #                   given, blank otherwise
848 # - autoconf_minversion  minimum supported autoconf version
849 # - do_changelog    false if --no-changelog was given, : otherwise
850 # - doit            : if actions shall be executed, false if only to be printed
851 # - symbolic        true if --symlink or --more-symlinks was given, blank
852 #                   otherwise
853 # - lsymbolic       true if --local-symlink was given, blank otherwise
854 # - do_copyrights   blank if --more-symlinks was given, true otherwise
855 {
856   mode=
857   destdir=
858   local_gnulib_dir=
859   verbose=0
860   do_cache=:
861   libname=libgnu
862   supplied_libname=
863   sourcebase=
864   m4base=
865   pobase=
866   docbase=
867   testsbase=
868   auxdir=
869   inctests=
870   incobsolete=
871   avoidlist=
872   lgpl=
873   makefile_name=
874   libtool=
875   macro_prefix=
876   po_domain=
877   vc_files=
878   do_changelog=:
879   doit=:
880   symbolic=
881   lsymbolic=
882   do_copyrights=true
883
884   supplied_opts="$@"
885
886   while test $# -gt 0; do
887     case "$1" in
888       --list | --lis )
889         mode=list
890         shift ;;
891       --import | --impor | --impo | --imp | --im | --i )
892         mode=import
893         shift ;;
894       --update | --updat | --upda | --upd | --up | --u )
895         mode=update
896         shift ;;
897       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
898         mode=create-testdir
899         shift ;;
900       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
901         mode=create-megatestdir
902         shift ;;
903       --test | --tes | --te | --t )
904         mode=test
905         shift ;;
906       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
907         mode=megatest
908         shift ;;
909       --extract-* )
910         mode=`echo "X$1" | sed -e 's/^X--//'`
911         shift ;;
912       --copy-file | --copy-fil | --copy-fi | --copy-f | --copy- | --copy | --cop | --co )
913         mode=copy-file
914         shift ;;
915       --dir )
916         shift
917         if test $# = 0; then
918           func_fatal_error "missing argument for --dir"
919         fi
920         destdir=$1
921         shift ;;
922       --dir=* )
923         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
924         shift ;;
925       --local-dir )
926         shift
927         if test $# = 0; then
928           func_fatal_error "missing argument for --local-dir"
929         fi
930         local_gnulib_dir=$1
931         shift ;;
932       --local-dir=* )
933         local_gnulib_dir=`echo "X$1" | sed -e 's/^X--local-dir=//'`
934         shift ;;
935       --verbose | --verbos | --verbo | --verb )
936         verbose=`expr $verbose + 1`
937         shift ;;
938       --quiet | --quie | --qui | --qu | --q )
939         verbose=`expr $verbose - 1`
940         shift ;;
941       --no-cache | --no-cach | --no-cac | --no-ca )
942         do_cache=false
943         shift ;;
944       --lib )
945         shift
946         if test $# = 0; then
947           func_fatal_error "missing argument for --lib"
948         fi
949         libname=$1
950         supplied_libname=true
951         shift ;;
952       --lib=* )
953         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
954         supplied_libname=true
955         shift ;;
956       --source-base )
957         shift
958         if test $# = 0; then
959           func_fatal_error "missing argument for --source-base"
960         fi
961         sourcebase=$1
962         shift ;;
963       --source-base=* )
964         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
965         shift ;;
966       --m4-base )
967         shift
968         if test $# = 0; then
969           func_fatal_error "missing argument for --m4-base"
970         fi
971         m4base=$1
972         shift ;;
973       --m4-base=* )
974         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
975         shift ;;
976       --po-base )
977         shift
978         if test $# = 0; then
979           func_fatal_error "missing argument for --po-base"
980         fi
981         pobase=$1
982         shift ;;
983       --po-base=* )
984         pobase=`echo "X$1" | sed -e 's/^X--po-base=//'`
985         shift ;;
986       --doc-base )
987         shift
988         if test $# = 0; then
989           func_fatal_error "missing argument for --doc-base"
990         fi
991         docbase=$1
992         shift ;;
993       --doc-base=* )
994         docbase=`echo "X$1" | sed -e 's/^X--doc-base=//'`
995         shift ;;
996       --tests-base )
997         shift
998         if test $# = 0; then
999           func_fatal_error "missing argument for --tests-base"
1000         fi
1001         testsbase=$1
1002         shift ;;
1003       --tests-base=* )
1004         testsbase=`echo "X$1" | sed -e 's/^X--tests-base=//'`
1005         shift ;;
1006       --aux-dir )
1007         shift
1008         if test $# = 0; then
1009           func_fatal_error "missing argument for --aux-dir"
1010         fi
1011         auxdir=$1
1012         shift ;;
1013       --aux-dir=* )
1014         auxdir=`echo "X$1" | sed -e 's/^X--aux-dir=//'`
1015         shift ;;
1016       --with-tests )
1017         inctests=true
1018         shift ;;
1019       --with-obsolete )
1020         incobsolete=true
1021         shift ;;
1022       --avoid )
1023         shift
1024         if test $# = 0; then
1025           func_fatal_error "missing argument for --avoid"
1026         fi
1027         func_append avoidlist " $1"
1028         shift ;;
1029       --avoid=* )
1030         arg=`echo "X$1" | sed -e 's/^X--avoid=//'`
1031         func_append avoidlist " $arg"
1032         shift ;;
1033       --lgpl )
1034         lgpl=yes
1035         shift ;;
1036       --lgpl=* )
1037         arg=`echo "X$1" | sed -e 's/^X--lgpl=//'`
1038         case "$arg" in
1039           2 | 3) ;;
1040           *) func_fatal_error "invalid LGPL version number for --lgpl" ;;
1041         esac
1042         lgpl=$arg
1043         shift ;;
1044       --makefile-name )
1045         shift
1046         if test $# = 0; then
1047           func_fatal_error "missing argument for --makefile-name"
1048         fi
1049         makefile_name="$1"
1050         shift ;;
1051       --makefile-name=* )
1052         makefile_name=`echo "X$1" | sed -e 's/^X--makefile-name=//'`
1053         shift ;;
1054       --libtool )
1055         libtool=true
1056         shift ;;
1057       --no-libtool )
1058         libtool=false
1059         shift ;;
1060       --macro-prefix )
1061         shift
1062         if test $# = 0; then
1063           func_fatal_error "missing argument for --macro-prefix"
1064         fi
1065         macro_prefix="$1"
1066         shift ;;
1067       --macro-prefix=* )
1068         macro_prefix=`echo "X$1" | sed -e 's/^X--macro-prefix=//'`
1069         shift ;;
1070       --po-domain )
1071         shift
1072         if test $# = 0; then
1073           func_fatal_error "missing argument for --po-domain"
1074         fi
1075         po_domain="$1"
1076         shift ;;
1077       --po-domain=* )
1078         po_domain=`echo "X$1" | sed -e 's/^X--po-domain=//'`
1079         shift ;;
1080       --vc-files )
1081         vc_files=true
1082         shift ;;
1083       --no-vc-files )
1084         vc_files=false
1085         shift ;;
1086       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch )
1087         do_changelog=false
1088         shift ;;
1089       --dry-run )
1090         doit=false
1091         shift ;;
1092       -s | --symbolic | --symboli | --symbol | --symbo | --symb | --symlink | --symlin | --symli | --syml | --sym | --sy )
1093         symbolic=true
1094         shift ;;
1095       --local-symlink | --local-symlin | --local-symli | --local-syml | --local-sym | --local-sy | --local-s )
1096         lsymbolic=true
1097         shift ;;
1098       -S | --more-symlinks | --more-symlink | --more-symlin | --more-symli | --more-syml | --more-sym | --more-sy | --more-s | --more- | --more | --mor | --mo )
1099         symbolic=true
1100         do_copyrights=
1101         shift ;;
1102       --help | --hel | --he | --h )
1103         func_usage
1104         func_exit $? ;;
1105       --version | --versio | --versi | --vers )
1106         func_version
1107         func_exit $? ;;
1108       -- )
1109         # Stop option processing
1110         shift
1111         break ;;
1112       -* )
1113         echo "gnulib-tool: unknown option $1" 1>&2
1114         echo "Try 'gnulib-tool --help' for more information." 1>&2
1115         func_exit 1 ;;
1116       * )
1117         break ;;
1118     esac
1119   done
1120
1121   if test "$mode" = update; then
1122     if test $# != 0; then
1123       echo "gnulib-tool: too many arguments in 'update' mode" 1>&2
1124       echo "Try 'gnulib-tool --help' for more information." 1>&2
1125       echo "If you really want to modify the gnulib configuration of your project," 1>&2
1126       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
1127       func_exit 1
1128     fi
1129     if test -n "$local_gnulib_dir" || test -n "$supplied_libname" \
1130        || test -n "$sourcebase" || test -n "$m4base" || test -n "$pobase" \
1131        || test -n "$docbase" || test -n "$testsbase" || test -n "$auxdir" \
1132        || test -n "$inctests" || test -n "$incobsolete" \
1133        || test -n "$avoidlist" || test -n "$lgpl" || test -n "$makefile_name" \
1134        || test -n "$macro_prefix" || test -n "$po_domain" \
1135        || test -n "$vc_files"; then
1136       echo "gnulib-tool: invalid options for 'update' mode" 1>&2
1137       echo "Try 'gnulib-tool --help' for more information." 1>&2
1138       echo "If you really want to modify the gnulib configuration of your project," 1>&2
1139       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
1140       func_exit 1
1141     fi
1142     do_changelog=false
1143   fi
1144   if test -n "$pobase" && test -z "$po_domain"; then
1145     echo "gnulib-tool: together with --po-base, you need to specify --po-domain" 1>&2
1146     echo "Try 'gnulib-tool --help' for more information." 1>&2
1147     func_exit 1
1148   fi
1149   if test -z "$pobase" && test -n "$po_domain"; then
1150     func_warning "--po-domain has no effect without a --po-base option"
1151   fi
1152
1153   # Determine the minimum supported autoconf version from the project's
1154   # configure.ac.
1155   DEFAULT_AUTOCONF_MINVERSION="2.59"
1156   autoconf_minversion=
1157   configure_ac=
1158   if { test "$mode" = import || test "$mode" = update; } && test -n "$destdir"; then
1159     if test -f "$destdir"/configure.ac; then
1160       configure_ac="$destdir/configure.ac"
1161     else
1162       if test -f "$destdir"/configure.in; then
1163         configure_ac="$destdir/configure.in"
1164       fi
1165     fi
1166   else
1167     if test -f configure.ac; then
1168       configure_ac="configure.ac"
1169     else
1170       if test -f configure.in; then
1171         configure_ac="configure.in"
1172       fi
1173     fi
1174   fi
1175   if test -n "$configure_ac"; then
1176     # Use sed, not autoconf --trace, to look for the AC_PREREQ invocation,
1177     # because when some m4 files are omitted from a CVS repository,
1178     # "autoconf --trace=AC_PREREQ" fails with an error message like this:
1179     #   m4: aclocal.m4:851: Cannot open m4/absolute-header.m4: No such file or directory
1180     #   autom4te: m4 failed with exit status: 1
1181     prereqs=
1182     my_sed_traces='
1183       s,#.*$,,
1184       s,^dnl .*$,,
1185       s, dnl .*$,,
1186       /AC_PREREQ/ {
1187         s,^.*AC_PREREQ([[ ]*\([^])]*\).*$,\1,p
1188       }'
1189     prereqs=`sed -n -e "$my_sed_traces" < "$configure_ac"`
1190     if test -n "$prereqs"; then
1191       autoconf_minversion=`
1192         for version in $prereqs; do echo $version; done |
1193         LC_ALL=C sort -nru | sed -e 1q
1194       `
1195     fi
1196   fi
1197   if test -z "$autoconf_minversion"; then
1198     autoconf_minversion=$DEFAULT_AUTOCONF_MINVERSION
1199   fi
1200   case "$autoconf_minversion" in
1201     1.* | 2.[0-4]* | 2.5[0-8]*)
1202       func_fatal_error "minimum supported autoconf version is 2.59. Try adding AC_PREREQ([$DEFAULT_AUTOCONF_MINVERSION]) to your configure.ac." ;;
1203   esac
1204
1205   # Remove trailing slashes from the directory names. This is necessary for
1206   # m4base (to avoid an error in func_import) and optional for the others.
1207   sed_trimtrailingslashes='s,\([^/]\)//*$,\1,'
1208   case "$local_gnulib_dir" in
1209     */ ) local_gnulib_dir=`echo "$local_gnulib_dir" | sed -e "$sed_trimtrailingslashes"` ;;
1210   esac
1211   case "$sourcebase" in
1212     */ ) sourcebase=`echo "$sourcebase" | sed -e "$sed_trimtrailingslashes"` ;;
1213   esac
1214   case "$m4base" in
1215     */ ) m4base=`echo "$m4base" | sed -e "$sed_trimtrailingslashes"` ;;
1216   esac
1217   case "$pobase" in
1218     */ ) pobase=`echo "$pobase" | sed -e "$sed_trimtrailingslashes"` ;;
1219   esac
1220   case "$docbase" in
1221     */ ) docbase=`echo "$docbase" | sed -e "$sed_trimtrailingslashes"` ;;
1222   esac
1223   case "$testsbase" in
1224     */ ) testsbase=`echo "$testsbase" | sed -e "$sed_trimtrailingslashes"` ;;
1225   esac
1226   case "$auxdir" in
1227     */ ) auxdir=`echo "$auxdir" | sed -e "$sed_trimtrailingslashes"` ;;
1228   esac
1229 }
1230
1231 func_gnulib_dir
1232 func_tmpdir
1233 trap 'exit_status=$?
1234       if test "$signal" != 0; then
1235         echo "caught signal $signal" >&2
1236       fi
1237       rm -rf "$tmp"
1238       exit $exit_status' 0
1239 for signal in 1 2 3 13 15; do
1240   trap '{ signal='$signal'; func_exit 1; }' $signal
1241 done
1242 signal=0
1243
1244 # func_lookup_file file
1245 # looks up a file in $local_gnulib_dir or $gnulib_dir, or combines it through
1246 # 'patch'.
1247 # Input:
1248 # - local_gnulib_dir  from --local-dir
1249 # Output:
1250 # - lookedup_file   name of the merged (combined) file
1251 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
1252 func_lookup_file ()
1253 {
1254   lkfile="$1"
1255   if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile"; then
1256     lookedup_file="$local_gnulib_dir/$lkfile"
1257     lookedup_tmp=
1258   else
1259     if test -f "$gnulib_dir/$lkfile"; then
1260       if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile.diff"; then
1261         lkbase=`echo "$lkfile" | sed -e 's,^.*/,,'`
1262         rm -f "$tmp/$lkbase"
1263         cp "$gnulib_dir/$lkfile" "$tmp/$lkbase"
1264         patch -s "$tmp/$lkbase" < "$local_gnulib_dir/$lkfile.diff" \
1265           || func_fatal_error "patch file $local_gnulib_dir/$lkfile.diff didn't apply cleanly"
1266         lookedup_file="$tmp/$lkbase"
1267         lookedup_tmp=true
1268       else
1269         lookedup_file="$gnulib_dir/$lkfile"
1270         lookedup_tmp=
1271       fi
1272     else
1273       func_fatal_error "file $gnulib_dir/$lkfile not found"
1274     fi
1275   fi
1276 }
1277
1278 # func_all_modules
1279 # Input:
1280 # - local_gnulib_dir  from --local-dir
1281 func_all_modules ()
1282 {
1283   # Filter out metainformation files like README, which are not modules.
1284   # Filter out unit test modules; they can be retrieved through
1285   # --extract-tests-module if desired.
1286   {
1287     (cd "$gnulib_dir" && find modules -type f -print | sed -e 's,^modules/,,')
1288     if test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules"; then
1289       (cd "$local_gnulib_dir" && find modules -type f -print | sed -e 's,^modules/,,' -e 's,\.diff$,,')
1290     fi
1291   } \
1292       | sed -e '/^CVS\//d' -e '/\/CVS\//d' \
1293             -e '/^ChangeLog$/d' -e '/\/ChangeLog$/d' \
1294             -e '/^COPYING$/d' -e '/\/COPYING$/d' \
1295             -e '/^README$/d' -e '/\/README$/d' \
1296             -e '/^TEMPLATE$/d' \
1297             -e '/^TEMPLATE-EXTENDED$/d' \
1298             -e '/^TEMPLATE-TESTS$/d' \
1299             -e '/^\..*/d' \
1300             -e '/~$/d' \
1301       | sed -e '/-tests$/d' \
1302       | LC_ALL=C sort -u
1303 }
1304
1305 # func_verify_module
1306 # verifies a module name
1307 # Input:
1308 # - local_gnulib_dir  from --local-dir
1309 # - module          module name argument
1310 func_verify_module ()
1311 {
1312   if { test -f "$gnulib_dir/modules/$module" \
1313        || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
1314             && test -f "$local_gnulib_dir/modules/$module"; }; } \
1315      && test "CVS" != "$module" \
1316      && test "ChangeLog" != "$module" \
1317      && test "COPYING" != "$module" \
1318      && test "README" != "$module" \
1319      && test "TEMPLATE" != "$module" \
1320      && test "TEMPLATE-EXTENDED" != "$module" \
1321      && test "TEMPLATE-TESTS" != "$module"; then
1322     # OK, $module is a correct module name.
1323     # Verify that building the module description with 'patch' succeeds.
1324     func_lookup_file "modules/$module"
1325   else
1326     func_warning "module $module doesn't exist"
1327     module=
1328   fi
1329 }
1330
1331 # func_verify_nontests_module
1332 # verifies a module name, excluding tests modules
1333 # Input:
1334 # - local_gnulib_dir  from --local-dir
1335 # - module          module name argument
1336 func_verify_nontests_module ()
1337 {
1338   case "$module" in
1339     *-tests ) module= ;;
1340     * ) func_verify_module ;;
1341   esac
1342 }
1343
1344 # func_verify_tests_module
1345 # verifies a module name, considering only tests modules
1346 # Input:
1347 # - local_gnulib_dir  from --local-dir
1348 # - module          module name argument
1349 func_verify_tests_module ()
1350 {
1351   case "$module" in
1352     *-tests ) func_verify_module ;;
1353     * ) module= ;;
1354   esac
1355 }
1356
1357 # func_cache_var file
1358 #
1359 # return the cache variable name corresponding to $file.
1360 # Output:
1361 # - cachevar
1362 if test -n "$BASH_VERSION"; then
1363   func_cache_var ()
1364   {
1365     cachevar=c_${1//[!a-zA-Z0-9_]/_}
1366   }
1367 else
1368   func_cache_var ()
1369   {
1370     case $1 in
1371       *[!a-zA-Z0-9_]*)
1372         cachevar=c_`echo "$1" | LC_ALL=C sed 's/[^a-zA-Z0-9_]/_/g'` ;;
1373       *)
1374         cachevar=c_$1 ;;
1375     esac
1376   }
1377 fi
1378
1379 # Extract headers from module descriptions.
1380 # NOTE: Keep this in sync with sed_extract_prog below!
1381 sed_extract_cache_prog="
1382   s/^Description:[       ]*$/desc/
1383   s/^Status:[    ]*$/status/
1384   s/^Notice:[    ]*$/notice/
1385   s/^Applicability:[     ]*$/applicability/
1386   s/^Files:[     ]*$/files/
1387   s/^Depends-on:[        ]*$/deps/
1388   s/^configure\.ac-early:[       ]*$/config_early/
1389   s/^configure\.ac:[     ]*$/config/
1390   s/^Makefile\.am:[      ]*$/makefile/
1391   s/^Include:[   ]*$/include/
1392   s/^Link:[      ]*$/link/
1393   s/^License:[   ]*$/license/
1394   s/^Maintainer:[        ]*$/maint/"
1395
1396 # func_cache_lookup_module file
1397 #
1398 # look up a module, like 'func_lookup_file modules/$file', and store all of its
1399 # relevant data in a cache.  If already cached, do not look it up again.
1400 # Input:
1401 # - file
1402 # Output:
1403 # - cachevar
1404 # - ${cachevar}_cached
1405 # - ${cachevar}_desc
1406 # - ${cachevar}_status
1407 # - ...
1408 func_cache_lookup_module ()
1409 {
1410   func_cache_var "$1"
1411   if eval test -z \"\$${cachevar}_cached\"; then
1412     func_lookup_file "modules/$1"
1413     # Turn module descriptions into shell script assignments,
1414     # suitable to be eval'ed.  All active characters are escaped.
1415     # This script turns
1416     #   Description:
1417     #   Some module's description
1418     #
1419     #   Files:
1420     #   lib/file.h
1421     # into:
1422     #   c_MODULE_cached='yes
1423     #   '
1424     #   c_MODULE_desc=\
1425     #   'Some module'\''s description
1426     #
1427     #   '
1428     #   c_MODULE_files=\
1429     #   'lib/file.h'
1430     # The script consists of four parts:
1431     # 1) Insert the "c_MODULE_cached='yes" line,
1432     # 2) The 'match' loop, treating non-header lines by escaping single
1433     #    quotes and adding a closing quote in the last line,
1434     # 3) The 'header' treatment, closing the last quote and inserting
1435     #    the "c_MODULE_HEADER=" assignment line as well starting the
1436     #    following line with an opening quote, if any.
1437     # 4) Special treatment if two headers are only separated by one newline.
1438     sed_cache_module='
1439       1i\'$nl$cachevar'_cached='\''yes\'$nl'
1440
1441       :match
1442       t match
1443       '"$sed_extract_cache_prog"'
1444       t hdr
1445       s/'\''/&"'\''"&/g
1446       :more
1447       $s/$/'\''/
1448       n
1449       b match
1450
1451       :hdr
1452       s/\([a-zA-Z0-9_]\{1,\}\)[  ]*$/'\'\\$nl${cachevar}_'\1=/
1453       $!s/$/\\/
1454       n
1455       t clear
1456       :clear
1457       '"$sed_extract_cache_prog"'
1458       t hdr2
1459       s/'\''/&"'\''"&/g
1460       s/^/'\''/
1461       b more
1462
1463       :hdr2
1464       s/^/'\'\\$nl'/
1465       b hdr
1466       '
1467     # Strip trailing newlines from quoted variable assignment strings.
1468     sed_strip_trailing_nl='
1469       :more
1470       $b cut
1471       /\\$/{
1472         p
1473         d
1474       }
1475       N
1476       /\n$/b more
1477       :cut
1478       s/\n\n*\('\''\)$/\1/
1479       '
1480     cache_script=`sed "$sed_cache_module" < "$lookedup_file" \
1481                     | sed "$sed_strip_trailing_nl"`
1482     eval "$cache_script"
1483   fi
1484 }
1485
1486 # Extract headers from module descriptions, without caching.
1487 # NOTE: Keep this in sync with sed_extract_cache_prog above!
1488 sed_extract_prog=':[     ]*$/ {
1489   :a
1490     n
1491     s/^Description:[     ]*$//
1492     s/^Status:[  ]*$//
1493     s/^Notice:[  ]*$//
1494     s/^Applicability:[   ]*$//
1495     s/^Files:[   ]*$//
1496     s/^Depends-on:[      ]*$//
1497     s/^configure\.ac-early:[     ]*$//
1498     s/^configure\.ac:[   ]*$//
1499     s/^Makefile\.am:[    ]*$//
1500     s/^Include:[         ]*$//
1501     s/^Link:[    ]*$//
1502     s/^License:[         ]*$//
1503     s/^Maintainer:[      ]*$//
1504     tb
1505     p
1506     ba
1507   :b
1508 }'
1509
1510
1511 # func_get_description module
1512 # Input:
1513 # - local_gnulib_dir  from --local-dir
1514 func_get_description ()
1515 {
1516   if $do_cache; then
1517     func_cache_lookup_module "$1"
1518     eval "echo \"\$${cachevar}_desc\""
1519   else
1520     func_lookup_file "modules/$1"
1521     sed -n -e "/^Description$sed_extract_prog" < "$lookedup_file"
1522   fi
1523 }
1524
1525 # func_get_status module
1526 # Input:
1527 # - local_gnulib_dir  from --local-dir
1528 func_get_status ()
1529 {
1530   if $do_cache; then
1531     func_cache_lookup_module "$1"
1532     eval "echo \"\$${cachevar}_status\""
1533   else
1534     func_lookup_file "modules/$1"
1535     sed -n -e "/^Status$sed_extract_prog" < "$lookedup_file"
1536   fi
1537 }
1538
1539 # func_get_notice module
1540 # Input:
1541 # - local_gnulib_dir  from --local-dir
1542 func_get_notice ()
1543 {
1544   if $do_cache; then
1545     func_cache_lookup_module "$1"
1546     eval "echo \"\$${cachevar}_notice\""
1547   else
1548     func_lookup_file "modules/$1"
1549     sed -n -e "/^Notice$sed_extract_prog" < "$lookedup_file"
1550   fi
1551 }
1552
1553 # func_get_applicability module
1554 # Input:
1555 # - local_gnulib_dir  from --local-dir
1556 # The expected result (on stdout) is either 'main', or 'tests', or 'all'.
1557 func_get_applicability ()
1558 {
1559   if $do_cache; then
1560     func_cache_lookup_module "$1"
1561     eval my_applicability=\$${cachevar}_applicability
1562   else
1563     func_lookup_file "modules/$1"
1564     my_applicability=`sed -n -e "/^Applicability$sed_extract_prog" < "$lookedup_file"`
1565   fi
1566   if test -n "$my_applicability"; then
1567     echo $my_applicability
1568   else
1569     # The default is 'main' or 'tests', depending on the module's name.
1570     case $1 in
1571       *-tests) echo tests;;
1572       *)       echo main;;
1573     esac
1574   fi
1575 }
1576
1577 # func_get_filelist module
1578 # Input:
1579 # - local_gnulib_dir  from --local-dir
1580 func_get_filelist ()
1581 {
1582   if $do_cache; then
1583     func_cache_lookup_module "$1"
1584     eval "echo \"\$${cachevar}_files\""
1585   else
1586     func_lookup_file "modules/$1"
1587     sed -n -e "/^Files$sed_extract_prog" < "$lookedup_file"
1588   fi
1589   echo m4/00gnulib.m4
1590   echo m4/gnulib-common.m4
1591   case "$autoconf_minversion" in
1592     2.59)
1593       echo m4/onceonly.m4
1594       ;;
1595   esac
1596 }
1597
1598 # func_filter_filelist outputvar separator filelist prefix suffix removed_prefix removed_suffix [added_prefix [added_suffix]]
1599 # stores in outputvar the filtered and processed filelist. Filtering: Only the
1600 # elements starting with prefix and ending with suffix are considered.
1601 # Processing: removed_prefix and removed_suffix are removed from each element,
1602 # added_prefix and added_suffix are added to each element.
1603 # prefix, suffix should not contain shell-special characters.
1604 # removed_prefix, removed_suffix should not contain the characters "$`\{}[]^|.
1605 # added_prefix, added_suffix should not contain the characters \|&.
1606 func_filter_filelist ()
1607 {
1608   if test "$2" != "$nl" \
1609      || { $fast_func_append \
1610           && { test -z "$6" || $fast_func_remove_prefix; } \
1611           && { test -z "$7" || $fast_func_remove_suffix; }; \
1612         }; then
1613     ffflist=
1614     for fff in $3; do
1615       # Do not quote possibly-empty parameters in case patterns,
1616       # AIX and HP-UX ksh won't match them if they are empty.
1617       case "$fff" in
1618         $4*$5)
1619           if test -n "$6"; then
1620             func_remove_prefix fff "$6"
1621           fi
1622           if test -n "$7"; then
1623             func_remove_suffix fff "$7"
1624           fi
1625           fff="$8${fff}$9"
1626           if test -z "$ffflist"; then
1627             ffflist="${fff}"
1628           else
1629             func_append ffflist "$2${fff}"
1630           fi
1631           ;;
1632       esac
1633     done
1634   else
1635     sed_fff_filter="s|^$6\(.*\)$7\$|$8\\1$9|"
1636     ffflist=`for fff in $3; do
1637                case "$fff" in
1638                  $4*$5) echo "$fff" ;;
1639                esac
1640              done | sed -e "$sed_fff_filter"`
1641   fi
1642   eval "$1=\"\$ffflist\""
1643 }
1644
1645 # func_get_dependencies module
1646 # Input:
1647 # - local_gnulib_dir  from --local-dir
1648 func_get_dependencies ()
1649 {
1650   # ${module}-tests always implicitly depends on ${module}.
1651   case "$1" in
1652     *-tests)
1653       fgd1="$1"
1654       func_remove_suffix fgd1 '-tests'
1655       module_deps=$fgd1
1656       ;;
1657   esac
1658   # Then the explicit dependencies listed in the module description.
1659   if $do_cache; then
1660     func_cache_lookup_module "$1"
1661     eval "echo \"\$${cachevar}_deps\""
1662   else
1663     func_lookup_file "modules/$1"
1664     sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
1665   fi
1666 }
1667
1668 # func_get_autoconf_early_snippet module
1669 # Input:
1670 # - local_gnulib_dir  from --local-dir
1671 func_get_autoconf_early_snippet ()
1672 {
1673   if $do_cache; then
1674     func_cache_lookup_module "$1"
1675     eval "echo \"\$${cachevar}_config_early\""
1676   else
1677     func_lookup_file "modules/$1"
1678     sed -n -e "/^configure\.ac-early$sed_extract_prog" < "$lookedup_file"
1679   fi
1680 }
1681
1682 # func_get_autoconf_snippet module
1683 # Input:
1684 # - local_gnulib_dir  from --local-dir
1685 func_get_autoconf_snippet ()
1686 {
1687   if $do_cache; then
1688     func_cache_lookup_module "$1"
1689     eval "echo \"\$${cachevar}_config\""
1690   else
1691     func_lookup_file "modules/$1"
1692     sed -n -e "/^configure\.ac$sed_extract_prog" < "$lookedup_file"
1693   fi
1694 }
1695
1696 # func_get_automake_snippet module
1697 # Input:
1698 # - local_gnulib_dir  from --local-dir
1699 func_get_automake_snippet ()
1700 {
1701   if $do_cache; then
1702     func_cache_lookup_module "$1"
1703     eval "echo \"\$${cachevar}_makefile\""
1704   else
1705     func_lookup_file "modules/$1"
1706     sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file"
1707   fi
1708   case "$1" in
1709     *-tests)
1710       # *-tests module live in tests/, not lib/.
1711       # Synthesize an EXTRA_DIST augmentation.
1712       all_files=`func_get_filelist $1`
1713       func_filter_filelist tests_files " " "$all_files" 'tests/' '' 'tests/' ''
1714       extra_files="$tests_files"
1715       if test -n "$extra_files"; then
1716         echo "EXTRA_DIST +=" $extra_files
1717         echo
1718       fi
1719       ;;
1720     *)
1721       # Synthesize an EXTRA_DIST augmentation.
1722       sed_combine_lines='/\\$/{
1723         :a
1724         N
1725         s/\\\n/ /
1726         s/\\$/\\/
1727         ta
1728       }'
1729       sed_extract_mentioned_files='s/^lib_SOURCES[       ]*+=[   ]*//p'
1730       if $do_cache; then
1731         already_mentioned_files=` \
1732           { eval 'echo "$'${cachevar}'_makefile"'; echo; } \
1733           | sed -e "$sed_combine_lines" \
1734           | sed -n -e "$sed_extract_mentioned_files" | sed -e 's/#.*//'`
1735       else
1736         already_mentioned_files=` \
1737           sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file" \
1738           | sed -e "$sed_combine_lines" \
1739           | sed -n -e "$sed_extract_mentioned_files" | sed -e 's/#.*//'`
1740       fi
1741       all_files=`func_get_filelist $1`
1742       func_filter_filelist lib_files "$nl" "$all_files" 'lib/' '' 'lib/' ''
1743       # Remove $already_mentioned_files from $lib_files.
1744       echo "$lib_files" | LC_ALL=C sort -u > "$tmp"/lib-files
1745       extra_files=`func_reset_sigpipe; \
1746                    for f in $already_mentioned_files; do echo $f; done \
1747                    | LC_ALL=C sort -u | LC_ALL=C join -v 2 - "$tmp"/lib-files`
1748       if test -n "$extra_files"; then
1749         echo "EXTRA_DIST +=" $extra_files
1750         echo
1751       fi
1752       # Synthesize also an EXTRA_lib_SOURCES augmentation.
1753       # This is necessary so that automake can generate the right list of
1754       # dependency rules.
1755       # A possible approach would be to use autom4te --trace of the redefined
1756       # AC_LIBOBJ and AC_REPLACE_FUNCS macros when creating the Makefile.am
1757       # (use autom4te --trace, not just grep, so that AC_LIBOBJ invocations
1758       # inside autoconf's built-in macros are not missed).
1759       # But it's simpler and more robust to do it here, based on the file list.
1760       # If some .c file exists and is not used with AC_LIBOBJ - for example,
1761       # a .c file is preprocessed into another .c file for BUILT_SOURCES -,
1762       # automake will generate a useless dependency; this is harmless.
1763       case "$1" in
1764         relocatable-prog-wrapper) ;;
1765         *)
1766           func_filter_filelist extra_files "$nl" "$extra_files" '' '.c' '' ''
1767           if test -n "$extra_files"; then
1768             echo "EXTRA_lib_SOURCES +=" $extra_files
1769             echo
1770           fi
1771           ;;
1772       esac
1773       # Synthesize an EXTRA_DIST augmentation also for the files in build-aux/.
1774       func_filter_filelist buildaux_files "$nl" "$all_files" 'build-aux/' '' 'build-aux/' ''
1775       if test -n "$buildaux_files"; then
1776         sed_prepend_auxdir='s,^,$(top_srcdir)/'"$auxdir"'/,'
1777         echo "EXTRA_DIST += "`echo "$buildaux_files" | sed -e "$sed_prepend_auxdir"`
1778         echo
1779       fi
1780       # Synthesize an EXTRA_DIST augmentation also for the files from top/.
1781       func_filter_filelist top_files "$nl" "$all_files" 'top/' '' 'top/' ''
1782       if test -n "$top_files"; then
1783         sed_prepend_topdir='s,^,$(top_srcdir)/,'
1784         echo "EXTRA_DIST += "`echo "$top_files" | sed -e "$sed_prepend_topdir"`
1785         echo
1786       fi
1787       ;;
1788   esac
1789 }
1790
1791 # func_get_include_directive module
1792 # Input:
1793 # - local_gnulib_dir  from --local-dir
1794 func_get_include_directive ()
1795 {
1796   if $do_cache; then
1797     func_cache_lookup_module "$1"
1798     eval "echo \"\$${cachevar}_include\"" | \
1799     sed -e 's/^\(["<]\)/#include \1/'
1800   else
1801     func_lookup_file "modules/$1"
1802     sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
1803     sed -e 's/^\(["<]\)/#include \1/'
1804   fi
1805 }
1806
1807 # func_get_link_directive module
1808 # Input:
1809 # - local_gnulib_dir  from --local-dir
1810 func_get_link_directive ()
1811 {
1812   if $do_cache; then
1813     func_cache_lookup_module "$1"
1814     eval "echo \"\$${cachevar}_link\""
1815   else
1816     func_lookup_file "modules/$1"
1817     sed -n -e "/^Link$sed_extract_prog" < "$lookedup_file"
1818   fi
1819 }
1820
1821 # func_get_license module
1822 # Input:
1823 # - local_gnulib_dir  from --local-dir
1824 func_get_license ()
1825 {
1826   {
1827     if $do_cache; then
1828       func_cache_lookup_module "$1"
1829       eval "echo \"\$${cachevar}_license\""
1830     else
1831       func_lookup_file "modules/$1"
1832       sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
1833     fi
1834     # The default is GPL.
1835     echo "GPL"
1836   } | sed -e 's,^ *$,,' | sed -e 1q
1837 }
1838
1839 # func_get_maintainer module
1840 # Input:
1841 # - local_gnulib_dir  from --local-dir
1842 func_get_maintainer ()
1843 {
1844   if $do_cache; then
1845     func_cache_lookup_module "$1"
1846     eval "echo \"\$${cachevar}_maint\""
1847   else
1848     func_lookup_file "modules/$1"
1849     sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
1850   fi
1851 }
1852
1853 # func_get_tests_module module
1854 # Input:
1855 # - local_gnulib_dir  from --local-dir
1856 func_get_tests_module ()
1857 {
1858   # The naming convention for tests modules is hardwired: ${module}-tests.
1859   if test -f "$gnulib_dir/modules/$1"-tests \
1860      || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
1861           && test -f "$local_gnulib_dir/modules/$1"-tests; }; then
1862     echo "$1"-tests
1863   fi
1864 }
1865
1866 # func_acceptable module
1867 # tests whether a module is acceptable.
1868 # Input:
1869 # - avoidlist       list of modules to avoid
1870 func_acceptable ()
1871 {
1872   for avoid in $avoidlist; do
1873     if test "$avoid" = "$1"; then
1874       return 1
1875     fi
1876   done
1877   return 0
1878 }
1879
1880 # func_modules_transitive_closure
1881 # Input:
1882 # - local_gnulib_dir  from --local-dir
1883 # - modules         list of specified modules
1884 # - inctests        true if tests should be included, blank otherwise
1885 # - incobsolete     true if obsolete modules among dependencies should be
1886 #                   included, blank otherwise
1887 # - avoidlist       list of modules to avoid
1888 # - tmp             pathname of a temporary directory
1889 # Output:
1890 # - modules         list of modules, including dependencies
1891 func_modules_transitive_closure ()
1892 {
1893   # In order to process every module only once (for speed), process an "input
1894   # list" of modules, producing an "output list" of modules. During each round,
1895   # more modules can be queued in the input list. Once a module on the input
1896   # list has been processed, it is added to the "handled list", so we can avoid
1897   # to process it again.
1898   handledmodules=
1899   inmodules="$modules"
1900   outmodules=
1901   while test -n "$inmodules"; do
1902     inmodules_this_round="$inmodules"
1903     inmodules=                    # Accumulator, queue for next round
1904     for module in $inmodules_this_round; do
1905       func_verify_module
1906       if test -n "$module"; then
1907         if func_acceptable $module; then
1908           func_append outmodules " $module"
1909           deps=`func_get_dependencies $module`
1910           # Duplicate dependencies are harmless, but Jim wants a warning.
1911           duplicated_deps=`echo "$deps" | LC_ALL=C sort | LC_ALL=C uniq -d`
1912           if test -n "$duplicated_deps"; then
1913             func_warning "module $module has duplicated dependencies: "`echo $duplicated_deps`
1914           fi
1915           for dep in $deps; do
1916             if test -n "$incobsolete" \
1917                || { status=`func_get_status $dep`; test "$status" != obsolete; }; then
1918               func_append inmodules " $dep"
1919             fi
1920           done
1921           if test -n "$inctests"; then
1922             testsmodule=`func_get_tests_module $module`
1923             if test -n "$testsmodule"; then
1924               func_append inmodules " $testsmodule"
1925             fi
1926           fi
1927         fi
1928       fi
1929     done
1930     handledmodules=`for m in $handledmodules $inmodules_this_round; do echo $m; done | LC_ALL=C sort -u`
1931     # Remove $handledmodules from $inmodules.
1932     for m in $inmodules; do echo $m; done | LC_ALL=C sort -u > "$tmp"/queued-modules
1933     inmodules=`func_reset_sigpipe; echo "$handledmodules" | LC_ALL=C join -v 2 - "$tmp"/queued-modules`
1934   done
1935   modules=`for m in $outmodules; do echo $m; done | LC_ALL=C sort -u`
1936   rm -f "$tmp"/queued-modules
1937 }
1938
1939 # func_modules_add_dummy
1940 # Input:
1941 # - local_gnulib_dir  from --local-dir
1942 # - modules         list of modules, including dependencies
1943 # Output:
1944 # - modules         list of modules, including 'dummy' if needed
1945 func_modules_add_dummy ()
1946 {
1947   # Determine whether any module provides a lib_SOURCES augmentation.
1948   have_lib_SOURCES=
1949   sed_remove_backslash_newline=':a
1950 /\\$/{
1951 s/\\$//
1952 N
1953 s/\n//
1954 ba
1955 }'
1956   for module in $modules; do
1957     func_verify_nontests_module
1958     if test -n "$module"; then
1959       # Extract the value of "lib_SOURCES += ...".
1960       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
1961         # Ignore .h files since they are not compiled.
1962         case "$file" in
1963           *.h) ;;
1964           *)
1965             have_lib_SOURCES=yes
1966             break 2
1967             ;;
1968         esac
1969       done
1970     fi
1971   done
1972   # Add the dummy module, to make sure the library will be non-empty.
1973   if test -z "$have_lib_SOURCES"; then
1974     if func_acceptable "dummy"; then
1975       func_append modules " dummy"
1976     fi
1977   fi
1978 }
1979
1980 # func_modules_notice
1981 # Input:
1982 # - local_gnulib_dir  from --local-dir
1983 # - verbose         integer, default 0, inc/decremented by --verbose/--quiet
1984 # - modules         list of modules, including dependencies
1985 func_modules_notice ()
1986 {
1987   if test $verbose -ge -1; then
1988     for module in $modules; do
1989       func_verify_module
1990       if test -n "$module"; then
1991         msg=`func_get_notice $module`
1992         if test -n "$msg"; then
1993           echo "Notice from module $module:"
1994           echo "$msg" | sed -e 's/^/  /'
1995         fi
1996       fi
1997     done
1998   fi
1999 }
2000
2001 # func_modules_to_filelist
2002 # Input:
2003 # - local_gnulib_dir  from --local-dir
2004 # - modules         list of modules, including dependencies
2005 # Output:
2006 # - files           list of files
2007 func_modules_to_filelist ()
2008 {
2009   files=
2010   for module in $modules; do
2011     func_verify_module
2012     if test -n "$module"; then
2013       fs=`func_get_filelist $module`
2014       func_append files " $fs"
2015     fi
2016   done
2017   files=`for f in $files; do echo $f; done | LC_ALL=C sort -u`
2018 }
2019
2020 # func_execute_command command [args...]
2021 # Executes a command.
2022 # Uses also the variables
2023 # - verbose         integer, default 0, inc/decremented by --verbose/--quiet
2024 func_execute_command ()
2025 {
2026   if test $verbose -ge 0; then
2027     echo "executing $*"
2028     "$@"
2029   else
2030     # Commands like automake produce output to stderr even when they succeed.
2031     # Turn this output off if the command succeeds.
2032     "$@" > "$tmp"/cmdout 2>&1
2033     cmdret=$?
2034     if test $cmdret = 0; then
2035       rm -f "$tmp"/cmdout
2036     else
2037       echo "executing $*"
2038       cat "$tmp"/cmdout 1>&2
2039       rm -f "$tmp"/cmdout
2040       (exit $cmdret)
2041     fi
2042   fi
2043 }
2044
2045 # func_dest_tmpfilename file
2046 # determines the name of a temporary file (file is relative to destdir).
2047 # Input:
2048 # - destdir         target directory
2049 # - doit            : if actions shall be executed, false if only to be printed
2050 # - tmp             pathname of a temporary directory
2051 # Sets variable:
2052 #   - tmpfile       absolute filename of the temporary file
2053 func_dest_tmpfilename ()
2054 {
2055   if $doit; then
2056     # Put the new contents of $file in a file in the same directory (needed
2057     # to guarantee that an 'mv' to "$destdir/$file" works).
2058     tmpfile="$destdir/$1.tmp"
2059   else
2060     # Put the new contents of $file in a file in a temporary directory
2061     # (because the directory of "$file" might not exist).
2062     tmpfile="$tmp"/`basename "$1"`.tmp
2063   fi
2064 }
2065
2066 # func_add_file
2067 # copies a file from gnulib into the destination directory. The destination
2068 # is known to not exist.
2069 # Input:
2070 # - destdir         target directory
2071 # - local_gnulib_dir  from --local-dir
2072 # - f               the original file name
2073 # - lookedup_file   name of the merged (combined) file
2074 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
2075 # - g               the rewritten file name
2076 # - tmpfile         absolute filename of the temporary file
2077 # - doit            : if actions shall be executed, false if only to be printed
2078 # - symbolic        true if files should be symlinked, copied otherwise
2079 # - lsymbolic       true if files from local_gnulib_dir should be symlinked,
2080 #                   copied otherwise
2081 func_add_file ()
2082 {
2083   if $doit; then
2084     echo "Copying file $g"
2085     if { test -n "$symbolic" \
2086          || { test -n "$lsymbolic" \
2087               && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
2088        && test -z "$lookedup_tmp" \
2089        && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
2090       func_ln_if_changed "$lookedup_file" "$destdir/$g"
2091     else
2092       mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
2093     fi
2094   else
2095     echo "Copy file $g"
2096   fi
2097 }
2098
2099 # func_update_file
2100 # copies a file from gnulib into the destination directory. The destination
2101 # is known to exist.
2102 # Input:
2103 # - destdir         target directory
2104 # - local_gnulib_dir  from --local-dir
2105 # - f               the original file name
2106 # - lookedup_file   name of the merged (combined) file
2107 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
2108 # - g               the rewritten file name
2109 # - tmpfile         absolute filename of the temporary file
2110 # - doit            : if actions shall be executed, false if only to be printed
2111 # - symbolic        true if files should be symlinked, copied otherwise
2112 # - lsymbolic       true if files from local_gnulib_dir should be symlinked,
2113 #                   copied otherwise
2114 # - already_present  nonempty if the file should already exist, empty otherwise
2115 func_update_file ()
2116 {
2117   if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
2118     : # The file has not changed.
2119   else
2120     # Replace the file.
2121     if $doit; then
2122       if test -n "$already_present"; then
2123         echo "Updating file $g (backup in ${g}~)"
2124       else
2125         echo "Replacing file $g (non-gnulib code backed up in ${g}~) !!"
2126       fi
2127       mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
2128       if { test -n "$symbolic" \
2129            || { test -n "$lsymbolic" \
2130                 && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
2131          && test -z "$lookedup_tmp" \
2132          && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
2133         func_ln_if_changed "$lookedup_file" "$destdir/$g"
2134       else
2135         mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
2136       fi
2137     else
2138       if test -n "$already_present"; then
2139         echo "Update file $g (backup in ${g}~)"
2140       else
2141         echo "Replace file $g (non-gnulib code backed up in ${g}~) !!"
2142       fi
2143     fi
2144   fi
2145 }
2146
2147 # func_emit_lib_Makefile_am
2148 # emits the contents of library makefile to standard output.
2149 # Input:
2150 # - local_gnulib_dir  from --local-dir
2151 # - modules         list of modules, including dependencies
2152 # - libname         library name
2153 # - pobase          directory relative to destdir where to place *.po files
2154 # - auxdir          directory relative to destdir where to place build aux files
2155 # - makefile_name   from --makefile-name
2156 # - libtool         true if libtool will be used, false or blank otherwise
2157 # - macro_prefix    prefix of gl_LIBOBJS macros to use
2158 # - po_domain       prefix of i18n domain to use (without -gnulib suffix)
2159 # - actioncmd       (optional) command that will reproduce this invocation
2160 # - for_test        true if creating a package for testing, false otherwise
2161 # - destfile        filename relative to destdir of makefile being generated
2162 # Input/Output:
2163 # - makefile_am_edits and makefile_am_edit${edit}_{dir,var,val}
2164 #                   list of edits to be done to Makefile.am variables
2165 # Output:
2166 # - uses_subdirs    nonempty if object files in subdirs exist
2167 func_emit_lib_Makefile_am ()
2168 {
2169   # When creating an includable Makefile.am snippet, augment variables with
2170   # += instead of assigning them.
2171   if test -n "$makefile_name"; then
2172     assign='+='
2173   else
2174     assign='='
2175   fi
2176   if test "$libtool" = true; then
2177     libext=la
2178     perhapsLT=LT
2179     sed_eliminate_LDFLAGS="$sed_noop"
2180   else
2181     libext=a
2182     perhapsLT=
2183     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
2184   fi
2185   if $for_test; then
2186     # When creating a package for testing: Attempt to provoke failures,
2187     # especially link errors, already during "make" rather than during
2188     # "make check", because "make check" is not possible in a cross-compiling
2189     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
2190     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
2191   else
2192     sed_transform_check_PROGRAMS="$sed_noop"
2193   fi
2194   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
2195   echo "## Process this file with automake to produce Makefile.in."
2196   func_emit_copyright_notice
2197   if test -n "$actioncmd"; then
2198     # The maximum line length (excluding the terminating newline) of any file
2199     # that is to be preprocessed by config.status is 3070.  config.status uses
2200     # awk, and the HP-UX 11.00 awk fails if a line has length >= 3071;
2201     # similarly, the IRIX 6.5 awk fails if a line has length >= 3072.
2202     len=`echo "$actioncmd" | wc -c`
2203     if test -n "$len" && test "$len" -le 3000; then
2204       echo "# Reproduce by: $actioncmd"
2205     fi
2206   fi
2207   echo
2208   uses_subdirs=
2209   {
2210     for module in $modules; do
2211       func_verify_nontests_module
2212       if test -n "$module"; then
2213         {
2214           func_get_automake_snippet "$module" |
2215             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
2216                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' \
2217                 -e "$sed_eliminate_LDFLAGS" \
2218                 -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' \
2219                 -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
2220                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' \
2221                 -e "$sed_transform_check_PROGRAMS"
2222           if test "$module" = 'alloca'; then
2223             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
2224             echo "${libname}_${libext}_DEPENDENCIES += @${perhapsLT}ALLOCA@"
2225           fi
2226         } > "$tmp"/amsnippet
2227         # Skip the contents if it's entirely empty.
2228         if grep '[^      ]' "$tmp"/amsnippet > /dev/null ; then
2229           echo "## begin gnulib module $module"
2230           echo
2231           cat "$tmp"/amsnippet
2232           echo "## end   gnulib module $module"
2233           echo
2234         fi
2235         rm -f "$tmp"/amsnippet
2236         # Test whether there are some source files in subdirectories.
2237         for f in `func_get_filelist "$module"`; do
2238           case $f in
2239             lib/*/*.c)
2240               uses_subdirs=yes
2241               break
2242               ;;
2243           esac
2244         done
2245       fi
2246     done
2247   } > "$tmp"/allsnippets
2248   if test -z "$makefile_name"; then
2249     # If there are source files in subdirectories, prevent collision of the
2250     # object files (example: hash.c and libxml/hash.c).
2251     subdir_options=
2252     if test -n "$uses_subdirs"; then
2253       subdir_options=' subdir-objects'
2254     fi
2255     echo "AUTOMAKE_OPTIONS = 1.5 gnits${subdir_options}"
2256   fi
2257   echo
2258   if test -z "$makefile_name"; then
2259     echo "SUBDIRS ="
2260     echo "noinst_HEADERS ="
2261     echo "noinst_LIBRARIES ="
2262     echo "noinst_LTLIBRARIES ="
2263     # Automake versions < 1.9b create an empty pkgdatadir at installation time
2264     # if you specify pkgdata_DATA to empty. This is a workaround.
2265     if grep '^pkgdata_DATA *+=' "$tmp"/allsnippets > /dev/null; then
2266       echo "pkgdata_DATA ="
2267     fi
2268     echo "EXTRA_DIST ="
2269     echo "BUILT_SOURCES ="
2270     echo "SUFFIXES ="
2271   fi
2272   echo "MOSTLYCLEANFILES $assign core *.stackdump"
2273   if test -z "$makefile_name"; then
2274     echo "MOSTLYCLEANDIRS ="
2275     echo "CLEANFILES ="
2276     echo "DISTCLEANFILES ="
2277     echo "MAINTAINERCLEANFILES ="
2278   fi
2279   # Execute edits that apply to the Makefile.am being generated.
2280   edit=0
2281   while test $edit != $makefile_am_edits; do
2282     edit=`expr $edit + 1`
2283     eval dir=\"\$makefile_am_edit${edit}_dir\"
2284     eval var=\"\$makefile_am_edit${edit}_var\"
2285     eval val=\"\$makefile_am_edit${edit}_val\"
2286     if test -n "$var"; then
2287       if test "${dir}Makefile.am" = "$destfile" || test "./${dir}Makefile.am" = "$destfile"; then
2288         echo "${var} += ${val}"
2289         eval "makefile_am_edit${edit}_var="
2290       fi
2291     fi
2292   done
2293   if test -z "$makefile_name"; then
2294     echo
2295     echo "AM_CPPFLAGS ="
2296     echo "AM_CFLAGS ="
2297   fi
2298   echo
2299   if LC_ALL=C grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *+\{0,1\}= *$libname\\.$libext\$" "$tmp"/allsnippets > /dev/null \
2300      || { test -n "$makefile_name" \
2301           && test -f "$sourcebase/Makefile.am" \
2302           && LC_ALL=C grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *+\{0,1\}= *$libname\\.$libext\$" "$sourcebase/Makefile.am" > /dev/null; \
2303         }; then
2304     # One of the snippets or the user's Makefile.am already specifies an
2305     # installation location for the library. Don't confuse automake by saying
2306     # it should not be installed.
2307     :
2308   else
2309     # By default, the generated library should not be installed.
2310     echo "noinst_${perhapsLT}LIBRARIES += $libname.$libext"
2311   fi
2312   echo
2313   echo "${libname}_${libext}_SOURCES ="
2314   # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
2315   # automake during its analysis looks for $(LIBOBJS), not for @LIBOBJS@.
2316   echo "${libname}_${libext}_LIBADD = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
2317   echo "${libname}_${libext}_DEPENDENCIES = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
2318   echo "EXTRA_${libname}_${libext}_SOURCES ="
2319   if test "$libtool" = true; then
2320     echo "${libname}_${libext}_LDFLAGS = \$(AM_LDFLAGS)"
2321   fi
2322   echo
2323   if test -n "$pobase"; then
2324     echo "AM_CPPFLAGS += -DDEFAULT_TEXT_DOMAIN=\\\"${po_domain}-gnulib\\\""
2325     echo
2326   fi
2327   cat "$tmp"/allsnippets \
2328     | sed -e 's|\$(top_srcdir)/build-aux/|$(top_srcdir)/'"$auxdir"'/|g'
2329   echo
2330   echo "mostlyclean-local: mostlyclean-generic"
2331   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
2332   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
2333   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
2334   echo "          fi; \\"
2335   echo "        done; \\"
2336   echo "        :"
2337   rm -f "$tmp"/allsnippets
2338 }
2339
2340 # func_emit_po_Makevars
2341 # emits the contents of po/ makefile parameterization to standard output.
2342 # Input:
2343 # - local_gnulib_dir  from --local-dir
2344 # - sourcebase      directory relative to destdir where to place source code
2345 # - pobase          directory relative to destdir where to place *.po files
2346 # - po_domain       prefix of i18n domain to use (without -gnulib suffix)
2347 func_emit_po_Makevars ()
2348 {
2349   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
2350   func_emit_copyright_notice
2351   echo
2352   echo "# Usually the message domain is the same as the package name."
2353   echo "# But here it has a '-gnulib' suffix."
2354   echo "DOMAIN = ${po_domain}-gnulib"
2355   echo
2356   echo "# These two variables depend on the location of this directory."
2357   echo "subdir = ${pobase}"
2358   echo "top_builddir = "`echo "$pobase" | sed -e 's,//*,/,g' -e 's,[^/][^/]*,..,g'`
2359   echo
2360   cat <<\EOF
2361 # These options get passed to xgettext.
2362 XGETTEXT_OPTIONS = \
2363   --keyword=_ --flag=_:1:pass-c-format \
2364   --keyword=N_ --flag=N_:1:pass-c-format \
2365   --keyword='proper_name:1,"This is a proper name. See the gettext manual, section Names."' \
2366   --keyword='proper_name_utf8:1,"This is a proper name. See the gettext manual, section Names."' \
2367   --flag=error:3:c-format --flag=error_at_line:5:c-format
2368
2369 # This is the copyright holder that gets inserted into the header of the
2370 # $(DOMAIN).pot file.  gnulib is copyrighted by the FSF.
2371 COPYRIGHT_HOLDER = Free Software Foundation, Inc.
2372
2373 # This is the email address or URL to which the translators shall report
2374 # bugs in the untranslated strings:
2375 # - Strings which are not entire sentences, see the maintainer guidelines
2376 #   in the GNU gettext documentation, section 'Preparing Strings'.
2377 # - Strings which use unclear terms or require additional context to be
2378 #   understood.
2379 # - Strings which make invalid assumptions about notation of date, time or
2380 #   money.
2381 # - Pluralisation problems.
2382 # - Incorrect English spelling.
2383 # - Incorrect formatting.
2384 # It can be your email address, or a mailing list address where translators
2385 # can write to without being subscribed, or the URL of a web page through
2386 # which the translators can contact you.
2387 MSGID_BUGS_ADDRESS = bug-gnulib@gnu.org
2388
2389 # This is the list of locale categories, beyond LC_MESSAGES, for which the
2390 # message catalogs shall be used.  It is usually empty.
2391 EXTRA_LOCALE_CATEGORIES =
2392
2393 # This tells whether the $(DOMAIN).pot file contains messages with an 'msgctxt'
2394 # context.  Possible values are "yes" and "no".  Set this to yes if the
2395 # package uses functions taking also a message context, like pgettext(), or
2396 # if in $(XGETTEXT_OPTIONS) you define keywords with a context argument.
2397 USE_MSGCTXT = no
2398 EOF
2399 }
2400
2401 # func_emit_po_POTFILES_in
2402 # emits the file list to be passed to xgettext to standard output.
2403 # Input:
2404 # - local_gnulib_dir  from --local-dir
2405 # - sourcebase      directory relative to destdir where to place source code
2406 # - files           list of new files
2407 func_emit_po_POTFILES_in ()
2408 {
2409   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
2410   func_emit_copyright_notice
2411   echo
2412   echo "# List of files which contain translatable strings."
2413   echo "$files" | sed -n -e "s,^lib/,$sourcebase/,p"
2414 }
2415
2416 # func_emit_tests_Makefile_am
2417 # emits the contents of tests makefile to standard output.
2418 # Input:
2419 # - local_gnulib_dir  from --local-dir
2420 # - modules         list of modules, including dependencies
2421 # - libname         library name
2422 # - auxdir          directory relative to destdir where to place build aux files
2423 # - makefile_name   from --makefile-name
2424 # - libtool         true if libtool will be used, false or blank otherwise
2425 # - sourcebase      relative directory containing lib source code
2426 # - m4base          relative directory containing autoconf macros
2427 # - testsbase       relative directory containing unit test code
2428 # - macro_prefix    prefix of gl_LIBOBJS macros to use
2429 # - for_test        true if creating a package for testing, false otherwise
2430 # - use_libtests    true if a libtests.a should be built, false otherwise
2431 # - destfile        filename relative to destdir of makefile being generated
2432 # Input/Output:
2433 # - makefile_am_edits and makefile_am_edit${edit}_{dir,var,val}
2434 #                   list of edits to be done to Makefile.am variables
2435 # Output:
2436 # - uses_subdirs    nonempty if object files in subdirs exist
2437 func_emit_tests_Makefile_am ()
2438 {
2439   if test "$libtool" = true; then
2440     libext=la
2441     sed_eliminate_LDFLAGS="$sed_noop"
2442   else
2443     libext=a
2444     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
2445   fi
2446   if $for_test; then
2447     # When creating a package for testing: Attempt to provoke failures,
2448     # especially link errors, already during "make" rather than during
2449     # "make check", because "make check" is not possible in a cross-compiling
2450     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
2451     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
2452   else
2453     sed_transform_check_PROGRAMS="$sed_noop"
2454   fi
2455   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
2456   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
2457   echo "## Process this file with automake to produce Makefile.in."
2458   func_emit_copyright_notice
2459   echo
2460   uses_subdirs=
2461   {
2462     for module in $modules; do
2463       if $for_test; then
2464         func_verify_tests_module
2465       else
2466         func_verify_module
2467       fi
2468       if test -n "$module"; then
2469         {
2470           func_get_automake_snippet "$module" |
2471             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
2472                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' \
2473                 -e "$sed_eliminate_LDFLAGS" \
2474                 -e 's,lib_\([A-Z][A-Z]*\),libtests_a_\1,g' \
2475                 -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
2476                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' \
2477                 -e "$sed_transform_check_PROGRAMS"
2478           if $use_libtests && test "$module" = 'alloca'; then
2479             echo "libtests_a_LIBADD += @${perhapsLT}ALLOCA@"
2480             echo "libtests_a_DEPENDENCIES += @${perhapsLT}ALLOCA@"
2481           fi
2482         } > "$tmp"/amsnippet
2483         # Skip the contents if it's entirely empty.
2484         if grep '[^      ]' "$tmp"/amsnippet > /dev/null ; then
2485           echo "## begin gnulib module $module"
2486           echo
2487           cat "$tmp"/amsnippet
2488           echo "## end   gnulib module $module"
2489           echo
2490         fi
2491         rm -f "$tmp"/amsnippet
2492         # Test whether there are some source files in subdirectories.
2493         for f in `func_get_filelist "$module"`; do
2494           case $f in
2495             lib/*/*.c | tests/*/*.c)
2496               uses_subdirs=yes
2497               break
2498               ;;
2499           esac
2500         done
2501       fi
2502     done
2503   } > "$tmp"/allsnippets
2504   # Generate dependencies here, since it eases the debugging of test failures.
2505   # If there are source files in subdirectories, prevent collision of the
2506   # object files (example: hash.c and libxml/hash.c).
2507   subdir_options=
2508   if test -n "$uses_subdirs"; then
2509     subdir_options=' subdir-objects'
2510   fi
2511   echo "AUTOMAKE_OPTIONS = 1.5 foreign${subdir_options}"
2512   echo
2513   if $for_test; then
2514     echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
2515     echo
2516   fi
2517   # Nothing is being added to SUBDIRS; nevertheless the existence of this
2518   # variable is needed to avoid an error from automake:
2519   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
2520   echo "SUBDIRS ="
2521   echo "TESTS ="
2522   echo "XFAIL_TESTS ="
2523   echo "TESTS_ENVIRONMENT ="
2524   echo "noinst_PROGRAMS ="
2525   if ! $for_test; then
2526     echo "check_PROGRAMS ="
2527   fi
2528   echo "noinst_HEADERS ="
2529   echo "noinst_LIBRARIES ="
2530   if $use_libtests; then
2531     if $for_test; then
2532       echo "noinst_LIBRARIES += libtests.a"
2533     else
2534       echo "check_LIBRARIES = libtests.a"
2535     fi
2536   fi
2537   # Automake versions < 1.9b create an empty pkgdatadir at installation time
2538   # if you specify pkgdata_DATA to empty. This is a workaround.
2539   if grep '^pkgdata_DATA *+=' "$tmp"/allsnippets > /dev/null; then
2540     echo "pkgdata_DATA ="
2541   fi
2542   echo "EXTRA_DIST ="
2543   echo "BUILT_SOURCES ="
2544   echo "SUFFIXES ="
2545   echo "MOSTLYCLEANFILES = core *.stackdump"
2546   echo "MOSTLYCLEANDIRS ="
2547   echo "CLEANFILES ="
2548   echo "DISTCLEANFILES ="
2549   echo "MAINTAINERCLEANFILES ="
2550   # Execute edits that apply to the Makefile.am being generated.
2551   edit=0
2552   while test $edit != $makefile_am_edits; do
2553     edit=`expr $edit + 1`
2554     eval dir=\"\$makefile_am_edit${edit}_dir\"
2555     eval var=\"\$makefile_am_edit${edit}_var\"
2556     eval val=\"\$makefile_am_edit${edit}_val\"
2557     if test -n "$var"; then
2558       if test "${dir}Makefile.am" = "$destfile" || test "./${dir}Makefile.am" = "$destfile"; then
2559         echo "${var} += ${val}"
2560         eval "makefile_am_edit${edit}_var="
2561       fi
2562     fi
2563   done
2564   echo
2565   echo "AM_CPPFLAGS = \\"
2566   echo "  -I. -I\$(srcdir) \\"
2567   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
2568   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
2569   echo
2570   local_ldadd_before=''
2571   local_ldadd_after=''
2572   if $use_libtests; then
2573     # All test programs need to be linked with libtests.a.
2574     # It needs to be passed to the linker before ${libname}.${libext}, since
2575     # the tests-related modules depend on the main modules.
2576     # It also needs to be passed to the linker after ${libname}.${libext}
2577     # because the latter might contain incomplete modules (such as the 'error'
2578     # module whose dependency to 'progname' is voluntarily omitted).
2579     # The LIBTESTS_LIBDEPS can be passed to the linker once or twice, it does
2580     # not matter.
2581     local_ldadd_before=' libtests.a'
2582     local_ldadd_after=' libtests.a $(LIBTESTS_LIBDEPS)'
2583   fi
2584   echo "LDADD =${local_ldadd_before} ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}${local_ldadd_after}"
2585   echo
2586   if $use_libtests; then
2587     echo "libtests_a_SOURCES ="
2588     # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
2589     # automake during its analysis looks for $(LIBOBJS), not for @LIBOBJS@.
2590     echo "libtests_a_LIBADD = \$(${macro_prefix}tests_LIBOBJS)"
2591     echo "libtests_a_DEPENDENCIES = \$(${macro_prefix}tests_LIBOBJS)"
2592     echo "EXTRA_libtests_a_SOURCES ="
2593     # The circular dependency in LDADD requires this.
2594     echo "AM_LIBTOOLFLAGS = --preserve-dup-deps"
2595     echo
2596   fi
2597   # Many test scripts use ${EXEEXT} or ${srcdir}.
2598   # EXEEXT is defined by AC_PROG_CC through autoconf.
2599   # srcdir is defined by autoconf and automake.
2600   echo "TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@' srcdir='\$(srcdir)'"
2601   echo
2602   cat "$tmp"/allsnippets \
2603     | sed -e 's|\$(top_srcdir)/build-aux/|$(top_srcdir)/'"$auxdir"'/|g'
2604   echo "# Clean up after Solaris cc."
2605   echo "clean-local:"
2606   echo "        rm -rf SunWS_cache"
2607   echo
2608   echo "mostlyclean-local: mostlyclean-generic"
2609   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
2610   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
2611   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
2612   echo "          fi; \\"
2613   echo "        done; \\"
2614   echo "        :"
2615   rm -f "$tmp"/allsnippets
2616 }
2617
2618 # func_emit_initmacro_start macro_prefix
2619 # emits the first few statements of the gl_INIT macro to standard output.
2620 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
2621 func_emit_initmacro_start ()
2622 {
2623   macro_prefix_arg="$1"
2624   # Overriding AC_LIBOBJ and AC_REPLACE_FUNCS has the effect of storing
2625   # platform-dependent object files in ${macro_prefix_arg}_LIBOBJS instead of
2626   # LIBOBJS.  The purpose is to allow several gnulib instantiations under
2627   # a single configure.ac file.  (AC_CONFIG_LIBOBJ_DIR does not allow this
2628   # flexibility.)
2629   # Furthermore it avoids an automake error like this when a Makefile.am
2630   # that uses pieces of gnulib also uses $(LIBOBJ):
2631   #   automatically discovered file `error.c' should not be explicitly mentioned
2632   echo "  m4_pushdef([AC_LIBOBJ], m4_defn([${macro_prefix_arg}_LIBOBJ]))"
2633   echo "  m4_pushdef([AC_REPLACE_FUNCS], m4_defn([${macro_prefix_arg}_REPLACE_FUNCS]))"
2634   # Overriding AC_LIBSOURCES has the same purpose of avoiding the automake
2635   # error when a Makefile.am that uses pieces of gnulib also uses $(LIBOBJ):
2636   #   automatically discovered file `error.c' should not be explicitly mentioned
2637   # We let automake know about the files to be distributed through the
2638   # EXTRA_lib_SOURCES variable.
2639   echo "  m4_pushdef([AC_LIBSOURCES], m4_defn([${macro_prefix_arg}_LIBSOURCES]))"
2640   # Create data variables for checking the presence of files that are mentioned
2641   # as AC_LIBSOURCES arguments. These are m4 variables, not shell variables,
2642   # because we want the check to happen when the configure file is created,
2643   # not when it is run. ${macro_prefix_arg}_LIBSOURCES_LIST is the list of
2644   # files to check for. ${macro_prefix_arg}_LIBSOURCES_DIR is the subdirectory
2645   # in which to expect them.
2646   echo "  m4_pushdef([${macro_prefix_arg}_LIBSOURCES_LIST], [])"
2647   echo "  m4_pushdef([${macro_prefix_arg}_LIBSOURCES_DIR], [])"
2648   echo "  gl_COMMON"
2649 }
2650
2651 # func_emit_initmacro_end macro_prefix
2652 # emits the last few statements of the gl_INIT macro to standard output.
2653 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
2654 func_emit_initmacro_end ()
2655 {
2656   macro_prefix_arg="$1"
2657   # Check the presence of files that are mentioned as AC_LIBSOURCES arguments.
2658   # The check is performed only when autoconf is run from the directory where
2659   # the configure.ac resides; if it is run from a different directory, the
2660   # check is skipped.
2661   echo "  m4_ifval(${macro_prefix_arg}_LIBSOURCES_LIST, ["
2662   echo "    m4_syscmd([test ! -d ]m4_defn([${macro_prefix_arg}_LIBSOURCES_DIR])[ ||"
2663   echo "      for gl_file in ]${macro_prefix_arg}_LIBSOURCES_LIST[ ; do"
2664   echo "        if test ! -r ]m4_defn([${macro_prefix_arg}_LIBSOURCES_DIR])[/\$gl_file ; then"
2665   echo "          echo \"missing file ]m4_defn([${macro_prefix_arg}_LIBSOURCES_DIR])[/\$gl_file\" >&2"
2666   echo "          exit 1"
2667   echo "        fi"
2668   echo "      done])dnl"
2669   echo "      m4_if(m4_sysval, [0], [],"
2670   echo "        [AC_FATAL([expected source file, required through AC_LIBSOURCES, not found])])"
2671   echo "  ])"
2672   echo "  m4_popdef([${macro_prefix_arg}_LIBSOURCES_DIR])"
2673   echo "  m4_popdef([${macro_prefix_arg}_LIBSOURCES_LIST])"
2674   echo "  m4_popdef([AC_LIBSOURCES])"
2675   echo "  m4_popdef([AC_REPLACE_FUNCS])"
2676   echo "  m4_popdef([AC_LIBOBJ])"
2677   echo "  AC_CONFIG_COMMANDS_PRE(["
2678   echo "    ${macro_prefix_arg}_libobjs="
2679   echo "    ${macro_prefix_arg}_ltlibobjs="
2680   echo "    if test -n \"\$${macro_prefix_arg}_LIBOBJS\"; then"
2681   echo "      # Remove the extension."
2682   echo "      sed_drop_objext='s/\\.o\$//;s/\\.obj\$//'"
2683   echo "      for i in \`for i in \$${macro_prefix_arg}_LIBOBJS; do echo \"\$i\"; done | sed -e \"\$sed_drop_objext\" | sort | uniq\`; do"
2684   echo "        ${macro_prefix_arg}_libobjs=\"\$${macro_prefix_arg}_libobjs \$i.\$ac_objext\""
2685   echo "        ${macro_prefix_arg}_ltlibobjs=\"\$${macro_prefix_arg}_ltlibobjs \$i.lo\""
2686   echo "      done"
2687   echo "    fi"
2688   echo "    AC_SUBST([${macro_prefix_arg}_LIBOBJS], [\$${macro_prefix_arg}_libobjs])"
2689   echo "    AC_SUBST([${macro_prefix_arg}_LTLIBOBJS], [\$${macro_prefix_arg}_ltlibobjs])"
2690   echo "  ])"
2691 }
2692
2693 # func_emit_initmacro_done macro_prefix sourcebase
2694 # emits a few statements after the gl_INIT macro to standard output.
2695 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
2696 # - sourcebase      directory relative to destdir where to place source code
2697 func_emit_initmacro_done ()
2698 {
2699   macro_prefix_arg="$1"
2700   sourcebase_arg="$2"
2701   echo
2702   echo "# Like AC_LIBOBJ, except that the module name goes"
2703   echo "# into ${macro_prefix_arg}_LIBOBJS instead of into LIBOBJS."
2704   echo "AC_DEFUN([${macro_prefix_arg}_LIBOBJ], ["
2705   echo "  AS_LITERAL_IF([\$1], [${macro_prefix_arg}_LIBSOURCES([\$1.c])])dnl"
2706   echo "  ${macro_prefix_arg}_LIBOBJS=\"\$${macro_prefix_arg}_LIBOBJS \$1.\$ac_objext\""
2707   echo "])"
2708   echo
2709   echo "# Like AC_REPLACE_FUNCS, except that the module name goes"
2710   echo "# into ${macro_prefix_arg}_LIBOBJS instead of into LIBOBJS."
2711   echo "AC_DEFUN([${macro_prefix_arg}_REPLACE_FUNCS], ["
2712   echo "  m4_foreach_w([gl_NAME], [\$1], [AC_LIBSOURCES(gl_NAME[.c])])dnl"
2713   echo "  AC_CHECK_FUNCS([\$1], , [${macro_prefix_arg}_LIBOBJ(\$ac_func)])"
2714   echo "])"
2715   echo
2716   echo "# Like AC_LIBSOURCES, except the directory where the source file is"
2717   echo "# expected is derived from the gnulib-tool parameterization,"
2718   echo "# and alloca is special cased (for the alloca-opt module)."
2719   echo "# We could also entirely rely on EXTRA_lib..._SOURCES."
2720   echo "AC_DEFUN([${macro_prefix_arg}_LIBSOURCES], ["
2721   echo "  m4_foreach([_gl_NAME], [\$1], ["
2722   echo "    m4_if(_gl_NAME, [alloca.c], [], ["
2723   echo "      m4_define([${macro_prefix_arg}_LIBSOURCES_DIR], [$sourcebase_arg])"
2724   echo "      m4_append([${macro_prefix_arg}_LIBSOURCES_LIST], _gl_NAME, [ ])"
2725   echo "    ])"
2726   echo "  ])"
2727   echo "])"
2728 }
2729
2730 # func_import modules
2731 # Uses also the variables
2732 # - destdir         target directory
2733 # - local_gnulib_dir  from --local-dir
2734 # - verbose         integer, default 0, inc/decremented by --verbose/--quiet
2735 # - libname         library name
2736 # - sourcebase      directory relative to destdir where to place source code
2737 # - m4base          directory relative to destdir where to place *.m4 macros
2738 # - pobase          directory relative to destdir where to place *.po files
2739 # - docbase         directory relative to destdir where to place doc files
2740 # - testsbase       directory relative to destdir where to place unit test code
2741 # - auxdir          directory relative to destdir where to place build aux files
2742 # - inctests        true if --with-tests was given, blank otherwise
2743 # - incobsolete     true if --with-obsolete was given, blank otherwise
2744 # - avoidlist       list of modules to avoid, from --avoid
2745 # - lgpl            yes or a number if library's license shall be LGPL,
2746 #                   blank otherwise
2747 # - makefile_name   from --makefile-name
2748 # - libtool         true if --libtool was given, false if --no-libtool was
2749 #                   given, blank otherwise
2750 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
2751 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
2752 # - po_domain       prefix of i18n domain to use (without -gnulib suffix)
2753 # - vc_files        true if --vc-files was given, false if --no-vc-files was
2754 #                   given, blank otherwise
2755 # - autoconf_minversion  minimum supported autoconf version
2756 # - doit            : if actions shall be executed, false if only to be printed
2757 # - symbolic        true if files should be symlinked, copied otherwise
2758 # - lsymbolic       true if files from local_gnulib_dir should be symlinked,
2759 #                   copied otherwise
2760 # - do_copyrights   true if copyright notices in files should be replaced,
2761 #                   blank otherwise
2762 func_import ()
2763 {
2764   # Get the cached settings.
2765   cached_local_gnulib_dir=
2766   cached_specified_modules=
2767   cached_incobsolete=
2768   cached_avoidlist=
2769   cached_sourcebase=
2770   cached_m4base=
2771   cached_pobase=
2772   cached_docbase=
2773   cached_testsbase=
2774   cached_inctests=
2775   cached_libname=
2776   cached_lgpl=
2777   cached_makefile_name=
2778   cached_libtool=
2779   cached_macro_prefix=
2780   cached_po_domain=
2781   cached_vc_files=
2782   cached_files=
2783   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
2784     cached_libtool=false
2785     my_sed_traces='
2786       s,#.*$,,
2787       s,^dnl .*$,,
2788       s, dnl .*$,,
2789       /gl_LOCAL_DIR(/ {
2790         s,^.*gl_LOCAL_DIR([[ ]*\([^]"$`\\)]*\).*$,cached_local_gnulib_dir="\1",p
2791       }
2792       /gl_MODULES(/ {
2793         ta
2794         :a
2795           s/)/)/
2796           tb
2797           N
2798           ba
2799         :b
2800         s,^.*gl_MODULES([[ ]*\([^]"$`\\)]*\).*$,cached_specified_modules="\1",p
2801       }
2802       /gl_WITH_OBSOLETE/ {
2803         s,^.*$,cached_incobsolete=true,p
2804       }
2805       /gl_AVOID(/ {
2806         s,^.*gl_AVOID([[ ]*\([^]"$`\\)]*\).*$,cached_avoidlist="\1",p
2807       }
2808       /gl_SOURCE_BASE(/ {
2809         s,^.*gl_SOURCE_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_sourcebase="\1",p
2810       }
2811       /gl_M4_BASE(/ {
2812         s,^.*gl_M4_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_m4base="\1",p
2813       }
2814       /gl_PO_BASE(/ {
2815         s,^.*gl_PO_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_pobase="\1",p
2816       }
2817       /gl_DOC_BASE(/ {
2818         s,^.*gl_DOC_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_docbase="\1",p
2819       }
2820       /gl_TESTS_BASE(/ {
2821         s,^.*gl_TESTS_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_testsbase="\1",p
2822       }
2823       /gl_WITH_TESTS/ {
2824         s,^.*$,cached_inctests=true,p
2825       }
2826       /gl_LIB(/ {
2827         s,^.*gl_LIB([[ ]*\([^]"$`\\)]*\).*$,cached_libname="\1",p
2828       }
2829       /gl_LGPL(/ {
2830         s,^.*gl_LGPL([[ ]*\([^]"$`\\)]*\).*$,cached_lgpl="\1",p
2831       }
2832       /gl_LGPL/ {
2833         s,^.*$,cached_lgpl=yes,p
2834       }
2835       /gl_MAKEFILE_NAME(/ {
2836         s,^.*gl_MAKEFILE_NAME([[ ]*\([^]"$`\\)]*\).*$,cached_makefile_name="\1",p
2837       }
2838       /gl_LIBTOOL/ {
2839         s,^.*$,cached_libtool=true,p
2840       }
2841       /gl_MACRO_PREFIX(/ {
2842         s,^.*gl_MACRO_PREFIX([[ ]*\([^]"$`\\)]*\).*$,cached_macro_prefix="\1",p
2843       }
2844       /gl_PO_DOMAIN(/ {
2845         s,^.*gl_PO_DOMAIN([[ ]*\([^]"$`\\)]*\).*$,cached_po_domain="\1",p
2846       }
2847       /gl_VC_FILES(/ {
2848         s,^.*gl_VC_FILES([[ ]*\([^]"$`\\)]*\).*$,cached_vc_files="\1",p
2849       }'
2850     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
2851     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
2852       my_sed_traces='
2853         s,#.*$,,
2854         s,^dnl .*$,,
2855         s, dnl .*$,,
2856         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
2857           s,^.*$,cached_files=",p
2858           n
2859           ta
2860           :a
2861           s,^\]).*$,",
2862           tb
2863           s,["$`\\],,g
2864           p
2865           n
2866           ba
2867           :b
2868           p
2869         }'
2870       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
2871     fi
2872   fi
2873
2874   # Merge the cached settings with the specified ones.
2875   # The m4base must be the same as expected from the pathname.
2876   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
2877     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
2878   fi
2879   # The local_gnulib_dir defaults to the cached one. Recall that the cached one
2880   # is relative to $destdir, whereas the one we use is relative to . or absolute.
2881   if test -z "$local_gnulib_dir"; then
2882     if test -n "$cached_local_gnulib_dir"; then
2883       case "$destdir" in
2884         /*)
2885           local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
2886         *)
2887           case "$cached_local_gnulib_dir" in
2888             /*)
2889               local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
2890             *)
2891               func_relconcat "$destdir" "$cached_local_gnulib_dir"
2892               local_gnulib_dir="$relconcat" ;;
2893           esac ;;
2894       esac
2895     fi
2896   fi
2897   # Append the cached and the specified module names. So that
2898   # "gnulib-tool --import foo" means to add the module foo.
2899   specified_modules="$cached_specified_modules $1"
2900   # Included obsolete modules among the dependencies if specified either way.
2901   if test -z "$incobsolete"; then
2902     incobsolete="$cached_incobsolete"
2903   fi
2904   # Append the cached and the specified avoidlist. This is probably better
2905   # than dropping the cached one when --avoid is specified at least once.
2906   avoidlist=`for m in $cached_avoidlist $avoidlist; do echo $m; done | LC_ALL=C sort -u`
2907   avoidlist=`echo $avoidlist`
2908
2909   # The sourcebase defaults to the cached one.
2910   if test -z "$sourcebase"; then
2911     sourcebase="$cached_sourcebase"
2912     if test -z "$sourcebase"; then
2913       func_fatal_error "missing --source-base option"
2914     fi
2915   fi
2916   # The pobase defaults to the cached one.
2917   if test -z "$pobase"; then
2918     pobase="$cached_pobase"
2919   fi
2920   # The docbase defaults to the cached one.
2921   if test -z "$docbase"; then
2922     docbase="$cached_docbase"
2923     if test -z "$docbase"; then
2924       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."
2925     fi
2926   fi
2927   # The testsbase defaults to the cached one.
2928   if test -z "$testsbase"; then
2929     testsbase="$cached_testsbase"
2930     if test -z "$testsbase"; then
2931       func_fatal_error "missing --tests-base option"
2932     fi
2933   fi
2934   # Require the tests if specified either way.
2935   if test -z "$inctests"; then
2936     inctests="$cached_inctests"
2937   fi
2938   # The libname defaults to the cached one.
2939   if test -z "$supplied_libname"; then
2940     libname="$cached_libname"
2941     if test -z "$libname"; then
2942       func_fatal_error "missing --lib option"
2943     fi
2944   fi
2945   # Require LGPL if specified either way.
2946   if test -z "$lgpl"; then
2947     lgpl="$cached_lgpl"
2948   fi
2949   # The makefile_name defaults to the cached one.
2950   if test -z "$makefile_name"; then
2951     makefile_name="$cached_makefile_name"
2952   fi
2953   # Use libtool if specified either way, or if guessed.
2954   if test -z "$libtool"; then
2955     if test -n "$cached_m4base"; then
2956       libtool="$cached_libtool"
2957     else
2958       libtool="$guessed_libtool"
2959     fi
2960   fi
2961   # The macro_prefix defaults to the cached one.
2962   if test -z "$macro_prefix"; then
2963     macro_prefix="$cached_macro_prefix"
2964     if test -z "$macro_prefix"; then
2965       func_fatal_error "missing --macro-prefix option"
2966     fi
2967   fi
2968   # The po_domain defaults to the cached one.
2969   if test -z "$po_domain"; then
2970     po_domain="$cached_po_domain"
2971   fi
2972   # The vc_files defaults to the cached one.
2973   if test -z "$vc_files"; then
2974     vc_files="$cached_vc_files"
2975   fi
2976
2977   # Canonicalize the list of specified modules.
2978   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort -u`
2979
2980   # Determine final module list.
2981   modules="$specified_modules"
2982   func_modules_transitive_closure
2983   if test $verbose -ge 0; then
2984     echo "Module list with included dependencies:"
2985     echo "$modules" | sed -e 's/^/  /'
2986   fi
2987   final_modules="$modules"
2988
2989   # Determine main module list and tests-related module list separately.
2990   # The main module list is the transitive closure of the specified modules,
2991   # ignoring tests modules. Its lib/* sources go into $sourcebase/. If --lgpl
2992   # is specified, it will consist only of LGPLed source.
2993   # The tests-related module list is the transitive closure of the specified
2994   # modules, including tests modules, minus the main module list excluding
2995   # modules of applicability 'all'. Its lib/* sources (brought in through
2996   # dependencies of *-tests modules) go into $testsbase/. It may contain GPLed
2997   # source, even if --lgpl is specified.
2998   # Determine main module list.
2999   saved_inctests="$inctests"
3000   inctests=""
3001   modules="$specified_modules"
3002   func_modules_transitive_closure
3003   main_modules="$modules"
3004   inctests="$saved_inctests"
3005   if test $verbose -ge 1; then
3006     echo "Main module list:"
3007     echo "$main_modules" | sed -e 's/^/  /'
3008   fi
3009   # Determine tests-related module list.
3010   echo "$final_modules" | LC_ALL=C sort -u > "$tmp"/final-modules
3011   testsrelated_modules=`func_reset_sigpipe
3012                         for module in $main_modules; do
3013                           if test \`func_get_applicability $module\` = main; then
3014                             echo $module
3015                           fi
3016                         done \
3017                         | LC_ALL=C sort -u | LC_ALL=C join -v 2 - "$tmp"/final-modules`
3018   if test $verbose -ge 1; then
3019     echo "Tests-related module list:"
3020     echo "$testsrelated_modules" | sed -e 's/^/  /'
3021   fi
3022
3023   # Add the dummy module to the main module list if needed.
3024   modules="$main_modules"
3025   func_modules_add_dummy
3026   main_modules="$modules"
3027
3028   # Determine whether a $testsbase/libtests.a is needed.
3029   use_libtests=false
3030   for module in $testsrelated_modules; do
3031     func_verify_nontests_module
3032     if test -n "$module"; then
3033       all_files=`func_get_filelist $module`
3034       # Test whether some file in $all_files lies in lib/.
3035       for f in $all_files; do
3036         case $f in
3037           lib/*)
3038             use_libtests=true
3039             break 2
3040             ;;
3041         esac
3042       done
3043     fi
3044   done
3045
3046   # Add the dummy module to the tests-related module list if needed.
3047   if $use_libtests; then
3048     modules="$testsrelated_modules"
3049     func_modules_add_dummy
3050     testsrelated_modules="$modules"
3051   fi
3052
3053   # If --lgpl, verify that the licenses of modules are compatible.
3054   if test -n "$lgpl"; then
3055     license_incompatibilities=
3056     for module in $main_modules; do
3057       license=`func_get_license $module`
3058       case $license in
3059         'GPLed build tool') ;;
3060         'public domain' | 'unlimited' | 'unmodifiable license text') ;;
3061         *)
3062           case "$lgpl" in
3063             yes | 3)
3064               case $license in
3065                 LGPL | LGPLv2+ | LGPLv3+) ;;
3066                 *) func_append license_incompatibilities "$module $license$nl" ;;
3067               esac
3068               ;;
3069             2)
3070               case $license in
3071                 LGPLv2+) ;;
3072                 *) func_append license_incompatibilities "$module $license$nl" ;;
3073               esac
3074               ;;
3075             *) func_fatal_error "invalid value lgpl=$lgpl" ;;
3076           esac
3077           ;;
3078       esac
3079     done
3080     if test -n "$license_incompatibilities"; then
3081       # Format the license incompatibilities as a table.
3082       sed_expand_column1_width50_indent17='s,^\([^ ]*\) ,\1                                                   ,
3083 s,^\(.................................................[^ ]*\) *,                 \1 ,'
3084       license_incompatibilities=`echo "$license_incompatibilities" | sed -e "$sed_expand_column1_width50_indent17"`
3085       func_fatal_error "incompatible license on modules:$nl$license_incompatibilities"
3086     fi
3087   fi
3088
3089   # Show banner notice of every module.
3090   modules="$main_modules"
3091   func_modules_notice
3092
3093   # Determine script to apply to imported library files.
3094   sed_transform_lib_file=
3095   for module in $main_modules; do
3096     if test $module = config-h; then
3097       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
3098       sed_transform_lib_file=$sed_transform_lib_file'
3099         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
3100       '
3101       break
3102     fi
3103   done
3104   sed_transform_main_lib_file="$sed_transform_lib_file"
3105   if test -n "$do_copyrights"; then
3106     if test -n "$lgpl"; then
3107       # Update license.
3108       case "$lgpl" in
3109         yes | 3)
3110           sed_transform_main_lib_file=$sed_transform_main_lib_file'
3111             s/GNU General/GNU Lesser General/g
3112           '
3113           ;;
3114         2)
3115           sed_transform_main_lib_file=$sed_transform_main_lib_file'
3116             s/GNU General/GNU Lesser General/g
3117             s/version [23]\([ ,]\)/version 2.1\1/g
3118           '
3119           ;;
3120         *) func_fatal_error "invalid value lgpl=$lgpl" ;;
3121       esac
3122     else
3123       # Update license.
3124       sed_transform_main_lib_file=$sed_transform_main_lib_file'
3125         s/GNU Lesser General/GNU General/g
3126         s/GNU Library General/GNU General/g
3127         s/version 2\(.1\)\{0,1\}\([ ,]\)/version 3\2/g
3128       '
3129     fi
3130   fi
3131
3132   # Determine script to apply to auxiliary files that go into $auxdir/.
3133   sed_transform_build_aux_file=
3134   if test -n "$do_copyrights"; then
3135     # Update license.
3136     sed_transform_build_aux_file=$sed_transform_build_aux_file'
3137       s/GNU Lesser General/GNU General/g
3138       s/GNU Library General/GNU General/g
3139       s/version 2\(.1\)\{0,1\}\([ ,]\)/version 3\2/g
3140     '
3141   fi
3142
3143   # Determine script to apply to library files that go into $testsbase/.
3144   sed_transform_testsrelated_lib_file="$sed_transform_lib_file"
3145   if test -n "$do_copyrights"; then
3146     # Update license.
3147     sed_transform_testsrelated_lib_file=$sed_transform_testsrelated_lib_file'
3148       s/GNU Lesser General/GNU General/g
3149       s/GNU Library General/GNU General/g
3150       s/version 2\(.1\)\{0,1\}\([ ,]\)/version 3\2/g
3151     '
3152   fi
3153
3154   # Determine the final file lists.
3155   # They must be computed separately, because files in lib/* go into
3156   # $sourcebase/ if they are in the main file list but into $testsbase/
3157   # if they are in the tests-related file list. Furthermore lib/dummy.c
3158   # can be in both.
3159   # Determine final main file list.
3160   modules="$main_modules"
3161   func_modules_to_filelist
3162   main_files="$files"
3163   # Determine final tests-related file list.
3164   modules="$testsrelated_modules"
3165   func_modules_to_filelist
3166   testsrelated_files=`echo "$files" | sed -e 's,^lib/,tests=lib/,'`
3167   # Merge both file lists.
3168   sed_remove_empty_lines='/^$/d'
3169   files=`{ echo "$main_files"; echo "$testsrelated_files"; } | sed -e "$sed_remove_empty_lines" | LC_ALL=C sort -u`
3170   if test $verbose -ge 0; then
3171     echo "File list:"
3172     sed_prettyprint_files='s,^tests=lib/\(.*\)$,lib/\1 -> tests/\1,'
3173     echo "$files" | sed -e "$sed_prettyprint_files" -e 's/^/  /'
3174   fi
3175
3176   test -n "$files" \
3177     || func_fatal_error "refusing to do nothing"
3178
3179   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
3180   new_files="$files m4/gnulib-tool.m4"
3181   old_files="$cached_files"
3182   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
3183     func_append old_files " m4/gnulib-tool.m4"
3184   fi
3185
3186   rewritten='%REWRITTEN%'
3187   sed_rewrite_old_files="\
3188     s,^build-aux/,$rewritten$auxdir/,
3189     s,^doc/,$rewritten$cached_docbase/,
3190     s,^lib/,$rewritten$cached_sourcebase/,
3191     s,^m4/,$rewritten$cached_m4base/,
3192     s,^tests/,$rewritten$cached_testsbase/,
3193     s,^tests=lib/,$rewritten$cached_testsbase/,
3194     s,^top/,$rewritten,
3195     s,^$rewritten,,"
3196   sed_rewrite_new_files="\
3197     s,^build-aux/,$rewritten$auxdir/,
3198     s,^doc/,$rewritten$docbase/,
3199     s,^lib/,$rewritten$sourcebase/,
3200     s,^m4/,$rewritten$m4base/,
3201     s,^tests/,$rewritten$testsbase/,
3202     s,^tests=lib/,$rewritten$testsbase/,
3203     s,^top/,$rewritten,
3204     s,^$rewritten,,"
3205
3206   # Create directories.
3207   { echo "$sourcebase"
3208     echo "$m4base"
3209     if test -n "$pobase"; then
3210       echo "$pobase"
3211     fi
3212     docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
3213     if test -n "$docfiles"; then
3214       echo "$docbase"
3215     fi
3216     if test -n "$inctests"; then
3217       echo "$testsbase"
3218     fi
3219     echo "$auxdir"
3220     for f in $files; do echo $f; done \
3221       | sed -e "$sed_rewrite_new_files" \
3222       | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
3223       | LC_ALL=C sort -u
3224   } > "$tmp"/dirs
3225   { # Rearrange file descriptors. Needed because "while ... done < ..."
3226     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3227     exec 5<&0 < "$tmp"/dirs
3228     while read d; do
3229       if test ! -d "$destdir/$d"; then
3230         if $doit; then
3231           echo "Creating directory $destdir/$d"
3232           mkdir -p "$destdir/$d" || func_fatal_error "failed"
3233         else
3234           echo "Create directory $destdir/$d"
3235         fi
3236       fi
3237     done
3238     exec 0<&5 5<&-
3239   }
3240
3241   # Copy files or make symbolic links. Remove obsolete files.
3242   added_files=''
3243   removed_files=''
3244   delimiter='   '
3245   # Construct a table with 2 columns: rewritten-file-name original-file-name,
3246   # representing the files according to the last gnulib-tool invocation.
3247   for f in $old_files; do echo $f; done \
3248     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_old_files" \
3249     | LC_ALL=C sort \
3250     > "$tmp"/old-files
3251   # Construct a table with 2 columns: rewritten-file-name original-file-name,
3252   # representing the files after this gnulib-tool invocation.
3253   for f in $new_files; do echo $f; done \
3254     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" \
3255     | LC_ALL=C sort \
3256     > "$tmp"/new-files
3257   # First the files that are in old-files, but not in new-files:
3258   sed_take_first_column='s,'"$delimiter"'.*,,'
3259   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
3260     # Remove the file. Do nothing if the user already removed it.
3261     if test -f "$destdir/$g" || test -h "$destdir/$g"; then
3262       if $doit; then
3263         echo "Removing file $g (backup in ${g}~)"
3264         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
3265       else
3266         echo "Remove file $g (backup in ${g}~)"
3267       fi
3268       func_append removed_files "$g$nl"
3269     fi
3270   done
3271   # func_add_or_update handles a file that ought to be present afterwards.
3272   # Uses parameters
3273   # - f             the original file name
3274   # - g             the rewritten file name
3275   # - already_present  nonempty if the file should already exist, empty
3276   #                    otherwise
3277   func_add_or_update ()
3278   {
3279     of="$f"
3280     case "$f" in
3281       tests=lib/*) f=`echo "$f" | sed -e 's,^tests=lib/,lib/,'` ;;
3282     esac
3283     func_dest_tmpfilename "$g"
3284     func_lookup_file "$f"
3285     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
3286     if test -n "$sed_transform_main_lib_file"; then
3287       case "$of" in
3288         lib/*)
3289           sed -e "$sed_transform_main_lib_file" \
3290             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
3291           ;;
3292       esac
3293     fi
3294     if test -n "$sed_transform_build_aux_file"; then
3295       case "$of" in
3296         build-aux/*)
3297           sed -e "$sed_transform_build_aux_file" \
3298             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
3299           ;;
3300       esac
3301     fi
3302     if test -n "$sed_transform_testsrelated_lib_file"; then
3303       case "$of" in
3304         tests=lib/*)
3305           sed -e "$sed_transform_testsrelated_lib_file" \
3306             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
3307           ;;
3308       esac
3309     fi
3310     if test -f "$destdir/$g"; then
3311       # The file already exists.
3312       func_update_file
3313     else
3314       # Install the file.
3315       # Don't protest if the file should be there but isn't: it happens
3316       # frequently that developers don't put autogenerated files into CVS.
3317       func_add_file
3318       func_append added_files "$g$nl"
3319     fi
3320     rm -f "$tmpfile"
3321   }
3322   # Then the files that are in new-files, but not in old-files:
3323   sed_take_last_column='s,^.*'"$delimiter"',,'
3324   already_present=
3325   LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files \
3326     | sed -e "$sed_take_last_column" \
3327     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/added-files
3328   { # Rearrange file descriptors. Needed because "while ... done < ..."
3329     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3330     exec 5<&0 < "$tmp"/added-files
3331     while read g f; do
3332       func_add_or_update
3333     done
3334     exec 0<&5 5<&-
3335   }
3336   # Then the files that are in new-files and in old-files:
3337   already_present=true
3338   LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files \
3339     | sed -e "$sed_take_last_column" \
3340     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/kept-files
3341   { # Rearrange file descriptors. Needed because "while ... done < ..."
3342     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3343     exec 5<&0 < "$tmp"/kept-files
3344     while read g f; do
3345       func_add_or_update
3346     done
3347     exec 0<&5 5<&-
3348   }
3349
3350   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
3351   actioncmd="gnulib-tool --import"
3352   func_append actioncmd " --dir=$destdir"
3353   if test -n "$local_gnulib_dir"; then
3354     func_append actioncmd " --local-dir=$local_gnulib_dir"
3355   fi
3356   func_append actioncmd " --lib=$libname"
3357   func_append actioncmd " --source-base=$sourcebase"
3358   func_append actioncmd " --m4-base=$m4base"
3359   if test -n "$pobase"; then
3360     func_append actioncmd " --po-base=$pobase"
3361   fi
3362   func_append actioncmd " --doc-base=$docbase"
3363   func_append actioncmd " --tests-base=$testsbase"
3364   func_append actioncmd " --aux-dir=$auxdir"
3365   if test -n "$inctests"; then
3366     func_append actioncmd " --with-tests"
3367   fi
3368   if test -n "$incobsolete"; then
3369     func_append actioncmd " --with-obsolete"
3370   fi
3371   for module in $avoidlist; do
3372     func_append actioncmd " --avoid=$module"
3373   done
3374   if test -n "$lgpl"; then
3375     if test "$lgpl" = yes; then
3376       func_append actioncmd " --lgpl"
3377     else
3378       func_append actioncmd " --lgpl=$lgpl"
3379     fi
3380   fi
3381   if test -n "$makefile_name"; then
3382     func_append actioncmd " --makefile-name=$makefile_name"
3383   fi
3384   if test "$libtool" = true; then
3385     func_append actioncmd " --libtool"
3386   else
3387     func_append actioncmd " --no-libtool"
3388   fi
3389   func_append actioncmd " --macro-prefix=$macro_prefix"
3390   if test -n "$po_domain"; then
3391     func_append actioncmd " --po-domain=$po_domain"
3392   fi
3393   if test -n "$vc_files"; then
3394     if test "$vc_files" = true; then
3395       func_append actioncmd " --vc-files"
3396     else
3397       func_append actioncmd " --no-vc-files"
3398     fi
3399   fi
3400   func_append actioncmd " `echo $specified_modules`"
3401
3402   # Default the makefile name to Makefile.am.
3403   if test -n "$makefile_name"; then
3404     makefile_am=$makefile_name
3405   else
3406     makefile_am=Makefile.am
3407   fi
3408
3409   # Create normal Makefile.ams.
3410   for_test=false
3411
3412   # Setup list of Makefile.am edits that are to be performed afterwards.
3413   # Some of these edits apply to files that we will generate; others are
3414   # under the responsibility of the developer.
3415   makefile_am_edits=0
3416   # func_note_Makefile_am_edit dir var value
3417   # remembers that ${dir}Makefile.am needs to be edited to that ${var} mentions
3418   # ${value}.
3419   func_note_Makefile_am_edit ()
3420   {
3421     makefile_am_edits=`expr $makefile_am_edits + 1`
3422     eval makefile_am_edit${makefile_am_edits}_dir=\"\$1\"
3423     eval makefile_am_edit${makefile_am_edits}_var=\"\$2\"
3424     eval makefile_am_edit${makefile_am_edits}_val=\"\$3\"
3425   }
3426   if test "$makefile_am" = Makefile.am; then
3427     sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
3428     sourcebase_base=`basename "$sourcebase"`
3429     func_note_Makefile_am_edit "$sourcebase_dir" SUBDIRS "$sourcebase_base"
3430   fi
3431   if test -n "$pobase"; then
3432     pobase_dir=`echo "$pobase" | sed -n -e 's,/[^/]*$,/,p'`
3433     pobase_base=`basename "$pobase"`
3434     func_note_Makefile_am_edit "$pobase_dir" SUBDIRS "$pobase_base"
3435   fi
3436   if test -n "$inctests"; then
3437     if test "$makefile_am" = Makefile.am; then
3438       testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
3439       testsbase_base=`basename "$testsbase"`
3440       func_note_Makefile_am_edit "$testsbase_dir" SUBDIRS "$testsbase_base"
3441     fi
3442   fi
3443   func_note_Makefile_am_edit "" ACLOCAL_AMFLAGS "-I ${m4base}"
3444   {
3445     # Find the first parent directory of $m4base that contains or will contain
3446     # a Makefile.am.
3447     sed_last='s,^.*/\([^/][^/]*\)//*$,\1/,
3448 s,//*$,/,'
3449     sed_butlast='s,[^/][^/]*//*$,,'
3450     dir1="${m4base}/"; dir2=""
3451     while test -n "$dir1" \
3452           && ! { test -f "${destdir}/${dir1}Makefile.am" \
3453                  || test "${dir1}Makefile.am" = "$sourcebase/$makefile_am" \
3454                  || test "./${dir1}Makefile.am" = "$sourcebase/$makefile_am" \
3455                  || { test -n "$inctests" \
3456                       && { test "${dir1}Makefile.am" = "$testsbase/$makefile_am" \
3457                            || test "./${dir1}Makefile.am" = "$testsbase/$makefile_am"; }; }; }; do
3458       dir2=`echo "$dir1" | sed -e "$sed_last"`"$dir2"
3459       dir1=`echo "$dir1" | sed -e "$sed_butlast"`
3460     done
3461     func_note_Makefile_am_edit "$dir1" EXTRA_DIST "${dir2}gnulib-cache.m4"
3462   }
3463
3464   # Create library makefile.
3465   func_dest_tmpfilename $sourcebase/$makefile_am
3466   destfile="$sourcebase/$makefile_am"
3467   modules="$main_modules"
3468   func_emit_lib_Makefile_am > "$tmpfile"
3469   if test -f "$destdir"/$sourcebase/$makefile_am; then
3470     if cmp "$destdir"/$sourcebase/$makefile_am "$tmpfile" > /dev/null; then
3471       rm -f "$tmpfile"
3472     else
3473       if $doit; then
3474         echo "Updating $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
3475         mv -f "$destdir"/$sourcebase/$makefile_am "$destdir"/$sourcebase/$makefile_am~
3476         mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
3477       else
3478         echo "Update $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
3479         rm -f "$tmpfile"
3480       fi
3481     fi
3482   else
3483     if $doit; then
3484       echo "Creating $sourcebase/$makefile_am"
3485       mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
3486     else
3487       echo "Create $sourcebase/$makefile_am"
3488       rm -f "$tmpfile"
3489     fi
3490     func_append added_files "$sourcebase/$makefile_am$nl"
3491   fi
3492
3493   # Create po/ directory.
3494   if test -n "$pobase"; then
3495     # Create po makefile and auxiliary files.
3496     for file in Makefile.in.in remove-potcdate.sin; do
3497       func_dest_tmpfilename $pobase/$file
3498       func_lookup_file build-aux/po/$file
3499       cat "$lookedup_file" > "$tmpfile"
3500       if test -f "$destdir"/$pobase/$file; then
3501         if cmp "$destdir"/$pobase/$file "$tmpfile" > /dev/null; then
3502           rm -f "$tmpfile"
3503         else
3504           if $doit; then
3505             echo "Updating $pobase/$file (backup in $pobase/$file~)"
3506             mv -f "$destdir"/$pobase/$file "$destdir"/$pobase/$file~
3507             mv -f "$tmpfile" "$destdir"/$pobase/$file
3508           else
3509             echo "Update $pobase/$file (backup in $pobase/$file~)"
3510             rm -f "$tmpfile"
3511           fi
3512         fi
3513       else
3514         if $doit; then
3515           echo "Creating $pobase/$file"
3516           mv -f "$tmpfile" "$destdir"/$pobase/$file
3517         else
3518           echo "Create $pobase/$file"
3519           rm -f "$tmpfile"
3520         fi
3521         func_append added_files "$pobase/$file$nl"
3522       fi
3523     done
3524     # Create po makefile parameterization, part 1.
3525     func_dest_tmpfilename $pobase/Makevars
3526     func_emit_po_Makevars > "$tmpfile"
3527     if test -f "$destdir"/$pobase/Makevars; then
3528       if cmp "$destdir"/$pobase/Makevars "$tmpfile" > /dev/null; then
3529         rm -f "$tmpfile"
3530       else
3531         if $doit; then
3532           echo "Updating $pobase/Makevars (backup in $pobase/Makevars~)"
3533           mv -f "$destdir"/$pobase/Makevars "$destdir"/$pobase/Makevars~
3534           mv -f "$tmpfile" "$destdir"/$pobase/Makevars
3535         else
3536           echo "Update $pobase/Makevars (backup in $pobase/Makevars~)"
3537           rm -f "$tmpfile"
3538         fi
3539       fi
3540     else
3541       if $doit; then
3542         echo "Creating $pobase/Makevars"
3543         mv -f "$tmpfile" "$destdir"/$pobase/Makevars
3544       else
3545         echo "Create $pobase/Makevars"
3546         rm -f "$tmpfile"
3547       fi
3548       func_append added_files "$pobase/Makevars$nl"
3549     fi
3550     # Create po makefile parameterization, part 2.
3551     func_dest_tmpfilename $pobase/POTFILES.in
3552     func_emit_po_POTFILES_in > "$tmpfile"
3553     if test -f "$destdir"/$pobase/POTFILES.in; then
3554       if cmp "$destdir"/$pobase/POTFILES.in "$tmpfile" > /dev/null; then
3555         rm -f "$tmpfile"
3556       else
3557         if $doit; then
3558           echo "Updating $pobase/POTFILES.in (backup in $pobase/POTFILES.in~)"
3559           mv -f "$destdir"/$pobase/POTFILES.in "$destdir"/$pobase/POTFILES.in~
3560           mv -f "$tmpfile" "$destdir"/$pobase/POTFILES.in
3561         else
3562           echo "Update $pobase/POTFILES.in (backup in $pobase/POTFILES.in~)"
3563           rm -f "$tmpfile"
3564         fi
3565       fi
3566     else
3567       if $doit; then
3568         echo "Creating $pobase/POTFILES.in"
3569         mv -f "$tmpfile" "$destdir"/$pobase/POTFILES.in
3570       else
3571         echo "Create $pobase/POTFILES.in"
3572         rm -f "$tmpfile"
3573       fi
3574       func_append added_files "$pobase/POTFILES.in$nl"
3575     fi
3576     # Fetch PO files.
3577     TP_URL="http://translationproject.org/latest/"
3578     TP_RSYNC_URI="translationproject.org::tp/latest/"
3579     if $doit; then
3580       echo "Fetching gnulib PO files from $TP_URL"
3581       (cd "$destdir"/$pobase \
3582        && { # Prefer rsync over wget if it is available, since it consumes
3583             # less network bandwidth, due to compression.
3584             if type rsync 2>/dev/null | grep / > /dev/null; then
3585               rsync -Lrtz "${TP_RSYNC_URI}gnulib/" .
3586             else
3587               wget --quiet -r -l1 -nd -np -A.po "${TP_URL}gnulib"
3588             fi
3589           }
3590       )
3591     else
3592       echo "Fetch gnulib PO files from $TP_URL"
3593     fi
3594     # Create po/LINGUAS.
3595     if $doit; then
3596       func_dest_tmpfilename $pobase/LINGUAS
3597       (cd "$destdir"/$pobase \
3598        && { echo '# Set of available languages.'
3599             LC_ALL=C ls -1 *.po | sed -e 's,\.po$,,'
3600           }
3601       ) > "$tmpfile"
3602       if test -f "$destdir"/$pobase/LINGUAS; then
3603         if cmp "$destdir"/$pobase/LINGUAS "$tmpfile" > /dev/null; then
3604           rm -f "$tmpfile"
3605         else
3606           echo "Updating $pobase/LINGUAS (backup in $pobase/LINGUAS~)"
3607           mv -f "$destdir"/$pobase/LINGUAS "$destdir"/$pobase/LINGUAS~
3608           mv -f "$tmpfile" "$destdir"/$pobase/LINGUAS
3609         fi
3610       else
3611         echo "Creating $pobase/LINGUAS"
3612         mv -f "$tmpfile" "$destdir"/$pobase/LINGUAS
3613         func_append added_files "$pobase/LINGUAS$nl"
3614       fi
3615     else
3616       if test -f "$destdir"/$pobase/LINGUAS; then
3617         echo "Update $pobase/LINGUAS (backup in $pobase/LINGUAS~)"
3618       else
3619         echo "Create $pobase/LINGUAS"
3620       fi
3621     fi
3622   fi
3623
3624   # Create m4/gnulib-cache.m4.
3625   func_dest_tmpfilename $m4base/gnulib-cache.m4
3626   (
3627     func_emit_copyright_notice
3628     echo "#"
3629     echo "# This file represents the specification of how gnulib-tool is used."
3630     echo "# It acts as a cache: It is written and read by gnulib-tool."
3631     echo "# In projects using CVS, this file is meant to be stored in CVS,"
3632     echo "# like the configure.ac and various Makefile.am files."
3633     echo
3634     echo
3635     echo "# Specification in the form of a command-line invocation:"
3636     echo "#   $actioncmd"
3637     echo
3638     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
3639     # Store the local_gnulib_dir relative to destdir.
3640     case "$local_gnulib_dir" in
3641       "" | /*)
3642         relative_local_gnulib_dir="$local_gnulib_dir" ;;
3643       * )
3644         case "$destdir" in
3645           /*) relative_local_gnulib_dir="$local_gnulib_dir" ;;
3646           *)
3647             # destdir, local_gnulib_dir are both relative.
3648             func_relativize "$destdir" "$local_gnulib_dir"
3649             relative_local_gnulib_dir="$reldir" ;;
3650         esac ;;
3651     esac
3652     echo "gl_LOCAL_DIR([$relative_local_gnulib_dir])"
3653     echo "gl_MODULES(["
3654     echo "$specified_modules" | sed -e 's/^/  /g'
3655     echo "])"
3656     test -z "$incobsolete" || echo "gl_WITH_OBSOLETE"
3657     echo "gl_AVOID([$avoidlist])"
3658     echo "gl_SOURCE_BASE([$sourcebase])"
3659     echo "gl_M4_BASE([$m4base])"
3660     echo "gl_PO_BASE([$pobase])"
3661     echo "gl_DOC_BASE([$docbase])"
3662     echo "gl_TESTS_BASE([$testsbase])"
3663     test -z "$inctests" || echo "gl_WITH_TESTS"
3664     echo "gl_LIB([$libname])"
3665     if test -n "$lgpl"; then
3666       if test "$lgpl" = yes; then
3667         echo "gl_LGPL"
3668       else
3669         echo "gl_LGPL([$lgpl])"
3670       fi
3671     fi
3672     echo "gl_MAKEFILE_NAME([$makefile_name])"
3673     if test "$libtool" = true; then
3674       echo "gl_LIBTOOL"
3675     fi
3676     echo "gl_MACRO_PREFIX([$macro_prefix])"
3677     echo "gl_PO_DOMAIN([$po_domain])"
3678     if test -n "$vc_files"; then
3679       echo "gl_VC_FILES([$vc_files])"
3680     fi
3681   ) > "$tmpfile"
3682   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
3683     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
3684       rm -f "$tmpfile"
3685     else
3686       if $doit; then
3687         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
3688         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
3689         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
3690       else
3691         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
3692         if false; then
3693           cat "$tmpfile"
3694           echo
3695           echo "# gnulib-cache.m4 ends here"
3696         fi
3697         rm -f "$tmpfile"
3698       fi
3699     fi
3700   else
3701     if $doit; then
3702       echo "Creating $m4base/gnulib-cache.m4"
3703       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
3704     else
3705       echo "Create $m4base/gnulib-cache.m4"
3706       cat "$tmpfile"
3707       rm -f "$tmpfile"
3708     fi
3709   fi
3710
3711   # Create m4/gnulib-comp.m4.
3712   func_dest_tmpfilename $m4base/gnulib-comp.m4
3713   (
3714     echo "# DO NOT EDIT! GENERATED AUTOMATICALLY!"
3715     func_emit_copyright_notice
3716     echo "#"
3717     echo "# This file represents the compiled summary of the specification in"
3718     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
3719     echo "# to be invoked from configure.ac."
3720     echo "# In projects using CVS, this file can be treated like other built files."
3721     echo
3722     echo
3723     echo "# This macro should be invoked from $configure_ac, in the section"
3724     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
3725     echo "# any checks for libraries, header files, types and library functions."
3726     echo "AC_DEFUN([${macro_prefix}_EARLY],"
3727     echo "["
3728     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
3729     echo "  m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
3730     echo "  m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
3731     echo "  m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
3732     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
3733     if test -n "$uses_subdirs"; then
3734       echo "  AC_REQUIRE([AM_PROG_CC_C_O])"
3735     fi
3736     for module in $final_modules; do
3737       func_verify_module
3738       if test -n "$module"; then
3739         func_get_autoconf_early_snippet "$module"
3740       fi
3741     done \
3742       | sed -e '/^$/d;' -e 's/^/  /'
3743     echo "])"
3744     echo
3745     echo "# This macro should be invoked from $configure_ac, in the section"
3746     echo "# \"Check for header files, types and library functions\"."
3747     echo "AC_DEFUN([${macro_prefix}_INIT],"
3748     echo "["
3749     if test "$libtool" = true; then
3750       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
3751       echo "  gl_cond_libtool=true"
3752     else
3753       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
3754       echo "  gl_cond_libtool=false"
3755       echo "  gl_libdeps="
3756       echo "  gl_ltlibdeps="
3757     fi
3758     if test "$auxdir" != "build-aux"; then
3759       sed_replace_build_aux='
3760         :a
3761         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
3762           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
3763           ba
3764         }'
3765     else
3766       sed_replace_build_aux="$sed_noop"
3767     fi
3768     func_emit_initmacro_start $macro_prefix
3769     echo "  gl_source_base='$sourcebase'"
3770     for module in $main_modules; do
3771       func_verify_module
3772       if test -n "$module"; then
3773         func_get_autoconf_snippet "$module" \
3774           | sed -e '/^$/d;' -e 's/^/  /' \
3775                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
3776                 -e "$sed_replace_build_aux"
3777         if test "$module" = 'alloca' && test "$libtool" = true; then
3778           echo 'changequote(,)dnl'
3779           echo 'LTALLOCA=`echo "$ALLOCA" | sed -e '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
3780           echo 'changequote([, ])dnl'
3781           echo 'AC_SUBST([LTALLOCA])'
3782         fi
3783       fi
3784     done
3785     func_emit_initmacro_end $macro_prefix
3786     echo "  gltests_libdeps="
3787     echo "  gltests_ltlibdeps="
3788     func_emit_initmacro_start ${macro_prefix}tests
3789     echo "  gl_source_base='$testsbase'"
3790     for module in $testsrelated_modules; do
3791       func_verify_module
3792       if test -n "$module"; then
3793         func_get_autoconf_snippet "$module" \
3794           | sed -e '/^$/d;' -e 's/^/  /' \
3795                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
3796                 -e "$sed_replace_build_aux" \
3797                 -e 's/\$gl_cond_libtool/false/g' \
3798                 -e 's/gl_libdeps/gltests_libdeps/g' \
3799                 -e 's/gl_ltlibdeps/gltests_ltlibdeps/g'
3800       fi
3801     done
3802     func_emit_initmacro_end ${macro_prefix}tests
3803     # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
3804     # created using libtool, because libtool already handles the dependencies.
3805     if test "$libtool" != true; then
3806       libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z-' 'A-Z_'`
3807       echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
3808       echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
3809       echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
3810       echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
3811     fi
3812     if $use_libtests; then
3813       echo "  LIBTESTS_LIBDEPS=\"\$gltests_libdeps\""
3814       echo "  AC_SUBST([LIBTESTS_LIBDEPS])"
3815     fi
3816     echo "])"
3817     func_emit_initmacro_done $macro_prefix $sourcebase
3818     func_emit_initmacro_done ${macro_prefix}tests $testsbase
3819     echo
3820     echo "# This macro records the list of files which have been installed by"
3821     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
3822     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
3823     echo "$files" | sed -e 's,^,  ,'
3824     echo "])"
3825   ) > "$tmpfile"
3826   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
3827     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
3828       rm -f "$tmpfile"
3829     else
3830       if $doit; then
3831         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
3832         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
3833         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
3834       else
3835         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
3836         if false; then
3837           cat "$tmpfile"
3838           echo
3839           echo "# gnulib-comp.m4 ends here"
3840         fi
3841         rm -f "$tmpfile"
3842       fi
3843     fi
3844   else
3845     if $doit; then
3846       echo "Creating $m4base/gnulib-comp.m4"
3847       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
3848     else
3849       echo "Create $m4base/gnulib-comp.m4"
3850       cat "$tmpfile"
3851       rm -f "$tmpfile"
3852     fi
3853   fi
3854
3855   if test -n "$inctests"; then
3856     # Create tests makefile.
3857     func_dest_tmpfilename $testsbase/$makefile_am
3858     destfile="$testsbase/$makefile_am"
3859     modules="$testsrelated_modules"
3860     func_emit_tests_Makefile_am > "$tmpfile"
3861     if test -f "$destdir"/$testsbase/$makefile_am; then
3862       if cmp "$destdir"/$testsbase/$makefile_am "$tmpfile" > /dev/null; then
3863         rm -f "$tmpfile"
3864       else
3865         if $doit; then
3866           echo "Updating $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
3867           mv -f "$destdir"/$testsbase/$makefile_am "$destdir"/$testsbase/$makefile_am~
3868           mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
3869         else
3870           echo "Update $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
3871           rm -f "$tmpfile"
3872         fi
3873       fi
3874     else
3875       if $doit; then
3876         echo "Creating $testsbase/$makefile_am"
3877         mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
3878       else
3879         echo "Create $testsbase/$makefile_am"
3880         rm -f "$tmpfile"
3881       fi
3882     func_append added_files "$testsbase/$makefile_am$nl"
3883     fi
3884   fi
3885
3886   if test "$vc_files" != false; then
3887     # Update the .cvsignore and .gitignore files.
3888     { echo "$added_files" | sed -e '/^$/d' -e 's,\([^/]*\)$,|A|\1,'
3889       echo "$removed_files" | sed -e '/^$/d' -e 's,\([^/]*\)$,|R|\1,'
3890       # Treat gnulib-comp.m4 like an added file, even if it already existed.
3891       echo "$m4base/|A|gnulib-comp.m4"
3892     } | LC_ALL=C sort -t'|' -k1,1 > "$tmp"/fileset-changes
3893     { # Rearrange file descriptors. Needed because "while ... done < ..."
3894       # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3895       exec 5<&0 < "$tmp"/fileset-changes
3896       func_update_ignorelist ()
3897       {
3898         ignore="$1"
3899         if test "$ignore" = .gitignore; then
3900           # In a .gitignore file, "foo" applies to the current directory and all
3901           # subdirectories, whereas "/foo" applies to the current directory only.
3902           anchor='/'
3903           escaped_anchor='\/'
3904           doubly_escaped_anchor='\\/'
3905         else
3906           anchor=''
3907           escaped_anchor=''
3908           doubly_escaped_anchor=''
3909         fi
3910         if test -f "$destdir/$dir$ignore"; then
3911           if test -n "$dir_added" || test -n "$dir_removed"; then
3912             sed -e "s|^$anchor||" < "$destdir/$dir$ignore" | LC_ALL=C sort > "$tmp"/ignore
3913             (func_reset_sigpipe
3914              echo "$dir_added" | sed -e '/^$/d' | LC_ALL=C sort -u \
3915                | LC_ALL=C join -v 1 - "$tmp"/ignore > "$tmp"/ignore-added
3916              echo "$dir_removed" | sed -e '/^$/d' | LC_ALL=C sort -u \
3917                | LC_ALL=C join -v 1 - "$tmp"/ignore > "$tmp"/ignore-removed
3918             )
3919             if test -s "$tmp"/ignore-added || test -s "$tmp"/ignore-removed; then
3920               if $doit; then
3921                 echo "Updating $destdir/$dir$ignore (backup in $destdir/$dir${ignore}~)"
3922                 mv -f "$destdir/$dir$ignore" "$destdir/$dir$ignore"~
3923                 { sed -e 's,/,\\/,g' -e 's,^,/^,' -e 's,$,\$/d,' < "$tmp"/ignore-removed
3924                   if test -n "$anchor"; then sed -e 's,/,\\/,g' -e "s,^,/^${doubly_escaped_anchor}," -e 's,$,$/d,' < "$tmp"/ignore-removed; fi
3925                 } > "$tmp"/sed-ignore-removed
3926                 { cat "$destdir/$dir$ignore"~
3927                   sed -e "s|^|$anchor|" < "$tmp"/ignore-added
3928                 } | sed -f "$tmp"/sed-ignore-removed \
3929                   > "$destdir/$dir$ignore"
3930               else
3931                 echo "Update $destdir/$dir$ignore (backup in $destdir/$dir${ignore}~)"
3932               fi
3933             fi
3934           fi
3935         else
3936           if test -n "$dir_added"; then
3937             if $doit; then
3938               echo "Creating $destdir/$dir$ignore"
3939               {
3940                 if test "$ignore" = .cvsignore; then
3941                   echo ".deps"
3942                   # Automake generates Makefile rules that create .dirstamp files.
3943                   echo ".dirstamp"
3944                 fi
3945                 echo "$dir_added" | sed -e '/^$/d' -e "s|^|$anchor|" | LC_ALL=C sort -u
3946               } > "$destdir/$dir$ignore"
3947             else
3948               echo "Create $destdir/$dir$ignore"
3949             fi
3950           fi
3951         fi
3952       }
3953       func_done_dir ()
3954       {
3955         dir="$1"
3956         dir_added="$2"
3957         dir_removed="$3"
3958         if test -d "$destdir/CVS" || test -d "$destdir/${dir}CVS" || test -f "$destdir/${dir}.cvsignore"; then
3959           func_update_ignorelist .cvsignore
3960         fi
3961         if test -d "$destdir/.git" || test -f "$destdir/${dir}.gitignore"; then
3962           func_update_ignorelist .gitignore
3963         fi
3964       }
3965       last_dir=
3966       last_dir_added=
3967       last_dir_removed=
3968       while read line; do
3969         # Why not ''read next_dir op file'' ? Because the dir column can be empty.
3970         next_dir=`echo "$line" | sed -e 's,|.*,,'`
3971         op=`echo "$line" | sed -e 's,^[^|]*|\([^|]*\)|.*$,\1,'`
3972         file=`echo "$line" | sed -e 's,^[^|]*|[^|]*|,,'`
3973         if test "$next_dir" != "$last_dir"; then
3974           func_done_dir "$last_dir" "$last_dir_added" "$last_dir_removed"
3975           last_dir="$next_dir"
3976           last_dir_added=
3977           last_dir_removed=
3978         fi
3979         case $op in
3980           A) func_append last_dir_added "$file$nl";;
3981           R) func_append last_dir_removed "$file$nl";;
3982         esac
3983       done
3984       func_done_dir "$last_dir" "$last_dir_added" "$last_dir_removed"
3985       exec 0<&5 5<&-
3986     }
3987   fi
3988
3989   echo "Finished."
3990   echo
3991   echo "You may need to add #include directives for the following .h files."
3992   # Intersect $specified_modules and $main_modules
3993   # (since $specified_modules is not necessarily of subset of $main_modules
3994   # - some may have been skipped through --avoid, and since the elements of
3995   # $main_modules but not in $specified_modules can go away without explicit
3996   # notice - through changes in the module dependencies).
3997   echo "$specified_modules" > "$tmp"/modules1 # a sorted list, one module per line
3998   echo "$main_modules" > "$tmp"/modules2 # also a sorted list, one module per line
3999   # First the #include <...> directives without #ifs, sorted for convenience,
4000   # then the #include "..." directives without #ifs, sorted for convenience,
4001   # then the #include directives that are surrounded by #ifs. Not sorted.
4002   for module in `LC_ALL=C join "$tmp"/modules1 "$tmp"/modules2`; do
4003     include_directive=`func_get_include_directive "$module"`
4004     case "$nl$include_directive" in
4005       *"$nl#if"*)
4006         echo "$include_directive" 1>&5
4007         ;;
4008       *)
4009         echo "$include_directive" | grep -v 'include "' 1>&6
4010         echo "$include_directive" | grep 'include "' 1>&7
4011         ;;
4012     esac
4013   done 5> "$tmp"/include-if 6> "$tmp"/include-angles 7> "$tmp"/include-quotes
4014   (
4015    LC_ALL=C sort -u "$tmp"/include-angles
4016    LC_ALL=C sort -u "$tmp"/include-quotes
4017    cat "$tmp"/include-if
4018   ) | sed -e '/^$/d' -e 's/^/  /'
4019   rm -f "$tmp"/include-angles "$tmp"/include-quotes "$tmp"/include-if
4020
4021   for module in $main_modules; do
4022     func_get_link_directive "$module"
4023   done \
4024     | LC_ALL=C sort -u | sed -e '/^$/d' -e 's/^/  /' > "$tmp"/link
4025   if test `wc -l < "$tmp"/link` != 0; then
4026     echo
4027     echo "You may need to use the following Makefile variables when linking."
4028     echo "Use them in <program>_LDADD when linking a program, or"
4029     echo "in <library>_a_LDFLAGS or <library>_la_LDFLAGS when linking a library."
4030     cat "$tmp"/link
4031   fi
4032   rm -f "$tmp"/link
4033
4034   echo
4035   echo "Don't forget to"
4036   if test "$makefile_am" = Makefile.am; then
4037     echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
4038   else
4039     echo "  - \"include $makefile_name\" from within \"$sourcebase/Makefile.am\","
4040   fi
4041   if test -n "$pobase"; then
4042     echo "  - add \"$pobase/Makefile.in\" to AC_CONFIG_FILES in $configure_ac,"
4043   fi
4044   if test -n "$inctests"; then
4045     if test "$makefile_am" = Makefile.am; then
4046       echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
4047     else
4048       echo "  - \"include $makefile_name\" from within \"$testsbase/Makefile.am\","
4049     fi
4050   fi
4051   edit=0
4052   while test $edit != $makefile_am_edits; do
4053     edit=`expr $edit + 1`
4054     eval dir=\"\$makefile_am_edit${edit}_dir\"
4055     eval var=\"\$makefile_am_edit${edit}_var\"
4056     eval val=\"\$makefile_am_edit${edit}_val\"
4057     if test -n "$var"; then
4058       echo "  - mention \"${val}\" in ${var} in ${dir}Makefile.am,"
4059     fi
4060   done
4061   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
4062   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
4063 }
4064
4065 # func_create_testdir testdir modules
4066 # Input:
4067 # - local_gnulib_dir  from --local-dir
4068 # - auxdir          directory relative to destdir where to place build aux files
4069 func_create_testdir ()
4070 {
4071   testdir="$1"
4072   modules="$2"
4073   if test -z "$modules"; then
4074     # All modules together.
4075     # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
4076     # Except ftruncate, mountlist, which abort the configuration on mingw. FIXME.
4077     modules=`func_all_modules`
4078     modules=`for m in $modules; do case $m in config-h | ftruncate | mountlist) ;; *) echo $m;; esac; done`
4079   fi
4080   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort -u`
4081
4082   # Check that the license of every module is consistent with the license of
4083   # its dependencies.
4084   saved_modules="$modules"
4085   saved_inctests="$inctests"
4086   # When computing transitive closures, don't consider $module to depend on
4087   # $module-tests. Need this becauses tests are implicitly GPL and may depend
4088   # on GPL modules - therefore we don't want a warning in this case.
4089   inctests=""
4090   for requested_module in $saved_modules; do
4091     requested_license=`func_get_license "$requested_module"`
4092     if test "$requested_license" != GPL; then
4093       # Here we use func_modules_transitive_closure, not just
4094       # func_get_dependencies, so that we also detect weird situations like
4095       # an LGPL module which depends on a GPLed build tool module which depends
4096       # on a GPL module.
4097       modules="$requested_module"
4098       func_modules_transitive_closure
4099       for module in $modules; do
4100         license=`func_get_license "$module"`
4101         case "$license" in
4102           'GPLed build tool') ;;
4103           'public domain' | 'unlimited' | 'unmodifiable license text') ;;
4104           *)
4105             case "$requested_license" in
4106               GPLv2+)
4107                 case "$license" in
4108                   GPLv2+ | LGPLv2+) ;;
4109                   *) func_warning "module $requested_module depends on a module with an incompatible license: $module" ;;
4110                 esac
4111                 ;;
4112               LGPL)
4113                 case "$license" in
4114                   LGPL | LGPLv2+) ;;
4115                   *) func_warning "module $requested_module depends on a module with an incompatible license: $module" ;;
4116                 esac
4117                 ;;
4118               LGPLv2+)
4119                 case "$license" in
4120                   LGPLv2+) ;;
4121                   *) func_warning "module $requested_module depends on a module with an incompatible license: $module" ;;
4122                 esac
4123                 ;;
4124             esac
4125             ;;
4126         esac
4127       done
4128     fi
4129   done
4130   modules="$saved_modules"
4131   inctests="$saved_inctests"
4132
4133   # Subdirectory names.
4134   sourcebase=gllib
4135   m4base=glm4
4136   pobase=
4137   docbase=gldoc
4138   testsbase=gltests
4139   macro_prefix=gl
4140   po_domain=
4141   vc_files=
4142
4143   # Determine final module list.
4144   func_modules_transitive_closure
4145   if test $verbose -ge 0; then
4146     echo "Module list with included dependencies:"
4147     echo "$modules" | sed -e 's/^/  /'
4148   fi
4149
4150   # Add the dummy module if needed.
4151   func_modules_add_dummy
4152
4153   # Show banner notice of every module.
4154   func_modules_notice
4155
4156   # Determine final file list.
4157   func_modules_to_filelist
4158   if test $verbose -ge 0; then
4159     echo "File list:"
4160     echo "$files" | sed -e 's/^/  /'
4161   fi
4162
4163   rewritten='%REWRITTEN%'
4164   sed_rewrite_files="\
4165     s,^build-aux/,$rewritten$auxdir/,
4166     s,^doc/,$rewritten$docbase/,
4167     s,^lib/,$rewritten$sourcebase/,
4168     s,^m4/,$rewritten$m4base/,
4169     s,^tests/,$rewritten$testsbase/,
4170     s,^top/,$rewritten,
4171     s,^$rewritten,,"
4172
4173   # Create directories.
4174   for f in $files; do echo $f; done \
4175     | sed -e "$sed_rewrite_files" \
4176     | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
4177     | LC_ALL=C sort -u \
4178     > "$tmp"/dirs
4179   { # Rearrange file descriptors. Needed because "while ... done < ..."
4180     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
4181     exec 5<&0 < "$tmp"/dirs
4182     while read d; do
4183       mkdir -p "$testdir/$d"
4184     done
4185     exec 0<&5 5<&-
4186   }
4187
4188   # Copy files or make symbolic links.
4189   delimiter='   '
4190   for f in $files; do echo $f; done \
4191     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_files" \
4192     | LC_ALL=C sort \
4193     > "$tmp"/files
4194   { # Rearrange file descriptors. Needed because "while ... done < ..."
4195     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
4196     exec 5<&0 < "$tmp"/files
4197     while read g f; do
4198       func_lookup_file "$f"
4199       if test -n "$lookedup_tmp"; then
4200         cp -p "$lookedup_file" "$testdir/$g"
4201       else
4202         ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
4203         if { test -n "$symbolic" \
4204              || { test -n "$lsymbolic" \
4205                   && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; }; then
4206           func_ln "$lookedup_file" "$testdir/$g"
4207         else
4208           cp -p "$lookedup_file" "$testdir/$g"
4209         fi
4210       fi
4211     done
4212     exec 0<&5 5<&-
4213   }
4214
4215   # Create Makefile.ams that are for testing.
4216   for_test=true
4217
4218   # No special edits are needed.
4219   makefile_am_edits=0
4220
4221   # Create $sourcebase/Makefile.am.
4222   mkdir -p "$testdir/$sourcebase"
4223   destfile="$sourcebase/Makefile.am"
4224   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
4225   any_uses_subdirs="$uses_subdirs"
4226
4227   # Create $m4base/Makefile.am.
4228   mkdir -p "$testdir/$m4base"
4229   (echo "## Process this file with automake to produce Makefile.in."
4230    echo
4231    echo "EXTRA_DIST ="
4232    for f in $files; do
4233      case "$f" in
4234        m4/* )
4235          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
4236      esac
4237    done
4238   ) > "$testdir/$m4base/Makefile.am"
4239
4240   subdirs="$sourcebase $m4base"
4241   subdirs_with_configure_ac=""
4242
4243   if false && test -f "$testdir"/$m4base/gettext.m4; then
4244     # Avoid stupid error message from automake:
4245     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
4246     mkdir -p "$testdir/po"
4247     (echo "## Process this file with automake to produce Makefile.in."
4248     ) > "$testdir/po/Makefile.am"
4249     func_append subdirs " po"
4250   fi
4251
4252   if test -n "$inctests"; then
4253     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
4254     # Viewed from the $testsbase subdirectory, $auxdir is different.
4255     saved_auxdir="$auxdir"
4256     auxdir=`echo "$testsbase/" | sed -e 's%[^/][^/]*//*%../%g'`"$auxdir"
4257     # Create $testsbase/Makefile.am.
4258     use_libtests=false
4259     destfile="$testsbase/Makefile.am"
4260     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
4261     any_uses_subdirs="$any_uses_subdirs$uses_subdirs"
4262     # Create $testsbase/configure.ac.
4263     (echo "# Process this file with autoconf to produce a configure script."
4264      echo "AC_INIT([dummy], [0])"
4265      echo "AC_CONFIG_AUX_DIR([$auxdir])"
4266      echo "AM_INIT_AUTOMAKE"
4267      echo
4268      echo "AC_CONFIG_HEADERS([config.h])"
4269      echo
4270      echo "AC_PROG_CC"
4271      echo "AC_PROG_INSTALL"
4272      echo "AC_PROG_MAKE_SET"
4273      echo "AC_PROG_RANLIB"
4274      echo
4275      if test -n "$uses_subdirs"; then
4276        echo "AM_PROG_CC_C_O"
4277        echo
4278      fi
4279      for module in $modules; do
4280        func_verify_module
4281        if test -n "$module"; then
4282          case $module in
4283            gnumakefile | maintainer-makefile)
4284              # These modules are meant to be used only in the top-level directory.
4285              ;;
4286            *)
4287              func_get_autoconf_early_snippet "$module"
4288              ;;
4289          esac
4290        fi
4291      done \
4292        | sed -e '/^$/d;' -e 's/AC_REQUIRE(\[\([^()]*\)\])/\1/'
4293      if test "$libtool" = true; then
4294        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
4295        echo "gl_cond_libtool=true"
4296      else
4297        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
4298        echo "gl_cond_libtool=false"
4299        echo "gl_libdeps="
4300        echo "gl_ltlibdeps="
4301      fi
4302      # Wrap the set of autoconf snippets into an autoconf macro that is then
4303      # invoked. This is needed because autoconf does not support AC_REQUIRE
4304      # at the top level:
4305      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
4306      # but we want the AC_REQUIRE to have its normal meaning (provide one
4307      # expansion of the required macro before the current point, and only one
4308      # expansion total).
4309      echo "AC_DEFUN([gl_INIT], ["
4310      sed_replace_build_aux='
4311        :a
4312        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
4313          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
4314          ba
4315        }'
4316      func_emit_initmacro_start $macro_prefix
4317      # We don't have explicit ordering constraints between the various
4318      # autoconf snippets. It's cleanest to put those of the library before
4319      # those of the tests.
4320      echo "gl_source_base='../$sourcebase'"
4321      for module in $modules; do
4322        func_verify_nontests_module
4323        if test -n "$module"; then
4324          case $module in
4325            gnumakefile | maintainer-makefile)
4326              # These modules are meant to be used only in the top-level directory.
4327              ;;
4328            *)
4329              func_get_autoconf_snippet "$module" \
4330                | sed -e "$sed_replace_build_aux"
4331              ;;
4332          esac
4333        fi
4334      done
4335      echo "gl_source_base='.'"
4336      for module in $modules; do
4337        func_verify_tests_module
4338        if test -n "$module"; then
4339          func_get_autoconf_snippet "$module" \
4340            | sed -e "$sed_replace_build_aux"
4341        fi
4342      done
4343      func_emit_initmacro_end $macro_prefix
4344      # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
4345      # created using libtool, because libtool already handles the dependencies.
4346      if test "$libtool" != true; then
4347        libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z-' 'A-Z_'`
4348        echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
4349        echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
4350        echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
4351        echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
4352      fi
4353      echo "])"
4354      func_emit_initmacro_done $macro_prefix $sourcebase # FIXME use $sourcebase or $testsbase?
4355      echo
4356      echo "gl_INIT"
4357      echo
4358      # Usually $testsbase/config.h will be a superset of config.h. Verify this
4359      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
4360      echo "AH_TOP([#include \"../config.h\"])"
4361      echo
4362      echo "AC_CONFIG_FILES([Makefile])"
4363      echo "AC_OUTPUT"
4364     ) > "$testdir/$testsbase/configure.ac"
4365     auxdir="$saved_auxdir"
4366     func_append subdirs " $testsbase"
4367     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
4368   fi
4369
4370   # Create Makefile.am.
4371   (echo "## Process this file with automake to produce Makefile.in."
4372    echo
4373    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
4374    echo
4375    echo "SUBDIRS = $subdirs"
4376    echo
4377    echo "ACLOCAL_AMFLAGS = -I $m4base"
4378   ) > "$testdir/Makefile.am"
4379
4380   # Create configure.ac.
4381   (echo "# Process this file with autoconf to produce a configure script."
4382    echo "AC_INIT([dummy], [0])"
4383    if test "$auxdir" != "."; then
4384      echo "AC_CONFIG_AUX_DIR([$auxdir])"
4385    fi
4386    echo "AM_INIT_AUTOMAKE"
4387    echo
4388    echo "AC_CONFIG_HEADERS([config.h])"
4389    echo
4390    echo "AC_PROG_CC"
4391    echo "AC_PROG_INSTALL"
4392    echo "AC_PROG_MAKE_SET"
4393    echo
4394    echo "# For autobuild."
4395    echo "AC_CANONICAL_BUILD"
4396    echo "AC_CANONICAL_HOST"
4397    echo
4398    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
4399    echo "m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
4400    echo "m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
4401    echo "m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
4402    echo
4403    echo "AC_PROG_RANLIB"
4404    echo
4405    if test -n "$any_uses_subdirs"; then
4406      echo "AM_PROG_CC_C_O"
4407      echo
4408    fi
4409    for module in $modules; do
4410      func_verify_nontests_module
4411      if test -n "$module"; then
4412        func_get_autoconf_early_snippet "$module"
4413      fi
4414    done \
4415      | sed -e '/^$/d;' -e 's/AC_REQUIRE(\[\([^()]*\)\])/\1/'
4416    if test "$libtool" = true; then
4417      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
4418      echo "gl_cond_libtool=true"
4419    else
4420      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
4421      echo "gl_cond_libtool=false"
4422      echo "gl_libdeps="
4423      echo "gl_ltlibdeps="
4424    fi
4425    # Wrap the set of autoconf snippets into an autoconf macro that is then
4426    # invoked. This is needed because autoconf does not support AC_REQUIRE
4427    # at the top level:
4428    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
4429    # but we want the AC_REQUIRE to have its normal meaning (provide one
4430    # expansion of the required macro before the current point, and only one
4431    # expansion total).
4432    echo "AC_DEFUN([gl_INIT], ["
4433    if test "$auxdir" != "build-aux"; then
4434      sed_replace_build_aux='
4435        :a
4436        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
4437          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
4438          ba
4439        }'
4440    else
4441      sed_replace_build_aux="$sed_noop"
4442    fi
4443    func_emit_initmacro_start $macro_prefix
4444    echo "gl_source_base='$sourcebase'"
4445    for module in $modules; do
4446      func_verify_nontests_module
4447      if test -n "$module"; then
4448        func_get_autoconf_snippet "$module" \
4449          | sed -e "$sed_replace_build_aux"
4450      fi
4451    done
4452    func_emit_initmacro_end $macro_prefix
4453    # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
4454    # created using libtool, because libtool already handles the dependencies.
4455    if test "$libtool" != true; then
4456      libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z-' 'A-Z_'`
4457      echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
4458      echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
4459      echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
4460      echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
4461    fi
4462    echo "])"
4463    func_emit_initmacro_done $macro_prefix $sourcebase
4464    echo
4465    echo "gl_INIT"
4466    echo
4467    if test -n "$subdirs_with_configure_ac"; then
4468      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
4469    fi
4470    makefiles="Makefile"
4471    for d in $subdirs; do
4472      # For subdirs that have a configure.ac by their own, it's the subdir's
4473      # configure.ac which creates the subdir's Makefile.am, not this one.
4474      case " $subdirs_with_configure_ac " in
4475        *" $d "*) ;;
4476        *) func_append makefiles " $d/Makefile" ;;
4477      esac
4478    done
4479    echo "AC_CONFIG_FILES([$makefiles])"
4480    echo "AC_OUTPUT"
4481   ) > "$testdir/configure.ac"
4482
4483   # Create autogenerated files.
4484   (cd "$testdir"
4485    # Do not use "${AUTORECONF} --force --install", because it may invoke
4486    # autopoint, which brings in older versions of some of our .m4 files.
4487    if test -f $m4base/gettext.m4; then
4488      func_execute_command ${AUTOPOINT} --force || func_exit 1
4489      for f in $m4base/*.m4~; do
4490        if test -f $f; then
4491          mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
4492        fi
4493      done
4494    fi
4495    func_execute_command ${ACLOCAL} -I $m4base || func_exit 1
4496    if ! test -d build-aux; then
4497      func_execute_command mkdir build-aux || func_exit 1
4498    fi
4499    func_execute_command ${AUTOCONF} || func_exit 1
4500    func_execute_command ${AUTOHEADER} || func_exit 1
4501    func_execute_command ${AUTOMAKE} --add-missing --copy || func_exit 1
4502   ) || func_exit 1
4503   if test -n "$inctests"; then
4504     # Create autogenerated files.
4505     (cd "$testdir/$testsbase" || func_exit 1
4506      # Do not use "${AUTORECONF} --force --install", because it may invoke
4507      # autopoint, which brings in older versions of some of our .m4 files.
4508      if test -f ../$m4base/gettext.m4; then
4509        func_execute_command ${AUTOPOINT} --force || func_exit 1
4510        for f in ../$m4base/*.m4~; do
4511          if test -f $f; then
4512            mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
4513          fi
4514        done
4515      fi
4516      func_execute_command ${ACLOCAL} -I ../$m4base || func_exit 1
4517      if ! test -d ../build-aux; then
4518        func_execute_command mkdir ../build-aux
4519      fi
4520      func_execute_command ${AUTOCONF} || func_exit 1
4521      func_execute_command ${AUTOHEADER} || func_exit 1
4522      func_execute_command ${AUTOMAKE} --add-missing --copy || func_exit 1
4523     ) || func_exit 1
4524   fi
4525   # Need to run configure and make once, to create built files that are to be
4526   # distributed (such as getdate.c).
4527   # Extract the value of "CLEANFILES += ..." and "MOSTLYCLEANFILES += ...".
4528   cleaned_files=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
4529                  | sed -n -e 's,^CLEANFILES[     ]*+=\([^#]*\).*$,\1,p' -e 's,^MOSTLYCLEANFILES[         ]*+=\([^#]*\).*$,\1,p'`
4530   cleaned_files=`for file in $cleaned_files; do echo " $file "; done`
4531   # Extract the value of "BUILT_SOURCES += ...". Remove variable references
4532   # such $(FOO_H) because they don't refer to distributed files.
4533   sed_remove_make_variables='s,[$]([A-Za-z0-9_]*),,g'
4534   built_sources=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
4535                  | sed -n -e 's,^BUILT_SOURCES[  ]*+=\([^#]*\).*$,\1,p' \
4536                  | sed -e "$sed_remove_make_variables"`
4537   distributed_built_sources=`for file in $built_sources; do
4538                                case "$cleaned_files" in
4539                                  *" "$file" "*) ;;
4540                                  *) echo $file ;;
4541                                esac;
4542                              done`
4543   if test -n "$distributed_built_sources"; then
4544     (cd "$testdir"
4545      ./configure || func_exit 1
4546        cd "$sourcebase"
4547        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
4548        $MAKE built_sources || func_exit 1
4549        cd ..
4550      $MAKE distclean || func_exit 1
4551     ) || func_exit 1
4552   fi
4553 }
4554
4555 # func_create_megatestdir megatestdir allmodules
4556 # Input:
4557 # - local_gnulib_dir  from --local-dir
4558 # - auxdir          directory relative to destdir where to place build aux files
4559 func_create_megatestdir ()
4560 {
4561   megatestdir="$1"
4562   allmodules="$2"
4563   if test -z "$allmodules"; then
4564     allmodules=`func_all_modules`
4565   fi
4566
4567   megasubdirs=
4568   # First, all modules one by one.
4569   for onemodule in $allmodules; do
4570     func_create_testdir "$megatestdir/$onemodule" $onemodule
4571     func_append megasubdirs "$onemodule "
4572   done
4573   # Then, all modules all together.
4574   # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
4575   allmodules=`for m in $allmodules; do if test $m != config-h; then echo $m; fi; done`
4576   func_create_testdir "$megatestdir/ALL" "$allmodules"
4577   func_append megasubdirs "ALL"
4578
4579   # Create autobuild.
4580   cvsdate=`if test -f "$gnulib_dir/CVS/Entries"; then \
4581              vc_witness="$gnulib_dir/CVS/Entries"; \
4582            else \
4583              vc_witness="$gnulib_dir/.git/refs/heads/master"; \
4584            fi; \
4585            sh "$gnulib_dir/build-aux/mdate-sh" "$vc_witness" \
4586              | sed -e 's,January,01,'   -e 's,Jan,01,' \
4587                    -e 's,February,02,'  -e 's,Feb,02,' \
4588                    -e 's,March,03,'     -e 's,Mar,03,' \
4589                    -e 's,April,04,'     -e 's,Apr,04,' \
4590                    -e 's,May,05,'                      \
4591                    -e 's,June,06,'      -e 's,Jun,06,' \
4592                    -e 's,July,07,'      -e 's,Jul,07,' \
4593                    -e 's,August,08,'    -e 's,Aug,08,' \
4594                    -e 's,September,09,' -e 's,Sep,09,' \
4595                    -e 's,October,10,'   -e 's,Oct,10,' \
4596                    -e 's,November,11,'  -e 's,Nov,11,' \
4597                    -e 's,December,12,'  -e 's,Dec,12,' \
4598                    -e 's,^,00,' -e 's,^[0-9]*\([0-9][0-9] \),\1,' \
4599                    -e 's,^\([0-9]*\) \([0-9]*\) \([0-9]*\),\3\2\1,'`
4600   (echo '#!/bin/sh'
4601    echo "CVSDATE=$cvsdate"
4602    echo ": \${MAKE=make}"
4603    echo "test -d logs || mkdir logs"
4604    echo "for module in $megasubdirs; do"
4605    echo "  echo \"Working on module \$module...\""
4606    echo "  safemodule=\`echo \$module | sed -e 's|/|-|g'\`"
4607    echo "  (echo \"To: gnulib@autobuild.josefsson.org\""
4608    echo "   echo"
4609    echo "   set -x"
4610    echo "   : autobuild project... \$module"
4611    echo "   : autobuild revision... cvs-\$CVSDATE-000000"
4612    echo "   : autobuild timestamp... \`date \"+%Y%m%d-%H%M%S\"\`"
4613    echo "   : autobuild hostname... \`hostname\`"
4614    echo "   cd \$module && ./configure \$CONFIGURE_OPTIONS && \$MAKE && \$MAKE check && \$MAKE distclean"
4615    echo "   echo rc=\$?"
4616    echo "  ) 2>&1 | { if test -n \"\$AUTOBUILD_SUBST\"; then sed -e \"\$AUTOBUILD_SUBST\"; else cat; fi; } > logs/\$safemodule"
4617    echo "done"
4618   ) > "$megatestdir/do-autobuild"
4619   chmod a+x "$megatestdir/do-autobuild"
4620
4621   # Create Makefile.am.
4622   (echo "## Process this file with automake to produce Makefile.in."
4623    echo
4624    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
4625    echo
4626    echo "SUBDIRS = $megasubdirs"
4627    echo
4628    echo "EXTRA_DIST = do-autobuild"
4629   ) > "$megatestdir/Makefile.am"
4630
4631   # Create configure.ac.
4632   (echo "# Process this file with autoconf to produce a configure script."
4633    echo "AC_INIT([dummy], [0])"
4634    if test "$auxdir" != "."; then
4635      echo "AC_CONFIG_AUX_DIR([$auxdir])"
4636    fi
4637    echo "AM_INIT_AUTOMAKE"
4638    echo
4639    echo "AC_PROG_MAKE_SET"
4640    echo
4641    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
4642    echo "AC_CONFIG_FILES([Makefile])"
4643    echo "AC_OUTPUT"
4644   ) > "$megatestdir/configure.ac"
4645
4646   # Create autogenerated files.
4647   (cd "$megatestdir"
4648    # Do not use "${AUTORECONF} --install", because autoreconf operates
4649    # recursively, but the subdirectories are already finished, therefore
4650    # calling autoreconf here would only waste lots of CPU time.
4651    func_execute_command ${ACLOCAL} || func_exit 1
4652    func_execute_command mkdir build-aux
4653    func_execute_command ${AUTOCONF} || func_exit 1
4654    func_execute_command ${AUTOMAKE} --add-missing --copy || func_exit 1
4655   ) || func_exit 1
4656 }
4657
4658 case $mode in
4659   "" )
4660     func_fatal_error "no mode specified" ;;
4661
4662   list )
4663     func_all_modules
4664     ;;
4665
4666   import | update )
4667
4668     # Where to import.
4669     if test -z "$destdir"; then
4670       destdir=.
4671     fi
4672     test -d "$destdir" \
4673       || func_fatal_error "destination directory does not exist: $destdir"
4674
4675     # Prefer configure.ac to configure.in.
4676     if test -f "$destdir"/configure.ac; then
4677       configure_ac="$destdir/configure.ac"
4678     else
4679       if test -f "$destdir"/configure.in; then
4680         configure_ac="$destdir/configure.in"
4681       else
4682         func_fatal_error "cannot find $destdir/configure.ac - make sure you run gnulib-tool from within your package's directory"
4683       fi
4684     fi
4685
4686     # Analyze configure.ac.
4687     guessed_auxdir="."
4688     guessed_libtool=false
4689     my_sed_traces='
4690       s,#.*$,,
4691       s,^dnl .*$,,
4692       s, dnl .*$,,
4693       /AC_CONFIG_AUX_DIR/ {
4694         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^]"$`\\)]*\).*$,guessed_auxdir="\1",p
4695       }
4696       /A[CM]_PROG_LIBTOOL/ {
4697         s,^.*$,guessed_libtool=true,p
4698       }'
4699     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
4700
4701     if test -z "$auxdir"; then
4702       auxdir="$guessed_auxdir"
4703     fi
4704
4705     # Determine where to apply func_import.
4706     if test -n "$m4base"; then
4707       # Apply func_import to a particular gnulib directory.
4708       # Any number of additional modules can be given.
4709       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
4710         # First use of gnulib in the given m4base.
4711         test -n "$supplied_libname" || supplied_libname=true
4712         test -n "$sourcebase" || sourcebase="lib"
4713         test -n "$docbase" || docbase="doc"
4714         test -n "$testsbase" || testsbase="tests"
4715         test -n "$macro_prefix" || macro_prefix="gl"
4716       fi
4717       func_import "$*"
4718     else
4719       # Apply func_import to all gnulib directories.
4720       # To get this list of directories, look at Makefile.am. (Not at
4721       # configure, because it may be omitted from CVS. Also, don't run
4722       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
4723       m4dirs=
4724       m4dirs_count=0
4725       if test -f "$destdir"/Makefile.am; then
4726         aclocal_amflags=`sed -n -e 's/^ACLOCAL_AMFLAGS[  ]*=\(.*\)$/\1/p' "$destdir"/Makefile.am`
4727         m4dir_is_next=
4728         for arg in $aclocal_amflags; do
4729           if test -n "$m4dir_is_next"; then
4730             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
4731             case "$arg" in
4732               /*) ;;
4733               *)
4734                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
4735                   func_append m4dirs " $arg"
4736                   m4dirs_count=`expr $m4dirs_count + 1`
4737                 fi
4738                 ;;
4739             esac
4740             m4dir_is_next=
4741           else
4742             if test "X$arg" = "X-I"; then
4743               m4dir_is_next=yes
4744             else
4745               m4dir_is_next=
4746             fi
4747           fi
4748         done
4749       else
4750         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
4751         if test -f "$destdir"/aclocal.m4; then
4752           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
4753           sedexpr2='s,^[^/]*$,.,'
4754           sedexpr3='s,/[^/]*$,,'
4755           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort -u`
4756           m4dirs_count=`printf %s "$m4dirs" | wc -l`
4757         fi
4758       fi
4759       if test $m4dirs_count = 0; then
4760         # First use of gnulib in a package.
4761         # Any number of additional modules can be given.
4762         test -n "$supplied_libname" || supplied_libname=true
4763         test -n "$sourcebase" || sourcebase="lib"
4764         m4base="m4"
4765         test -n "$docbase" || docbase="doc"
4766         test -n "$testsbase" || testsbase="tests"
4767         test -n "$macro_prefix" || macro_prefix="gl"
4768         func_import "$*"
4769       else
4770         if test $m4dirs_count = 1; then
4771           # There's only one use of gnulib here. Assume the user means it.
4772           # Any number of additional modules can be given.
4773           for m4base in $m4dirs; do
4774             func_import "$*"
4775           done
4776         else
4777           # Ambiguous - guess what the user meant.
4778           if test $# = 0; then
4779             # No further arguments. Guess the user wants to update all of them.
4780             for m4base in $m4dirs; do
4781               # Perform func_import in a subshell, so that variable values
4782               # such as
4783               #   local_gnulib_dir, incobsolete, avoidlist, sourcebase, m4base,
4784               #   pobase, docbase, testsbase, inctests, libname, lgpl,
4785               #   makefile_name, libtool, macro_prefix, po_domain, vc_files
4786               # don't propagate from one directory to another.
4787               (func_import) || func_exit 1
4788             done
4789           else
4790             # Really ambiguous.
4791             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
4792           fi
4793         fi
4794       fi
4795     fi
4796     ;;
4797
4798   create-testdir )
4799     if test -z "$destdir"; then
4800       func_fatal_error "please specify --dir option"
4801     fi
4802     mkdir "$destdir"
4803     test -d "$destdir" \
4804       || func_fatal_error "could not create destination directory"
4805     test -n "$auxdir" || auxdir="build-aux"
4806     func_create_testdir "$destdir" "$*"
4807     ;;
4808
4809   create-megatestdir )
4810     if test -z "$destdir"; then
4811       func_fatal_error "please specify --dir option"
4812     fi
4813     mkdir "$destdir" || func_fatal_error "could not create destination directory"
4814     test -n "$auxdir" || auxdir="build-aux"
4815     func_create_megatestdir "$destdir" "$*"
4816     ;;
4817
4818   test )
4819     test -n "$destdir" || destdir=testdir$$
4820     mkdir "$destdir" || func_fatal_error "could not create destination directory"
4821     test -n "$auxdir" || auxdir="build-aux"
4822     func_create_testdir "$destdir" "$*"
4823     cd "$destdir"
4824       mkdir build
4825       cd build
4826         ../configure || func_exit 1
4827         $MAKE || func_exit 1
4828         $MAKE check || func_exit 1
4829         $MAKE distclean || func_exit 1
4830         remaining=`find . -type f -print`
4831         if test -n "$remaining"; then
4832           echo "Remaining files:" $remaining 1>&2
4833           echo "gnulib-tool: *** Stop." 1>&2
4834           func_exit 1
4835         fi
4836       cd ..
4837     cd ..
4838     rm -rf "$destdir"
4839     ;;
4840
4841   megatest )
4842     test -n "$destdir" || destdir=testdir$$
4843     mkdir "$destdir" || func_fatal_error "could not create destination directory"
4844     test -n "$auxdir" || auxdir="build-aux"
4845     func_create_megatestdir "$destdir" "$*"
4846     cd "$destdir"
4847       mkdir build
4848       cd build
4849         ../configure
4850         $MAKE
4851         $MAKE check
4852         $MAKE distclean
4853         remaining=`find . -type f -print`
4854         if test -n "$remaining"; then
4855           echo "Remaining files:" $remaining 1>&2
4856           echo "gnulib-tool: *** Stop." 1>&2
4857           func_exit 1
4858         fi
4859       cd ..
4860     cd ..
4861     rm -rf "$destdir"
4862     ;;
4863
4864   extract-description )
4865     for module
4866     do
4867       func_verify_module
4868       if test -n "$module"; then
4869         func_get_description "$module"
4870       fi
4871     done
4872     ;;
4873
4874   extract-status )
4875     for module
4876     do
4877       func_verify_module
4878       if test -n "$module"; then
4879         func_get_status "$module"
4880       fi
4881     done
4882     ;;
4883
4884   extract-notice )
4885     for module
4886     do
4887       func_verify_module
4888       if test -n "$module"; then
4889         func_get_notice "$module"
4890       fi
4891     done
4892     ;;
4893
4894   extract-applicability )
4895     for module
4896     do
4897       func_verify_module
4898       if test -n "$module"; then
4899         func_get_applicability "$module"
4900       fi
4901     done
4902     ;;
4903
4904   extract-filelist )
4905     for module
4906     do
4907       func_verify_module
4908       if test -n "$module"; then
4909         func_get_filelist "$module"
4910       fi
4911     done
4912     ;;
4913
4914   extract-dependencies )
4915     for module
4916     do
4917       func_verify_module
4918       if test -n "$module"; then
4919         func_get_dependencies "$module"
4920       fi
4921     done
4922     ;;
4923
4924   extract-autoconf-snippet )
4925     for module
4926     do
4927       func_verify_module
4928       if test -n "$module"; then
4929         func_get_autoconf_snippet "$module"
4930       fi
4931     done
4932     ;;
4933
4934   extract-automake-snippet )
4935     for module
4936     do
4937       func_verify_module
4938       if test -n "$module"; then
4939         func_get_automake_snippet "$module"
4940       fi
4941     done
4942     ;;
4943
4944   extract-include-directive )
4945     for module
4946     do
4947       func_verify_module
4948       if test -n "$module"; then
4949         func_get_include_directive "$module"
4950       fi
4951     done
4952     ;;
4953
4954   extract-link-directive )
4955     for module
4956     do
4957       func_verify_module
4958       if test -n "$module"; then
4959         func_get_link_directive "$module"
4960       fi
4961     done
4962     ;;
4963
4964   extract-license )
4965     for module
4966     do
4967       func_verify_module
4968       if test -n "$module"; then
4969         func_get_license "$module"
4970       fi
4971     done
4972     ;;
4973
4974   extract-maintainer )
4975     for module
4976     do
4977       func_verify_module
4978       if test -n "$module"; then
4979         func_get_maintainer "$module"
4980       fi
4981     done
4982     ;;
4983
4984   extract-tests-module )
4985     for module
4986     do
4987       func_verify_module
4988       if test -n "$module"; then
4989         func_get_tests_module "$module"
4990       fi
4991     done
4992     ;;
4993
4994   copy-file )
4995     # Verify the number of arguments.
4996     if test $# -lt 1 || test $# -gt 2; then
4997       func_fatal_error "invalid number of arguments for --$mode"
4998     fi
4999
5000     # The first argument is the file to be copied.
5001     f="$1"
5002     # Verify the file exists.
5003     func_lookup_file "$f"
5004
5005     # The second argument is the destination; either a directory ot a file.
5006     # It defaults to the current directory.
5007     dest="$2"
5008     test -n "$dest" || dest='.'
5009     test -n "$sourcebase" || sourcebase="lib"
5010     test -n "$m4base" || m4base="m4"
5011     test -n "$docbase" || docbase="doc"
5012     test -n "$testsbase" || testsbase="tests"
5013     test -n "$auxdir" || auxdir="build-aux"
5014     rewritten='%REWRITTEN%'
5015     sed_rewrite_files="\
5016       s,^build-aux/,$rewritten$auxdir/,
5017       s,^doc/,$rewritten$docbase/,
5018       s,^lib/,$rewritten$sourcebase/,
5019       s,^m4/,$rewritten$m4base/,
5020       s,^tests/,$rewritten$testsbase/,
5021       s,^top/,$rewritten,
5022       s,^$rewritten,,"
5023     if test -d "$dest"; then
5024       destdir="$dest"
5025       g=`echo "$f" | sed -e "$sed_rewrite_files"`
5026     else
5027       destdir=`dirname "$dest"`
5028       g=`basename "$dest"`
5029     fi
5030
5031     # Create the directory for destfile.
5032     d=`dirname "$destdir/$g"`
5033     if $doit; then
5034       if test -n "$d" && test ! -d "$d"; then
5035         mkdir -p "$d" || func_fatal_error "failed"
5036       fi
5037     fi
5038     # Copy the file.
5039     func_dest_tmpfilename "$g"
5040     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
5041     already_present=true
5042     if test -f "$destdir/$g"; then
5043       # The file already exists.
5044       func_update_file
5045     else
5046       # Install the file.
5047       # Don't protest if the file should be there but isn't: it happens
5048       # frequently that developers don't put autogenerated files into CVS.
5049       func_add_file
5050     fi
5051     rm -f "$tmpfile"
5052     ;;
5053
5054   * )
5055     func_fatal_error "unknown operation mode --$mode" ;;
5056 esac
5057
5058 rm -rf "$tmp"
5059 # Undo the effect of the previous 'trap' command. Some shellology:
5060 # We cannot use "trap - 0 1 2 3 13 15", because Solaris sh would attempt to
5061 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
5062 # exit); for the others we need to call 'exit' explicitly. The value of $? is
5063 # 128 + signal number and is set before the trap-registered command is run.
5064 trap '' 0
5065 trap 'func_exit $?' 1 2 3 13 15
5066
5067 exit 0
5068
5069 # Local Variables:
5070 # indent-tabs-mode: nil
5071 # whitespace-check-buffer-indent: nil
5072 # End: