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