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