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