New option --no-libtool.
[pspp] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2006 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 2, or (at your option)
8 # 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, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19
20 # This program is meant for authors or maintainers which want to import
21 # modules from gnulib into their packages.
22
23 progname=$0
24 package=gnulib
25 cvsdatestamp='$Date: 2006-07-31 11:35:20 $'
26 last_checkin_date=`echo "$cvsdatestamp" | sed -e 's,^\$[D]ate: ,,'`
27 version=`echo "$last_checkin_date" | sed -e 's/ .*$//' -e 's,/,-,g'`
28
29 # You can set AUTOCONFPATH to empty if autoconf 2.57 is already in your PATH.
30 AUTOCONFPATH=
31 #case $USER in
32 #  bruno )
33 #    AUTOCONFBINDIR=/packages/gnu-inst-autoconf/2.57/bin
34 #    AUTOCONFPATH="eval env PATH=${AUTOCONFBINDIR}:\$PATH "
35 #    ;;
36 #esac
37
38 # You can set AUTOMAKEPATH to empty if automake 1.9.x is already in your PATH.
39 AUTOMAKEPATH=
40
41 # If you didn't set AUTOCONFPATH and AUTOMAKEPATH, you can also set the
42 # variables AUTOCONF, ACLOCAL, AUTOMAKE, AUTORECONF individually.
43 if test -z "${AUTOCONF}" || test -n "${AUTOCONFPATH}"; then
44   AUTOCONF="${AUTOCONFPATH}autoconf"
45 fi
46 if test -z "${ACLOCAL}" || test -n "${AUTOMAKEPATH}"; then
47   ACLOCAL="${AUTOMAKEPATH}aclocal"
48 fi
49 if test -z "${AUTOMAKE}" || test -n "${AUTOMAKEPATH}"; then
50   AUTOMAKE="${AUTOMAKEPATH}automake"
51 fi
52 if test -z "${AUTORECONF}" || test -n "${AUTOCONFPATH}"; then
53   AUTORECONF="${AUTOCONFPATH}autoreconf"
54 fi
55
56 # GNU sort is needed. Set SORT to its location (not needed if it's called
57 # 'sort' and already in the PATH).
58 if test -z "$SORT"; then
59   SORT=sort
60 fi
61
62 # func_usage
63 # outputs to stdout the --help usage message.
64 func_usage ()
65 {
66   echo "\
67 Usage: gnulib-tool --list
68        gnulib-tool --import [module1 ... moduleN]
69        gnulib-tool --update
70        gnulib-tool --create-testdir --dir=directory module1 ... moduleN
71        gnulib-tool --create-megatestdir --dir=directory [module1 ... moduleN]
72        gnulib-tool --test --dir=directory module1 ... moduleN
73        gnulib-tool --megatest --dir=directory [module1 ... moduleN]
74        gnulib-tool --extract-description module
75        gnulib-tool --extract-filelist module
76        gnulib-tool --extract-dependencies module
77        gnulib-tool --extract-autoconf-snippet module
78        gnulib-tool --extract-automake-snippet module
79        gnulib-tool --extract-include-directive module
80        gnulib-tool --extract-license module
81        gnulib-tool --extract-maintainer module
82        gnulib-tool --extract-tests-module module
83
84 Operation modes:
85       --list                print the available module names
86       --import              import the given modules into the current package;
87                             if no modules are specified, update the current
88                             package from the current gnulib
89       --update              update the current package, restore files omitted
90                             from CVS
91       --create-testdir      create a scratch package with the given modules
92       --create-megatestdir  create a mega scratch package with the given modules
93                             one by one and all together
94       --test                test the combination of the given modules
95                             (recommended to use CC=\"gcc -Wall\" here)
96       --megatest            test the given modules one by one and all together
97                             (recommended to use CC=\"gcc -Wall\" here)
98       --extract-description        extract the description
99       --extract-filelist           extract the list of files
100       --extract-dependencies       extract the dependencies
101       --extract-autoconf-snippet   extract the snippet for configure.ac
102       --extract-automake-snippet   extract the snippet for lib/Makefile.am
103       --extract-include-directive  extract the #include directive
104       --extract-license            report the license terms of the source files
105                                    under lib/
106       --extract-maintainer         report the maintainer(s) inside gnulib
107       --extract-tests-module       report the unit test module, if it exists
108
109 General options:
110       --dir=DIRECTORY       Specify the target directory.
111                             For --import, this specifies where your
112                             configure.ac can be found.  Defaults to current
113                             directory.
114       --local-dir=DIRECTORY  Specify a local override directory where to look
115                             up files before looking in gnulib's directory.
116
117 Options for --import:
118       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
119       --source-base=DIRECTORY
120                             Directory relative to --dir where source code is
121                             placed (default \"lib\").
122       --m4-base=DIRECTORY   Directory relative to --dir where *.m4 macros are
123                             placed (default \"m4\").
124       --doc-base=DIRECTORY  Directory relative to --dir where doc files are
125                             placed (default \"doc\").
126       --tests-base=DIRECTORY
127                             Directory relative to --dir where unit tests are
128                             placed (default \"tests\").
129       --aux-dir=DIRECTORY   Directory relative to --dir where auxiliary build
130                             tools are placed (default \"build-aux\").
131       --with-tests          Include unit tests for the included modules.
132       --avoid=MODULE        Avoid including the given MODULE. Useful if you
133                             have code that provides equivalent functionality.
134                             This option can be repeated.
135       --lgpl                Abort if modules aren't available under the LGPL.
136                             Also modify license template from GPL to LGPL.
137       --libtool             Use libtool rules.
138       --no-libtool          Don't use libtool rules.
139       --macro-prefix=PREFIX  Specify the prefix of the macros 'gl_EARLY' and
140                             'gl_INIT'. Default is 'gl'.
141       --no-changelog        don't update or create ChangeLog files
142
143 Options for --import and --update:
144       --dry-run             For --import, only print what would have been done.
145   -s, --symbolic, --symlink Make symbolic links instead of copying files.
146
147 Report bugs to <bug-gnulib@gnu.org>."
148 }
149
150 # func_version
151 # outputs to stdout the --version message.
152 func_version ()
153 {
154   year=`echo "$last_checkin_date" | sed -e 's,/.*$,,'`
155   echo "$progname (GNU $package) $version"
156   echo "Copyright (C) $year Free Software Foundation, Inc.
157 This is free software; see the source for copying conditions.  There is NO
158 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
159   echo "Written by" "Bruno Haible" "and" "Simon Josefsson"
160 }
161
162 # func_emit_copyright_notice
163 # outputs to stdout a header for a generated file.
164 func_emit_copyright_notice ()
165 {
166   echo "# Copyright (C) 2004-2006 Free Software Foundation, Inc."
167   echo "#"
168   echo "# This file is free software, distributed under the terms of the GNU"
169   echo "# General Public License.  As a special exception to the GNU General"
170   echo "# Public License, this file may be distributed as part of a program"
171   echo "# that contains a configuration script generated by Autoconf, under"
172   echo "# the same distribution terms as the rest of that program."
173   echo "#"
174   echo "# Generated by gnulib-tool."
175 }
176
177 # func_tmpdir
178 # creates a temporary directory.
179 # Sets variable
180 # - tmp             pathname of freshly created temporary directory
181 func_tmpdir ()
182 {
183   # Use the environment variable TMPDIR, falling back to /tmp. This allows
184   # users to specify a different temporary directory, for example, if their
185   # /tmp is filled up or too small.
186   : ${TMPDIR=/tmp}
187   {
188     # Use the mktemp program if available. If not available, hide the error
189     # message.
190     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
191     test -n "$tmp" && test -d "$tmp"
192   } ||
193   {
194     # Use a simple mkdir command. It is guaranteed to fail if the directory
195     # already exists.  $RANDOM is bash specific and expands to empty in shells
196     # other than bash, ksh and zsh.  Its use does not increase security;
197     # rather, it minimizes the probability of failure in a very cluttered /tmp
198     # directory.
199     tmp=$TMPDIR/gl$$-$RANDOM
200     (umask 077 && mkdir "$tmp")
201   } ||
202   {
203     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
204     { (exit 1); exit 1; }
205   }
206 }
207
208 # func_fatal_error message
209 # outputs to stderr a fatal error message, and terminates the program.
210 func_fatal_error ()
211 {
212   echo "gnulib-tool: *** $1" 1>&2
213   echo "gnulib-tool: *** Stop." 1>&2
214   exit 1
215 }
216
217 # func_readlink SYMLINK
218 # outputs the target of the given symlink.
219 if (type -p readlink) > /dev/null 2>&1; then
220   func_readlink ()
221   {
222     # Use the readlink program from GNU coreutils.
223     readlink "$1"
224   }
225 else
226   func_readlink ()
227   {
228     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
229     # would do the wrong thing if the link target contains " -> ".
230     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
231   }
232 fi
233
234 # func_ln_if_changed SRC DEST
235 # Like ln -s, but avoids munging timestamps if the link is correct.
236 func_ln_if_changed ()
237 {
238   if test $# -ne 2; then
239     echo "usage: func_ln_if_changed SRC DEST" >&2
240   fi
241   if test -L "$2" && test "$1" = "`func_readlink "$2"`"; then
242     :
243   else
244     rm -f "$2"
245     ln -s "$1" "$2"
246   fi
247 }
248
249 # Command-line option processing.
250 # Removes the OPTIONS from the arguments. Sets the variables:
251 # - mode            list or import or create-testdir or create-megatestdir
252 # - destdir         from --dir
253 # - libname, supplied_libname  from --lib
254 # - sourcebase      from --source-base
255 # - m4base          from --m4-base
256 # - docbase         from --doc-base
257 # - testsbase       from --tests-base
258 # - auxdir          from --aux-dir
259 # - inctests        true if --with-tests was given, blank otherwise
260 # - avoidlist       list of modules to avoid, from --avoid
261 # - lgpl            true if --lgpl was given, blank otherwise
262 # - libtool         true if --libtool was given, false if --no-libtool was
263 #                   given, blank otherwise
264 # - macro_prefix    from --macro-prefix
265 # - autoconf_minversion  minimum supported autoconf version
266 # - do_changelog    false if --no-changelog was given, : otherwise
267 # - doit            : if actions shall be executed, false if only to be printed
268 # - local_gnulib_dir  from --local-dir
269 # - symbolic        true if --symbolic was given, blank otherwise
270 {
271   mode=
272   destdir=
273   libname=libgnu
274   supplied_libname=
275   sourcebase=
276   m4base=
277   docbase=
278   testsbase=
279   auxdir=
280   inctests=
281   avoidlist=
282   lgpl=
283   libtool=
284   macro_prefix=
285   autoconf_minversion=
286   do_changelog=:
287   doit=:
288   local_gnulib_dir=
289   symbolic=
290
291   supplied_opts="$@"
292
293   while test $# -gt 0; do
294     case "$1" in
295       --list | --lis )
296         mode=list
297         shift ;;
298       --import | --impor | --impo | --imp | --im | --i )
299         mode=import
300         shift ;;
301       --update | --updat | --upda | --upd | --up | --u )
302         mode=update
303         shift ;;
304       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
305         mode=create-testdir
306         shift ;;
307       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
308         mode=create-megatestdir
309         shift ;;
310       --test | --tes | --te | --t )
311         mode=test
312         shift ;;
313       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
314         mode=megatest
315         shift ;;
316       --extract-* )
317         mode=`echo "X$1" | sed -e 's/^X--//'`
318         shift ;;
319       --dir )
320         shift
321         if test $# = 0; then
322           func_fatal_error "missing argument for --dir"
323         fi
324         destdir=$1
325         shift ;;
326       --dir=* )
327         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
328         shift ;;
329       --lib )
330         shift
331         if test $# = 0; then
332           func_fatal_error "missing argument for --lib"
333         fi
334         libname=$1
335         supplied_libname=true
336         shift ;;
337       --lib=* )
338         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
339         supplied_libname=true
340         shift ;;
341       --source-base )
342         shift
343         if test $# = 0; then
344           func_fatal_error "missing argument for --source-base"
345         fi
346         sourcebase=$1
347         shift ;;
348       --source-base=* )
349         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
350         shift ;;
351       --m4-base )
352         shift
353         if test $# = 0; then
354           func_fatal_error "missing argument for --m4-base"
355         fi
356         m4base=$1
357         shift ;;
358       --m4-base=* )
359         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
360         shift ;;
361       --doc-base )
362         shift
363         if test $# = 0; then
364           func_fatal_error "missing argument for --doc-base"
365         fi
366         docbase=$1
367         shift ;;
368       --doc-base=* )
369         docbase=`echo "X$1" | sed -e 's/^X--doc-base=//'`
370         shift ;;
371       --tests-base )
372         shift
373         if test $# = 0; then
374           func_fatal_error "missing argument for --tests-base"
375         fi
376         testsbase=$1
377         shift ;;
378       --tests-base=* )
379         testsbase=`echo "X$1" | sed -e 's/^X--tests-base=//'`
380         shift ;;
381       --aux-dir )
382         shift
383         if test $# = 0; then
384           func_fatal_error "missing argument for --aux-dir"
385         fi
386         auxdir=$1
387         shift ;;
388       --aux-dir=* )
389         auxdir=`echo "X$1" | sed -e 's/^X--aux-dir=//'`
390         shift ;;
391       --with-tests )
392         inctests=true
393         shift ;;
394       --avoid )
395         shift
396         if test $# = 0; then
397           func_fatal_error "missing argument for --avoid"
398         fi
399         avoidlist="$avoidlist $1"
400         shift ;;
401       --avoid=* )
402         avoidlist="$avoidlist "`echo "X$1" | sed -e 's/^X--avoid=//'`
403         shift ;;
404       --lgpl )
405         lgpl=true
406         shift ;;
407       --libtool )
408         libtool=true
409         shift ;;
410       --no-libtool )
411         libtool=false
412         shift ;;
413       --macro-prefix )
414         shift
415         if test $# = 0; then
416           func_fatal_error "missing argument for --macro-prefix"
417         fi
418         macro_prefix="$1"
419         shift ;;
420       --macro-prefix=* )
421         macro_prefix=`echo "X$1" | sed -e 's/^X--macro-prefix=//'`
422         shift ;;
423       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
424         do_changelog=false
425         shift ;;
426       --dry-run )
427         doit=false
428         shift ;;
429       --local-dir )
430         shift
431         if test $# = 0; then
432           func_fatal_error "missing argument for --local-dir"
433         fi
434         local_gnulib_dir=$1
435         shift ;;
436       --local-dir=* )
437         local_gnulib_dir=`echo "X$1" | sed -e 's/^X--local-dir=//'`
438         shift ;;
439       -s | --symbolic | --symboli | --symbol | --symbo | --symb | --symlink | --symlin | --symli | --syml | --sym | --sy )
440         symbolic=true
441         shift ;;
442       --help | --hel | --he | --h )
443         func_usage
444         exit 0 ;;
445       --version | --versio | --versi | --vers | --ver | --ve | --v )
446         func_version
447         exit 0 ;;
448       -- )
449         # Stop option processing
450         shift
451         break ;;
452       -* )
453         echo "gnulib-tool: unknown option $1" 1>&2
454         echo "Try 'gnulib-tool --help' for more information." 1>&2
455         exit 1 ;;
456       * )
457         break ;;
458     esac
459   done
460
461   if test "$mode" = update; then
462     if test $# != 0; then
463       echo "gnulib-tool: too many arguments in 'update' mode" 1>&2
464       echo "Try 'gnulib-tool --help' for more information." 1>&2
465       echo "If you really want to modify the gnulib configuration of your project," 1>&2
466       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
467       exit 1
468     fi
469     if test -n "$supplied_libname" || test -n "$sourcebase" || test -n "$m4base" \
470        || test -n "$docbase" || test -n "$testsbase" || test -n "$auxdir" \
471        || test -n "$inctests" || test -n "$avoidlist" || test -n "$lgpl" \
472        || test -n "$macro_prefix"; then
473       echo "gnulib-tool: invalid options for 'update' mode" 1>&2
474       echo "Try 'gnulib-tool --help' for more information." 1>&2
475       echo "If you really want to modify the gnulib configuration of your project," 1>&2
476       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
477       exit 1
478     fi
479     do_changelog=false
480   fi
481
482   # Determine the minimum supported autoconf version from the project's
483   # configure.ac.
484   DEFAULT_AUTOCONF_MINVERSION="2.59"
485   if test -f configure.ac; then
486     autoconf_minversion=`$AUTOCONF --trace=AC_PREREQ:'$1' | $SORT -g | uniq | tail -1`
487   fi
488   if test -z "$autoconf_minversion"; then
489     autoconf_minversion=$DEFAULT_AUTOCONF_MINVERSION
490   fi
491   case "$autoconf_minversion" in
492     1.* | 2.[0-4]* | 2.5[0-8]*)
493       func_fatal_error "minimum supported autoconf version is 2.59. Try adding AC_PREREQ([$DEFAULT_AUTOCONF_MINVERSION]) to your configure.ac." ;;
494   esac
495
496   # Remove trailing slashes from the directory names. This is necessary for
497   # m4base (to avoid an error in func_import) and optional for the others.
498   sed_trimtrailingslashes='s,\([^/]\)//*$,\1,'
499   case "$sourcebase" in
500     */ ) sourcebase=`echo "$sourcebase" | sed -e "$sed_trimtrailingslashes"` ;;
501   esac
502   case "$m4base" in
503     */ ) m4base=`echo "$m4base" | sed -e "$sed_trimtrailingslashes"` ;;
504   esac
505   case "$docbase" in
506     */ ) docbase=`echo "$docbase" | sed -e "$sed_trimtrailingslashes"` ;;
507   esac
508   case "$testsbase" in
509     */ ) testsbase=`echo "$testsbase" | sed -e "$sed_trimtrailingslashes"` ;;
510   esac
511   case "$auxdir" in
512     */ ) auxdir=`echo "$auxdir" | sed -e "$sed_trimtrailingslashes"` ;;
513   esac
514 }
515
516 case "$0" in
517   /*) self_abspathname="$0" ;;
518   */*) self_abspathname=`pwd`/"$0" ;;
519   *) for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`; do
520        if test -x "$d/$0" && test ! -d "$d/$0"; then
521          self_abspathname="$d/$0"
522          break
523        fi
524      done
525      if test -z "$self_abspathname"; then
526        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
527      fi
528      ;;
529 esac
530 while test -h "$self_abspathname"; do
531   # Resolve symbolic link.
532   linkval=`func_readlink "$self_abspathname"`
533   test -n "$linkval" || break
534   case "$linkval" in
535     /* ) self_abspathname="$linkval" ;;
536     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
537   esac
538 done
539 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
540
541 func_tmpdir
542 trap 'rm -rf "$tmp"' 0 1 2 3 15
543
544 # func_lookup_file file
545 # looks up a file in $local_gnulib_dir or $gnulib_dir, or combines it through
546 # 'patch'.
547 # Output:
548 # - lookedup_file   name of the merged (combined) file
549 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
550 func_lookup_file ()
551 {
552   lkfile="$1"
553   if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile"; then
554     lookedup_file="$local_gnulib_dir/$lkfile"
555     lookedup_tmp=
556   else
557     if test -f "$gnulib_dir/$lkfile"; then
558       if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile.diff"; then
559         lkbase=`echo "$lkfile" | sed -e 's,^.*/,,'`
560         rm -f "$tmp/$lkbase"
561         cp "$gnulib_dir/$lkfile" "$tmp/$lkbase"
562         patch -s "$tmp/$lkbase" < "$local_gnulib_dir/$lkfile.diff" \
563           || func_fatal_error "patch file $local_gnulib_dir/$lkfile.diff didn't apply cleanly"
564         lookedup_file="$tmp/$lkbase"
565         lookedup_tmp=true
566       else
567         lookedup_file="$gnulib_dir/$lkfile"
568         lookedup_tmp=
569       fi
570     else
571       func_fatal_error "file $gnulib_dir/$lkfile not found"
572     fi
573   fi
574 }
575
576 # func_all_modules
577 func_all_modules ()
578 {
579   # Filter out metainformation files like README, which are not modules.
580   # Filter out unit test modules; they can be retrieved through
581   # --extract-tests-module if desired.
582   {
583     (cd "$gnulib_dir/modules" && ls -1)
584     if test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules"; then
585       (cd "$local_gnulib_dir/modules" && ls -1 | sed -e 's,\.diff$,,')
586     fi
587   } \
588       | sed -e '/^CVS$/d' -e '/^ChangeLog$/d' -e '/^README$/d' -e '/^TEMPLATE$/d' -e '/^TEMPLATE-TESTS$/d' -e '/~$/d' \
589       | sed -e '/-tests$/d' \
590       | LC_ALL=C sort \
591       | LC_ALL=C uniq
592 }
593
594 # func_verify_module
595 # verifies a module name
596 func_verify_module ()
597 {
598   if ! { test -f "$gnulib_dir/modules/$module" \
599          || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
600               && test -f "$local_gnulib_dir/modules/$module"; }; } \
601      || test "CVS" = "$module" \
602      || test "ChangeLog" = "$module" \
603      || test "README" = "$module" \
604      || test "TEMPLATE" = "$module" \
605      || test "TEMPLATE-TESTS" = "$module"; then
606     echo "gnulib-tool: module $module doesn't exist" 1>&2
607     module=
608   fi
609 }
610
611 # func_verify_nontests_module
612 # verifies a module name, excluding tests modules
613 func_verify_nontests_module ()
614 {
615   case "$module" in
616     *-tests ) module= ;;
617     * ) func_verify_module ;;
618   esac
619 }
620
621 # func_verify_tests_module
622 # verifies a module name, considering only tests modules
623 func_verify_tests_module ()
624 {
625   case "$module" in
626     *-tests ) func_verify_module ;;
627     * ) module= ;;
628   esac
629 }
630
631 sed_extract_prog=':[     ]*$/ {
632   :a
633     n
634     s/^Description:[     ]*$//
635     s/^Files:[   ]*$//
636     s/^Depends-on:[      ]*$//
637     s/^configure\.ac:[   ]*$//
638     s/^Makefile\.am:[    ]*$//
639     s/^Include:[         ]*$//
640     s/^License:[         ]*$//
641     s/^Maintainer:[      ]*$//
642     tb
643     p
644     ba
645   :b
646 }'
647
648 # func_get_description module
649 func_get_description ()
650 {
651   func_lookup_file "modules/$1"
652   sed -n -e "/^Description$sed_extract_prog" < "$lookedup_file"
653 }
654
655 # func_get_filelist module
656 func_get_filelist ()
657 {
658   func_lookup_file "modules/$1"
659   sed -n -e "/^Files$sed_extract_prog" < "$lookedup_file"
660   case "$autoconf_minversion" in
661     2.59)
662       #echo m4/onceonly.m4
663       echo m4/onceonly_2_57.m4
664       ;;
665   esac
666 }
667
668 # func_get_dependencies module
669 func_get_dependencies ()
670 {
671   # ${module}-tests always implicitly depends on ${module}.
672   echo "$1" | sed -n -e 's/-tests//p'
673   # Then the explicit dependencies listed in the module description.
674   func_lookup_file "modules/$1"
675   sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
676 }
677
678 # func_get_autoconf_snippet module
679 func_get_autoconf_snippet ()
680 {
681   func_lookup_file "modules/$1"
682   sed -n -e "/^configure\.ac$sed_extract_prog" < "$lookedup_file"
683 }
684
685 # func_get_automake_snippet module
686 func_get_automake_snippet ()
687 {
688   func_lookup_file "modules/$1"
689   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file"
690 }
691
692 # func_get_include_directive module
693 func_get_include_directive ()
694 {
695   func_lookup_file "modules/$1"
696   sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
697   sed -e 's/^\(["<]\)/#include \1/'
698 }
699
700 # func_get_license module
701 func_get_license ()
702 {
703   func_lookup_file "modules/$1"
704   sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
705 }
706
707 # func_get_maintainer module
708 func_get_maintainer ()
709 {
710   func_lookup_file "modules/$1"
711   sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
712 }
713
714 # func_get_tests_module module
715 func_get_tests_module ()
716 {
717   # The naming convention for tests modules is hardwired: ${module}-tests.
718   if test -f modules/"$1"-tests; then
719     echo "$1"-tests
720   fi
721 }
722
723 # func_acceptable module
724 # tests whether a module is acceptable.
725 # Input:
726 # - avoidlist       list of modules to avoid
727 func_acceptable ()
728 {
729   for avoid in $avoidlist; do
730     if test "$avoid" = "$1"; then
731       return 1
732     fi
733   done
734   return 0
735 }
736
737 # func_modules_transitive_closure
738 # Input:
739 # - modules         list of specified modules
740 # - inctests        true if tests should be included, blank otherwise
741 # - avoidlist       list of modules to avoid
742 # Output:
743 # - modules         list of modules, including dependencies
744 func_modules_transitive_closure ()
745 {
746   while true; do
747     xmodules=
748     for module in $modules; do
749       func_verify_module
750       if test -n "$module"; then
751         # Duplicate dependencies are harmless, but Jim wants a warning.
752         duplicated_deps=`func_get_dependencies $module | LC_ALL=C sort | LC_ALL=C uniq -d`
753         if test -n "$duplicated_deps"; then
754           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
755         fi
756         if func_acceptable $module; then
757           xmodules="$xmodules $module"
758           for depmodule in `func_get_dependencies $module`; do
759             if func_acceptable $depmodule; then
760               xmodules="$xmodules $depmodule"
761             fi
762           done
763           if test -n "$inctests"; then
764             testsmodule=`func_get_tests_module $module`
765             if test -n "$testsmodule"; then
766               if func_acceptable $testsmodule; then
767                 xmodules="$xmodules $testsmodule"
768                 for depmodule in `func_get_dependencies $testsmodule`; do
769                   if func_acceptable $depmodule; then
770                     xmodules="$xmodules $depmodule"
771                   fi
772                 done
773               fi
774             fi
775           fi
776         fi
777       fi
778     done
779     xmodules=`for m in $xmodules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
780     if test "$xmodules" = "$modules"; then
781       break
782     fi
783     modules="$xmodules"
784   done
785 }
786
787 # func_modules_add_dummy
788 # Input:
789 # - modules         list of modules, including dependencies
790 # Output:
791 # - modules         list of modules, including 'dummy' if needed
792 func_modules_add_dummy ()
793 {
794   have_lib_SOURCES=
795   sed_remove_backslash_newline=':a
796 /\\$/{
797 s/\\$//
798 N
799 s/\n//
800 ba
801 }'
802   for module in $modules; do
803     func_verify_nontests_module
804     if test -n "$module"; then
805       # Extract the value of "lib_SOURCES += ...".
806       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
807         # Ignore .h files since they are not compiled.
808         case "$file" in
809           *.h) ;;
810           *) have_lib_SOURCES=yes ;;
811         esac
812       done
813     fi
814   done
815   # Add the dummy module, to make sure the library will be non-empty.
816   if test -z "$have_lib_SOURCES"; then
817     modules="$modules dummy"
818   fi
819 }
820
821 # func_modules_to_filelist
822 # Input:
823 # - modules         list of modules, including dependencies
824 # Output:
825 # - files           list of files
826 func_modules_to_filelist ()
827 {
828   files=
829   for module in $modules; do
830     func_verify_module
831     if test -n "$module"; then
832       files="$files "`func_get_filelist $module`
833     fi
834   done
835   files=`for f in $files; do echo $f; done | LC_ALL=C sort | LC_ALL=C uniq`
836 }
837
838 # func_emit_lib_Makefile_am
839 # emits the contents of lib/Makefile.am to standard output.
840 # Input:
841 # - modules         list of modules, including dependencies
842 # - libname         library name
843 # - libtool         true if libtool will be used, false or blank otherwise
844 # - actioncmd       (optional) command that will reproduce this invocation
845 func_emit_lib_Makefile_am ()
846 {
847   if test "$libtool" = true; then
848     libext=la
849     perhapsLT=LT
850   else
851     libext=a
852     perhapsLT=
853   fi
854   echo "## Process this file with automake to produce Makefile.in."
855   func_emit_copyright_notice
856   if test -n "$actioncmd"; then
857     echo "# Reproduce by: $actioncmd"
858   fi
859   echo
860   # No need to generate dependencies since the sources are in gnulib, not here.
861   echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
862   echo
863   echo "noinst_${perhapsLT}LIBRARIES = $libname.$libext"
864   echo
865   echo "${libname}_${libext}_SOURCES ="
866   echo "${libname}_${libext}_LIBADD = @${perhapsLT}LIBOBJS@"
867   echo "noinst_HEADERS ="
868   echo "EXTRA_DIST ="
869   echo "BUILT_SOURCES ="
870   echo "SUFFIXES ="
871   echo "MOSTLYCLEANFILES ="
872   echo "MOSTLYCLEANDIRS ="
873   echo "CLEANFILES ="
874   echo "DISTCLEANFILES ="
875   echo "MAINTAINERCLEANFILES ="
876   echo
877   echo "AM_CPPFLAGS ="
878   echo
879   for module in $modules; do
880     func_verify_nontests_module
881     if test -n "$module"; then
882       {
883         func_get_automake_snippet "$module" |
884           sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g'
885         if test "$module" = 'alloca'; then
886           echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
887         fi
888       } > amsnippet.tmp
889       # Skip the contents if its entirely empty.
890       if grep '[^        ]' amsnippet.tmp > /dev/null ; then
891         echo "## begin gnulib module $module"
892         echo
893         cat amsnippet.tmp
894         echo "## end   gnulib module $module"
895         echo
896       fi
897       rm -f amsnippet.tmp
898     fi
899   done
900   echo
901   echo "mostlyclean-local:"
902   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
903   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
904   echo "            if test -d \$\$dir; then \\"
905   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
906   echo "            fi; \\"
907   echo "          done"
908   echo
909   echo "# Makefile.am ends here"
910 }
911
912 # func_emit_tests_Makefile_am
913 # emits the contents of tests/Makefile.am to standard output.
914 # Input:
915 # - modules         list of modules, including dependencies
916 # - libname         library name
917 # - libtool         true if libtool will be used, false or blank otherwise
918 # - sourcebase      relative directory containing lib source code
919 # - m4base          relative directory containing autoconf macros
920 # - testsbase       relative directory containing unit test code
921 func_emit_tests_Makefile_am ()
922 {
923   if test "$libtool" = true; then
924     libext=la
925   else
926     libext=a
927   fi
928   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
929   echo "## Process this file with automake to produce Makefile.in."
930   func_emit_copyright_notice
931   echo
932   # Generate dependencies here, since it eases the debugging of test failures.
933   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
934   echo
935   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
936   echo
937   # Nothing is being added to SUBDIRS; nevertheless the existence of this
938   # variable is needed to avoid an error from automake:
939   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
940   echo "SUBDIRS ="
941   echo "TESTS ="
942   echo "TESTS_ENVIRONMENT ="
943   echo "noinst_PROGRAMS ="
944   echo "check_PROGRAMS ="
945   echo "noinst_HEADERS ="
946   echo "EXTRA_DIST ="
947   echo "BUILT_SOURCES ="
948   echo "SUFFIXES ="
949   echo "MOSTLYCLEANFILES ="
950   echo "MOSTLYCLEANDIRS ="
951   echo "CLEANFILES ="
952   echo "DISTCLEANFILES ="
953   echo "MAINTAINERCLEANFILES ="
954   echo
955   echo "AM_CPPFLAGS = \\"
956   echo "  -I. -I\$(srcdir) \\"
957   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
958   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
959   echo
960   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
961   echo
962   for module in $modules; do
963     func_verify_tests_module
964     if test -n "$module"; then
965       func_get_automake_snippet "$module" > amsnippet.tmp
966       # Skip the contents if its entirely empty.
967       if grep '[^        ]' amsnippet.tmp > /dev/null ; then
968         echo "## begin gnulib module $module"
969         echo
970         cat amsnippet.tmp
971         echo "## end   gnulib module $module"
972         echo
973       fi
974       rm -f amsnippet.tmp
975     fi
976   done
977   echo "# Clean up after Solaris cc."
978   echo "clean-local:"
979   echo "        rm -rf SunWS_cache"
980   echo
981   echo "mostlyclean-local:"
982   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
983   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
984   echo "            if test -d \$\$dir; then \\"
985   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
986   echo "            fi; \\"
987   echo "          done"
988   echo
989   echo "# Makefile.am ends here"
990 }
991
992 # func_import modules
993 # Uses also the variables
994 # - destdir         target directory
995 # - libname         library name
996 # - sourcebase      directory relative to destdir where to place source code
997 # - m4base          directory relative to destdir where to place *.m4 macros
998 # - docbase         directory relative to destdir where to place doc files
999 # - testsbase       directory relative to destdir where to place unit test code
1000 # - auxdir          directory relative to destdir where to place build aux files
1001 # - inctests        true if --with-tests was given, blank otherwise
1002 # - avoidlist       list of modules to avoid, from --avoid
1003 # - lgpl            true if library's license shall be LGPL, blank otherwise
1004 # - libtool         true if --libtool was given, false if --no-libtool was
1005 #                   given, blank otherwise
1006 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
1007 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1008 # - autoconf_minversion  minimum supported autoconf version
1009 # - doit            : if actions shall be executed, false if only to be printed
1010 # - symbolic        true if files should be symlinked, copied otherwise
1011 func_import ()
1012 {
1013   # Get the cached settings.
1014   cached_specified_modules=
1015   cached_avoidlist=
1016   cached_sourcebase=
1017   cached_m4base=
1018   cached_docbase=
1019   cached_testsbase=
1020   cached_libname=
1021   cached_lgpl=
1022   cached_libtool=
1023   cached_macro_prefix=
1024   cached_files=
1025   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1026     cached_libtool=false
1027     my_sed_traces='
1028       s,#.*$,,
1029       s,^dnl .*$,,
1030       s, dnl .*$,,
1031       /gl_MODULES(/ {
1032         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
1033       }
1034       /gl_AVOID(/ {
1035         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
1036       }
1037       /gl_SOURCE_BASE(/ {
1038         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
1039       }
1040       /gl_M4_BASE(/ {
1041         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
1042       }
1043       /gl_DOC_BASE(/ {
1044         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
1045       }
1046       /gl_TESTS_BASE(/ {
1047         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
1048       }
1049       /gl_LIB(/ {
1050         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
1051       }
1052       /gl_LGPL/ {
1053         s,^.*$,cached_lgpl=true,p
1054       }
1055       /gl_LIBTOOL/ {
1056         s,^.*$,cached_libtool=true,p
1057       }
1058       /gl_MACRO_PREFIX(/ {
1059         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
1060       }'
1061     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
1062     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1063       my_sed_traces='
1064         s,#.*$,,
1065         s,^dnl .*$,,
1066         s, dnl .*$,,
1067         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
1068           s,^.*$,cached_files=",p
1069           n
1070           ta
1071           :a
1072           s,^\]).*$,",
1073           tb
1074           p
1075           n
1076           ba
1077           :b
1078           p
1079         }'
1080       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1081     fi
1082   fi
1083
1084   # Merge the cached settings with the specified ones.
1085   # The m4base must be the same as expected from the pathname.
1086   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1087     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1088   fi
1089   # Append the cached and the specified module names. So that
1090   # "gnulib-tool --import foo" means to add the module foo.
1091   specified_modules="$cached_specified_modules $1"
1092   # Append the cached and the specified avoidlist. This is probably better
1093   # than dropping the cached one when --avoid is specified at least once.
1094   avoidlist=`echo $cached_avoidlist $avoidlist`
1095   # The sourcebase defaults to the cached one.
1096   if test -z "$sourcebase"; then
1097     sourcebase="$cached_sourcebase"
1098     if test -z "$sourcebase"; then
1099       func_fatal_error "missing --source-base option"
1100     fi
1101   fi
1102   # The docbase defaults to the cached one.
1103   if test -z "$docbase"; then
1104     docbase="$cached_docbase"
1105     if test -z "$docbase"; then
1106       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."
1107     fi
1108   fi
1109   # The testsbase defaults to the cached one.
1110   if test -z "$testsbase"; then
1111     testsbase="$cached_testsbase"
1112     if test -z "$testsbase"; then
1113       func_fatal_error "missing --tests-base option"
1114     fi
1115   fi
1116   # The libname defaults to the cached one.
1117   if test -z "$supplied_libname"; then
1118     libname="$cached_libname"
1119     if test -z "$libname"; then
1120       func_fatal_error "missing --lib option"
1121     fi
1122   fi
1123   # Require LGPL if specified either way.
1124   if test -z "$lgpl"; then
1125     lgpl="$cached_lgpl"
1126   fi
1127   # Use libtool if specified either way, or if guessed.
1128   if test -z "$libtool"; then
1129     if test -n "$cached_m4base"; then
1130       libtool="$cached_libtool"
1131     else
1132       libtool="$guessed_libtool"
1133     fi
1134   fi
1135   # The macro_prefix defaults to the cached one.
1136   if test -z "$macro_prefix"; then
1137     macro_prefix="$cached_macro_prefix"
1138     if test -z "$macro_prefix"; then
1139       func_fatal_error "missing --macro-prefix option"
1140     fi
1141   fi
1142
1143   # Canonicalize the list of specified modules.
1144   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
1145
1146   # Determine final module list.
1147   modules="$specified_modules"
1148   func_modules_transitive_closure
1149   echo "Module list with included dependencies:"
1150   echo "$modules" | sed -e 's/^/  /'
1151
1152   # Add the dummy module if needed.
1153   func_modules_add_dummy
1154
1155   # If --lgpl, check the license of modules are compatible.
1156   if test -n "$lgpl"; then
1157     for module in $modules; do
1158       license=`func_get_license $module`
1159       case $license in
1160         LGPL | 'GPLed build tool' | 'public domain' | 'unlimited') ;;
1161         *) func_fatal_error "incompatible license on module $module: $license" ;;
1162       esac
1163     done
1164   fi
1165
1166   # Determine final file list.
1167   func_modules_to_filelist
1168   echo "File list:"
1169   echo "$files" | sed -e 's/^/  /'
1170
1171   test -n "$files" \
1172     || func_fatal_error "refusing to do nothing"
1173
1174   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1175   new_files="$files m4/gnulib-tool.m4"
1176   old_files="$cached_files"
1177   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1178     old_files="$old_files m4/gnulib-tool.m4"
1179   fi
1180
1181   # Create directories.
1182   if test ! -d "$destdir/$sourcebase"; then
1183     if $doit; then
1184       echo "Creating directory $destdir/$sourcebase"
1185       mkdir "$destdir/$sourcebase" || func_fatal_error "failed"
1186     else
1187       echo "Create directory $destdir/$sourcebase"
1188     fi
1189   fi
1190   if test ! -d "$destdir/$m4base"; then
1191     if $doit; then
1192       echo "Creating directory $destdir/$m4base"
1193       mkdir "$destdir/$m4base" || func_fatal_error "failed"
1194     else
1195       echo "Create directory $destdir/$m4base"
1196     fi
1197   fi
1198   docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1199   if test -n "$docfiles"; then
1200     if test ! -d "$destdir/$docbase"; then
1201       if $doit; then
1202         echo "Creating directory $destdir/$docbase"
1203         mkdir "$destdir/$docbase" || func_fatal_error "failed"
1204       else
1205         echo "Create directory $destdir/$docbase"
1206       fi
1207     fi
1208   fi
1209   if test -n "$inctests"; then
1210     if test ! -d "$destdir/$testsbase"; then
1211       if $doit; then
1212         echo "Creating directory $destdir/$testsbase"
1213         mkdir "$destdir/$testsbase" || func_fatal_error "failed"
1214       else
1215         echo "Create directory $destdir/$testsbase"
1216       fi
1217     fi
1218   fi
1219   if test ! -d "$destdir/$auxdir"; then
1220     if $doit; then
1221       echo "Creating directory $destdir/$auxdir"
1222       mkdir "$destdir/$auxdir" || func_fatal_error "failed"
1223     else
1224       echo "Create directory $destdir/$auxdir"
1225     fi
1226   fi
1227
1228   # func_dest_tmpfilename file
1229   # determines the name of a temporary file (file is relative to destdir).
1230   # Sets variable:
1231   #   - tmpfile       absolute filename of the temporary file
1232   func_dest_tmpfilename ()
1233   {
1234     if $doit; then
1235       # Put the new contents of $file in a file in the same directory (needed
1236       # to guarantee that an 'mv' to "$destdir/$file" works).
1237       tmpfile="$destdir/$1.tmp"
1238     else
1239       # Put the new contents of $file in a file in a temporary directory
1240       # (because the directory of "$file" might not exist).
1241       tmpfile="$tmp"/`basename "$1"`.tmp
1242     fi
1243   }
1244
1245   # Copy files or make symbolic links. Remove obsolete files.
1246   delimiter='   '
1247   for f in $old_files; do
1248     case "$f" in
1249       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1250       doc/*) g=`echo "$f" | sed -e "s,^doc/,$cached_docbase/,"` ;;
1251       lib/*) g=`echo "$f" | sed -e "s,^lib/,$cached_sourcebase/,"` ;;
1252       m4/*) g=`echo "$f" | sed -e "s,^m4/,$cached_m4base/,"` ;;
1253       tests/*) g=`echo "$f" | sed -e "s,^tests/,$cached_testsbase/,"` ;;
1254       *) g="$f" ;;
1255     esac
1256     echo "$g""$delimiter""$f"
1257   done | LC_ALL=C sort > "$tmp"/old-files
1258   for f in $new_files; do
1259     case "$f" in
1260       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1261       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1262       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1263       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1264       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1265       *) g="$f" ;;
1266     esac
1267     echo "$g""$delimiter""$f"
1268   done | LC_ALL=C sort > "$tmp"/new-files
1269   # First the files that are in old-files, but not in new-files:
1270   sed_take_first_column='s,'"$delimiter"'.*,,'
1271   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1272     # Remove the file. Do nothing if the user already removed it.
1273     if test -f "$destdir/$g"; then
1274       if $doit; then
1275         echo "Removing file $g (backup in ${g}~)"
1276         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1277       else
1278         echo "Remove file $g (backup in ${g}~)"
1279       fi
1280     fi
1281   done
1282   # func_add_or_update handles a file that ought to be present afterwards.
1283   # Uses parameters f, g, already_present.
1284   func_add_or_update ()
1285   {
1286     func_dest_tmpfilename "$g"
1287     func_lookup_file "$f"
1288     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
1289     if test -n "$lgpl"; then
1290       # Update license.
1291       case "$f" in
1292         lib/*)
1293           sed -e 's/GNU General/GNU Lesser General/g' \
1294               -e 's/version 2\([ ,]\)/version 2.1\1/g' \
1295             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
1296           ;;
1297       esac
1298     fi
1299     if test -f "$destdir/$g"; then
1300       # The file already exists.
1301       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1302         : # The file has not changed.
1303       else
1304         # Replace the file.
1305         if $doit; then
1306           if test -n "$already_present"; then
1307             echo "Updating file $g (backup in ${g}~)"
1308           else
1309             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1310           fi
1311           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1312           if test -n "$symbolic" && test -z "$lookedup_tmp" \
1313              && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1314             func_ln_if_changed "$lookedup_file" "$destdir/$g"
1315           else
1316             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1317           fi
1318         else
1319           if test -n "$already_present"; then
1320             echo "Update file $g (backup in ${g}~)"
1321           else
1322             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1323           fi
1324         fi
1325       fi
1326     else
1327       # Install the file.
1328       # Don't protest if the file should be there but isn't: it happens
1329       # frequently that developers don't put autogenerated files into CVS.
1330       if $doit; then
1331         echo "Copying file $g"
1332         if test -n "$symbolic" && test -z "$lookedup_tmp" \
1333            && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1334           func_ln_if_changed "$lookedup_file" "$destdir/$g"
1335         else
1336           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1337         fi
1338       else
1339         echo "Copy file $g"
1340       fi
1341     fi
1342     rm -f "$tmpfile"
1343   }
1344   # Then the files that are in new-files, but not in old-files:
1345   sed_take_last_column='s,^.*'"$delimiter"',,'
1346   already_present=
1347   for f in `LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1348     case "$f" in
1349       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1350       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1351       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1352       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1353       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1354       *) g="$f" ;;
1355     esac
1356     func_add_or_update
1357   done
1358   # Then the files that are in new-files and in old-files:
1359   already_present=true
1360   for f in `LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1361     case "$f" in
1362       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1363       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1364       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1365       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1366       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1367       *) g="$f" ;;
1368     esac
1369     func_add_or_update
1370   done
1371
1372   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1373   actioncmd="gnulib-tool --import"
1374   actioncmd="$actioncmd --dir=$destdir"
1375   actioncmd="$actioncmd --lib=$libname"
1376   actioncmd="$actioncmd --source-base=$sourcebase"
1377   actioncmd="$actioncmd --m4-base=$m4base"
1378   actioncmd="$actioncmd --doc-base=$docbase"
1379   actioncmd="$actioncmd --aux-dir=$auxdir"
1380   for module in $avoidlist; do
1381     actioncmd="$actioncmd --avoid=$module"
1382   done
1383   if test -n "$lgpl"; then
1384     actioncmd="$actioncmd --lgpl"
1385   fi
1386   if test "$libtool" = true; then
1387     actioncmd="$actioncmd --libtool"
1388   else
1389     actioncmd="$actioncmd --no-libtool"
1390   fi
1391   actioncmd="$actioncmd --macro-prefix=$macro_prefix"
1392   actioncmd="$actioncmd `echo $specified_modules`"
1393
1394   # Create lib/Makefile.am.
1395   func_dest_tmpfilename $sourcebase/Makefile.am
1396   func_emit_lib_Makefile_am > "$tmpfile"
1397   if test -f "$destdir"/$sourcebase/Makefile.am; then
1398     if cmp "$destdir"/$sourcebase/Makefile.am "$tmpfile" > /dev/null; then
1399       rm -f "$tmpfile"
1400     else
1401       if $doit; then
1402         echo "Updating $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1403         mv -f "$destdir"/$sourcebase/Makefile.am "$destdir"/$sourcebase/Makefile.am~
1404         mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1405       else
1406         echo "Update $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1407         rm -f "$tmpfile"
1408       fi
1409     fi
1410   else
1411     if $doit; then
1412       echo "Creating $sourcebase/Makefile.am"
1413       mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1414     else
1415       echo "Create $sourcebase/Makefile.am"
1416       rm -f "$tmpfile"
1417     fi
1418   fi
1419
1420   # Create m4/gnulib-cache.m4.
1421   func_dest_tmpfilename $m4base/gnulib-cache.m4
1422   (
1423     func_emit_copyright_notice
1424     echo "#"
1425     echo "# This file represents the specification of how gnulib-tool is used."
1426     echo "# It acts as a cache: It is written and read by gnulib-tool."
1427     echo "# In projects using CVS, this file is meant to be stored in CVS,"
1428     echo "# like the configure.ac and various Makefile.am files."
1429     echo
1430     echo
1431     echo "# Specification in the form of a command-line invocation:"
1432     echo "#   $actioncmd"
1433     echo
1434     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
1435     echo "gl_MODULES(["`echo $specified_modules`"])"
1436     echo "gl_AVOID([$avoidlist])"
1437     echo "gl_SOURCE_BASE([$sourcebase])"
1438     echo "gl_M4_BASE([$m4base])"
1439     echo "gl_DOC_BASE([$docbase])"
1440     echo "gl_TESTS_BASE([$testsbase])"
1441     echo "gl_LIB([$libname])"
1442     test -z "$lgpl" || echo "gl_LGPL"
1443     if test "$libtool" = true; then
1444       echo "gl_LIBTOOL"
1445     fi
1446     echo "gl_MACRO_PREFIX([$macro_prefix])"
1447   ) > "$tmpfile"
1448   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1449     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
1450       rm -f "$tmpfile"
1451     else
1452       if $doit; then
1453         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1454         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
1455         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1456       else
1457         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1458         if false; then
1459           cat "$tmpfile"
1460           echo
1461           echo "# gnulib-cache.m4 ends here"
1462         fi
1463         rm -f "$tmpfile"
1464       fi
1465     fi
1466   else
1467     if $doit; then
1468       echo "Creating $m4base/gnulib-cache.m4"
1469       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1470     else
1471       echo "Create $m4base/gnulib-cache.m4"
1472       cat "$tmpfile"
1473       rm -f "$tmpfile"
1474     fi
1475   fi
1476
1477   # Create m4/gnulib-comp.m4.
1478   func_dest_tmpfilename $m4base/gnulib-comp.m4
1479   (
1480     func_emit_copyright_notice
1481     echo "#"
1482     echo "# This file represents the compiled summary of the specification in"
1483     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
1484     echo "# to be invoked from configure.ac."
1485     echo "# In projects using CVS, this file can be treated like other built files."
1486     echo
1487     echo
1488     echo "# This macro should be invoked from $configure_ac, in the section"
1489     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
1490     echo "# any checks for libraries, header files, types and library functions."
1491     echo "AC_DEFUN([${macro_prefix}_EARLY],"
1492     echo "["
1493     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
1494     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1495       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
1496     fi
1497     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1498       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
1499     fi
1500     if grep gl_LOCK "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1501       echo "  AC_REQUIRE([gl_LOCK])"
1502     fi
1503     echo "])"
1504     echo
1505     echo "# This macro should be invoked from $configure_ac, in the section"
1506     echo "# \"Check for header files, types and library functions\"."
1507     echo "AC_DEFUN([${macro_prefix}_INIT],"
1508     echo "["
1509     if test "$libtool" = true; then
1510       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1511     else
1512       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1513     fi
1514     if test "$auxdir" != "build-aux"; then
1515       sed_replace_build_aux='
1516         :a
1517         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1518           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1519           ba
1520         }'
1521       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1522     else
1523       sed_replace_build_aux=
1524     fi
1525     for module in $modules; do
1526       func_verify_module
1527       if test -n "$module"; then
1528         func_get_autoconf_snippet "$module" \
1529           | sed -e '/^$/d;' -e 's/^/  /' \
1530                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
1531                 -e "$sed_replace_build_aux"
1532         if test "$module" = 'alloca' && test "$libtool" = true; then
1533           echo 'changequote(,)dnl'
1534           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
1535           echo 'changequote([, ])dnl'
1536           echo 'AC_SUBST([LTALLOCA])'
1537         fi
1538       fi
1539     done
1540     echo "])"
1541     echo
1542     echo "# This macro records the list of files which have been installed by"
1543     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
1544     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
1545     echo "$files" | sed -e 's,^,  ,'
1546     echo "])"
1547   ) > "$tmpfile"
1548   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1549     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
1550       rm -f "$tmpfile"
1551     else
1552       if $doit; then
1553         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1554         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
1555         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1556       else
1557         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1558         if false; then
1559           cat "$tmpfile"
1560           echo
1561           echo "# gnulib-comp.m4 ends here"
1562         fi
1563         rm -f "$tmpfile"
1564       fi
1565     fi
1566   else
1567     if $doit; then
1568       echo "Creating $m4base/gnulib-comp.m4"
1569       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1570     else
1571       echo "Create $m4base/gnulib-comp.m4"
1572       cat "$tmpfile"
1573       rm -f "$tmpfile"
1574     fi
1575   fi
1576
1577   if test -n "$inctests"; then
1578     # Create tests/Makefile.am.
1579     func_dest_tmpfilename $testsbase/Makefile.am
1580     func_emit_tests_Makefile_am > "$tmpfile"
1581     if test -f "$destdir"/$testsbase/Makefile.am; then
1582       if cmp "$destdir"/$testsbase/Makefile.am "$tmpfile" > /dev/null; then
1583         rm -f "$tmpfile"
1584       else
1585         if $doit; then
1586           echo "Updating $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1587           mv -f "$destdir"/$testsbase/Makefile.am "$destdir"/$testsbase/Makefile.am~
1588           mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1589         else
1590           echo "Update $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1591           rm -f "$tmpfile"
1592         fi
1593       fi
1594     else
1595       if $doit; then
1596         echo "Creating $testsbase/Makefile.am"
1597         mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1598       else
1599         echo "Create $testsbase/Makefile.am"
1600         rm -f "$tmpfile"
1601       fi
1602     fi
1603   fi
1604
1605   echo "Finished."
1606   echo
1607   echo "You may need to add #include directives for the following .h files."
1608   (
1609    # First the #include <...> directives without #ifs, sorted for convenience.
1610    for module in $modules; do
1611      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1612        :
1613      else
1614        func_get_include_directive "$module" | grep -v 'include "'
1615      fi
1616    done | LC_ALL=C sort -u
1617    # Then the #include "..." directives without #ifs, sorted for convenience.
1618    for module in $modules; do
1619      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1620        :
1621      else
1622        func_get_include_directive "$module" | grep 'include "'
1623      fi
1624    done | LC_ALL=C sort -u
1625    # Then the #include directives that are surrounded by #ifs. Not sorted.
1626    for module in $modules; do
1627      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1628        func_get_include_directive "$module"
1629      fi
1630    done
1631   ) | sed -e '/^$/d;' -e 's/^/  /'
1632   echo
1633   echo "Don't forget to"
1634   echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1635   if test -n "$inctests"; then
1636     echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1637   fi
1638   sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
1639   sourcebase_base=`basename "$sourcebase"`
1640   echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
1641   if test -n "$inctests"; then
1642     testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
1643     testsbase_base=`basename "$testsbase"`
1644     echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
1645   fi
1646   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
1647   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
1648   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
1649 }
1650
1651 # func_create_testdir testdir modules
1652 # Input:
1653 # - auxdir          directory relative to destdir where to place build aux files
1654 func_create_testdir ()
1655 {
1656   testdir="$1"
1657   modules="$2"
1658   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
1659
1660   # Determine final module list.
1661   func_modules_transitive_closure
1662   echo "Module list with included dependencies:"
1663   echo "$modules" | sed -e 's/^/  /'
1664
1665   # Add the dummy module if needed.
1666   func_modules_add_dummy
1667
1668   # Determine final file list.
1669   func_modules_to_filelist
1670   echo "File list:"
1671   echo "$files" | sed -e 's/^/  /'
1672
1673   # Create directories.
1674   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
1675     if test "$d" = build-aux; then
1676       mkdir -p "$testdir/$auxdir"
1677     else
1678       mkdir -p "$testdir/$d"
1679     fi
1680   done
1681
1682   # Copy files or make symbolic links.
1683   for f in $files; do
1684     func_lookup_file "$f"
1685     case "$f" in
1686       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1687       *) g="$f" ;;
1688     esac
1689     if test -n "$lookedup_tmp"; then
1690       cp -p "$lookedup_file" "$testdir/$g"
1691     else
1692       ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
1693       if test -z "$symbolic"; then
1694         cp -p "$lookedup_file" "$testdir/$g"
1695       else
1696         ln -s "$lookedup_file" "$testdir/$g"
1697       fi
1698     fi
1699   done
1700
1701   # Create lib/Makefile.am.
1702   mkdir -p "$testdir/lib"
1703   func_emit_lib_Makefile_am > "$testdir/lib/Makefile.am"
1704
1705   # Create m4/Makefile.am.
1706   mkdir -p "$testdir/m4"
1707   (echo "## Process this file with automake to produce Makefile.in."
1708    echo
1709    echo "EXTRA_DIST ="
1710    for f in $files; do
1711      case "$f" in
1712        m4/* )
1713          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
1714      esac
1715    done
1716   ) > "$testdir/m4/Makefile.am"
1717
1718   subdirs="lib m4"
1719   subdirs_with_configure_ac=""
1720
1721   if test -f "$testdir"/m4/gettext.m4; then
1722     # Avoid stupid error message from automake:
1723     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
1724     mkdir -p "$testdir/po"
1725     (echo "## Process this file with automake to produce Makefile.in."
1726     ) > "$testdir/po/Makefile.am"
1727     subdirs="$subdirs po"
1728   fi
1729
1730   if test -n "$inctests"; then
1731     test -d "$testdir/tests" || mkdir "$testdir/tests"
1732     # Create tests/Makefile.am.
1733     sourcebase=lib
1734     m4base=m4
1735     testsbase=tests
1736     func_emit_tests_Makefile_am > "$testdir/tests/Makefile.am"
1737     # Create tests/configure.ac.
1738     (echo "# Process this file with autoconf to produce a configure script."
1739      echo "AC_INIT([dummy], [0])"
1740      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
1741      echo "AM_INIT_AUTOMAKE"
1742      echo
1743      echo "AM_CONFIG_HEADER([config.h])"
1744      echo
1745      echo "AC_PROG_CC"
1746      echo "AC_PROG_INSTALL"
1747      echo "AC_PROG_MAKE_SET"
1748      echo "AC_PROG_RANLIB"
1749      echo
1750      if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1751        echo "AC_GNU_SOURCE"
1752        echo
1753      fi
1754      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1755        echo "gl_USE_SYSTEM_EXTENSIONS"
1756        echo
1757      fi
1758      if grep gl_LOCK "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1759        echo "gl_LOCK"
1760        echo
1761      fi
1762      if test "$libtool" = true; then
1763        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1764      else
1765        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1766      fi
1767      if test "$auxdir" != "build-aux"; then
1768        sed_replace_build_aux='
1769          :a
1770          /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1771            s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
1772            ba
1773          }'
1774        sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1775      else
1776        sed_replace_build_aux=
1777      fi
1778      # We don't have explicit ordering constraints between the various
1779      # autoconf snippets. It's cleanest to put those of the library before
1780      # those of the tests.
1781      for module in $modules; do
1782        func_verify_nontests_module
1783        if test -n "$module"; then
1784          func_get_autoconf_snippet "$module" \
1785            | sed -e "$sed_replace_build_aux"
1786        fi
1787      done
1788      for module in $modules; do
1789        func_verify_tests_module
1790        if test -n "$module"; then
1791          func_get_autoconf_snippet "$module" \
1792            | sed -e "$sed_replace_build_aux"
1793        fi
1794      done
1795      echo
1796      # Usually tests/config.h will be a superset of config.h. Verify this by
1797      # "merging" config.h into tests/config.h; look out for gcc warnings.
1798      echo "AH_TOP([#include \"../config.h\"])"
1799      echo
1800      echo "AC_OUTPUT([Makefile])"
1801     ) > "$testdir/tests/configure.ac"
1802     subdirs="$subdirs tests"
1803     subdirs_with_configure_ac="$subdirs_with_configure_ac tests"
1804   fi
1805
1806   # Create Makefile.am.
1807   (echo "## Process this file with automake to produce Makefile.in."
1808    echo
1809    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1810    echo
1811    echo "SUBDIRS = $subdirs"
1812    echo
1813    echo "ACLOCAL_AMFLAGS = -I m4"
1814   ) > "$testdir/Makefile.am"
1815
1816   # Create configure.ac.
1817   (echo "# Process this file with autoconf to produce a configure script."
1818    echo "AC_INIT([dummy], [0])"
1819    if test "$auxdir" != "."; then
1820      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1821    fi
1822    echo "AM_INIT_AUTOMAKE"
1823    echo
1824    echo "AM_CONFIG_HEADER([config.h])"
1825    echo
1826    echo "AC_PROG_CC"
1827    echo "AC_PROG_INSTALL"
1828    echo "AC_PROG_MAKE_SET"
1829    echo "AC_PROG_RANLIB"
1830    echo
1831    if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1832      echo "AC_GNU_SOURCE"
1833      echo
1834    fi
1835    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1836      echo "gl_USE_SYSTEM_EXTENSIONS"
1837      echo
1838    fi
1839    if grep gl_LOCK "$testdir"/m4/*.m4 >/dev/null 2>/dev/null; then
1840      echo "gl_LOCK"
1841      echo
1842    fi
1843    if test "$libtool" = true; then
1844      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1845    else
1846      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1847    fi
1848    if test "$auxdir" != "build-aux"; then
1849      sed_replace_build_aux='
1850        :a
1851        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1852          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1853          ba
1854        }'
1855      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1856    else
1857      sed_replace_build_aux=
1858    fi
1859    for module in $modules; do
1860      func_verify_nontests_module
1861      if test -n "$module"; then
1862        func_get_autoconf_snippet "$module" \
1863          | sed -e "$sed_replace_build_aux"
1864      fi
1865    done
1866    echo
1867    if test -n "$subdirs_with_configure_ac"; then
1868      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
1869    fi
1870    makefiles="Makefile"
1871    for d in $subdirs; do
1872      # For subdirs that have a configure.ac by their own, it's the subdir's
1873      # configure.ac which creates the subdir's Makefile.am, not this one.
1874      case " $subdirs_with_configure_ac " in
1875        *" $d "*) ;;
1876        *) makefiles="$makefiles $d/Makefile" ;;
1877      esac
1878    done
1879    echo "AC_OUTPUT([$makefiles])"
1880   ) > "$testdir/configure.ac"
1881
1882   # Create autogenerated files.
1883   (cd "$testdir"
1884    echo "executing ${AUTORECONF} --force --install"
1885    ${AUTORECONF} --force --install
1886   )
1887   if grep '^BUILT_SOURCES *+=' "$testdir/lib/Makefile.am" > /dev/null; then
1888     (cd "$testdir"
1889      ./configure
1890        cd lib
1891        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
1892        make built_sources
1893        cd ..
1894      make distclean
1895     )
1896   fi
1897 }
1898
1899 # func_create_megatestdir megatestdir allmodules
1900 # Input:
1901 # - auxdir          directory relative to destdir where to place build aux files
1902 func_create_megatestdir ()
1903 {
1904   megatestdir="$1"
1905   allmodules="$2"
1906   if test -z "$allmodules"; then
1907     allmodules=`func_all_modules`
1908   fi
1909
1910   megasubdirs=
1911   # First, all modules one by one.
1912   for onemodule in $allmodules; do
1913     func_create_testdir "$megatestdir/$onemodule" $onemodule
1914     megasubdirs="${megasubdirs}$onemodule "
1915   done
1916   # Then, all modules all together.
1917   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
1918   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
1919   func_create_testdir "$megatestdir/ALL" "$allmodules"
1920   megasubdirs="${megasubdirs}ALL"
1921
1922   # Create Makefile.am.
1923   (echo "## Process this file with automake to produce Makefile.in."
1924    echo
1925    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1926    echo
1927    echo "SUBDIRS = $megasubdirs"
1928   ) > "$megatestdir/Makefile.am"
1929
1930   # Create configure.ac.
1931   (echo "# Process this file with autoconf to produce a configure script."
1932    echo "AC_INIT([dummy], [0])"
1933    if test "$auxdir" != "."; then
1934      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1935    fi
1936    echo "AM_INIT_AUTOMAKE"
1937    echo
1938    echo "AC_PROG_MAKE_SET"
1939    echo
1940    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
1941    echo "AC_OUTPUT([Makefile])"
1942   ) > "$megatestdir/configure.ac"
1943
1944   # Create autogenerated files.
1945   (cd "$megatestdir"
1946    # Do not use "${AUTORECONF} --install", because autoreconf operates
1947    # recursively, but the subdirectories are already finished, therefore
1948    # calling autoreconf here would only waste lots of CPU time.
1949    echo "executing ${ACLOCAL}"
1950    ${ACLOCAL}
1951    echo "executing mkdir build-aux"
1952    mkdir build-aux
1953    echo "executing ${AUTOCONF}"
1954    ${AUTOCONF}
1955    echo "executing ${AUTOMAKE} --add-missing --copy"
1956    ${AUTOMAKE} --add-missing --copy
1957   )
1958 }
1959
1960 case $mode in
1961   "" )
1962     func_fatal_error "no mode specified" ;;
1963
1964   list )
1965     func_all_modules
1966     ;;
1967
1968   import | update )
1969
1970     # Where to import.
1971     if test -z "$destdir"; then
1972       destdir=.
1973     fi
1974     test -d "$destdir" \
1975       || func_fatal_error "destination directory does not exist: $destdir"
1976
1977     # Prefer configure.ac to configure.in.
1978     if test -f "$destdir"/configure.ac; then
1979       configure_ac="$destdir/configure.ac"
1980     else
1981       if test -f "$destdir"/configure.in; then
1982         configure_ac="$destdir/configure.in"
1983       else
1984         func_fatal_error "cannot find $destdir/configure.ac"
1985       fi
1986     fi
1987
1988     # Analyze configure.ac.
1989     guessed_auxdir="."
1990     guessed_libtool=false
1991     my_sed_traces='
1992       s,#.*$,,
1993       s,^dnl .*$,,
1994       s, dnl .*$,,
1995       /AC_CONFIG_AUX_DIR/ {
1996         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
1997       }
1998       /A[CM]_PROG_LIBTOOL/ {
1999         s,^.*$,guessed_libtool=true,p
2000       }'
2001     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
2002
2003     if test -z "$auxdir"; then
2004       auxdir="$guessed_auxdir"
2005     fi
2006
2007     # Determine where to apply func_import.
2008     if test -n "$m4base"; then
2009       # Apply func_import to a particular gnulib directory.
2010       # Any number of additional modules can be given.
2011       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
2012         # First use of gnulib in the given m4base.
2013         test -n "$supplied_libname" || supplied_libname=true
2014         test -n "$sourcebase" || sourcebase="lib"
2015         test -n "$docbase" || docbase="doc"
2016         test -n "$testsbase" || testsbase="tests"
2017         test -n "$macro_prefix" || macro_prefix="gl"
2018       fi
2019       func_import "$*"
2020     else
2021       # Apply func_import to all gnulib directories.
2022       # To get this list of directories, look at Makefile.am. (Not at
2023       # configure, because it may be omitted from CVS. Also, don't run
2024       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
2025       m4dirs=
2026       m4dirs_count=0
2027       if test -f "$destdir"/Makefile.am; then
2028         aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[         ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
2029         m4dir_is_next=
2030         for arg in $aclocal_amflags; do
2031           if test -n "$m4dir_is_next"; then
2032             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
2033             case "$arg" in
2034               /*) ;;
2035               *)
2036                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
2037                   m4dirs="$m4dirs $arg"
2038                   m4dirs_count=`expr $m4dirs_count + 1`
2039                 fi
2040                 ;;
2041             esac
2042           else
2043             if test "X$arg" = "X-I"; then
2044               m4dir_is_next=yes
2045             else
2046               m4dir_is_next=
2047             fi
2048           fi
2049         done
2050       else
2051         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
2052         if test -f "$destdir"/aclocal.m4; then
2053           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
2054           sedexpr2='s,^[^/]*$,.,'
2055           sedexpr3='s,/[^/]*$,,'
2056           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort | LC_ALL=C uniq`
2057           m4dirs_count=`echo "$m4dirs" | wc -l`
2058         fi
2059       fi
2060       if test $m4dirs_count = 0; then
2061         # First use of gnulib in a package.
2062         # Any number of additional modules can be given.
2063         test -n "$supplied_libname" || supplied_libname=true
2064         test -n "$sourcebase" || sourcebase="lib"
2065         m4base="m4"
2066         test -n "$docbase" || docbase="doc"
2067         test -n "$testsbase" || testsbase="tests"
2068         test -n "$macro_prefix" || macro_prefix="gl"
2069         func_import "$*"
2070       else
2071         if test $m4dirs_count = 1; then
2072           # There's only one use of gnulib here. Assume the user means it.
2073           # Any number of additional modules can be given.
2074           for m4base in $m4dirs; do
2075             func_import "$*"
2076           done
2077         else
2078           # Ambiguous - guess what the user meant.
2079           if test $# = 0; then
2080             # No further arguments. Guess the user wants to update all of them.
2081             for m4base in $m4dirs; do
2082               func_import
2083             done
2084           else
2085             # Really ambiguous.
2086             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
2087           fi
2088         fi
2089       fi
2090     fi
2091     ;;
2092
2093   create-testdir )
2094     if test -z "$destdir"; then
2095       func_fatal_error "please specify --dir option"
2096     fi
2097     mkdir "$destdir"
2098     test -d "$destdir" \
2099       || func_fatal_error "could not create destination directory"
2100     test -n "$auxdir" || auxdir="build-aux"
2101     func_create_testdir "$destdir" "$*"
2102     ;;
2103
2104   create-megatestdir )
2105     if test -z "$destdir"; then
2106       func_fatal_error "please specify --dir option"
2107     fi
2108     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2109     test -n "$auxdir" || auxdir="build-aux"
2110     func_create_megatestdir "$destdir" "$*"
2111     ;;
2112
2113   test )
2114     test -n "$destdir" || destdir=testdir$$
2115     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2116     test -n "$auxdir" || auxdir="build-aux"
2117     func_create_testdir "$destdir" "$*"
2118     cd "$destdir"
2119       mkdir build
2120       cd build
2121         ../configure
2122         make
2123         make check
2124         make distclean
2125         remaining=`find . -type f -print`
2126         if test -n "$remaining"; then
2127           echo "Remaining files:" $remaining 1>&2
2128           echo "gnulib-tool: *** Stop." 1>&2
2129           exit 1
2130         fi
2131       cd ..
2132     cd ..
2133     rm -rf "$destdir"
2134     ;;
2135
2136   megatest )
2137     test -n "$destdir" || destdir=testdir$$
2138     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2139     test -n "$auxdir" || auxdir="build-aux"
2140     func_create_megatestdir "$destdir" "$*"
2141     cd "$destdir"
2142       mkdir build
2143       cd build
2144         ../configure
2145         make
2146         make check
2147         make distclean
2148         remaining=`find . -type f -print`
2149         if test -n "$remaining"; then
2150           echo "Remaining files:" $remaining 1>&2
2151           echo "gnulib-tool: *** Stop." 1>&2
2152           exit 1
2153         fi
2154       cd ..
2155     cd ..
2156     rm -rf "$destdir"
2157     ;;
2158
2159   extract-description )
2160     for module
2161     do
2162       func_verify_module
2163       if test -n "$module"; then
2164         func_get_description "$module"
2165       fi
2166     done
2167     ;;
2168
2169   extract-filelist )
2170     for module
2171     do
2172       func_verify_module
2173       if test -n "$module"; then
2174         func_get_filelist "$module"
2175       fi
2176     done
2177     ;;
2178
2179   extract-dependencies )
2180     for module
2181     do
2182       func_verify_module
2183       if test -n "$module"; then
2184         func_get_dependencies "$module"
2185       fi
2186     done
2187     ;;
2188
2189   extract-autoconf-snippet )
2190     for module
2191     do
2192       func_verify_module
2193       if test -n "$module"; then
2194         func_get_autoconf_snippet "$module"
2195       fi
2196     done
2197     ;;
2198
2199   extract-automake-snippet )
2200     for module
2201     do
2202       func_verify_module
2203       if test -n "$module"; then
2204         func_get_automake_snippet "$module"
2205       fi
2206     done
2207     ;;
2208
2209   extract-include-directive )
2210     for module
2211     do
2212       func_verify_module
2213       if test -n "$module"; then
2214         func_get_include_directive "$module"
2215       fi
2216     done
2217     ;;
2218
2219   extract-license )
2220     for module
2221     do
2222       func_verify_module
2223       if test -n "$module"; then
2224         func_get_license "$module"
2225       fi
2226     done
2227     ;;
2228
2229   extract-maintainer )
2230     for module
2231     do
2232       func_verify_module
2233       if test -n "$module"; then
2234         func_get_maintainer "$module"
2235       fi
2236     done
2237     ;;
2238
2239   extract-tests-module )
2240     for module
2241     do
2242       func_verify_module
2243       if test -n "$module"; then
2244         func_get_tests_module "$module"
2245       fi
2246     done
2247     ;;
2248
2249   * )
2250     func_fatal_error "unknown operation mode --$mode" ;;
2251 esac
2252
2253 rm -rf "$tmp"
2254 # Undo the effect of the previous 'trap' command. Some shellology:
2255 # We cannot use "trap - 0 1 2 3 15", because Solaris sh would attempt to
2256 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
2257 # exit); for the others we need to call 'exit' explicitly. The value of $? is
2258 # 128 + signal number and is set before the trap-registered command is run.
2259 trap '' 0
2260 trap 'exit $?' 1 2 3 15
2261
2262 exit 0