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