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