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