autoupdate
[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 # prefix, suffix should not contain shell-special characters.
1432 # removed_prefix, removed_suffix should not contain the characters "$`\{}[]^|.
1433 # added_prefix, added_suffix should not contain the characters \|&.
1434 func_filter_filelist ()
1435 {
1436   if test "$2" != "$nl" \
1437      || { $fast_func_append \
1438           && { test -z "$6" || $fast_func_remove_prefix; } \
1439           && { test -z "$7" || $fast_func_remove_suffix; }; \
1440         }; then
1441     ffflist=
1442     for fff in $3; do
1443       # Do not quote possibly-empty parameters in case patterns,
1444       # AIX and HP-UX ksh won't match them if they are empty.
1445       case "$fff" in
1446         $4*$5)
1447           if test -n "$6"; then
1448             func_remove_prefix fff "$6"
1449           fi
1450           if test -n "$7"; then
1451             func_remove_suffix fff "$7"
1452           fi
1453           fff="$8${fff}$9"
1454           if test -z "$ffflist"; then
1455             ffflist="${fff}"
1456           else
1457             func_append ffflist "$2${fff}"
1458           fi
1459           ;;
1460       esac
1461     done
1462   else
1463     sed_fff_filter="s|^$6\(.*\)$7\$|$8\\1$9|"
1464     ffflist=`for fff in $3; do
1465                case "$fff" in
1466                  $4*$5) echo "$fff" ;;
1467                esac
1468              done | sed -e "$sed_fff_filter"`
1469   fi
1470   eval "$1=\"\$ffflist\""
1471 }
1472
1473 # func_get_dependencies module
1474 # Input:
1475 # - local_gnulib_dir  from --local-dir
1476 func_get_dependencies ()
1477 {
1478   # ${module}-tests always implicitly depends on ${module}.
1479   case "$1" in
1480     *-tests)
1481       fgd1="$1"
1482       func_remove_suffix fgd1 '-tests'
1483       echo "$fgd1"
1484       ;;
1485   esac
1486   # Then the explicit dependencies listed in the module description.
1487   func_lookup_file "modules/$1"
1488   sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
1489 }
1490
1491 # func_get_autoconf_early_snippet module
1492 # Input:
1493 # - local_gnulib_dir  from --local-dir
1494 func_get_autoconf_early_snippet ()
1495 {
1496   func_lookup_file "modules/$1"
1497   sed -n -e "/^configure\.ac-early$sed_extract_prog" < "$lookedup_file"
1498 }
1499
1500 # func_get_autoconf_snippet module
1501 # Input:
1502 # - local_gnulib_dir  from --local-dir
1503 func_get_autoconf_snippet ()
1504 {
1505   func_lookup_file "modules/$1"
1506   sed -n -e "/^configure\.ac$sed_extract_prog" < "$lookedup_file"
1507 }
1508
1509 # func_get_automake_snippet module
1510 # Input:
1511 # - local_gnulib_dir  from --local-dir
1512 func_get_automake_snippet ()
1513 {
1514   func_lookup_file "modules/$1"
1515   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file"
1516   case "$1" in
1517     *-tests)
1518       # *-tests module live in tests/, not lib/.
1519       # Synthesize an EXTRA_DIST augmentation.
1520       all_files=`func_get_filelist $1`
1521       func_filter_filelist tests_files " " "$all_files" 'tests/' '' 'tests/' ''
1522       extra_files="$tests_files"
1523       if test -n "$extra_files"; then
1524         echo "EXTRA_DIST +=" $extra_files
1525         echo
1526       fi
1527       ;;
1528     *)
1529       # Synthesize an EXTRA_DIST augmentation.
1530       sed_combine_lines='/\\$/{
1531         :a
1532         N
1533         s/\\\n/ /
1534         s/\\$/\\/
1535         ta
1536       }'
1537       sed_extract_mentioned_files='s/^lib_SOURCES[       ]*+=[   ]*//p'
1538       already_mentioned_files=` \
1539         sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file" \
1540         | sed -e "$sed_combine_lines" \
1541         | sed -n -e "$sed_extract_mentioned_files" | sed -e 's/#.*//'`
1542       all_files=`func_get_filelist $1`
1543       func_filter_filelist lib_files "$nl" "$all_files" 'lib/' '' 'lib/' ''
1544       # Remove $already_mentioned_files from $lib_files.
1545       echo "$lib_files" | LC_ALL=C sort -u > "$tmp"/lib-files
1546       extra_files=`func_reset_sigpipe; \
1547                    for f in $already_mentioned_files; do echo $f; done \
1548                    | LC_ALL=C sort -u | LC_ALL=C join -v 2 - "$tmp"/lib-files`
1549       if test -n "$extra_files"; then
1550         echo "EXTRA_DIST +=" $extra_files
1551         echo
1552       fi
1553       # Synthesize also an EXTRA_lib_SOURCES augmentation.
1554       # This is necessary so that automake can generate the right list of
1555       # dependency rules.
1556       # A possible approach would be to use autom4te --trace of the redefined
1557       # AC_LIBOBJ and AC_REPLACE_FUNCS macros when creating the Makefile.am
1558       # (use autom4te --trace, not just grep, so that AC_LIBOBJ invocations
1559       # inside autoconf's built-in macros are not missed).
1560       # But it's simpler and more robust to do it here, based on the file list.
1561       # If some .c file exists and is not used with AC_LIBOBJ - for example,
1562       # a .c file is preprocessed into another .c file for BUILT_SOURCES -,
1563       # automake will generate a useless dependency; this is harmless.
1564       case "$1" in
1565         relocatable-prog-wrapper) ;;
1566         *)
1567           func_filter_filelist extra_files "$nl" "$extra_files" '' '.c' '' ''
1568           if test -n "$extra_files"; then
1569             echo "EXTRA_lib_SOURCES +=" $extra_files
1570             echo
1571           fi
1572           ;;
1573       esac
1574       # Synthesize an EXTRA_DIST augmentation also for the files in build-aux/.
1575       func_filter_filelist buildaux_files "$nl" "$all_files" 'build-aux/' '' 'build-aux/' ''
1576       if test -n "$buildaux_files"; then
1577         sed_prepend_auxdir='s,^,$(top_srcdir)/'"$auxdir"'/,'
1578         echo "EXTRA_DIST += "`echo "$buildaux_files" | sed -e "$sed_prepend_auxdir"`
1579         echo
1580       fi
1581       # Synthesize an EXTRA_DIST augmentation also for the files from top/.
1582       func_filter_filelist top_files "$nl" "$all_files" 'top/' '' 'top/' ''
1583       if test -n "$top_files"; then
1584         sed_prepend_topdir='s,^,$(top_srcdir)/,'
1585         echo "EXTRA_DIST += "`echo "$top_files" | sed -e "$sed_prepend_topdir"`
1586         echo
1587       fi
1588       ;;
1589   esac
1590 }
1591
1592 # func_get_include_directive module
1593 # Input:
1594 # - local_gnulib_dir  from --local-dir
1595 func_get_include_directive ()
1596 {
1597   func_lookup_file "modules/$1"
1598   sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
1599   sed -e 's/^\(["<]\)/#include \1/'
1600 }
1601
1602 # func_get_link_directive module
1603 # Input:
1604 # - local_gnulib_dir  from --local-dir
1605 func_get_link_directive ()
1606 {
1607   func_lookup_file "modules/$1"
1608   sed -n -e "/^Link$sed_extract_prog" < "$lookedup_file"
1609 }
1610
1611 # func_get_license module
1612 # Input:
1613 # - local_gnulib_dir  from --local-dir
1614 func_get_license ()
1615 {
1616   func_lookup_file "modules/$1"
1617   { sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
1618     # The default is GPL.
1619     echo "GPL"
1620   } | sed -e 's,^ *$,,' | sed -e 1q
1621 }
1622
1623 # func_get_maintainer module
1624 # Input:
1625 # - local_gnulib_dir  from --local-dir
1626 func_get_maintainer ()
1627 {
1628   func_lookup_file "modules/$1"
1629   sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
1630 }
1631
1632 # func_get_tests_module module
1633 # Input:
1634 # - local_gnulib_dir  from --local-dir
1635 func_get_tests_module ()
1636 {
1637   # The naming convention for tests modules is hardwired: ${module}-tests.
1638   if test -f "$gnulib_dir/modules/$1"-tests \
1639      || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
1640           && test -f "$local_gnulib_dir/modules/$1"-tests; }; then
1641     echo "$1"-tests
1642   fi
1643 }
1644
1645 # func_acceptable module
1646 # tests whether a module is acceptable.
1647 # Input:
1648 # - avoidlist       list of modules to avoid
1649 func_acceptable ()
1650 {
1651   for avoid in $avoidlist; do
1652     if test "$avoid" = "$1"; then
1653       return 1
1654     fi
1655   done
1656   return 0
1657 }
1658
1659 # func_modules_transitive_closure
1660 # Input:
1661 # - local_gnulib_dir  from --local-dir
1662 # - modules         list of specified modules
1663 # - inctests        true if tests should be included, blank otherwise
1664 # - incobsolete     true if obsolete modules among dependencies should be
1665 #                   included, blank otherwise
1666 # - avoidlist       list of modules to avoid
1667 # - tmp             pathname of a temporary directory
1668 # Output:
1669 # - modules         list of modules, including dependencies
1670 func_modules_transitive_closure ()
1671 {
1672   # In order to process every module only once (for speed), process an "input
1673   # list" of modules, producing an "output list" of modules. During each round,
1674   # more modules can be queued in the input list. Once a module on the input
1675   # list has been processed, it is added to the "handled list", so we can avoid
1676   # to process it again.
1677   handledmodules=
1678   inmodules="$modules"
1679   outmodules=
1680   while test -n "$inmodules"; do
1681     inmodules_this_round="$inmodules"
1682     inmodules=                    # Accumulator, queue for next round
1683     for module in $inmodules_this_round; do
1684       func_verify_module
1685       if test -n "$module"; then
1686         if func_acceptable $module; then
1687           func_append outmodules " $module"
1688           deps=`func_get_dependencies $module`
1689           # Duplicate dependencies are harmless, but Jim wants a warning.
1690           duplicated_deps=`echo "$deps" | LC_ALL=C sort | LC_ALL=C uniq -d`
1691           if test -n "$duplicated_deps"; then
1692             echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
1693           fi
1694           for dep in $deps; do
1695             if test -n "$incobsolete" \
1696                || { status=`func_get_status $dep`; test "$status" != obsolete; }; then
1697               func_append inmodules " $dep"
1698             fi
1699           done
1700           if test -n "$inctests"; then
1701             testsmodule=`func_get_tests_module $module`
1702             if test -n "$testsmodule"; then
1703               func_append inmodules " $testsmodule"
1704             fi
1705           fi
1706         fi
1707       fi
1708     done
1709     handledmodules=`for m in $handledmodules $inmodules_this_round; do echo $m; done | LC_ALL=C sort -u`
1710     # Remove $handledmodules from $inmodules.
1711     for m in $inmodules; do echo $m; done | LC_ALL=C sort -u > "$tmp"/queued-modules
1712     inmodules=`func_reset_sigpipe; echo "$handledmodules" | LC_ALL=C join -v 2 - "$tmp"/queued-modules`
1713   done
1714   modules=`for m in $outmodules; do echo $m; done | LC_ALL=C sort -u`
1715   rm -f "$tmp"/queued-modules
1716 }
1717
1718 # func_modules_add_dummy
1719 # Input:
1720 # - local_gnulib_dir  from --local-dir
1721 # - modules         list of modules, including dependencies
1722 # Output:
1723 # - modules         list of modules, including 'dummy' if needed
1724 func_modules_add_dummy ()
1725 {
1726   # Determine whether any module provides a lib_SOURCES augmentation.
1727   have_lib_SOURCES=
1728   sed_remove_backslash_newline=':a
1729 /\\$/{
1730 s/\\$//
1731 N
1732 s/\n//
1733 ba
1734 }'
1735   for module in $modules; do
1736     func_verify_nontests_module
1737     if test -n "$module"; then
1738       # Extract the value of "lib_SOURCES += ...".
1739       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
1740         # Ignore .h files since they are not compiled.
1741         case "$file" in
1742           *.h) ;;
1743           *)
1744             have_lib_SOURCES=yes
1745             break 2
1746             ;;
1747         esac
1748       done
1749     fi
1750   done
1751   # Add the dummy module, to make sure the library will be non-empty.
1752   if test -z "$have_lib_SOURCES"; then
1753     if func_acceptable "dummy"; then
1754       func_append modules " dummy"
1755     fi
1756   fi
1757 }
1758
1759 # func_modules_notice
1760 # Input:
1761 # - local_gnulib_dir  from --local-dir
1762 # - verbose         integer, default 0, inc/decremented by --verbose/--quiet
1763 # - modules         list of modules, including dependencies
1764 func_modules_notice ()
1765 {
1766   if test $verbose -ge -1; then
1767     for module in $modules; do
1768       func_verify_module
1769       if test -n "$module"; then
1770         msg=`func_get_notice $module`
1771         if test -n "$msg"; then
1772           echo "Notice from module $module:"
1773           echo "$msg" | sed -e 's/^/  /'
1774         fi
1775       fi
1776     done
1777   fi
1778 }
1779
1780 # func_modules_to_filelist
1781 # Input:
1782 # - local_gnulib_dir  from --local-dir
1783 # - modules         list of modules, including dependencies
1784 # Output:
1785 # - files           list of files
1786 func_modules_to_filelist ()
1787 {
1788   files=
1789   for module in $modules; do
1790     func_verify_module
1791     if test -n "$module"; then
1792       fs=`func_get_filelist $module`
1793       func_append files " $fs"
1794     fi
1795   done
1796   files=`for f in $files; do echo $f; done | LC_ALL=C sort -u`
1797 }
1798
1799 # func_execute_command command [args...]
1800 # Executes a command.
1801 # Uses also the variables
1802 # - verbose         integer, default 0, inc/decremented by --verbose/--quiet
1803 func_execute_command ()
1804 {
1805   if test $verbose -ge 0; then
1806     echo "executing $*"
1807     "$@"
1808   else
1809     # Commands like automake produce output to stderr even when they succeed.
1810     # Turn this output off if the command succeeds.
1811     "$@" > "$tmp"/cmdout 2>&1
1812     cmdret=$?
1813     if test $cmdret = 0; then
1814       rm -f "$tmp"/cmdout
1815     else
1816       echo "executing $*"
1817       cat "$tmp"/cmdout 1>&2
1818       rm -f "$tmp"/cmdout
1819       (exit $cmdret)
1820     fi
1821   fi
1822 }
1823
1824 # func_dest_tmpfilename file
1825 # determines the name of a temporary file (file is relative to destdir).
1826 # Input:
1827 # - destdir         target directory
1828 # - doit            : if actions shall be executed, false if only to be printed
1829 # - tmp             pathname of a temporary directory
1830 # Sets variable:
1831 #   - tmpfile       absolute filename of the temporary file
1832 func_dest_tmpfilename ()
1833 {
1834   if $doit; then
1835     # Put the new contents of $file in a file in the same directory (needed
1836     # to guarantee that an 'mv' to "$destdir/$file" works).
1837     tmpfile="$destdir/$1.tmp"
1838   else
1839     # Put the new contents of $file in a file in a temporary directory
1840     # (because the directory of "$file" might not exist).
1841     tmpfile="$tmp"/`basename "$1"`.tmp
1842   fi
1843 }
1844
1845 # func_add_file
1846 # copies a file from gnulib into the destination directory. The destination
1847 # is known to not exist.
1848 # Input:
1849 # - destdir         target directory
1850 # - local_gnulib_dir  from --local-dir
1851 # - f               the original file name
1852 # - lookedup_file   name of the merged (combined) file
1853 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
1854 # - g               the rewritten file name
1855 # - tmpfile         absolute filename of the temporary file
1856 # - doit            : if actions shall be executed, false if only to be printed
1857 # - symbolic        true if files should be symlinked, copied otherwise
1858 # - lsymbolic       true if files from local_gnulib_dir should be symlinked,
1859 #                   copied otherwise
1860 func_add_file ()
1861 {
1862   if $doit; then
1863     echo "Copying file $g"
1864     if { test -n "$symbolic" \
1865          || { test -n "$lsymbolic" \
1866               && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
1867        && test -z "$lookedup_tmp" \
1868        && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1869       func_ln_if_changed "$lookedup_file" "$destdir/$g"
1870     else
1871       mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1872     fi
1873   else
1874     echo "Copy file $g"
1875   fi
1876 }
1877
1878 # func_update_file
1879 # copies a file from gnulib into the destination directory. The destination
1880 # is known to exist.
1881 # Input:
1882 # - destdir         target directory
1883 # - local_gnulib_dir  from --local-dir
1884 # - f               the original file name
1885 # - lookedup_file   name of the merged (combined) file
1886 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
1887 # - g               the rewritten file name
1888 # - tmpfile         absolute filename of the temporary file
1889 # - doit            : if actions shall be executed, false if only to be printed
1890 # - symbolic        true if files should be symlinked, copied otherwise
1891 # - lsymbolic       true if files from local_gnulib_dir should be symlinked,
1892 #                   copied otherwise
1893 # - already_present  nonempty if the file should already exist, empty otherwise
1894 func_update_file ()
1895 {
1896   if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1897     : # The file has not changed.
1898   else
1899     # Replace the file.
1900     if $doit; then
1901       if test -n "$already_present"; then
1902         echo "Updating file $g (backup in ${g}~)"
1903       else
1904         echo "Replacing file $g (non-gnulib code backed up in ${g}~) !!"
1905       fi
1906       mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1907       if { test -n "$symbolic" \
1908            || { test -n "$lsymbolic" \
1909                 && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
1910          && test -z "$lookedup_tmp" \
1911          && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1912         func_ln_if_changed "$lookedup_file" "$destdir/$g"
1913       else
1914         mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1915       fi
1916     else
1917       if test -n "$already_present"; then
1918         echo "Update file $g (backup in ${g}~)"
1919       else
1920         echo "Replace file $g (non-gnulib code backed up in ${g}~) !!"
1921       fi
1922     fi
1923   fi
1924 }
1925
1926 # func_emit_lib_Makefile_am
1927 # emits the contents of library makefile to standard output.
1928 # Input:
1929 # - local_gnulib_dir  from --local-dir
1930 # - modules         list of modules, including dependencies
1931 # - libname         library name
1932 # - pobase          directory relative to destdir where to place *.po files
1933 # - auxdir          directory relative to destdir where to place build aux files
1934 # - makefile_name   from --makefile-name
1935 # - libtool         true if libtool will be used, false or blank otherwise
1936 # - macro_prefix    prefix of gl_LIBOBJS macros to use
1937 # - po_domain       prefix of i18n domain to use (without -gnulib suffix)
1938 # - actioncmd       (optional) command that will reproduce this invocation
1939 # - for_test        true if creating a package for testing, false otherwise
1940 # - destfile        filename relative to destdir of makefile being generated
1941 # Input/Output:
1942 # - makefile_am_edits and makefile_am_edit${edit}_{dir,var,val}
1943 #                   list of edits to be done to Makefile.am variables
1944 # Output:
1945 # - uses_subdirs    nonempty if object files in subdirs exist
1946 func_emit_lib_Makefile_am ()
1947 {
1948   # When creating an includable Makefile.am snippet, augment variables with
1949   # += instead of assigning them.
1950   if test -n "$makefile_name"; then
1951     assign='+='
1952   else
1953     assign='='
1954   fi
1955   if test "$libtool" = true; then
1956     libext=la
1957     perhapsLT=LT
1958     sed_eliminate_LDFLAGS="$sed_noop"
1959   else
1960     libext=a
1961     perhapsLT=
1962     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
1963   fi
1964   if $for_test; then
1965     # When creating a package for testing: Attempt to provoke failures,
1966     # especially link errors, already during "make" rather than during
1967     # "make check", because "make check" is not possible in a cross-compiling
1968     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
1969     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
1970   else
1971     sed_transform_check_PROGRAMS="$sed_noop"
1972   fi
1973   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
1974   echo "## Process this file with automake to produce Makefile.in."
1975   func_emit_copyright_notice
1976   if test -n "$actioncmd"; then
1977     # The maximum line length (excluding the terminating newline) of any file
1978     # that is to be preprocessed by config.status is 3070.  config.status uses
1979     # awk, and the HP-UX 11.00 awk fails if a line has length >= 3071;
1980     # similarly, the IRIX 6.5 awk fails if a line has length >= 3072.
1981     len=`echo "$actioncmd" | wc -c`
1982     if test -n "$len" && test "$len" -le 3000; then
1983       echo "# Reproduce by: $actioncmd"
1984     fi
1985   fi
1986   echo
1987   uses_subdirs=
1988   {
1989     for module in $modules; do
1990       func_verify_nontests_module
1991       if test -n "$module"; then
1992         {
1993           func_get_automake_snippet "$module" |
1994             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1995                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' \
1996                 -e "$sed_eliminate_LDFLAGS" \
1997                 -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' \
1998                 -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1999                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' \
2000                 -e "$sed_transform_check_PROGRAMS"
2001           if test "$module" = 'alloca'; then
2002             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
2003             echo "${libname}_${libext}_DEPENDENCIES += @${perhapsLT}ALLOCA@"
2004           fi
2005         } > "$tmp"/amsnippet
2006         # Skip the contents if it's entirely empty.
2007         if grep '[^      ]' "$tmp"/amsnippet > /dev/null ; then
2008           echo "## begin gnulib module $module"
2009           echo
2010           cat "$tmp"/amsnippet
2011           echo "## end   gnulib module $module"
2012           echo
2013         fi
2014         rm -f "$tmp"/amsnippet
2015         # Test whether there are some source files in subdirectories.
2016         for f in `func_get_filelist "$module"`; do
2017           case $f in
2018             lib/*/*.c)
2019               uses_subdirs=yes
2020               break
2021               ;;
2022           esac
2023         done
2024       fi
2025     done
2026   } > "$tmp"/allsnippets
2027   if test -z "$makefile_name"; then
2028     # If there are source files in subdirectories, prevent collision of the
2029     # object files (example: hash.c and libxml/hash.c).
2030     subdir_options=
2031     if test -n "$uses_subdirs"; then
2032       subdir_options=' subdir-objects'
2033     fi
2034     echo "AUTOMAKE_OPTIONS = 1.5 gnits${subdir_options}"
2035   fi
2036   echo
2037   if test -z "$makefile_name"; then
2038     echo "SUBDIRS ="
2039     echo "noinst_HEADERS ="
2040     echo "noinst_LIBRARIES ="
2041     echo "noinst_LTLIBRARIES ="
2042     # Automake versions < 1.9b create an empty pkgdatadir at installation time
2043     # if you specify pkgdata_DATA to empty. This is a workaround.
2044     if grep '^pkgdata_DATA *+=' "$tmp"/allsnippets > /dev/null; then
2045       echo "pkgdata_DATA ="
2046     fi
2047     echo "EXTRA_DIST ="
2048     echo "BUILT_SOURCES ="
2049     echo "SUFFIXES ="
2050   fi
2051   echo "MOSTLYCLEANFILES $assign core *.stackdump"
2052   if test -z "$makefile_name"; then
2053     echo "MOSTLYCLEANDIRS ="
2054     echo "CLEANFILES ="
2055     echo "DISTCLEANFILES ="
2056     echo "MAINTAINERCLEANFILES ="
2057   fi
2058   # Execute edits that apply to the Makefile.am being generated.
2059   edit=0
2060   while test $edit != $makefile_am_edits; do
2061     edit=`expr $edit + 1`
2062     eval dir=\"\$makefile_am_edit${edit}_dir\"
2063     eval var=\"\$makefile_am_edit${edit}_var\"
2064     eval val=\"\$makefile_am_edit${edit}_val\"
2065     if test -n "$var"; then
2066       if test "${dir}Makefile.am" = "$destfile" || test "./${dir}Makefile.am" = "$destfile"; then
2067         echo "${var} += ${val}"
2068         eval "makefile_am_edit${edit}_var="
2069       fi
2070     fi
2071   done
2072   if test -z "$makefile_name"; then
2073     echo
2074     echo "AM_CPPFLAGS ="
2075     echo "AM_CFLAGS ="
2076   fi
2077   echo
2078   if LC_ALL=C grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *+\{0,1\}= *$libname\\.$libext\$" "$tmp"/allsnippets > /dev/null \
2079      || { test -n "$makefile_name" \
2080           && test -f "$sourcebase/Makefile.am" \
2081           && LC_ALL=C grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *+\{0,1\}= *$libname\\.$libext\$" "$sourcebase/Makefile.am" > /dev/null; \
2082         }; then
2083     # One of the snippets or the user's Makefile.am already specifies an
2084     # installation location for the library. Don't confuse automake by saying
2085     # it should not be installed.
2086     :
2087   else
2088     # By default, the generated library should not be installed.
2089     echo "noinst_${perhapsLT}LIBRARIES += $libname.$libext"
2090   fi
2091   echo
2092   echo "${libname}_${libext}_SOURCES ="
2093   # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
2094   # automake during its analysis looks for $(LIBOBJS), not for @LIBOBJS@.
2095   echo "${libname}_${libext}_LIBADD = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
2096   echo "${libname}_${libext}_DEPENDENCIES = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
2097   echo "EXTRA_${libname}_${libext}_SOURCES ="
2098   if test "$libtool" = true; then
2099     echo "${libname}_${libext}_LDFLAGS = \$(AM_LDFLAGS)"
2100   fi
2101   echo
2102   if test -n "$pobase"; then
2103     echo "AM_CPPFLAGS += -DDEFAULT_TEXT_DOMAIN=\\\"${po_domain}-gnulib\\\""
2104     echo
2105   fi
2106   cat "$tmp"/allsnippets \
2107     | sed -e 's|\$(top_srcdir)/build-aux/|$(top_srcdir)/'"$auxdir"'/|g'
2108   echo
2109   echo "mostlyclean-local: mostlyclean-generic"
2110   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
2111   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
2112   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
2113   echo "          fi; \\"
2114   echo "        done; \\"
2115   echo "        :"
2116   rm -f "$tmp"/allsnippets
2117 }
2118
2119 # func_emit_po_Makevars
2120 # emits the contents of po/ makefile parameterization to standard output.
2121 # Input:
2122 # - local_gnulib_dir  from --local-dir
2123 # - sourcebase      directory relative to destdir where to place source code
2124 # - pobase          directory relative to destdir where to place *.po files
2125 # - po_domain       prefix of i18n domain to use (without -gnulib suffix)
2126 func_emit_po_Makevars ()
2127 {
2128   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
2129   func_emit_copyright_notice
2130   echo
2131   echo "# Usually the message domain is the same as the package name."
2132   echo "# But here it has a '-gnulib' suffix."
2133   echo "DOMAIN = ${po_domain}-gnulib"
2134   echo
2135   echo "# These two variables depend on the location of this directory."
2136   echo "subdir = ${pobase}"
2137   echo "top_builddir = "`echo "$pobase" | sed -e 's,//*,/,g' -e 's,[^/][^/]*,..,g'`
2138   echo
2139   cat <<\EOF
2140 # These options get passed to xgettext.
2141 XGETTEXT_OPTIONS = \
2142   --keyword=_ --flag=_:1:pass-c-format \
2143   --keyword=N_ --flag=N_:1:pass-c-format \
2144   --keyword='proper_name:1,"This is a proper name. See the gettext manual, section Names."' \
2145   --keyword='proper_name_utf8:1,"This is a proper name. See the gettext manual, section Names."' \
2146   --flag=error:3:c-format --flag=error_at_line:5:c-format
2147
2148 # This is the copyright holder that gets inserted into the header of the
2149 # $(DOMAIN).pot file.  gnulib is copyrighted by the FSF.
2150 COPYRIGHT_HOLDER = Free Software Foundation, Inc.
2151
2152 # This is the email address or URL to which the translators shall report
2153 # bugs in the untranslated strings:
2154 # - Strings which are not entire sentences, see the maintainer guidelines
2155 #   in the GNU gettext documentation, section 'Preparing Strings'.
2156 # - Strings which use unclear terms or require additional context to be
2157 #   understood.
2158 # - Strings which make invalid assumptions about notation of date, time or
2159 #   money.
2160 # - Pluralisation problems.
2161 # - Incorrect English spelling.
2162 # - Incorrect formatting.
2163 # It can be your email address, or a mailing list address where translators
2164 # can write to without being subscribed, or the URL of a web page through
2165 # which the translators can contact you.
2166 MSGID_BUGS_ADDRESS = bug-gnulib@gnu.org
2167
2168 # This is the list of locale categories, beyond LC_MESSAGES, for which the
2169 # message catalogs shall be used.  It is usually empty.
2170 EXTRA_LOCALE_CATEGORIES =
2171
2172 # This tells whether the $(DOMAIN).pot file contains messages with an 'msgctxt'
2173 # context.  Possible values are "yes" and "no".  Set this to yes if the
2174 # package uses functions taking also a message context, like pgettext(), or
2175 # if in $(XGETTEXT_OPTIONS) you define keywords with a context argument.
2176 USE_MSGCTXT = no
2177 EOF
2178 }
2179
2180 # func_emit_po_POTFILES_in
2181 # emits the file list to be passed to xgettext to standard output.
2182 # Input:
2183 # - local_gnulib_dir  from --local-dir
2184 # - sourcebase      directory relative to destdir where to place source code
2185 # - files           list of new files
2186 func_emit_po_POTFILES_in ()
2187 {
2188   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
2189   func_emit_copyright_notice
2190   echo
2191   echo "# List of files which contain translatable strings."
2192   echo "$files" | sed -n -e "s,^lib/,$sourcebase/,p"
2193 }
2194
2195 # func_emit_tests_Makefile_am
2196 # emits the contents of tests makefile to standard output.
2197 # Input:
2198 # - local_gnulib_dir  from --local-dir
2199 # - modules         list of modules, including dependencies
2200 # - libname         library name
2201 # - auxdir          directory relative to destdir where to place build aux files
2202 # - makefile_name   from --makefile-name
2203 # - libtool         true if libtool will be used, false or blank otherwise
2204 # - sourcebase      relative directory containing lib source code
2205 # - m4base          relative directory containing autoconf macros
2206 # - testsbase       relative directory containing unit test code
2207 # - macro_prefix    prefix of gl_LIBOBJS macros to use
2208 # - for_test        true if creating a package for testing, false otherwise
2209 # - use_libtests    true if a libtests.a should be built, false otherwise
2210 # - destfile        filename relative to destdir of makefile being generated
2211 # Input/Output:
2212 # - makefile_am_edits and makefile_am_edit${edit}_{dir,var,val}
2213 #                   list of edits to be done to Makefile.am variables
2214 # Output:
2215 # - uses_subdirs    nonempty if object files in subdirs exist
2216 func_emit_tests_Makefile_am ()
2217 {
2218   if test "$libtool" = true; then
2219     libext=la
2220     sed_eliminate_LDFLAGS="$sed_noop"
2221   else
2222     libext=a
2223     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
2224   fi
2225   if $for_test; then
2226     # When creating a package for testing: Attempt to provoke failures,
2227     # especially link errors, already during "make" rather than during
2228     # "make check", because "make check" is not possible in a cross-compiling
2229     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
2230     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
2231   else
2232     sed_transform_check_PROGRAMS="$sed_noop"
2233   fi
2234   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
2235   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
2236   echo "## Process this file with automake to produce Makefile.in."
2237   func_emit_copyright_notice
2238   echo
2239   uses_subdirs=
2240   {
2241     for module in $modules; do
2242       if $for_test; then
2243         func_verify_tests_module
2244       else
2245         func_verify_module
2246       fi
2247       if test -n "$module"; then
2248         {
2249           func_get_automake_snippet "$module" |
2250             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
2251                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' \
2252                 -e "$sed_eliminate_LDFLAGS" \
2253                 -e 's,lib_\([A-Z][A-Z]*\),libtests_a_\1,g' \
2254                 -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
2255                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' \
2256                 -e "$sed_transform_check_PROGRAMS"
2257           if $use_libtests && test "$module" = 'alloca'; then
2258             echo "libtests_a_LIBADD += @${perhapsLT}ALLOCA@"
2259             echo "libtests_a_DEPENDENCIES += @${perhapsLT}ALLOCA@"
2260           fi
2261         } > "$tmp"/amsnippet
2262         # Skip the contents if it's entirely empty.
2263         if grep '[^      ]' "$tmp"/amsnippet > /dev/null ; then
2264           echo "## begin gnulib module $module"
2265           echo
2266           cat "$tmp"/amsnippet
2267           echo "## end   gnulib module $module"
2268           echo
2269         fi
2270         rm -f "$tmp"/amsnippet
2271         # Test whether there are some source files in subdirectories.
2272         for f in `func_get_filelist "$module"`; do
2273           case $f in
2274             lib/*/*.c | tests/*/*.c)
2275               uses_subdirs=yes
2276               break
2277               ;;
2278           esac
2279         done
2280       fi
2281     done
2282   } > "$tmp"/allsnippets
2283   # Generate dependencies here, since it eases the debugging of test failures.
2284   # If there are source files in subdirectories, prevent collision of the
2285   # object files (example: hash.c and libxml/hash.c).
2286   subdir_options=
2287   if test -n "$uses_subdirs"; then
2288     subdir_options=' subdir-objects'
2289   fi
2290   echo "AUTOMAKE_OPTIONS = 1.5 foreign${subdir_options}"
2291   echo
2292   if $for_test; then
2293     echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
2294     echo
2295   fi
2296   # Nothing is being added to SUBDIRS; nevertheless the existence of this
2297   # variable is needed to avoid an error from automake:
2298   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
2299   echo "SUBDIRS ="
2300   echo "TESTS ="
2301   echo "XFAIL_TESTS ="
2302   echo "TESTS_ENVIRONMENT ="
2303   echo "noinst_PROGRAMS ="
2304   if ! $for_test; then
2305     echo "check_PROGRAMS ="
2306   fi
2307   echo "noinst_HEADERS ="
2308   echo "noinst_LIBRARIES ="
2309   if $use_libtests; then
2310     if $for_test; then
2311       echo "noinst_LIBRARIES += libtests.a"
2312     else
2313       echo "check_LIBRARIES = libtests.a"
2314     fi
2315   fi
2316   # Automake versions < 1.9b create an empty pkgdatadir at installation time
2317   # if you specify pkgdata_DATA to empty. This is a workaround.
2318   if grep '^pkgdata_DATA *+=' "$tmp"/allsnippets > /dev/null; then
2319     echo "pkgdata_DATA ="
2320   fi
2321   echo "EXTRA_DIST ="
2322   echo "BUILT_SOURCES ="
2323   echo "SUFFIXES ="
2324   echo "MOSTLYCLEANFILES = core *.stackdump"
2325   echo "MOSTLYCLEANDIRS ="
2326   echo "CLEANFILES ="
2327   echo "DISTCLEANFILES ="
2328   echo "MAINTAINERCLEANFILES ="
2329   # Execute edits that apply to the Makefile.am being generated.
2330   edit=0
2331   while test $edit != $makefile_am_edits; do
2332     edit=`expr $edit + 1`
2333     eval dir=\"\$makefile_am_edit${edit}_dir\"
2334     eval var=\"\$makefile_am_edit${edit}_var\"
2335     eval val=\"\$makefile_am_edit${edit}_val\"
2336     if test -n "$var"; then
2337       if test "${dir}Makefile.am" = "$destfile" || test "./${dir}Makefile.am" = "$destfile"; then
2338         echo "${var} += ${val}"
2339         eval "makefile_am_edit${edit}_var="
2340       fi
2341     fi
2342   done
2343   echo
2344   echo "AM_CPPFLAGS = \\"
2345   echo "  -I. -I\$(srcdir) \\"
2346   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
2347   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
2348   echo
2349   local_ldadd_before=''
2350   local_ldadd_after=''
2351   if $use_libtests; then
2352     # All test programs need to be linked with libtests.a.
2353     # It needs to be passed to the linker before ${libname}.${libext}, since
2354     # the tests-related modules depend on the main modules.
2355     # It also needs to be passed to the linker after ${libname}.${libext}
2356     # because the latter might contain incomplete modules (such as the 'error'
2357     # module whose dependency to 'progname' is voluntarily omitted).
2358     # The LIBTESTS_LIBDEPS can be passed to the linker once or twice, it does
2359     # not matter.
2360     local_ldadd_before=' libtests.a'
2361     local_ldadd_after=' libtests.a $(LIBTESTS_LIBDEPS)'
2362   fi
2363   echo "LDADD =${local_ldadd_before} ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}${local_ldadd_after}"
2364   echo
2365   if $use_libtests; then
2366     echo "libtests_a_SOURCES ="
2367     # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
2368     # automake during its analysis looks for $(LIBOBJS), not for @LIBOBJS@.
2369     echo "libtests_a_LIBADD = \$(${macro_prefix}tests_LIBOBJS)"
2370     echo "libtests_a_DEPENDENCIES = \$(${macro_prefix}tests_LIBOBJS)"
2371     echo "EXTRA_libtests_a_SOURCES ="
2372     # The circular dependency in LDADD requires this.
2373     echo "AM_LIBTOOLFLAGS = --preserve-dup-deps"
2374     echo
2375   fi
2376   cat "$tmp"/allsnippets \
2377     | sed -e 's|\$(top_srcdir)/build-aux/|$(top_srcdir)/'"$auxdir"'/|g'
2378   echo "# Clean up after Solaris cc."
2379   echo "clean-local:"
2380   echo "        rm -rf SunWS_cache"
2381   echo
2382   echo "mostlyclean-local: mostlyclean-generic"
2383   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
2384   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
2385   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
2386   echo "          fi; \\"
2387   echo "        done; \\"
2388   echo "        :"
2389   rm -f "$tmp"/allsnippets
2390 }
2391
2392 # func_emit_initmacro_start macro_prefix
2393 # emits the first few statements of the gl_INIT macro to standard output.
2394 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
2395 func_emit_initmacro_start ()
2396 {
2397   macro_prefix_arg="$1"
2398   # Overriding AC_LIBOBJ and AC_REPLACE_FUNCS has the effect of storing
2399   # platform-dependent object files in ${macro_prefix_arg}_LIBOBJS instead of
2400   # LIBOBJS.  The purpose is to allow several gnulib instantiations under
2401   # a single configure.ac file.  (AC_CONFIG_LIBOBJ_DIR does not allow this
2402   # flexibility.)
2403   # Furthermore it avoids an automake error like this when a Makefile.am
2404   # that uses pieces of gnulib also uses $(LIBOBJ):
2405   #   automatically discovered file `error.c' should not be explicitly mentioned
2406   echo "  m4_pushdef([AC_LIBOBJ], m4_defn([${macro_prefix_arg}_LIBOBJ]))"
2407   echo "  m4_pushdef([AC_REPLACE_FUNCS], m4_defn([${macro_prefix_arg}_REPLACE_FUNCS]))"
2408   # Overriding AC_LIBSOURCES has the same purpose of avoiding the automake
2409   # error when a Makefile.am that uses pieces of gnulib also uses $(LIBOBJ):
2410   #   automatically discovered file `error.c' should not be explicitly mentioned
2411   # We let automake know about the files to be distributed through the
2412   # EXTRA_lib_SOURCES variable.
2413   echo "  m4_pushdef([AC_LIBSOURCES], m4_defn([${macro_prefix_arg}_LIBSOURCES]))"
2414   # Create data variables for checking the presence of files that are mentioned
2415   # as AC_LIBSOURCES arguments. These are m4 variables, not shell variables,
2416   # because we want the check to happen when the configure file is created,
2417   # not when it is run. ${macro_prefix_arg}_LIBSOURCES_LIST is the list of
2418   # files to check for. ${macro_prefix_arg}_LIBSOURCES_DIR is the subdirectory
2419   # in which to expect them.
2420   echo "  m4_pushdef([${macro_prefix_arg}_LIBSOURCES_LIST], [])"
2421   echo "  m4_pushdef([${macro_prefix_arg}_LIBSOURCES_DIR], [])"
2422   echo "  gl_COMMON"
2423 }
2424
2425 # func_emit_initmacro_end macro_prefix
2426 # emits the last few statements of the gl_INIT macro to standard output.
2427 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
2428 func_emit_initmacro_end ()
2429 {
2430   macro_prefix_arg="$1"
2431   # Check the presence of files that are mentioned as AC_LIBSOURCES arguments.
2432   # The check is performed only when autoconf is run from the directory where
2433   # the configure.ac resides; if it is run from a different directory, the
2434   # check is skipped.
2435   echo "  m4_ifval(${macro_prefix_arg}_LIBSOURCES_LIST, ["
2436   echo "    m4_syscmd([test ! -d ]m4_defn([${macro_prefix_arg}_LIBSOURCES_DIR])[ ||"
2437   echo "      for gl_file in ]${macro_prefix_arg}_LIBSOURCES_LIST[ ; do"
2438   echo "        if test ! -r ]m4_defn([${macro_prefix_arg}_LIBSOURCES_DIR])[/\$gl_file ; then"
2439   echo "          echo \"missing file ]m4_defn([${macro_prefix_arg}_LIBSOURCES_DIR])[/\$gl_file\" >&2"
2440   echo "          exit 1"
2441   echo "        fi"
2442   echo "      done])dnl"
2443   echo "      m4_if(m4_sysval, [0], [],"
2444   echo "        [AC_FATAL([expected source file, required through AC_LIBSOURCES, not found])])"
2445   echo "  ])"
2446   echo "  m4_popdef([${macro_prefix_arg}_LIBSOURCES_DIR])"
2447   echo "  m4_popdef([${macro_prefix_arg}_LIBSOURCES_LIST])"
2448   echo "  m4_popdef([AC_LIBSOURCES])"
2449   echo "  m4_popdef([AC_REPLACE_FUNCS])"
2450   echo "  m4_popdef([AC_LIBOBJ])"
2451   echo "  AC_CONFIG_COMMANDS_PRE(["
2452   echo "    ${macro_prefix_arg}_libobjs="
2453   echo "    ${macro_prefix_arg}_ltlibobjs="
2454   echo "    if test -n \"\$${macro_prefix_arg}_LIBOBJS\"; then"
2455   echo "      # Remove the extension."
2456   echo "      sed_drop_objext='s/\\.o\$//;s/\\.obj\$//'"
2457   echo "      for i in \`for i in \$${macro_prefix_arg}_LIBOBJS; do echo \"\$i\"; done | sed -e \"\$sed_drop_objext\" | sort | uniq\`; do"
2458   echo "        ${macro_prefix_arg}_libobjs=\"\$${macro_prefix_arg}_libobjs \$i.\$ac_objext\""
2459   echo "        ${macro_prefix_arg}_ltlibobjs=\"\$${macro_prefix_arg}_ltlibobjs \$i.lo\""
2460   echo "      done"
2461   echo "    fi"
2462   echo "    AC_SUBST([${macro_prefix_arg}_LIBOBJS], [\$${macro_prefix_arg}_libobjs])"
2463   echo "    AC_SUBST([${macro_prefix_arg}_LTLIBOBJS], [\$${macro_prefix_arg}_ltlibobjs])"
2464   echo "  ])"
2465 }
2466
2467 # func_emit_initmacro_done macro_prefix sourcebase
2468 # emits a few statements after the gl_INIT macro to standard output.
2469 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
2470 # - sourcebase      directory relative to destdir where to place source code
2471 func_emit_initmacro_done ()
2472 {
2473   macro_prefix_arg="$1"
2474   sourcebase_arg="$2"
2475   echo
2476   echo "# Like AC_LIBOBJ, except that the module name goes"
2477   echo "# into ${macro_prefix_arg}_LIBOBJS instead of into LIBOBJS."
2478   echo "AC_DEFUN([${macro_prefix_arg}_LIBOBJ], ["
2479   echo "  AS_LITERAL_IF([\$1], [${macro_prefix_arg}_LIBSOURCES([\$1.c])])dnl"
2480   echo "  ${macro_prefix_arg}_LIBOBJS=\"\$${macro_prefix_arg}_LIBOBJS \$1.\$ac_objext\""
2481   echo "])"
2482   echo
2483   echo "# Like AC_REPLACE_FUNCS, except that the module name goes"
2484   echo "# into ${macro_prefix_arg}_LIBOBJS instead of into LIBOBJS."
2485   echo "AC_DEFUN([${macro_prefix_arg}_REPLACE_FUNCS], ["
2486   echo "  m4_foreach_w([gl_NAME], [\$1], [AC_LIBSOURCES(gl_NAME[.c])])dnl"
2487   echo "  AC_CHECK_FUNCS([\$1], , [${macro_prefix_arg}_LIBOBJ(\$ac_func)])"
2488   echo "])"
2489   echo
2490   echo "# Like AC_LIBSOURCES, except the directory where the source file is"
2491   echo "# expected is derived from the gnulib-tool parameterization,"
2492   echo "# and alloca is special cased (for the alloca-opt module)."
2493   echo "# We could also entirely rely on EXTRA_lib..._SOURCES."
2494   echo "AC_DEFUN([${macro_prefix_arg}_LIBSOURCES], ["
2495   echo "  m4_foreach([_gl_NAME], [\$1], ["
2496   echo "    m4_if(_gl_NAME, [alloca.c], [], ["
2497   echo "      m4_define([${macro_prefix_arg}_LIBSOURCES_DIR], [$sourcebase_arg])"
2498   echo "      m4_append([${macro_prefix_arg}_LIBSOURCES_LIST], _gl_NAME, [ ])"
2499   echo "    ])"
2500   echo "  ])"
2501   echo "])"
2502 }
2503
2504 # func_import modules
2505 # Uses also the variables
2506 # - destdir         target directory
2507 # - local_gnulib_dir  from --local-dir
2508 # - verbose         integer, default 0, inc/decremented by --verbose/--quiet
2509 # - libname         library name
2510 # - sourcebase      directory relative to destdir where to place source code
2511 # - m4base          directory relative to destdir where to place *.m4 macros
2512 # - pobase          directory relative to destdir where to place *.po files
2513 # - docbase         directory relative to destdir where to place doc files
2514 # - testsbase       directory relative to destdir where to place unit test code
2515 # - auxdir          directory relative to destdir where to place build aux files
2516 # - inctests        true if --with-tests was given, blank otherwise
2517 # - incobsolete     true if --with-obsolete was given, blank otherwise
2518 # - avoidlist       list of modules to avoid, from --avoid
2519 # - lgpl            yes or a number if library's license shall be LGPL,
2520 #                   blank otherwise
2521 # - makefile_name   from --makefile-name
2522 # - libtool         true if --libtool was given, false if --no-libtool was
2523 #                   given, blank otherwise
2524 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
2525 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
2526 # - po_domain       prefix of i18n domain to use (without -gnulib suffix)
2527 # - vc_files        true if --vc-files was given, false if --no-vc-files was
2528 #                   given, blank otherwise
2529 # - autoconf_minversion  minimum supported autoconf version
2530 # - doit            : if actions shall be executed, false if only to be printed
2531 # - symbolic        true if files should be symlinked, copied otherwise
2532 # - lsymbolic       true if files from local_gnulib_dir should be symlinked,
2533 #                   copied otherwise
2534 # - do_copyrights   true if copyright notices in files should be replaced,
2535 #                   blank otherwise
2536 func_import ()
2537 {
2538   # Get the cached settings.
2539   cached_local_gnulib_dir=
2540   cached_specified_modules=
2541   cached_incobsolete=
2542   cached_avoidlist=
2543   cached_sourcebase=
2544   cached_m4base=
2545   cached_pobase=
2546   cached_docbase=
2547   cached_testsbase=
2548   cached_inctests=
2549   cached_libname=
2550   cached_lgpl=
2551   cached_makefile_name=
2552   cached_libtool=
2553   cached_macro_prefix=
2554   cached_po_domain=
2555   cached_vc_files=
2556   cached_files=
2557   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
2558     cached_libtool=false
2559     my_sed_traces='
2560       s,#.*$,,
2561       s,^dnl .*$,,
2562       s, dnl .*$,,
2563       /gl_LOCAL_DIR(/ {
2564         s,^.*gl_LOCAL_DIR([[ ]*\([^]"$`\\)]*\).*$,cached_local_gnulib_dir="\1",p
2565       }
2566       /gl_MODULES(/ {
2567         ta
2568         :a
2569           s/)/)/
2570           tb
2571           N
2572           ba
2573         :b
2574         s,^.*gl_MODULES([[ ]*\([^]"$`\\)]*\).*$,cached_specified_modules="\1",p
2575       }
2576       /gl_WITH_OBSOLETE/ {
2577         s,^.*$,cached_incobsolete=true,p
2578       }
2579       /gl_AVOID(/ {
2580         s,^.*gl_AVOID([[ ]*\([^]"$`\\)]*\).*$,cached_avoidlist="\1",p
2581       }
2582       /gl_SOURCE_BASE(/ {
2583         s,^.*gl_SOURCE_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_sourcebase="\1",p
2584       }
2585       /gl_M4_BASE(/ {
2586         s,^.*gl_M4_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_m4base="\1",p
2587       }
2588       /gl_PO_BASE(/ {
2589         s,^.*gl_PO_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_pobase="\1",p
2590       }
2591       /gl_DOC_BASE(/ {
2592         s,^.*gl_DOC_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_docbase="\1",p
2593       }
2594       /gl_TESTS_BASE(/ {
2595         s,^.*gl_TESTS_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_testsbase="\1",p
2596       }
2597       /gl_WITH_TESTS/ {
2598         s,^.*$,cached_inctests=true,p
2599       }
2600       /gl_LIB(/ {
2601         s,^.*gl_LIB([[ ]*\([^]"$`\\)]*\).*$,cached_libname="\1",p
2602       }
2603       /gl_LGPL(/ {
2604         s,^.*gl_LGPL([[ ]*\([^]"$`\\)]*\).*$,cached_lgpl="\1",p
2605       }
2606       /gl_LGPL/ {
2607         s,^.*$,cached_lgpl=yes,p
2608       }
2609       /gl_MAKEFILE_NAME(/ {
2610         s,^.*gl_MAKEFILE_NAME([[ ]*\([^]"$`\\)]*\).*$,cached_makefile_name="\1",p
2611       }
2612       /gl_LIBTOOL/ {
2613         s,^.*$,cached_libtool=true,p
2614       }
2615       /gl_MACRO_PREFIX(/ {
2616         s,^.*gl_MACRO_PREFIX([[ ]*\([^]"$`\\)]*\).*$,cached_macro_prefix="\1",p
2617       }
2618       /gl_PO_DOMAIN(/ {
2619         s,^.*gl_PO_DOMAIN([[ ]*\([^]"$`\\)]*\).*$,cached_po_domain="\1",p
2620       }
2621       /gl_VC_FILES(/ {
2622         s,^.*gl_VC_FILES([[ ]*\([^]"$`\\)]*\).*$,cached_vc_files="\1",p
2623       }'
2624     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
2625     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
2626       my_sed_traces='
2627         s,#.*$,,
2628         s,^dnl .*$,,
2629         s, dnl .*$,,
2630         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
2631           s,^.*$,cached_files=",p
2632           n
2633           ta
2634           :a
2635           s,^\]).*$,",
2636           tb
2637           s,["$`\\],,g
2638           p
2639           n
2640           ba
2641           :b
2642           p
2643         }'
2644       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
2645     fi
2646   fi
2647
2648   # Merge the cached settings with the specified ones.
2649   # The m4base must be the same as expected from the pathname.
2650   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
2651     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
2652   fi
2653   # The local_gnulib_dir defaults to the cached one. Recall that the cached one
2654   # is relative to $destdir, whereas the one we use is relative to . or absolute.
2655   if test -z "$local_gnulib_dir"; then
2656     if test -n "$cached_local_gnulib_dir"; then
2657       case "$destdir" in
2658         /*)
2659           local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
2660         *)
2661           case "$cached_local_gnulib_dir" in
2662             /*)
2663               local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
2664             *)
2665               func_relconcat "$destdir" "$cached_local_gnulib_dir"
2666               local_gnulib_dir="$relconcat" ;;
2667           esac ;;
2668       esac
2669     fi
2670   fi
2671   # Append the cached and the specified module names. So that
2672   # "gnulib-tool --import foo" means to add the module foo.
2673   specified_modules="$cached_specified_modules $1"
2674   # Included obsolete modules among the dependencies if specified either way.
2675   if test -z "$incobsolete"; then
2676     incobsolete="$cached_incobsolete"
2677   fi
2678   # Append the cached and the specified avoidlist. This is probably better
2679   # than dropping the cached one when --avoid is specified at least once.
2680   avoidlist=`for m in $cached_avoidlist $avoidlist; do echo $m; done | LC_ALL=C sort -u`
2681   avoidlist=`echo $avoidlist`
2682
2683   # The sourcebase defaults to the cached one.
2684   if test -z "$sourcebase"; then
2685     sourcebase="$cached_sourcebase"
2686     if test -z "$sourcebase"; then
2687       func_fatal_error "missing --source-base option"
2688     fi
2689   fi
2690   # The pobase defaults to the cached one.
2691   if test -z "$pobase"; then
2692     pobase="$cached_pobase"
2693   fi
2694   # The docbase defaults to the cached one.
2695   if test -z "$docbase"; then
2696     docbase="$cached_docbase"
2697     if test -z "$docbase"; then
2698       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."
2699     fi
2700   fi
2701   # The testsbase defaults to the cached one.
2702   if test -z "$testsbase"; then
2703     testsbase="$cached_testsbase"
2704     if test -z "$testsbase"; then
2705       func_fatal_error "missing --tests-base option"
2706     fi
2707   fi
2708   # Require the tests if specified either way.
2709   if test -z "$inctests"; then
2710     inctests="$cached_inctests"
2711   fi
2712   # The libname defaults to the cached one.
2713   if test -z "$supplied_libname"; then
2714     libname="$cached_libname"
2715     if test -z "$libname"; then
2716       func_fatal_error "missing --lib option"
2717     fi
2718   fi
2719   # Require LGPL if specified either way.
2720   if test -z "$lgpl"; then
2721     lgpl="$cached_lgpl"
2722   fi
2723   # The makefile_name defaults to the cached one.
2724   if test -z "$makefile_name"; then
2725     makefile_name="$cached_makefile_name"
2726   fi
2727   # Use libtool if specified either way, or if guessed.
2728   if test -z "$libtool"; then
2729     if test -n "$cached_m4base"; then
2730       libtool="$cached_libtool"
2731     else
2732       libtool="$guessed_libtool"
2733     fi
2734   fi
2735   # The macro_prefix defaults to the cached one.
2736   if test -z "$macro_prefix"; then
2737     macro_prefix="$cached_macro_prefix"
2738     if test -z "$macro_prefix"; then
2739       func_fatal_error "missing --macro-prefix option"
2740     fi
2741   fi
2742   # The po_domain defaults to the cached one.
2743   if test -z "$po_domain"; then
2744     po_domain="$cached_po_domain"
2745   fi
2746   # The vc_files defaults to the cached one.
2747   if test -z "$vc_files"; then
2748     vc_files="$cached_vc_files"
2749   fi
2750
2751   # Canonicalize the list of specified modules.
2752   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort -u`
2753
2754   # Determine final module list.
2755   modules="$specified_modules"
2756   func_modules_transitive_closure
2757   if test $verbose -ge 0; then
2758     echo "Module list with included dependencies:"
2759     echo "$modules" | sed -e 's/^/  /'
2760   fi
2761   final_modules="$modules"
2762
2763   # Determine main module list and tests-related module list separately.
2764   # The main module list is the transitive closure of the specified modules,
2765   # ignoring tests modules. Its lib/* sources go into $sourcebase/. If --lgpl
2766   # is specified, it will consist only of LGPLed source.
2767   # The tests-related module list is the transitive closure of the specified
2768   # modules, including tests modules, minus the main module list excluding
2769   # modules of applicability 'all'. Its lib/* sources (brought in through
2770   # dependencies of *-tests modules) go into $testsbase/. It may contain GPLed
2771   # source, even if --lgpl is specified.
2772   # Determine main module list.
2773   saved_inctests="$inctests"
2774   inctests=""
2775   modules="$specified_modules"
2776   func_modules_transitive_closure
2777   main_modules="$modules"
2778   inctests="$saved_inctests"
2779   if test $verbose -ge 1; then
2780     echo "Main module list:"
2781     echo "$main_modules" | sed -e 's/^/  /'
2782   fi
2783   # Determine tests-related module list.
2784   echo "$final_modules" | LC_ALL=C sort -u > "$tmp"/final-modules
2785   testsrelated_modules=`func_reset_sigpipe
2786                         for module in $main_modules; do
2787                           if test \`func_get_applicability $module\` = main; then
2788                             echo $module
2789                           fi
2790                         done \
2791                         | LC_ALL=C sort -u | LC_ALL=C join -v 2 - "$tmp"/final-modules`
2792   if test $verbose -ge 1; then
2793     echo "Tests-related module list:"
2794     echo "$testsrelated_modules" | sed -e 's/^/  /'
2795   fi
2796
2797   # Add the dummy module to the main module list if needed.
2798   modules="$main_modules"
2799   func_modules_add_dummy
2800   main_modules="$modules"
2801
2802   # Determine whether a $testsbase/libtests.a is needed.
2803   use_libtests=false
2804   for module in $testsrelated_modules; do
2805     func_verify_nontests_module
2806     if test -n "$module"; then
2807       all_files=`func_get_filelist $module`
2808       # Test whether some file in $all_files lies in lib/.
2809       for f in $all_files; do
2810         case $f in
2811           lib/*)
2812             use_libtests=true
2813             break 2
2814             ;;
2815         esac
2816       done
2817     fi
2818   done
2819
2820   # Add the dummy module to the tests-related module list if needed.
2821   if $use_libtests; then
2822     modules="$testsrelated_modules"
2823     func_modules_add_dummy
2824     testsrelated_modules="$modules"
2825   fi
2826
2827   # If --lgpl, verify that the licenses of modules are compatible.
2828   if test -n "$lgpl"; then
2829     license_incompatibilities=
2830     for module in $main_modules; do
2831       license=`func_get_license $module`
2832       case $license in
2833         'GPLed build tool') ;;
2834         'public domain' | 'unlimited' | 'unmodifiable license text') ;;
2835         *)
2836           case "$lgpl" in
2837             yes | 3)
2838               case $license in
2839                 LGPL | LGPLv2+ | LGPLv3+) ;;
2840                 *) func_append license_incompatibilities "$module $license$nl" ;;
2841               esac
2842               ;;
2843             2)
2844               case $license in
2845                 LGPLv2+) ;;
2846                 *) func_append license_incompatibilities "$module $license$nl" ;;
2847               esac
2848               ;;
2849             *) func_fatal_error "invalid value lgpl=$lgpl" ;;
2850           esac
2851           ;;
2852       esac
2853     done
2854     if test -n "$license_incompatibilities"; then
2855       # Format the license incompatibilities as a table.
2856       sed_expand_column1_width50_indent17='s,^\([^ ]*\) ,\1                                                   ,
2857 s,^\(.................................................[^ ]*\) *,                 \1 ,'
2858       license_incompatibilities=`echo "$license_incompatibilities" | sed -e "$sed_expand_column1_width50_indent17"`
2859       func_fatal_error "incompatible license on modules:$nl$license_incompatibilities"
2860     fi
2861   fi
2862
2863   # Show banner notice of every module.
2864   modules="$main_modules"
2865   func_modules_notice
2866
2867   # Determine script to apply to imported library files.
2868   sed_transform_lib_file=
2869   for module in $main_modules; do
2870     if test $module = config-h; then
2871       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
2872       sed_transform_lib_file=$sed_transform_lib_file'
2873         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
2874       '
2875       break
2876     fi
2877   done
2878   sed_transform_main_lib_file="$sed_transform_lib_file"
2879   if test -n "$do_copyrights"; then
2880     if test -n "$lgpl"; then
2881       # Update license.
2882       case "$lgpl" in
2883         yes | 3)
2884           sed_transform_main_lib_file=$sed_transform_main_lib_file'
2885             s/GNU General/GNU Lesser General/g
2886           '
2887           ;;
2888         2)
2889           sed_transform_main_lib_file=$sed_transform_main_lib_file'
2890             s/GNU General/GNU Lesser General/g
2891             s/version [23]\([ ,]\)/version 2.1\1/g
2892           '
2893           ;;
2894         *) func_fatal_error "invalid value lgpl=$lgpl" ;;
2895       esac
2896     else
2897       # Update license.
2898       sed_transform_main_lib_file=$sed_transform_main_lib_file'
2899         s/GNU Lesser General/GNU General/g
2900         s/GNU Library General/GNU General/g
2901         s/version 2\(.1\)\{0,1\}\([ ,]\)/version 3\2/g
2902       '
2903     fi
2904   fi
2905
2906   # Determine script to apply to auxiliary files that go into $auxdir/.
2907   sed_transform_build_aux_file=
2908   if test -n "$do_copyrights"; then
2909     # Update license.
2910     sed_transform_build_aux_file=$sed_transform_build_aux_file'
2911       s/GNU Lesser General/GNU General/g
2912       s/GNU Library General/GNU General/g
2913       s/version 2\(.1\)\{0,1\}\([ ,]\)/version 3\2/g
2914     '
2915   fi
2916
2917   # Determine script to apply to library files that go into $testsbase/.
2918   sed_transform_testsrelated_lib_file="$sed_transform_lib_file"
2919   if test -n "$do_copyrights"; then
2920     # Update license.
2921     sed_transform_testsrelated_lib_file=$sed_transform_testsrelated_lib_file'
2922       s/GNU Lesser General/GNU General/g
2923       s/GNU Library General/GNU General/g
2924       s/version 2\(.1\)\{0,1\}\([ ,]\)/version 3\2/g
2925     '
2926   fi
2927
2928   # Determine the final file lists.
2929   # They must be computed separately, because files in lib/* go into
2930   # $sourcebase/ if they are in the main file list but into $testsbase/
2931   # if they are in the tests-related file list. Furthermore lib/dummy.c
2932   # can be in both.
2933   # Determine final main file list.
2934   modules="$main_modules"
2935   func_modules_to_filelist
2936   main_files="$files"
2937   # Determine final tests-related file list.
2938   modules="$testsrelated_modules"
2939   func_modules_to_filelist
2940   testsrelated_files=`echo "$files" | sed -e 's,^lib/,tests=lib/,'`
2941   # Merge both file lists.
2942   sed_remove_empty_lines='/^$/d'
2943   files=`{ echo "$main_files"; echo "$testsrelated_files"; } | sed -e "$sed_remove_empty_lines" | LC_ALL=C sort -u`
2944   if test $verbose -ge 0; then
2945     echo "File list:"
2946     sed_prettyprint_files='s,^tests=lib/\(.*\)$,lib/\1 -> tests/\1,'
2947     echo "$files" | sed -e "$sed_prettyprint_files" -e 's/^/  /'
2948   fi
2949
2950   test -n "$files" \
2951     || func_fatal_error "refusing to do nothing"
2952
2953   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
2954   new_files="$files m4/gnulib-tool.m4"
2955   old_files="$cached_files"
2956   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
2957     func_append old_files " m4/gnulib-tool.m4"
2958   fi
2959
2960   rewritten='%REWRITTEN%'
2961   sed_rewrite_old_files="\
2962     s,^build-aux/,$rewritten$auxdir/,
2963     s,^doc/,$rewritten$cached_docbase/,
2964     s,^lib/,$rewritten$cached_sourcebase/,
2965     s,^m4/,$rewritten$cached_m4base/,
2966     s,^tests/,$rewritten$cached_testsbase/,
2967     s,^tests=lib/,$rewritten$cached_testsbase/,
2968     s,^top/,$rewritten,
2969     s,^$rewritten,,"
2970   sed_rewrite_new_files="\
2971     s,^build-aux/,$rewritten$auxdir/,
2972     s,^doc/,$rewritten$docbase/,
2973     s,^lib/,$rewritten$sourcebase/,
2974     s,^m4/,$rewritten$m4base/,
2975     s,^tests/,$rewritten$testsbase/,
2976     s,^tests=lib/,$rewritten$testsbase/,
2977     s,^top/,$rewritten,
2978     s,^$rewritten,,"
2979
2980   # Create directories.
2981   { echo "$sourcebase"
2982     echo "$m4base"
2983     if test -n "$pobase"; then
2984       echo "$pobase"
2985     fi
2986     docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
2987     if test -n "$docfiles"; then
2988       echo "$docbase"
2989     fi
2990     if test -n "$inctests"; then
2991       echo "$testsbase"
2992     fi
2993     echo "$auxdir"
2994     for f in $files; do echo $f; done \
2995       | sed -e "$sed_rewrite_new_files" \
2996       | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
2997       | LC_ALL=C sort -u
2998   } > "$tmp"/dirs
2999   { # Rearrange file descriptors. Needed because "while ... done < ..."
3000     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3001     exec 5<&0 < "$tmp"/dirs
3002     while read d; do
3003       if test ! -d "$destdir/$d"; then
3004         if $doit; then
3005           echo "Creating directory $destdir/$d"
3006           mkdir -p "$destdir/$d" || func_fatal_error "failed"
3007         else
3008           echo "Create directory $destdir/$d"
3009         fi
3010       fi
3011     done
3012     exec 0<&5 5<&-
3013   }
3014
3015   # Copy files or make symbolic links. Remove obsolete files.
3016   added_files=''
3017   removed_files=''
3018   delimiter='   '
3019   # Construct a table with 2 columns: rewritten-file-name original-file-name,
3020   # representing the files according to the last gnulib-tool invocation.
3021   for f in $old_files; do echo $f; done \
3022     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_old_files" \
3023     | LC_ALL=C sort \
3024     > "$tmp"/old-files
3025   # Construct a table with 2 columns: rewritten-file-name original-file-name,
3026   # representing the files after this gnulib-tool invocation.
3027   for f in $new_files; do echo $f; done \
3028     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" \
3029     | LC_ALL=C sort \
3030     > "$tmp"/new-files
3031   # First the files that are in old-files, but not in new-files:
3032   sed_take_first_column='s,'"$delimiter"'.*,,'
3033   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
3034     # Remove the file. Do nothing if the user already removed it.
3035     if test -f "$destdir/$g" || test -h "$destdir/$g"; then
3036       if $doit; then
3037         echo "Removing file $g (backup in ${g}~)"
3038         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
3039       else
3040         echo "Remove file $g (backup in ${g}~)"
3041       fi
3042       func_append removed_files "$g$nl"
3043     fi
3044   done
3045   # func_add_or_update handles a file that ought to be present afterwards.
3046   # Uses parameters
3047   # - f             the original file name
3048   # - g             the rewritten file name
3049   # - already_present  nonempty if the file should already exist, empty
3050   #                    otherwise
3051   func_add_or_update ()
3052   {
3053     of="$f"
3054     case "$f" in
3055       tests=lib/*) f=`echo "$f" | sed -e 's,^tests=lib/,lib/,'` ;;
3056     esac
3057     func_dest_tmpfilename "$g"
3058     func_lookup_file "$f"
3059     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
3060     if test -n "$sed_transform_main_lib_file"; then
3061       case "$of" in
3062         lib/*)
3063           sed -e "$sed_transform_main_lib_file" \
3064             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
3065           ;;
3066       esac
3067     fi
3068     if test -n "$sed_transform_build_aux_file"; then
3069       case "$of" in
3070         build-aux/*)
3071           sed -e "$sed_transform_build_aux_file" \
3072             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
3073           ;;
3074       esac
3075     fi
3076     if test -n "$sed_transform_testsrelated_lib_file"; then
3077       case "$of" in
3078         tests=lib/*)
3079           sed -e "$sed_transform_testsrelated_lib_file" \
3080             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
3081           ;;
3082       esac
3083     fi
3084     if test -f "$destdir/$g"; then
3085       # The file already exists.
3086       func_update_file
3087     else
3088       # Install the file.
3089       # Don't protest if the file should be there but isn't: it happens
3090       # frequently that developers don't put autogenerated files into CVS.
3091       func_add_file
3092       func_append added_files "$g$nl"
3093     fi
3094     rm -f "$tmpfile"
3095   }
3096   # Then the files that are in new-files, but not in old-files:
3097   sed_take_last_column='s,^.*'"$delimiter"',,'
3098   already_present=
3099   LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files \
3100     | sed -e "$sed_take_last_column" \
3101     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/added-files
3102   { # Rearrange file descriptors. Needed because "while ... done < ..."
3103     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3104     exec 5<&0 < "$tmp"/added-files
3105     while read g f; do
3106       func_add_or_update
3107     done
3108     exec 0<&5 5<&-
3109   }
3110   # Then the files that are in new-files and in old-files:
3111   already_present=true
3112   LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files \
3113     | sed -e "$sed_take_last_column" \
3114     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/kept-files
3115   { # Rearrange file descriptors. Needed because "while ... done < ..."
3116     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3117     exec 5<&0 < "$tmp"/kept-files
3118     while read g f; do
3119       func_add_or_update
3120     done
3121     exec 0<&5 5<&-
3122   }
3123
3124   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
3125   actioncmd="gnulib-tool --import"
3126   func_append actioncmd " --dir=$destdir"
3127   if test -n "$local_gnulib_dir"; then
3128     func_append actioncmd " --local-dir=$local_gnulib_dir"
3129   fi
3130   func_append actioncmd " --lib=$libname"
3131   func_append actioncmd " --source-base=$sourcebase"
3132   func_append actioncmd " --m4-base=$m4base"
3133   if test -n "$pobase"; then
3134     func_append actioncmd " --po-base=$pobase"
3135   fi
3136   func_append actioncmd " --doc-base=$docbase"
3137   func_append actioncmd " --tests-base=$testsbase"
3138   func_append actioncmd " --aux-dir=$auxdir"
3139   if test -n "$inctests"; then
3140     func_append actioncmd " --with-tests"
3141   fi
3142   if test -n "$incobsolete"; then
3143     func_append actioncmd " --with-obsolete"
3144   fi
3145   for module in $avoidlist; do
3146     func_append actioncmd " --avoid=$module"
3147   done
3148   if test -n "$lgpl"; then
3149     if test "$lgpl" = yes; then
3150       func_append actioncmd " --lgpl"
3151     else
3152       func_append actioncmd " --lgpl=$lgpl"
3153     fi
3154   fi
3155   if test -n "$makefile_name"; then
3156     func_append actioncmd " --makefile-name=$makefile_name"
3157   fi
3158   if test "$libtool" = true; then
3159     func_append actioncmd " --libtool"
3160   else
3161     func_append actioncmd " --no-libtool"
3162   fi
3163   func_append actioncmd " --macro-prefix=$macro_prefix"
3164   if test -n "$po_domain"; then
3165     func_append actioncmd " --po-domain=$po_domain"
3166   fi
3167   if test -n "$vc_files"; then
3168     if test "$vc_files" = true; then
3169       func_append actioncmd " --vc-files"
3170     else
3171       func_append actioncmd " --no-vc-files"
3172     fi
3173   fi
3174   func_append actioncmd " `echo $specified_modules`"
3175
3176   # Default the makefile name to Makefile.am.
3177   if test -n "$makefile_name"; then
3178     makefile_am=$makefile_name
3179   else
3180     makefile_am=Makefile.am
3181   fi
3182
3183   # Create normal Makefile.ams.
3184   for_test=false
3185
3186   # Setup list of Makefile.am edits that are to be performed afterwards.
3187   # Some of these edits apply to files that we will generate; others are
3188   # under the responsibility of the developer.
3189   makefile_am_edits=0
3190   # func_note_Makefile_am_edit dir var value
3191   # remembers that ${dir}Makefile.am needs to be edited to that ${var} mentions
3192   # ${value}.
3193   func_note_Makefile_am_edit ()
3194   {
3195     makefile_am_edits=`expr $makefile_am_edits + 1`
3196     eval makefile_am_edit${makefile_am_edits}_dir=\"\$1\"
3197     eval makefile_am_edit${makefile_am_edits}_var=\"\$2\"
3198     eval makefile_am_edit${makefile_am_edits}_val=\"\$3\"
3199   }
3200   if test "$makefile_am" = Makefile.am; then
3201     sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
3202     sourcebase_base=`basename "$sourcebase"`
3203     func_note_Makefile_am_edit "$sourcebase_dir" SUBDIRS "$sourcebase_base"
3204   fi
3205   if test -n "$pobase"; then
3206     pobase_dir=`echo "$pobase" | sed -n -e 's,/[^/]*$,/,p'`
3207     pobase_base=`basename "$pobase"`
3208     func_note_Makefile_am_edit "$pobase_dir" SUBDIRS "$pobase_base"
3209   fi
3210   if test -n "$inctests"; then
3211     if test "$makefile_am" = Makefile.am; then
3212       testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
3213       testsbase_base=`basename "$testsbase"`
3214       func_note_Makefile_am_edit "$testsbase_dir" SUBDIRS "$testsbase_base"
3215     fi
3216   fi
3217   func_note_Makefile_am_edit "" ACLOCAL_AMFLAGS "-I ${m4base}"
3218   {
3219     # Find the first parent directory of $m4base that contains or will contain
3220     # a Makefile.am.
3221     sed_last='s,^.*/\([^/][^/]*\)//*$,\1/,
3222 s,//*$,/,'
3223     sed_butlast='s,[^/][^/]*//*$,,'
3224     dir1="${m4base}/"; dir2=""
3225     while test -n "$dir1" \
3226           && ! { test -f "${destdir}/${dir1}Makefile.am" \
3227                  || test "${dir1}Makefile.am" = "$sourcebase/$makefile_am" \
3228                  || test "./${dir1}Makefile.am" = "$sourcebase/$makefile_am" \
3229                  || { test -n "$inctests" \
3230                       && { test "${dir1}Makefile.am" = "$testsbase/$makefile_am" \
3231                            || test "./${dir1}Makefile.am" = "$testsbase/$makefile_am"; }; }; }; do
3232       dir2=`echo "$dir1" | sed -e "$sed_last"`"$dir2"
3233       dir1=`echo "$dir1" | sed -e "$sed_butlast"`
3234     done
3235     func_note_Makefile_am_edit "$dir1" EXTRA_DIST "${dir2}gnulib-cache.m4"
3236   }
3237
3238   # Create library makefile.
3239   func_dest_tmpfilename $sourcebase/$makefile_am
3240   destfile="$sourcebase/$makefile_am"
3241   modules="$main_modules"
3242   func_emit_lib_Makefile_am > "$tmpfile"
3243   if test -f "$destdir"/$sourcebase/$makefile_am; then
3244     if cmp "$destdir"/$sourcebase/$makefile_am "$tmpfile" > /dev/null; then
3245       rm -f "$tmpfile"
3246     else
3247       if $doit; then
3248         echo "Updating $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
3249         mv -f "$destdir"/$sourcebase/$makefile_am "$destdir"/$sourcebase/$makefile_am~
3250         mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
3251       else
3252         echo "Update $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
3253         rm -f "$tmpfile"
3254       fi
3255     fi
3256   else
3257     if $doit; then
3258       echo "Creating $sourcebase/$makefile_am"
3259       mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
3260     else
3261       echo "Create $sourcebase/$makefile_am"
3262       rm -f "$tmpfile"
3263     fi
3264     func_append added_files "$sourcebase/$makefile_am$nl"
3265   fi
3266
3267   # Create po/ directory.
3268   if test -n "$pobase"; then
3269     # Create po makefile and auxiliary files.
3270     for file in Makefile.in.in remove-potcdate.sin; do
3271       func_dest_tmpfilename $pobase/$file
3272       func_lookup_file build-aux/po/$file
3273       cat "$lookedup_file" > "$tmpfile"
3274       if test -f "$destdir"/$pobase/$file; then
3275         if cmp "$destdir"/$pobase/$file "$tmpfile" > /dev/null; then
3276           rm -f "$tmpfile"
3277         else
3278           if $doit; then
3279             echo "Updating $pobase/$file (backup in $pobase/$file~)"
3280             mv -f "$destdir"/$pobase/$file "$destdir"/$pobase/$file~
3281             mv -f "$tmpfile" "$destdir"/$pobase/$file
3282           else
3283             echo "Update $pobase/$file (backup in $pobase/$file~)"
3284             rm -f "$tmpfile"
3285           fi
3286         fi
3287       else
3288         if $doit; then
3289           echo "Creating $pobase/$file"
3290           mv -f "$tmpfile" "$destdir"/$pobase/$file
3291         else
3292           echo "Create $pobase/$file"
3293           rm -f "$tmpfile"
3294         fi
3295         func_append added_files "$pobase/$file$nl"
3296       fi
3297     done
3298     # Create po makefile parameterization, part 1.
3299     func_dest_tmpfilename $pobase/Makevars
3300     func_emit_po_Makevars > "$tmpfile"
3301     if test -f "$destdir"/$pobase/Makevars; then
3302       if cmp "$destdir"/$pobase/Makevars "$tmpfile" > /dev/null; then
3303         rm -f "$tmpfile"
3304       else
3305         if $doit; then
3306           echo "Updating $pobase/Makevars (backup in $pobase/Makevars~)"
3307           mv -f "$destdir"/$pobase/Makevars "$destdir"/$pobase/Makevars~
3308           mv -f "$tmpfile" "$destdir"/$pobase/Makevars
3309         else
3310           echo "Update $pobase/Makevars (backup in $pobase/Makevars~)"
3311           rm -f "$tmpfile"
3312         fi
3313       fi
3314     else
3315       if $doit; then
3316         echo "Creating $pobase/Makevars"
3317         mv -f "$tmpfile" "$destdir"/$pobase/Makevars
3318       else
3319         echo "Create $pobase/Makevars"
3320         rm -f "$tmpfile"
3321       fi
3322       func_append added_files "$pobase/Makevars$nl"
3323     fi
3324     # Create po makefile parameterization, part 2.
3325     func_dest_tmpfilename $pobase/POTFILES.in
3326     func_emit_po_POTFILES_in > "$tmpfile"
3327     if test -f "$destdir"/$pobase/POTFILES.in; then
3328       if cmp "$destdir"/$pobase/POTFILES.in "$tmpfile" > /dev/null; then
3329         rm -f "$tmpfile"
3330       else
3331         if $doit; then
3332           echo "Updating $pobase/POTFILES.in (backup in $pobase/POTFILES.in~)"
3333           mv -f "$destdir"/$pobase/POTFILES.in "$destdir"/$pobase/POTFILES.in~
3334           mv -f "$tmpfile" "$destdir"/$pobase/POTFILES.in
3335         else
3336           echo "Update $pobase/POTFILES.in (backup in $pobase/POTFILES.in~)"
3337           rm -f "$tmpfile"
3338         fi
3339       fi
3340     else
3341       if $doit; then
3342         echo "Creating $pobase/POTFILES.in"
3343         mv -f "$tmpfile" "$destdir"/$pobase/POTFILES.in
3344       else
3345         echo "Create $pobase/POTFILES.in"
3346         rm -f "$tmpfile"
3347       fi
3348       func_append added_files "$pobase/POTFILES.in$nl"
3349     fi
3350     # Fetch PO files.
3351     TP_URL="http://translationproject.org/latest/"
3352     TP_RSYNC_URI="translationproject.org::tp/latest/"
3353     if $doit; then
3354       echo "Fetching gnulib PO files from $TP_URL"
3355       (cd "$destdir"/$pobase \
3356        && { # Prefer rsync over wget if it is available, since it consumes
3357             # less network bandwidth, due to compression.
3358             if type rsync 2>/dev/null | grep / > /dev/null; then
3359               rsync -Lrtz "${TP_RSYNC_URI}gnulib/" .
3360             else
3361               wget --quiet -r -l1 -nd -np -A.po "${TP_URL}gnulib"
3362             fi
3363           }
3364       )
3365     else
3366       echo "Fetch gnulib PO files from $TP_URL"
3367     fi
3368     # Create po/LINGUAS.
3369     if $doit; then
3370       func_dest_tmpfilename $pobase/LINGUAS
3371       (cd "$destdir"/$pobase \
3372        && { echo '# Set of available languages.'
3373             LC_ALL=C ls -1 *.po | sed -e 's,\.po$,,'
3374           }
3375       ) > "$tmpfile"
3376       if test -f "$destdir"/$pobase/LINGUAS; then
3377         if cmp "$destdir"/$pobase/LINGUAS "$tmpfile" > /dev/null; then
3378           rm -f "$tmpfile"
3379         else
3380           echo "Updating $pobase/LINGUAS (backup in $pobase/LINGUAS~)"
3381           mv -f "$destdir"/$pobase/LINGUAS "$destdir"/$pobase/LINGUAS~
3382           mv -f "$tmpfile" "$destdir"/$pobase/LINGUAS
3383         fi
3384       else
3385         echo "Creating $pobase/LINGUAS"
3386         mv -f "$tmpfile" "$destdir"/$pobase/LINGUAS
3387         func_append added_files "$pobase/LINGUAS$nl"
3388       fi
3389     else
3390       if test -f "$destdir"/$pobase/LINGUAS; then
3391         echo "Update $pobase/LINGUAS (backup in $pobase/LINGUAS~)"
3392       else
3393         echo "Create $pobase/LINGUAS"
3394       fi
3395     fi
3396   fi
3397
3398   # Create m4/gnulib-cache.m4.
3399   func_dest_tmpfilename $m4base/gnulib-cache.m4
3400   (
3401     func_emit_copyright_notice
3402     echo "#"
3403     echo "# This file represents the specification of how gnulib-tool is used."
3404     echo "# It acts as a cache: It is written and read by gnulib-tool."
3405     echo "# In projects using CVS, this file is meant to be stored in CVS,"
3406     echo "# like the configure.ac and various Makefile.am files."
3407     echo
3408     echo
3409     echo "# Specification in the form of a command-line invocation:"
3410     echo "#   $actioncmd"
3411     echo
3412     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
3413     # Store the local_gnulib_dir relative to destdir.
3414     case "$local_gnulib_dir" in
3415       "" | /*)
3416         relative_local_gnulib_dir="$local_gnulib_dir" ;;
3417       * )
3418         case "$destdir" in
3419           /*) relative_local_gnulib_dir="$local_gnulib_dir" ;;
3420           *)
3421             # destdir, local_gnulib_dir are both relative.
3422             func_relativize "$destdir" "$local_gnulib_dir"
3423             relative_local_gnulib_dir="$reldir" ;;
3424         esac ;;
3425     esac
3426     echo "gl_LOCAL_DIR([$relative_local_gnulib_dir])"
3427     echo "gl_MODULES(["
3428     echo "$specified_modules" | sed -e 's/^/  /g'
3429     echo "])"
3430     test -z "$incobsolete" || echo "gl_WITH_OBSOLETE"
3431     echo "gl_AVOID([$avoidlist])"
3432     echo "gl_SOURCE_BASE([$sourcebase])"
3433     echo "gl_M4_BASE([$m4base])"
3434     echo "gl_PO_BASE([$pobase])"
3435     echo "gl_DOC_BASE([$docbase])"
3436     echo "gl_TESTS_BASE([$testsbase])"
3437     test -z "$inctests" || echo "gl_WITH_TESTS"
3438     echo "gl_LIB([$libname])"
3439     if test -n "$lgpl"; then
3440       if test "$lgpl" = yes; then
3441         echo "gl_LGPL"
3442       else
3443         echo "gl_LGPL([$lgpl])"
3444       fi
3445     fi
3446     echo "gl_MAKEFILE_NAME([$makefile_name])"
3447     if test "$libtool" = true; then
3448       echo "gl_LIBTOOL"
3449     fi
3450     echo "gl_MACRO_PREFIX([$macro_prefix])"
3451     echo "gl_PO_DOMAIN([$po_domain])"
3452     if test -n "$vc_files"; then
3453       echo "gl_VC_FILES([$vc_files])"
3454     fi
3455   ) > "$tmpfile"
3456   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
3457     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
3458       rm -f "$tmpfile"
3459     else
3460       if $doit; then
3461         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
3462         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
3463         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
3464       else
3465         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
3466         if false; then
3467           cat "$tmpfile"
3468           echo
3469           echo "# gnulib-cache.m4 ends here"
3470         fi
3471         rm -f "$tmpfile"
3472       fi
3473     fi
3474   else
3475     if $doit; then
3476       echo "Creating $m4base/gnulib-cache.m4"
3477       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
3478     else
3479       echo "Create $m4base/gnulib-cache.m4"
3480       cat "$tmpfile"
3481       rm -f "$tmpfile"
3482     fi
3483   fi
3484
3485   # Create m4/gnulib-comp.m4.
3486   func_dest_tmpfilename $m4base/gnulib-comp.m4
3487   (
3488     echo "# DO NOT EDIT! GENERATED AUTOMATICALLY!"
3489     func_emit_copyright_notice
3490     echo "#"
3491     echo "# This file represents the compiled summary of the specification in"
3492     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
3493     echo "# to be invoked from configure.ac."
3494     echo "# In projects using CVS, this file can be treated like other built files."
3495     echo
3496     echo
3497     echo "# This macro should be invoked from $configure_ac, in the section"
3498     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
3499     echo "# any checks for libraries, header files, types and library functions."
3500     echo "AC_DEFUN([${macro_prefix}_EARLY],"
3501     echo "["
3502     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
3503     echo "  m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
3504     echo "  m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
3505     echo "  m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
3506     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
3507     if test -n "$uses_subdirs"; then
3508       echo "  AC_REQUIRE([AM_PROG_CC_C_O])"
3509     fi
3510     for module in $final_modules; do
3511       func_verify_module
3512       if test -n "$module"; then
3513         func_get_autoconf_early_snippet "$module"
3514       fi
3515     done \
3516       | sed -e '/^$/d;' -e 's/^/  /'
3517     echo "])"
3518     echo
3519     echo "# This macro should be invoked from $configure_ac, in the section"
3520     echo "# \"Check for header files, types and library functions\"."
3521     echo "AC_DEFUN([${macro_prefix}_INIT],"
3522     echo "["
3523     if test "$libtool" = true; then
3524       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
3525       echo "  gl_cond_libtool=true"
3526     else
3527       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
3528       echo "  gl_cond_libtool=false"
3529       echo "  gl_libdeps="
3530       echo "  gl_ltlibdeps="
3531     fi
3532     if test "$auxdir" != "build-aux"; then
3533       sed_replace_build_aux='
3534         :a
3535         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
3536           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
3537           ba
3538         }'
3539     else
3540       sed_replace_build_aux="$sed_noop"
3541     fi
3542     func_emit_initmacro_start $macro_prefix
3543     echo "  gl_source_base='$sourcebase'"
3544     for module in $main_modules; do
3545       func_verify_module
3546       if test -n "$module"; then
3547         func_get_autoconf_snippet "$module" \
3548           | sed -e '/^$/d;' -e 's/^/  /' \
3549                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
3550                 -e "$sed_replace_build_aux"
3551         if test "$module" = 'alloca' && test "$libtool" = true; then
3552           echo 'changequote(,)dnl'
3553           echo 'LTALLOCA=`echo "$ALLOCA" | sed -e '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
3554           echo 'changequote([, ])dnl'
3555           echo 'AC_SUBST([LTALLOCA])'
3556         fi
3557       fi
3558     done
3559     func_emit_initmacro_end $macro_prefix
3560     echo "  gltests_libdeps="
3561     echo "  gltests_ltlibdeps="
3562     func_emit_initmacro_start ${macro_prefix}tests
3563     echo "  gl_source_base='$testsbase'"
3564     for module in $testsrelated_modules; do
3565       func_verify_module
3566       if test -n "$module"; then
3567         func_get_autoconf_snippet "$module" \
3568           | sed -e '/^$/d;' -e 's/^/  /' \
3569                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
3570                 -e "$sed_replace_build_aux" \
3571                 -e 's/\$gl_cond_libtool/false/g' \
3572                 -e 's/gl_libdeps/gltests_libdeps/g' \
3573                 -e 's/gl_ltlibdeps/gltests_ltlibdeps/g'
3574       fi
3575     done
3576     func_emit_initmacro_end ${macro_prefix}tests
3577     # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
3578     # created using libtool, because libtool already handles the dependencies.
3579     if test "$libtool" != true; then
3580       libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z-' 'A-Z_'`
3581       echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
3582       echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
3583       echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
3584       echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
3585     fi
3586     if $use_libtests; then
3587       echo "  LIBTESTS_LIBDEPS=\"\$gltests_libdeps\""
3588       echo "  AC_SUBST([LIBTESTS_LIBDEPS])"
3589     fi
3590     echo "])"
3591     func_emit_initmacro_done $macro_prefix $sourcebase
3592     func_emit_initmacro_done ${macro_prefix}tests $testsbase
3593     echo
3594     echo "# This macro records the list of files which have been installed by"
3595     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
3596     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
3597     echo "$files" | sed -e 's,^,  ,'
3598     echo "])"
3599   ) > "$tmpfile"
3600   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
3601     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
3602       rm -f "$tmpfile"
3603     else
3604       if $doit; then
3605         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
3606         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
3607         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
3608       else
3609         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
3610         if false; then
3611           cat "$tmpfile"
3612           echo
3613           echo "# gnulib-comp.m4 ends here"
3614         fi
3615         rm -f "$tmpfile"
3616       fi
3617     fi
3618   else
3619     if $doit; then
3620       echo "Creating $m4base/gnulib-comp.m4"
3621       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
3622     else
3623       echo "Create $m4base/gnulib-comp.m4"
3624       cat "$tmpfile"
3625       rm -f "$tmpfile"
3626     fi
3627   fi
3628
3629   if test -n "$inctests"; then
3630     # Create tests makefile.
3631     func_dest_tmpfilename $testsbase/$makefile_am
3632     destfile="$testsbase/$makefile_am"
3633     modules="$testsrelated_modules"
3634     func_emit_tests_Makefile_am > "$tmpfile"
3635     if test -f "$destdir"/$testsbase/$makefile_am; then
3636       if cmp "$destdir"/$testsbase/$makefile_am "$tmpfile" > /dev/null; then
3637         rm -f "$tmpfile"
3638       else
3639         if $doit; then
3640           echo "Updating $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
3641           mv -f "$destdir"/$testsbase/$makefile_am "$destdir"/$testsbase/$makefile_am~
3642           mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
3643         else
3644           echo "Update $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
3645           rm -f "$tmpfile"
3646         fi
3647       fi
3648     else
3649       if $doit; then
3650         echo "Creating $testsbase/$makefile_am"
3651         mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
3652       else
3653         echo "Create $testsbase/$makefile_am"
3654         rm -f "$tmpfile"
3655       fi
3656     func_append added_files "$testsbase/$makefile_am$nl"
3657     fi
3658   fi
3659
3660   if test "$vc_files" != false; then
3661     # Update the .cvsignore and .gitignore files.
3662     { echo "$added_files" | sed -e '/^$/d' -e 's,\([^/]*\)$,|A|\1,'
3663       echo "$removed_files" | sed -e '/^$/d' -e 's,\([^/]*\)$,|R|\1,'
3664       # Treat gnulib-comp.m4 like an added file, even if it already existed.
3665       echo "$m4base/|A|gnulib-comp.m4"
3666     } | LC_ALL=C sort -t'|' -k1,1 > "$tmp"/fileset-changes
3667     { # Rearrange file descriptors. Needed because "while ... done < ..."
3668       # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3669       exec 5<&0 < "$tmp"/fileset-changes
3670       func_update_ignorelist ()
3671       {
3672         ignore="$1"
3673         if test "$ignore" = .gitignore; then
3674           # In a .gitignore file, "foo" applies to the current directory and all
3675           # subdirectories, whereas "/foo" applies to the current directory only.
3676           anchor='/'
3677           escaped_anchor='\/'
3678           doubly_escaped_anchor='\\/'
3679         else
3680           anchor=''
3681           escaped_anchor=''
3682           doubly_escaped_anchor=''
3683         fi
3684         if test -f "$destdir/$dir$ignore"; then
3685           if test -n "$dir_added" || test -n "$dir_removed"; then
3686             sed -e "s|^$anchor||" < "$destdir/$dir$ignore" | LC_ALL=C sort > "$tmp"/ignore
3687             (func_reset_sigpipe
3688              echo "$dir_added" | sed -e '/^$/d' | LC_ALL=C sort -u \
3689                | LC_ALL=C join -v 1 - "$tmp"/ignore > "$tmp"/ignore-added
3690              echo "$dir_removed" | sed -e '/^$/d' | LC_ALL=C sort -u \
3691                | LC_ALL=C join -v 1 - "$tmp"/ignore > "$tmp"/ignore-removed
3692             )
3693             if test -s "$tmp"/ignore-added || test -s "$tmp"/ignore-removed; then
3694               if $doit; then
3695                 echo "Updating $destdir/$dir$ignore (backup in $destdir/$dir${ignore}~)"
3696                 mv -f "$destdir/$dir$ignore" "$destdir/$dir$ignore"~
3697                 { sed -e 's,/,\\/,g' -e 's,^,/^,' -e 's,$,\$/d,' < "$tmp"/ignore-removed
3698                   if test -n "$anchor"; then sed -e 's,/,\\/,g' -e "s,^,/^${doubly_escaped_anchor}," -e 's,$,$/d,' < "$tmp"/ignore-removed; fi
3699                 } > "$tmp"/sed-ignore-removed
3700                 { cat "$destdir/$dir$ignore"~
3701                   sed -e "s|^|$anchor|" < "$tmp"/ignore-added
3702                 } | sed -f "$tmp"/sed-ignore-removed \
3703                   > "$destdir/$dir$ignore"
3704               else
3705                 echo "Update $destdir/$dir$ignore (backup in $destdir/$dir${ignore}~)"
3706               fi
3707             fi
3708           fi
3709         else
3710           if test -n "$dir_added"; then
3711             if $doit; then
3712               echo "Creating $destdir/$dir$ignore"
3713               {
3714                 if test "$ignore" = .cvsignore; then
3715                   echo ".deps"
3716                   # Automake generates Makefile rules that create .dirstamp files.
3717                   echo ".dirstamp"
3718                 fi
3719                 echo "$dir_added" | sed -e '/^$/d' -e "s|^|$anchor|" | LC_ALL=C sort -u
3720               } > "$destdir/$dir$ignore"
3721             else
3722               echo "Create $destdir/$dir$ignore"
3723             fi
3724           fi
3725         fi
3726       }
3727       func_done_dir ()
3728       {
3729         dir="$1"
3730         dir_added="$2"
3731         dir_removed="$3"
3732         if test -d "$destdir/CVS" || test -d "$destdir/${dir}CVS" || test -f "$destdir/${dir}.cvsignore"; then
3733           func_update_ignorelist .cvsignore
3734         fi
3735         if test -d "$destdir/.git" || test -f "$destdir/${dir}.gitignore"; then
3736           func_update_ignorelist .gitignore
3737         fi
3738       }
3739       last_dir=
3740       last_dir_added=
3741       last_dir_removed=
3742       while read line; do
3743         # Why not ''read next_dir op file'' ? Because the dir column can be empty.
3744         next_dir=`echo "$line" | sed -e 's,|.*,,'`
3745         op=`echo "$line" | sed -e 's,^[^|]*|\([^|]*\)|.*$,\1,'`
3746         file=`echo "$line" | sed -e 's,^[^|]*|[^|]*|,,'`
3747         if test "$next_dir" != "$last_dir"; then
3748           func_done_dir "$last_dir" "$last_dir_added" "$last_dir_removed"
3749           last_dir="$next_dir"
3750           last_dir_added=
3751           last_dir_removed=
3752         fi
3753         case $op in
3754           A) func_append last_dir_added "$file$nl";;
3755           R) func_append last_dir_removed "$file$nl";;
3756         esac
3757       done
3758       func_done_dir "$last_dir" "$last_dir_added" "$last_dir_removed"
3759       exec 0<&5 5<&-
3760     }
3761   fi
3762
3763   echo "Finished."
3764   echo
3765   echo "You may need to add #include directives for the following .h files."
3766   # Intersect $specified_modules and $main_modules
3767   # (since $specified_modules is not necessarily of subset of $main_modules
3768   # - some may have been skipped through --avoid, and since the elements of
3769   # $main_modules but not in $specified_modules can go away without explicit
3770   # notice - through changes in the module dependencies).
3771   echo "$specified_modules" > "$tmp"/modules1 # a sorted list, one module per line
3772   echo "$main_modules" > "$tmp"/modules2 # also a sorted list, one module per line
3773   # First the #include <...> directives without #ifs, sorted for convenience,
3774   # then the #include "..." directives without #ifs, sorted for convenience,
3775   # then the #include directives that are surrounded by #ifs. Not sorted.
3776   for module in `LC_ALL=C join "$tmp"/modules1 "$tmp"/modules2`; do
3777     include_directive=`func_get_include_directive "$module"`
3778     case "$nl$include_directive" in
3779       *"$nl#if"*)
3780         echo "$include_directive" 1>&5
3781         ;;
3782       *)
3783         echo "$include_directive" | grep -v 'include "' 1>&6
3784         echo "$include_directive" | grep 'include "' 1>&7
3785         ;;
3786     esac
3787   done 5> "$tmp"/include-if 6> "$tmp"/include-angles 7> "$tmp"/include-quotes
3788   (
3789    LC_ALL=C sort -u "$tmp"/include-angles
3790    LC_ALL=C sort -u "$tmp"/include-quotes
3791    cat "$tmp"/include-if
3792   ) | sed -e '/^$/d' -e 's/^/  /'
3793   rm -f "$tmp"/include-angles "$tmp"/include-quotes "$tmp"/include-if
3794
3795   for module in $main_modules; do
3796     func_get_link_directive "$module"
3797   done \
3798     | LC_ALL=C sort -u | sed -e '/^$/d' -e 's/^/  /' > "$tmp"/link
3799   if test `wc -l < "$tmp"/link` != 0; then
3800     echo
3801     echo "You may need to use the following Makefile variables when linking."
3802     echo "Use them in <program>_LDADD when linking a program, or"
3803     echo "in <library>_a_LDFLAGS or <library>_la_LDFLAGS when linking a library."
3804     cat "$tmp"/link
3805   fi
3806   rm -f "$tmp"/link
3807
3808   echo
3809   echo "Don't forget to"
3810   if test "$makefile_am" = Makefile.am; then
3811     echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
3812   else
3813     echo "  - \"include $makefile_name\" from within \"$sourcebase/Makefile.am\","
3814   fi
3815   if test -n "$pobase"; then
3816     echo "  - add \"$pobase/Makefile.in\" to AC_CONFIG_FILES in $configure_ac,"
3817   fi
3818   if test -n "$inctests"; then
3819     if test "$makefile_am" = Makefile.am; then
3820       echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
3821     else
3822       echo "  - \"include $makefile_name\" from within \"$testsbase/Makefile.am\","
3823     fi
3824   fi
3825   edit=0
3826   while test $edit != $makefile_am_edits; do
3827     edit=`expr $edit + 1`
3828     eval dir=\"\$makefile_am_edit${edit}_dir\"
3829     eval var=\"\$makefile_am_edit${edit}_var\"
3830     eval val=\"\$makefile_am_edit${edit}_val\"
3831     if test -n "$var"; then
3832       echo "  - mention \"${val}\" in ${var} in ${dir}Makefile.am,"
3833     fi
3834   done
3835   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
3836   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
3837 }
3838
3839 # func_create_testdir testdir modules
3840 # Input:
3841 # - local_gnulib_dir  from --local-dir
3842 # - auxdir          directory relative to destdir where to place build aux files
3843 func_create_testdir ()
3844 {
3845   testdir="$1"
3846   modules="$2"
3847   if test -z "$modules"; then
3848     # All modules together.
3849     # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
3850     # Except ftruncate, mountlist, which abort the configuration on mingw. FIXME.
3851     modules=`func_all_modules`
3852     modules=`for m in $modules; do case $m in config-h | ftruncate | mountlist) ;; *) echo $m;; esac; done`
3853   fi
3854   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort -u`
3855
3856   # Check that the license of every module is consistent with the license of
3857   # its dependencies.
3858   saved_modules="$modules"
3859   saved_inctests="$inctests"
3860   # When computing transitive closures, don't consider $module to depend on
3861   # $module-tests. Need this becauses tests are implicitly GPL and may depend
3862   # on GPL modules - therefore we don't want a warning in this case.
3863   inctests=""
3864   for requested_module in $saved_modules; do
3865     requested_license=`func_get_license "$requested_module"`
3866     if test "$requested_license" != GPL; then
3867       # Here we use func_modules_transitive_closure, not just
3868       # func_get_dependencies, so that we also detect weird situations like
3869       # an LGPL module which depends on a GPLed build tool module which depends
3870       # on a GPL module.
3871       modules="$requested_module"
3872       func_modules_transitive_closure
3873       for module in $modules; do
3874         license=`func_get_license "$module"`
3875         case "$license" in
3876           'GPLed build tool') ;;
3877           'public domain' | 'unlimited' | 'unmodifiable license text') ;;
3878           *)
3879             case "$requested_license" in
3880               GPLv2+)
3881                 case "$license" in
3882                   GPLv2+ | LGPLv2+) ;;
3883                   *) echo "warning: module $requested_module depends on a module with an incompatible license: $module" 1>&2 ;;
3884                 esac
3885                 ;;
3886               LGPL)
3887                 case "$license" in
3888                   LGPL | LGPLv2+) ;;
3889                   *) echo "warning: module $requested_module depends on a module with an incompatible license: $module" 1>&2 ;;
3890                 esac
3891                 ;;
3892               LGPLv2+)
3893                 case "$license" in
3894                   LGPLv2+) ;;
3895                   *) echo "warning: module $requested_module depends on a module with an incompatible license: $module" 1>&2 ;;
3896                 esac
3897                 ;;
3898             esac
3899             ;;
3900         esac
3901       done
3902     fi
3903   done
3904   modules="$saved_modules"
3905   inctests="$saved_inctests"
3906
3907   # Subdirectory names.
3908   sourcebase=gllib
3909   m4base=glm4
3910   pobase=
3911   docbase=gldoc
3912   testsbase=gltests
3913   macro_prefix=gl
3914   po_domain=
3915   vc_files=
3916
3917   # Determine final module list.
3918   func_modules_transitive_closure
3919   if test $verbose -ge 0; then
3920     echo "Module list with included dependencies:"
3921     echo "$modules" | sed -e 's/^/  /'
3922   fi
3923
3924   # Add the dummy module if needed.
3925   func_modules_add_dummy
3926
3927   # Show banner notice of every module.
3928   func_modules_notice
3929
3930   # Determine final file list.
3931   func_modules_to_filelist
3932   if test $verbose -ge 0; then
3933     echo "File list:"
3934     echo "$files" | sed -e 's/^/  /'
3935   fi
3936
3937   rewritten='%REWRITTEN%'
3938   sed_rewrite_files="\
3939     s,^build-aux/,$rewritten$auxdir/,
3940     s,^doc/,$rewritten$docbase/,
3941     s,^lib/,$rewritten$sourcebase/,
3942     s,^m4/,$rewritten$m4base/,
3943     s,^tests/,$rewritten$testsbase/,
3944     s,^top/,$rewritten,
3945     s,^$rewritten,,"
3946
3947   # Create directories.
3948   for f in $files; do echo $f; done \
3949     | sed -e "$sed_rewrite_files" \
3950     | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
3951     | LC_ALL=C sort -u \
3952     > "$tmp"/dirs
3953   { # Rearrange file descriptors. Needed because "while ... done < ..."
3954     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3955     exec 5<&0 < "$tmp"/dirs
3956     while read d; do
3957       mkdir -p "$testdir/$d"
3958     done
3959     exec 0<&5 5<&-
3960   }
3961
3962   # Copy files or make symbolic links.
3963   delimiter='   '
3964   for f in $files; do echo $f; done \
3965     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_files" \
3966     | LC_ALL=C sort \
3967     > "$tmp"/files
3968   { # Rearrange file descriptors. Needed because "while ... done < ..."
3969     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
3970     exec 5<&0 < "$tmp"/files
3971     while read g f; do
3972       func_lookup_file "$f"
3973       if test -n "$lookedup_tmp"; then
3974         cp -p "$lookedup_file" "$testdir/$g"
3975       else
3976         ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
3977         if { test -n "$symbolic" \
3978              || { test -n "$lsymbolic" \
3979                   && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; }; then
3980           func_ln "$lookedup_file" "$testdir/$g"
3981         else
3982           cp -p "$lookedup_file" "$testdir/$g"
3983         fi
3984       fi
3985     done
3986     exec 0<&5 5<&-
3987   }
3988
3989   # Create Makefile.ams that are for testing.
3990   for_test=true
3991
3992   # No special edits are needed.
3993   makefile_am_edits=0
3994
3995   # Create $sourcebase/Makefile.am.
3996   mkdir -p "$testdir/$sourcebase"
3997   destfile="$sourcebase/Makefile.am"
3998   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
3999   any_uses_subdirs="$uses_subdirs"
4000
4001   # Create $m4base/Makefile.am.
4002   mkdir -p "$testdir/$m4base"
4003   (echo "## Process this file with automake to produce Makefile.in."
4004    echo
4005    echo "EXTRA_DIST ="
4006    for f in $files; do
4007      case "$f" in
4008        m4/* )
4009          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
4010      esac
4011    done
4012   ) > "$testdir/$m4base/Makefile.am"
4013
4014   subdirs="$sourcebase $m4base"
4015   subdirs_with_configure_ac=""
4016
4017   if false && test -f "$testdir"/$m4base/gettext.m4; then
4018     # Avoid stupid error message from automake:
4019     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
4020     mkdir -p "$testdir/po"
4021     (echo "## Process this file with automake to produce Makefile.in."
4022     ) > "$testdir/po/Makefile.am"
4023     func_append subdirs " po"
4024   fi
4025
4026   if test -n "$inctests"; then
4027     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
4028     # Viewed from the $testsbase subdirectory, $auxdir is different.
4029     saved_auxdir="$auxdir"
4030     auxdir=`echo "$testsbase/" | sed -e 's%[^/][^/]*//*%../%g'`"$auxdir"
4031     # Create $testsbase/Makefile.am.
4032     use_libtests=false
4033     destfile="$testsbase/Makefile.am"
4034     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
4035     any_uses_subdirs="$any_uses_subdirs$uses_subdirs"
4036     # Create $testsbase/configure.ac.
4037     (echo "# Process this file with autoconf to produce a configure script."
4038      echo "AC_INIT([dummy], [0])"
4039      echo "AC_CONFIG_AUX_DIR([$auxdir])"
4040      echo "AM_INIT_AUTOMAKE"
4041      echo
4042      echo "AC_CONFIG_HEADERS([config.h])"
4043      echo
4044      echo "AC_PROG_CC"
4045      echo "AC_PROG_INSTALL"
4046      echo "AC_PROG_MAKE_SET"
4047      echo "AC_PROG_RANLIB"
4048      echo
4049      if test -n "$uses_subdirs"; then
4050        echo "AM_PROG_CC_C_O"
4051        echo
4052      fi
4053      for module in $modules; do
4054        func_verify_module
4055        if test -n "$module"; then
4056          case $module in
4057            gnumakefile | maintainer-makefile)
4058              # These modules are meant to be used only in the top-level directory.
4059              ;;
4060            *)
4061              func_get_autoconf_early_snippet "$module"
4062              ;;
4063          esac
4064        fi
4065      done \
4066        | sed -e '/^$/d;' -e 's/AC_REQUIRE(\[\([^()]*\)\])/\1/'
4067      if test "$libtool" = true; then
4068        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
4069        echo "gl_cond_libtool=true"
4070      else
4071        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
4072        echo "gl_cond_libtool=false"
4073        echo "gl_libdeps="
4074        echo "gl_ltlibdeps="
4075      fi
4076      # Wrap the set of autoconf snippets into an autoconf macro that is then
4077      # invoked. This is needed because autoconf does not support AC_REQUIRE
4078      # at the top level:
4079      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
4080      # but we want the AC_REQUIRE to have its normal meaning (provide one
4081      # expansion of the required macro before the current point, and only one
4082      # expansion total).
4083      echo "AC_DEFUN([gl_INIT], ["
4084      sed_replace_build_aux='
4085        :a
4086        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
4087          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
4088          ba
4089        }'
4090      func_emit_initmacro_start $macro_prefix
4091      # We don't have explicit ordering constraints between the various
4092      # autoconf snippets. It's cleanest to put those of the library before
4093      # those of the tests.
4094      echo "gl_source_base='../$sourcebase'"
4095      for module in $modules; do
4096        func_verify_nontests_module
4097        if test -n "$module"; then
4098          case $module in
4099            gnumakefile | maintainer-makefile)
4100              # These modules are meant to be used only in the top-level directory.
4101              ;;
4102            *)
4103              func_get_autoconf_snippet "$module" \
4104                | sed -e "$sed_replace_build_aux"
4105              ;;
4106          esac
4107        fi
4108      done
4109      echo "gl_source_base='.'"
4110      for module in $modules; do
4111        func_verify_tests_module
4112        if test -n "$module"; then
4113          func_get_autoconf_snippet "$module" \
4114            | sed -e "$sed_replace_build_aux"
4115        fi
4116      done
4117      func_emit_initmacro_end $macro_prefix
4118      # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
4119      # created using libtool, because libtool already handles the dependencies.
4120      if test "$libtool" != true; then
4121        libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z-' 'A-Z_'`
4122        echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
4123        echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
4124        echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
4125        echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
4126      fi
4127      echo "])"
4128      func_emit_initmacro_done $macro_prefix $sourcebase # FIXME use $sourcebase or $testsbase?
4129      echo
4130      echo "gl_INIT"
4131      echo
4132      # Usually $testsbase/config.h will be a superset of config.h. Verify this
4133      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
4134      echo "AH_TOP([#include \"../config.h\"])"
4135      echo
4136      echo "AC_CONFIG_FILES([Makefile])"
4137      echo "AC_OUTPUT"
4138     ) > "$testdir/$testsbase/configure.ac"
4139     auxdir="$saved_auxdir"
4140     func_append subdirs " $testsbase"
4141     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
4142   fi
4143
4144   # Create Makefile.am.
4145   (echo "## Process this file with automake to produce Makefile.in."
4146    echo
4147    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
4148    echo
4149    echo "SUBDIRS = $subdirs"
4150    echo
4151    echo "ACLOCAL_AMFLAGS = -I $m4base"
4152   ) > "$testdir/Makefile.am"
4153
4154   # Create configure.ac.
4155   (echo "# Process this file with autoconf to produce a configure script."
4156    echo "AC_INIT([dummy], [0])"
4157    if test "$auxdir" != "."; then
4158      echo "AC_CONFIG_AUX_DIR([$auxdir])"
4159    fi
4160    echo "AM_INIT_AUTOMAKE"
4161    echo
4162    echo "AC_CONFIG_HEADERS([config.h])"
4163    echo
4164    echo "AC_PROG_CC"
4165    echo "AC_PROG_INSTALL"
4166    echo "AC_PROG_MAKE_SET"
4167    echo
4168    echo "# For autobuild."
4169    echo "AC_CANONICAL_BUILD"
4170    echo "AC_CANONICAL_HOST"
4171    echo
4172    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
4173    echo "m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
4174    echo "m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
4175    echo "m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
4176    echo
4177    echo "AC_PROG_RANLIB"
4178    echo
4179    if test -n "$any_uses_subdirs"; then
4180      echo "AM_PROG_CC_C_O"
4181      echo
4182    fi
4183    for module in $modules; do
4184      func_verify_nontests_module
4185      if test -n "$module"; then
4186        func_get_autoconf_early_snippet "$module"
4187      fi
4188    done \
4189      | sed -e '/^$/d;' -e 's/AC_REQUIRE(\[\([^()]*\)\])/\1/'
4190    if test "$libtool" = true; then
4191      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
4192      echo "gl_cond_libtool=true"
4193    else
4194      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
4195      echo "gl_cond_libtool=false"
4196      echo "gl_libdeps="
4197      echo "gl_ltlibdeps="
4198    fi
4199    # Wrap the set of autoconf snippets into an autoconf macro that is then
4200    # invoked. This is needed because autoconf does not support AC_REQUIRE
4201    # at the top level:
4202    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
4203    # but we want the AC_REQUIRE to have its normal meaning (provide one
4204    # expansion of the required macro before the current point, and only one
4205    # expansion total).
4206    echo "AC_DEFUN([gl_INIT], ["
4207    if test "$auxdir" != "build-aux"; then
4208      sed_replace_build_aux='
4209        :a
4210        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
4211          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
4212          ba
4213        }'
4214    else
4215      sed_replace_build_aux="$sed_noop"
4216    fi
4217    func_emit_initmacro_start $macro_prefix
4218    echo "gl_source_base='$sourcebase'"
4219    for module in $modules; do
4220      func_verify_nontests_module
4221      if test -n "$module"; then
4222        func_get_autoconf_snippet "$module" \
4223          | sed -e "$sed_replace_build_aux"
4224      fi
4225    done
4226    func_emit_initmacro_end $macro_prefix
4227    # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
4228    # created using libtool, because libtool already handles the dependencies.
4229    if test "$libtool" != true; then
4230      libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z-' 'A-Z_'`
4231      echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
4232      echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
4233      echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
4234      echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
4235    fi
4236    echo "])"
4237    func_emit_initmacro_done $macro_prefix $sourcebase
4238    echo
4239    echo "gl_INIT"
4240    echo
4241    if test -n "$subdirs_with_configure_ac"; then
4242      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
4243    fi
4244    makefiles="Makefile"
4245    for d in $subdirs; do
4246      # For subdirs that have a configure.ac by their own, it's the subdir's
4247      # configure.ac which creates the subdir's Makefile.am, not this one.
4248      case " $subdirs_with_configure_ac " in
4249        *" $d "*) ;;
4250        *) func_append makefiles " $d/Makefile" ;;
4251      esac
4252    done
4253    echo "AC_CONFIG_FILES([$makefiles])"
4254    echo "AC_OUTPUT"
4255   ) > "$testdir/configure.ac"
4256
4257   # Create autogenerated files.
4258   (cd "$testdir"
4259    # Do not use "${AUTORECONF} --force --install", because it may invoke
4260    # autopoint, which brings in older versions of some of our .m4 files.
4261    if test -f $m4base/gettext.m4; then
4262      func_execute_command ${AUTOPOINT} --force || func_exit 1
4263      for f in $m4base/*.m4~; do
4264        if test -f $f; then
4265          mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
4266        fi
4267      done
4268    fi
4269    func_execute_command ${ACLOCAL} -I $m4base || func_exit 1
4270    if ! test -d build-aux; then
4271      func_execute_command mkdir build-aux || func_exit 1
4272    fi
4273    func_execute_command ${AUTOCONF} || func_exit 1
4274    func_execute_command ${AUTOHEADER} || func_exit 1
4275    func_execute_command ${AUTOMAKE} --add-missing --copy || func_exit 1
4276   ) || func_exit 1
4277   if test -n "$inctests"; then
4278     # Create autogenerated files.
4279     (cd "$testdir/$testsbase" || func_exit 1
4280      # Do not use "${AUTORECONF} --force --install", because it may invoke
4281      # autopoint, which brings in older versions of some of our .m4 files.
4282      if test -f ../$m4base/gettext.m4; then
4283        func_execute_command ${AUTOPOINT} --force || func_exit 1
4284        for f in ../$m4base/*.m4~; do
4285          if test -f $f; then
4286            mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
4287          fi
4288        done
4289      fi
4290      func_execute_command ${ACLOCAL} -I ../$m4base || func_exit 1
4291      if ! test -d ../build-aux; then
4292        func_execute_command mkdir ../build-aux
4293      fi
4294      func_execute_command ${AUTOCONF} || func_exit 1
4295      func_execute_command ${AUTOHEADER} || func_exit 1
4296      func_execute_command ${AUTOMAKE} --add-missing --copy || func_exit 1
4297     ) || func_exit 1
4298   fi
4299   # Need to run configure and make once, to create built files that are to be
4300   # distributed (such as getdate.c).
4301   # Extract the value of "CLEANFILES += ..." and "MOSTLYCLEANFILES += ...".
4302   cleaned_files=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
4303                  | sed -n -e 's,^CLEANFILES[     ]*+=\([^#]*\).*$,\1,p' -e 's,^MOSTLYCLEANFILES[         ]*+=\([^#]*\).*$,\1,p'`
4304   cleaned_files=`for file in $cleaned_files; do echo " $file "; done`
4305   # Extract the value of "BUILT_SOURCES += ...". Remove variable references
4306   # such $(FOO_H) because they don't refer to distributed files.
4307   sed_remove_make_variables='s,[$]([A-Za-z0-9_]*),,g'
4308   built_sources=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
4309                  | sed -n -e 's,^BUILT_SOURCES[  ]*+=\([^#]*\).*$,\1,p' \
4310                  | sed -e "$sed_remove_make_variables"`
4311   distributed_built_sources=`for file in $built_sources; do
4312                                case "$cleaned_files" in
4313                                  *" "$file" "*) ;;
4314                                  *) echo $file ;;
4315                                esac;
4316                              done`
4317   if test -n "$distributed_built_sources"; then
4318     (cd "$testdir"
4319      ./configure || func_exit 1
4320        cd "$sourcebase"
4321        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
4322        $MAKE built_sources || func_exit 1
4323        cd ..
4324      $MAKE distclean || func_exit 1
4325     ) || func_exit 1
4326   fi
4327 }
4328
4329 # func_create_megatestdir megatestdir allmodules
4330 # Input:
4331 # - local_gnulib_dir  from --local-dir
4332 # - auxdir          directory relative to destdir where to place build aux files
4333 func_create_megatestdir ()
4334 {
4335   megatestdir="$1"
4336   allmodules="$2"
4337   if test -z "$allmodules"; then
4338     allmodules=`func_all_modules`
4339   fi
4340
4341   megasubdirs=
4342   # First, all modules one by one.
4343   for onemodule in $allmodules; do
4344     func_create_testdir "$megatestdir/$onemodule" $onemodule
4345     func_append megasubdirs "$onemodule "
4346   done
4347   # Then, all modules all together.
4348   # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
4349   allmodules=`for m in $allmodules; do if test $m != config-h; then echo $m; fi; done`
4350   func_create_testdir "$megatestdir/ALL" "$allmodules"
4351   func_append megasubdirs "ALL"
4352
4353   # Create autobuild.
4354   cvsdate=`if test -f "$gnulib_dir/CVS/Entries"; then \
4355              vc_witness="$gnulib_dir/CVS/Entries"; \
4356            else \
4357              vc_witness="$gnulib_dir/.git/refs/heads/master"; \
4358            fi; \
4359            sh "$gnulib_dir/build-aux/mdate-sh" "$vc_witness" \
4360              | sed -e 's,January,01,'   -e 's,Jan,01,' \
4361                    -e 's,February,02,'  -e 's,Feb,02,' \
4362                    -e 's,March,03,'     -e 's,Mar,03,' \
4363                    -e 's,April,04,'     -e 's,Apr,04,' \
4364                    -e 's,May,05,'                      \
4365                    -e 's,June,06,'      -e 's,Jun,06,' \
4366                    -e 's,July,07,'      -e 's,Jul,07,' \
4367                    -e 's,August,08,'    -e 's,Aug,08,' \
4368                    -e 's,September,09,' -e 's,Sep,09,' \
4369                    -e 's,October,10,'   -e 's,Oct,10,' \
4370                    -e 's,November,11,'  -e 's,Nov,11,' \
4371                    -e 's,December,12,'  -e 's,Dec,12,' \
4372                    -e 's,^,00,' -e 's,^[0-9]*\([0-9][0-9] \),\1,' \
4373                    -e 's,^\([0-9]*\) \([0-9]*\) \([0-9]*\),\3\2\1,'`
4374   (echo '#!/bin/sh'
4375    echo "CVSDATE=$cvsdate"
4376    echo ": \${MAKE=make}"
4377    echo "test -d logs || mkdir logs"
4378    echo "for module in $megasubdirs; do"
4379    echo "  echo \"Working on module \$module...\""
4380    echo "  safemodule=\`echo \$module | sed -e 's|/|-|g'\`"
4381    echo "  (echo \"To: gnulib@autobuild.josefsson.org\""
4382    echo "   echo"
4383    echo "   set -x"
4384    echo "   : autobuild project... \$module"
4385    echo "   : autobuild revision... cvs-\$CVSDATE-000000"
4386    echo "   : autobuild timestamp... \`date \"+%Y%m%d-%H%M%S\"\`"
4387    echo "   : autobuild hostname... \`hostname\`"
4388    echo "   cd \$module && ./configure \$CONFIGURE_OPTIONS && \$MAKE && \$MAKE check && \$MAKE distclean"
4389    echo "   echo rc=\$?"
4390    echo "  ) 2>&1 | { if test -n \"\$AUTOBUILD_SUBST\"; then sed -e \"\$AUTOBUILD_SUBST\"; else cat; fi; } > logs/\$safemodule"
4391    echo "done"
4392   ) > "$megatestdir/do-autobuild"
4393   chmod a+x "$megatestdir/do-autobuild"
4394
4395   # Create Makefile.am.
4396   (echo "## Process this file with automake to produce Makefile.in."
4397    echo
4398    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
4399    echo
4400    echo "SUBDIRS = $megasubdirs"
4401    echo
4402    echo "EXTRA_DIST = do-autobuild"
4403   ) > "$megatestdir/Makefile.am"
4404
4405   # Create configure.ac.
4406   (echo "# Process this file with autoconf to produce a configure script."
4407    echo "AC_INIT([dummy], [0])"
4408    if test "$auxdir" != "."; then
4409      echo "AC_CONFIG_AUX_DIR([$auxdir])"
4410    fi
4411    echo "AM_INIT_AUTOMAKE"
4412    echo
4413    echo "AC_PROG_MAKE_SET"
4414    echo
4415    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
4416    echo "AC_CONFIG_FILES([Makefile])"
4417    echo "AC_OUTPUT"
4418   ) > "$megatestdir/configure.ac"
4419
4420   # Create autogenerated files.
4421   (cd "$megatestdir"
4422    # Do not use "${AUTORECONF} --install", because autoreconf operates
4423    # recursively, but the subdirectories are already finished, therefore
4424    # calling autoreconf here would only waste lots of CPU time.
4425    func_execute_command ${ACLOCAL} || func_exit 1
4426    func_execute_command mkdir build-aux
4427    func_execute_command ${AUTOCONF} || func_exit 1
4428    func_execute_command ${AUTOMAKE} --add-missing --copy || func_exit 1
4429   ) || func_exit 1
4430 }
4431
4432 case $mode in
4433   "" )
4434     func_fatal_error "no mode specified" ;;
4435
4436   list )
4437     func_all_modules
4438     ;;
4439
4440   import | update )
4441
4442     # Where to import.
4443     if test -z "$destdir"; then
4444       destdir=.
4445     fi
4446     test -d "$destdir" \
4447       || func_fatal_error "destination directory does not exist: $destdir"
4448
4449     # Prefer configure.ac to configure.in.
4450     if test -f "$destdir"/configure.ac; then
4451       configure_ac="$destdir/configure.ac"
4452     else
4453       if test -f "$destdir"/configure.in; then
4454         configure_ac="$destdir/configure.in"
4455       else
4456         func_fatal_error "cannot find $destdir/configure.ac - make sure you run gnulib-tool from within your package's directory"
4457       fi
4458     fi
4459
4460     # Analyze configure.ac.
4461     guessed_auxdir="."
4462     guessed_libtool=false
4463     my_sed_traces='
4464       s,#.*$,,
4465       s,^dnl .*$,,
4466       s, dnl .*$,,
4467       /AC_CONFIG_AUX_DIR/ {
4468         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^]"$`\\)]*\).*$,guessed_auxdir="\1",p
4469       }
4470       /A[CM]_PROG_LIBTOOL/ {
4471         s,^.*$,guessed_libtool=true,p
4472       }'
4473     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
4474
4475     if test -z "$auxdir"; then
4476       auxdir="$guessed_auxdir"
4477     fi
4478
4479     # Determine where to apply func_import.
4480     if test -n "$m4base"; then
4481       # Apply func_import to a particular gnulib directory.
4482       # Any number of additional modules can be given.
4483       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
4484         # First use of gnulib in the given m4base.
4485         test -n "$supplied_libname" || supplied_libname=true
4486         test -n "$sourcebase" || sourcebase="lib"
4487         test -n "$docbase" || docbase="doc"
4488         test -n "$testsbase" || testsbase="tests"
4489         test -n "$macro_prefix" || macro_prefix="gl"
4490       fi
4491       func_import "$*"
4492     else
4493       # Apply func_import to all gnulib directories.
4494       # To get this list of directories, look at Makefile.am. (Not at
4495       # configure, because it may be omitted from CVS. Also, don't run
4496       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
4497       m4dirs=
4498       m4dirs_count=0
4499       if test -f "$destdir"/Makefile.am; then
4500         aclocal_amflags=`sed -n -e 's/^ACLOCAL_AMFLAGS[  ]*=\(.*\)$/\1/p' "$destdir"/Makefile.am`
4501         m4dir_is_next=
4502         for arg in $aclocal_amflags; do
4503           if test -n "$m4dir_is_next"; then
4504             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
4505             case "$arg" in
4506               /*) ;;
4507               *)
4508                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
4509                   func_append m4dirs " $arg"
4510                   m4dirs_count=`expr $m4dirs_count + 1`
4511                 fi
4512                 ;;
4513             esac
4514             m4dir_is_next=
4515           else
4516             if test "X$arg" = "X-I"; then
4517               m4dir_is_next=yes
4518             else
4519               m4dir_is_next=
4520             fi
4521           fi
4522         done
4523       else
4524         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
4525         if test -f "$destdir"/aclocal.m4; then
4526           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
4527           sedexpr2='s,^[^/]*$,.,'
4528           sedexpr3='s,/[^/]*$,,'
4529           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort -u`
4530           m4dirs_count=`printf %s "$m4dirs" | wc -l`
4531         fi
4532       fi
4533       if test $m4dirs_count = 0; then
4534         # First use of gnulib in a package.
4535         # Any number of additional modules can be given.
4536         test -n "$supplied_libname" || supplied_libname=true
4537         test -n "$sourcebase" || sourcebase="lib"
4538         m4base="m4"
4539         test -n "$docbase" || docbase="doc"
4540         test -n "$testsbase" || testsbase="tests"
4541         test -n "$macro_prefix" || macro_prefix="gl"
4542         func_import "$*"
4543       else
4544         if test $m4dirs_count = 1; then
4545           # There's only one use of gnulib here. Assume the user means it.
4546           # Any number of additional modules can be given.
4547           for m4base in $m4dirs; do
4548             func_import "$*"
4549           done
4550         else
4551           # Ambiguous - guess what the user meant.
4552           if test $# = 0; then
4553             # No further arguments. Guess the user wants to update all of them.
4554             for m4base in $m4dirs; do
4555               # Perform func_import in a subshell, so that variable values
4556               # such as
4557               #   local_gnulib_dir, incobsolete, avoidlist, sourcebase, m4base,
4558               #   pobase, docbase, testsbase, inctests, libname, lgpl,
4559               #   makefile_name, libtool, macro_prefix, po_domain, vc_files
4560               # don't propagate from one directory to another.
4561               (func_import) || func_exit 1
4562             done
4563           else
4564             # Really ambiguous.
4565             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
4566           fi
4567         fi
4568       fi
4569     fi
4570     ;;
4571
4572   create-testdir )
4573     if test -z "$destdir"; then
4574       func_fatal_error "please specify --dir option"
4575     fi
4576     mkdir "$destdir"
4577     test -d "$destdir" \
4578       || func_fatal_error "could not create destination directory"
4579     test -n "$auxdir" || auxdir="build-aux"
4580     func_create_testdir "$destdir" "$*"
4581     ;;
4582
4583   create-megatestdir )
4584     if test -z "$destdir"; then
4585       func_fatal_error "please specify --dir option"
4586     fi
4587     mkdir "$destdir" || func_fatal_error "could not create destination directory"
4588     test -n "$auxdir" || auxdir="build-aux"
4589     func_create_megatestdir "$destdir" "$*"
4590     ;;
4591
4592   test )
4593     test -n "$destdir" || destdir=testdir$$
4594     mkdir "$destdir" || func_fatal_error "could not create destination directory"
4595     test -n "$auxdir" || auxdir="build-aux"
4596     func_create_testdir "$destdir" "$*"
4597     cd "$destdir"
4598       mkdir build
4599       cd build
4600         ../configure || func_exit 1
4601         $MAKE || func_exit 1
4602         $MAKE check || func_exit 1
4603         $MAKE distclean || func_exit 1
4604         remaining=`find . -type f -print`
4605         if test -n "$remaining"; then
4606           echo "Remaining files:" $remaining 1>&2
4607           echo "gnulib-tool: *** Stop." 1>&2
4608           func_exit 1
4609         fi
4610       cd ..
4611     cd ..
4612     rm -rf "$destdir"
4613     ;;
4614
4615   megatest )
4616     test -n "$destdir" || destdir=testdir$$
4617     mkdir "$destdir" || func_fatal_error "could not create destination directory"
4618     test -n "$auxdir" || auxdir="build-aux"
4619     func_create_megatestdir "$destdir" "$*"
4620     cd "$destdir"
4621       mkdir build
4622       cd build
4623         ../configure
4624         $MAKE
4625         $MAKE check
4626         $MAKE distclean
4627         remaining=`find . -type f -print`
4628         if test -n "$remaining"; then
4629           echo "Remaining files:" $remaining 1>&2
4630           echo "gnulib-tool: *** Stop." 1>&2
4631           func_exit 1
4632         fi
4633       cd ..
4634     cd ..
4635     rm -rf "$destdir"
4636     ;;
4637
4638   extract-description )
4639     for module
4640     do
4641       func_verify_module
4642       if test -n "$module"; then
4643         func_get_description "$module"
4644       fi
4645     done
4646     ;;
4647
4648   extract-status )
4649     for module
4650     do
4651       func_verify_module
4652       if test -n "$module"; then
4653         func_get_status "$module"
4654       fi
4655     done
4656     ;;
4657
4658   extract-notice )
4659     for module
4660     do
4661       func_verify_module
4662       if test -n "$module"; then
4663         func_get_notice "$module"
4664       fi
4665     done
4666     ;;
4667
4668   extract-applicability )
4669     for module
4670     do
4671       func_verify_module
4672       if test -n "$module"; then
4673         func_get_applicability "$module"
4674       fi
4675     done
4676     ;;
4677
4678   extract-filelist )
4679     for module
4680     do
4681       func_verify_module
4682       if test -n "$module"; then
4683         func_get_filelist "$module"
4684       fi
4685     done
4686     ;;
4687
4688   extract-dependencies )
4689     for module
4690     do
4691       func_verify_module
4692       if test -n "$module"; then
4693         func_get_dependencies "$module"
4694       fi
4695     done
4696     ;;
4697
4698   extract-autoconf-snippet )
4699     for module
4700     do
4701       func_verify_module
4702       if test -n "$module"; then
4703         func_get_autoconf_snippet "$module"
4704       fi
4705     done
4706     ;;
4707
4708   extract-automake-snippet )
4709     for module
4710     do
4711       func_verify_module
4712       if test -n "$module"; then
4713         func_get_automake_snippet "$module"
4714       fi
4715     done
4716     ;;
4717
4718   extract-include-directive )
4719     for module
4720     do
4721       func_verify_module
4722       if test -n "$module"; then
4723         func_get_include_directive "$module"
4724       fi
4725     done
4726     ;;
4727
4728   extract-link-directive )
4729     for module
4730     do
4731       func_verify_module
4732       if test -n "$module"; then
4733         func_get_link_directive "$module"
4734       fi
4735     done
4736     ;;
4737
4738   extract-license )
4739     for module
4740     do
4741       func_verify_module
4742       if test -n "$module"; then
4743         func_get_license "$module"
4744       fi
4745     done
4746     ;;
4747
4748   extract-maintainer )
4749     for module
4750     do
4751       func_verify_module
4752       if test -n "$module"; then
4753         func_get_maintainer "$module"
4754       fi
4755     done
4756     ;;
4757
4758   extract-tests-module )
4759     for module
4760     do
4761       func_verify_module
4762       if test -n "$module"; then
4763         func_get_tests_module "$module"
4764       fi
4765     done
4766     ;;
4767
4768   copy-file )
4769     # Verify the number of arguments.
4770     if test $# -lt 1 || test $# -gt 2; then
4771       func_fatal_error "invalid number of arguments for --$mode"
4772     fi
4773
4774     # The first argument is the file to be copied.
4775     f="$1"
4776     # Verify the file exists.
4777     func_lookup_file "$f"
4778
4779     # The second argument is the destination; either a directory ot a file.
4780     # It defaults to the current directory.
4781     dest="$2"
4782     test -n "$dest" || dest='.'
4783     test -n "$sourcebase" || sourcebase="lib"
4784     test -n "$m4base" || m4base="m4"
4785     test -n "$docbase" || docbase="doc"
4786     test -n "$testsbase" || testsbase="tests"
4787     test -n "$auxdir" || auxdir="build-aux"
4788     rewritten='%REWRITTEN%'
4789     sed_rewrite_files="\
4790       s,^build-aux/,$rewritten$auxdir/,
4791       s,^doc/,$rewritten$docbase/,
4792       s,^lib/,$rewritten$sourcebase/,
4793       s,^m4/,$rewritten$m4base/,
4794       s,^tests/,$rewritten$testsbase/,
4795       s,^top/,$rewritten,
4796       s,^$rewritten,,"
4797     if test -d "$dest"; then
4798       destdir="$dest"
4799       g=`echo "$f" | sed -e "$sed_rewrite_files"`
4800     else
4801       destdir=`dirname "$dest"`
4802       g=`basename "$dest"`
4803     fi
4804
4805     # Create the directory for destfile.
4806     d=`dirname "$destdir/$g"`
4807     if $doit; then
4808       if test -n "$d" && test ! -d "$d"; then
4809         mkdir -p "$d" || func_fatal_error "failed"
4810       fi
4811     fi
4812     # Copy the file.
4813     func_dest_tmpfilename "$g"
4814     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
4815     already_present=true
4816     if test -f "$destdir/$g"; then
4817       # The file already exists.
4818       func_update_file
4819     else
4820       # Install the file.
4821       # Don't protest if the file should be there but isn't: it happens
4822       # frequently that developers don't put autogenerated files into CVS.
4823       func_add_file
4824     fi
4825     rm -f "$tmpfile"
4826     ;;
4827
4828   * )
4829     func_fatal_error "unknown operation mode --$mode" ;;
4830 esac
4831
4832 rm -rf "$tmp"
4833 # Undo the effect of the previous 'trap' command. Some shellology:
4834 # We cannot use "trap - 0 1 2 3 13 15", because Solaris sh would attempt to
4835 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
4836 # exit); for the others we need to call 'exit' explicitly. The value of $? is
4837 # 128 + signal number and is set before the trap-registered command is run.
4838 trap '' 0
4839 trap 'func_exit $?' 1 2 3 13 15
4840
4841 exit 0
4842
4843 # Local Variables:
4844 # indent-tabs-mode: nil
4845 # whitespace-check-buffer-indent: nil
4846 # End: