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