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