Initialize opt_lgpl.
[pspp] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002, 2003, 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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: 2004-10-15 15:43:58 $'
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
60 Operation modes:
61       --list                print the available module names
62       --import              import the given modules into the current package
63       --create-testdir      create a scratch package with the given modules
64       --create-megatestdir  create a mega scratch package with the given modules
65                             one by one and all together
66       --test                test the combination of the given modules
67                             (recommended to use CC=\"gcc -Wall\" here)
68       --megatest            test the given modules one by one and all together
69                             (recommended to use CC=\"gcc -Wall\" here)
70       --extract-description        extract the description
71       --extract-filelist           extract the list of files
72       --extract-dependencies       extract the dependencies
73       --extract-autoconf-snippet   extract the snippet for configure.ac
74       --extract-automake-snippet   extract the snippet for lib/Makefile.am
75       --extract-include-directive  extract the #include directive
76       --extract-license            report the license terms of the source files
77                                    under lib/
78       --extract-maintainer         report the maintainer(s) inside gnulib
79
80 Options:
81       --dir=DIRECTORY       specify the target directory
82                             For --import, this specifies where your
83                             configure.ac can be found.  Defaults to current
84                             directory.
85       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
86       --source-base=DIRECTORY
87                             Directory relative --dir where source code is
88                             placed (default \"lib\"), for --import.
89       --m4-base=DIRECTORY   Directory relative --dir where *.m4 macros are
90                             placed (default \"m4\"), for --import.
91       --lgpl                Abort if modules aren't available under the LGPL.
92                             Also modify license template from GPL to LGPL.
93       --libtool             Use libtool rules, for --import.
94       --no-changelog        don't update or create ChangeLog files
95       --dry-run             For --import, only print what would have been done.
96
97 Report bugs to <bug-gnulib@gnu.org>."
98 }
99
100 # func_version
101 # outputs to stdout the --version message.
102 func_version ()
103 {
104   echo "$progname (GNU $package) $version"
105   echo "Copyright (C) 2002 Free Software Foundation, Inc.
106 This is free software; see the source for copying conditions.  There is NO
107 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
108   echo "Written by" "Bruno Haible"
109 }
110
111 # func_fatal_error message
112 # outputs to stderr a fatal error message, and terminates the program.
113 func_fatal_error ()
114 {
115   echo "gnulib-tool: *** $1" 1>&2
116   echo "gnulib-tool: *** Stop." 1>&2
117   exit 1
118 }
119
120 # Command-line option processing.
121 # Removes the OPTIONS from the arguments. Sets the variables:
122 # - mode            list or import or create-testdir or create-megatestdir
123 # - destdir         from --dir
124 # - libname, supplied_libname  from --lib
125 # - sourcebase      from --source-base
126 # - m4base          from --m4-base
127 # - libtool         true if --libtool was given, blank otherwise
128 # - lgpl            true if --lgpl was given, blank otherwise
129 # - do_changelog    false if --no-changelog was given, : otherwise
130 # - dry_run         true if --dry-run was given, blank otherwise
131 {
132   mode=
133   destdir=
134   libname=libgnu
135   supplied_libname=
136   sourcebase=
137   m4base=
138   libtool=
139   lgpl=
140   do_changelog=:
141   dry_run=
142   lgpl=
143
144   supplied_opts="$@"
145
146   while test $# -gt 0; do
147     case "$1" in
148       --list | --lis )
149         mode=list
150         shift ;;
151       --import | --impor | --impo | --imp | --im | --i )
152         mode=import
153         shift ;;
154       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
155         mode=create-testdir
156         shift ;;
157       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
158         mode=create-megatestdir
159         shift ;;
160       --test | --tes | --te | --t )
161         mode=test
162         shift ;;
163       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
164         mode=megatest
165         shift ;;
166       --extract-* )
167         mode=`echo "X$1" | sed -e 's/^X--//'`
168         shift ;;
169       --dir )
170         shift
171         if test $# = 0; then
172           func_fatal_error "missing argument for --dir"
173         fi
174         destdir=$1
175         shift ;;
176       --dir=* )
177         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
178         shift ;;
179       --lib )
180         shift
181         if test $# = 0; then
182           func_fatal_error "missing argument for --lib"
183         fi
184         libname=$1
185         supplied_libname=true
186         shift ;;
187       --lib=* )
188         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
189         supplied_libname=true
190         shift ;;
191       --source-base )
192         shift
193         if test $# = 0; then
194           func_fatal_error "missing argument for --source-base"
195         fi
196         sourcebase=$1
197         shift ;;
198       --source-base=* )
199         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
200         shift ;;
201       --m4-base )
202         shift
203         if test $# = 0; then
204           func_fatal_error "missing argument for --m4-base"
205         fi
206         m4base=$1
207         shift ;;
208       --m4-base=* )
209         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
210         shift ;;
211       --libtool )
212         libtool=true
213         shift ;;
214       --lgpl )
215         lgpl=true
216         shift ;;
217       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
218         do_changelog=false
219         shift ;;
220       --dry-run )
221         dry_run=true
222         shift ;;
223       --help | --hel | --he | --h )
224         func_usage
225         exit 0 ;;
226       --version | --versio | --versi | --vers | --ver | --ve | --v )
227         func_version
228         exit 0 ;;
229       -- )
230         # Stop option prcessing
231         shift
232         break ;;
233       -* )
234         echo "gnulib-tool: unknown option $1" 1>&2
235         echo "Try 'gnulib-tool --help' for more information." 1>&2
236         exit 1 ;;
237       * )
238         break ;;
239     esac
240   done
241 }
242
243 case "$0" in
244   /*) self_abspathname="$0" ;;
245   */*) self_abspathname=`pwd`/"$0" ;;
246   *) for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`; do
247        if test -x "$d/$0" && test ! -d "$d/$0"; then
248          self_abspathname="$d/$0"
249          break
250        fi
251      done
252      if test -z "$self_abspathname"; then
253        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
254      fi
255      ;;
256 esac
257 while test -h "$self_abspathname"; do
258   # Resolve symbolic link.
259   sedexpr1='s, -> ,#%%#,'
260   sedexpr2='s,^.*#%%#\(.*\)$,\1,p'
261   linkval=`LC_ALL=C ls -l "$self_abspathname" | sed -e "$sedexpr1" | sed -n -e "$sedexpr2"`
262   test -n "$linkval" || break
263   case "$linkval" in
264     /* ) self_abspathname="$linkval" ;;
265     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
266   esac
267 done
268 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
269
270 # func_all_modules
271 func_all_modules ()
272 {
273   (cd "$gnulib_dir/modules" && ls -1) \
274       | sed -e '/^CVS$/d' -e '/^ChangeLog$/d' -e '/^README$/d' -e '/^TEMPLATE$/d' -e '/~$/d' \
275       | sort
276 }
277
278 # func_verify_module
279 # verifies a module name
280 func_verify_module ()
281 {
282   if test ! -f "$gnulib_dir/modules/$module" \
283      || test "CVS" = "$module" \
284      || test "ChangeLog" = "$module" \
285      || test "README" = "$module" \
286      || test "TEMPLATE" = "$module"; then
287     echo "gnulib-tool: module $module doesn't exist" 1>&2
288     module=
289   fi
290 }
291
292 sed_extract_prog=':[    ]*$/ {
293   :a
294     n
295     s/^Description:[    ]*$//
296     s/^Files:[  ]*$//
297     s/^Depends-on:[     ]*$//
298     s/^configure\.ac:[  ]*$//
299     s/^Makefile\.am:[   ]*$//
300     s/^Include:[        ]*$//
301     s/^License:[        ]*$//
302     s/^Maintainer:[     ]*$//
303     tb
304     p
305     ba
306   :b
307 }'
308
309 # func_get_description module
310 func_get_description ()
311 {
312   sed -n -e "/^Description$sed_extract_prog" < "$gnulib_dir/modules/$1"
313 }
314
315 # func_get_filelist module
316 func_get_filelist ()
317 {
318   sed -n -e "/^Files$sed_extract_prog" < "$gnulib_dir/modules/$1"
319   #echo m4/onceonly.m4
320   echo m4/onceonly_2_57.m4
321 }
322
323 # func_get_dependencies module
324 func_get_dependencies ()
325 {
326   sed -n -e "/^Depends-on$sed_extract_prog" < "$gnulib_dir/modules/$1"
327 }
328
329 # func_get_autoconf_snippet module
330 func_get_autoconf_snippet ()
331 {
332   sed -n -e "/^configure\.ac$sed_extract_prog" < "$gnulib_dir/modules/$1"
333 }
334
335 # func_get_automake_snippet module
336 func_get_automake_snippet ()
337 {
338   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$gnulib_dir/modules/$1"
339 }
340
341 # func_get_include_directive module
342 func_get_include_directive ()
343 {
344   sed -n -e "/^Include$sed_extract_prog" < "$gnulib_dir/modules/$1" | \
345   sed -e 's/^\(["<]\)/#include \1/'
346 }
347
348 # func_get_license module
349 func_get_license ()
350 {
351   sed -n -e "/^License$sed_extract_prog" < "$gnulib_dir/modules/$1"
352 }
353
354 # func_get_maintainer module
355 func_get_maintainer ()
356 {
357   sed -n -e "/^Maintainer$sed_extract_prog" < "$gnulib_dir/modules/$1"
358 }
359
360 # func_modules_transitive_closure
361 # Input:
362 # - modules         list of specified modules
363 # Output:
364 # - modules         list of modules, including dependencies
365 func_modules_transitive_closure ()
366 {
367   while true; do
368     xmodules=
369     for module in $modules; do
370       func_verify_module
371       if test -n "$module"; then
372         # Duplicate dependenies are harmless, but Jim wants a warning.
373         duplicated_deps=`func_get_dependencies $module | sort | uniq -d`
374         if test -n "$duplicated_deps"; then
375           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
376         fi
377         xmodules="$xmodules $module "`func_get_dependencies $module`
378       fi
379     done
380     xmodules=`for m in $xmodules; do echo $m; done | sort | uniq`
381     if test "$xmodules" = "$modules"; then
382       break
383     fi
384     modules="$xmodules"
385   done
386 }
387
388 # func_modules_to_filelist
389 # Input:
390 # - modules         list of modules, including dependencies
391 # Output:
392 # - files           list of files
393 func_modules_to_filelist ()
394 {
395   files=
396   for module in $modules; do
397     func_verify_module
398     if test -n "$module"; then
399       files="$files "`func_get_filelist $module`
400     fi
401   done
402   files=`for f in $files; do echo $f; done | sort | uniq`
403 }
404
405 # func_import modules
406 # Uses also the variables
407 # - destdir         target directory
408 # - libname         library name
409 # - sourcebase      directory relative to destdir where to place source code
410 # - m4base          directory relative to destdir where to place *.m4 macros
411 # - lgpl            true if library's license shall be LGPL, blank otherwise
412 # - dry_run         true if actions shall only be printed, blank otherwise
413 # - supplied_opts   all options passed to gnulib-tool
414 func_import ()
415 {
416   modules="$1"
417   modules=`for m in $modules; do echo $m; done | sort | uniq`
418
419   # Determine final module list.
420   func_modules_transitive_closure
421   echo "Module list with included dependencies:"
422   echo "$modules" | sed -e 's/^/  /'
423
424   # If --lgpl, check the license of modules are compatible.
425   if test -n "$lgpl"; then
426     for module in $modules; do
427       license=`func_get_license $module`
428       if test $license != LGPL; then
429         func_fatal_error "incompatible license on module \`$module\`: $license"
430       fi
431     done
432   fi
433
434   # Determine final file list.
435   func_modules_to_filelist
436   echo "File list:"
437   echo "$files" | sed -e 's/^/  /'
438
439   test -n "$files" \
440     || func_fatal_error "refusing to do nothing"
441
442   # Copy files.
443   for f in $files; do
444     source=
445     case "$f" in
446       config/*) g=`echo "$f" | sed -e 's,^config/,,'` ;;
447       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"`; source=true ;;
448       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
449       *) g="$f" ;;
450     esac
451     test -n "$dry_run" && dry=echo
452     $dry cp -p "$gnulib_dir/$f" "$destdir/$g"
453     # Update license.
454     if test -z "$dry_run" -a -n "$lgpl" -a -n "$source"; then
455       perl -pi -e 's/GNU General/GNU Lesser General/g;' \
456                -e 's/version 2([ ,])/version 2.1\1/g' \
457                $destdir/$g
458     fi
459   done
460
461   # Commands printed in a comment in generated files.
462   cmd="gnulib-tool $supplied_opts"
463   opt_libtool=
464   if test -n "$libtool"; then
465     opt_libtool="--libtool"
466   fi
467   opt_lgpl=
468   if test -n "$lgpl"; then
469     opt_lgpl="--lgpl"
470   fi
471   actioncmd="gnulib-tool --import --dir=$destdir --lib=$libname --source-base=$sourcebase --m4-base=$m4base $opt_libtool $opt_lgpl `echo $modules`"
472
473   # Create lib/Makefile.am.
474   echo "Creating $destdir/$sourcebase/Makefile.am..."
475   if test -n "$libtool"; then
476     libext=la
477     perhapsLT=LT
478   else
479     libext=a
480     perhapsLT=
481   fi
482   (
483     if test -z "$dry_run"; then
484       exec > $destdir/$sourcebase/Makefile.am
485     else
486       echo "# $destdir/$sourcebase/Makefile.am"
487     fi
488     echo "## Process this file with automake to produce Makefile.in."
489     echo "# Copyright (C) 2004 Free Software Foundation, Inc."
490     echo "#"
491     echo "# This file is free software, distributed under the terms of the GNU"
492     echo "# General Public License.  As a special exception to the GNU General"
493     echo "# Public License, this file may be distributed as part of a program"
494     echo "# that contains a configuration script generated by Automake, under"
495     echo "# the same distribution terms as the rest of that program."
496     echo "#"
497     echo "# Generated by gnulib-tool."
498     echo "#"
499     echo "# Invoked as: $cmd"
500     echo "# Reproduce by: $actioncmd"
501     echo
502     echo "AUTOMAKE_OPTIONS = 1.8 gnits"
503     echo
504     echo "noinst_${perhapsLT}LIBRARIES = $libname.$libext"
505     echo
506     echo "${libname}_${libext}_SOURCES ="
507     echo "${libname}_${libext}_LIBADD = @${perhapsLT}LIBOBJS@"
508     echo "EXTRA_DIST ="
509     echo "BUILT_SOURCES ="
510     echo "SUFFIXES ="
511     echo "MOSTLYCLEANFILES ="
512     echo "CLEANFILES ="
513     echo "DISTCLEANFILES ="
514     echo "MAINTAINERCLEANFILES ="
515     for module in $modules; do
516       func_verify_module
517       if test -n "$module"; then
518         func_get_automake_snippet "$module" | sed -e "s,lib_SOURCES,${libname}_${libext}_SOURCES,g" -e "s,lib_OBJECTS,${libname}_${libext}_OBJECTS,g"
519         if test "$module" = 'alloca'; then
520           echo "${libname}_${libext}_LIBADD += @ALLOCA@"
521         fi
522       fi
523     done
524     echo
525     echo "# Makefile.am ends here"
526   )
527
528   # Create gnulib.m4.
529   echo "Creating $destdir/$m4base/gnulib.m4..."
530   (
531     if test -z "$dry_run"; then
532       exec > $destdir/$m4base/gnulib.m4
533     else
534       echo "# $destdir/$m4base/gnulib.m4"
535     fi
536     echo "# Copyright (C) 2004 Free Software Foundation, Inc."
537     echo "# This file is free software, distributed under the terms of the GNU"
538     echo "# General Public License.  As a special exception to the GNU General"
539     echo "# Public License, this file may be distributed as part of a program"
540     echo "# that contains a configuration script generated by Autoconf, under"
541     echo "# the same distribution terms as the rest of that program."
542     echo "#"
543     echo "# Generated by gnulib-tool."
544     echo "#"
545     echo "# Invoked as: $cmd"
546     echo "# Reproduce by: $actioncmd"
547     echo
548     echo "AC_DEFUN([gl_EARLY],"
549     echo "["
550     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 > /dev/null; then
551       echo "  AC_GNU_SOURCE"
552     fi
553     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 > /dev/null; then
554       echo "  gl_USE_SYSTEM_EXTENSIONS"
555     fi
556     echo "])"
557     echo
558     echo "AC_DEFUN([gl_INIT],"
559     echo "["
560     for module in $modules; do
561       func_verify_module
562       if test -n "$module"; then
563         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./'
564       fi
565     done
566     echo "])"
567     echo
568     echo "dnl Usage: gl_MODULES(module1 module2 ...)"
569     echo "AC_DEFUN([gl_MODULES], [])"
570     echo
571     echo "dnl Usage: gl_SOURCE_BASE(DIR)"
572     echo "AC_DEFUN([gl_SOURCE_BASE], [])"
573     echo
574     echo "dnl Usage: gl_M4_BASE(DIR)"
575     echo "AC_DEFUN([gl_M4_BASE], [])"
576     echo
577     echo "dnl Usage: gl_LIB(LIBNAME)"
578     echo "AC_DEFUN([gl_LIB], [])"
579     echo
580     echo "dnl Usage: gl_LGPL"
581     echo "AC_DEFUN([gl_LGPL], [])"
582     echo
583     echo "# gnulib.m4 ends here"
584   )
585   echo "Finished."
586   echo
587   echo "Don't forget to add \"$sourcebase/Makefile\""
588   echo "to AC_CONFIG_FILES in \"$configure_ac\" and to mention"
589   echo "\"`basename $sourcebase`\" in SUBDIRS in some Makefile.am."
590 }
591
592 # func_create_testdir testdir modules
593 func_create_testdir ()
594 {
595   testdir="$1"
596   modules="$2"
597   modules=`for m in $modules; do echo $m; done | sort | uniq`
598
599   # Determine final module list.
600   func_modules_transitive_closure
601   echo "Module list with included dependencies:"
602   echo "$modules" | sed -e 's/^/  /'
603
604   # Determine final file list.
605   func_modules_to_filelist
606   echo "File list:"
607   echo "$files" | sed -e 's/^/  /'
608
609   # Create directories.
610   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
611     if test "$d" != config; then
612       mkdir -p "$testdir/$d"
613     fi
614   done
615
616   # Copy files.
617   for f in $files; do
618     case "$f" in
619       config/*) g=`echo "$f" | sed -e 's,^config/,,'` ;;
620       *) g="$f" ;;
621     esac
622     ln "$gnulib_dir/$f" "$testdir/$g" 2>/dev/null ||
623     cp -p "$gnulib_dir/$f" "$testdir/$g"
624   done
625
626   # Create lib/Makefile.am.
627   mkdir -p "$testdir/lib"
628   (echo "## Process this file with automake to produce Makefile.in."
629    echo
630    echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
631    echo
632    echo "noinst_LIBRARIES = $libname.a"
633    echo
634    echo "$libname"'_a_SOURCES ='
635    echo "$libname"'_a_LIBADD = @LIBOBJS@'
636    echo '#'"$libname"'_la_LIBADD = @LTLIBOBJS@'
637    echo "EXTRA_DIST ="
638    echo "BUILT_SOURCES ="
639    echo "SUFFIXES ="
640    echo "MOSTLYCLEANFILES ="
641    echo "CLEANFILES ="
642    echo "DISTCLEANFILES ="
643    echo "MAINTAINERCLEANFILES ="
644    for module in $modules; do
645      func_verify_module
646      if test -n "$module"; then
647        func_get_automake_snippet "$module" | sed -e "s,lib_SOURCES,$libname"'_a_SOURCES,g' -e "s,lib_OBJECTS,$libname"'_a_OBJECTS,g'
648        if test "$module" = 'alloca'; then
649          echo "$libname"'_a_LIBADD += @ALLOCA@'
650          echo '#'"$libname"'_la_LIBADD += @LTALLOCA@'
651        fi
652      fi
653    done
654   ) > "$testdir/lib/Makefile.am"
655
656   # Create m4/Makefile.am.
657   mkdir -p "$testdir/m4"
658   (echo "## Process this file with automake to produce Makefile.in."
659    echo
660    echo "EXTRA_DIST ="
661    for f in $files; do
662      case "$f" in
663        m4/* )
664          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
665      esac
666    done
667   ) > "$testdir/m4/Makefile.am"
668
669   subdirs="lib m4"
670
671   if test -f "$testdir"/m4/gettext.m4; then
672     # Avoid stupid error message from automake:
673     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
674     mkdir -p "$testdir/po"
675     (echo "## Process this file with automake to produce Makefile.in."
676     ) > "$testdir/po/Makefile.am"
677     subdirs="$subdirs po"
678   fi
679
680   # Create Makefile.am.
681   (echo "## Process this file with automake to produce Makefile.in."
682    echo
683    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
684    echo
685    echo "SUBDIRS = $subdirs"
686    echo
687    echo "ACLOCAL_AMFLAGS = -I m4"
688   ) > "$testdir/Makefile.am"
689
690   # Create configure.ac.
691   (echo "# Process this file with autoconf to produce a configure script."
692    echo "AC_INIT(dummy,0)"
693    echo "AM_INIT_AUTOMAKE"
694    echo
695    echo "AM_CONFIG_HEADER(config.h)"
696    echo
697    echo "AC_PROG_CC"
698    echo "AC_PROG_INSTALL"
699    echo "AC_PROG_MAKE_SET"
700    echo "AC_PROG_RANLIB"
701    echo
702    if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 > /dev/null; then
703      echo "AC_GNU_SOURCE"
704      echo
705    fi
706    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 > /dev/null; then
707      echo "gl_USE_SYSTEM_EXTENSIONS"
708      echo
709    fi
710    for module in $modules; do
711      func_verify_module
712      if test -n "$module"; then
713        func_get_autoconf_snippet "$module"
714      fi
715    done
716    echo
717    makefiles="Makefile"
718    for d in $subdirs; do
719      makefiles="$makefiles $d/Makefile"
720    done
721    echo "AC_OUTPUT([$makefiles])"
722   ) > "$testdir/configure.ac"
723
724   # Create autogenerated files.
725   (cd "$testdir"
726    echo "executing ${AUTORECONF} --force --install"
727    ${AUTORECONF} --force --install
728   )
729   if grep '^BUILT_SOURCES *+=' "$testdir/lib/Makefile.am" > /dev/null; then
730     (cd "$testdir"
731      ./configure
732        cd lib
733        built_sources=`grep '^BUILT_SOURCES *=' Makefile.in | sed -e 's/^BUILT_SOURCES *=//'`
734        if test -n "$built_sources"; then
735          make $built_sources
736        fi
737        cd ..
738      make distclean
739     )
740   fi
741 }
742
743 # func_create_megatestdir megatestdir allmodules
744 func_create_megatestdir ()
745 {
746   megatestdir="$1"
747   allmodules="$2"
748   if test -z "$allmodules"; then
749     allmodules=`func_all_modules`
750   fi
751
752   megasubdirs=
753   # First, all modules one by one.
754   for onemodule in $allmodules; do
755     func_create_testdir "$megatestdir/$onemodule" $onemodule
756     megasubdirs="${megasubdirs}$onemodule "
757   done
758   # Then, all modules all together.
759   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
760   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
761   func_create_testdir "$megatestdir/ALL" "$allmodules"
762   megasubdirs="${megasubdirs}ALL"
763
764   # Create Makefile.am.
765   (echo "## Process this file with automake to produce Makefile.in."
766    echo
767    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
768    echo
769    echo "SUBDIRS = $megasubdirs"
770   ) > "$megatestdir/Makefile.am"
771
772   # Create configure.ac.
773   (echo "# Process this file with autoconf to produce a configure script."
774    echo "AC_INIT(dummy,0)"
775    echo "AM_INIT_AUTOMAKE"
776    echo
777    echo "AC_PROG_MAKE_SET"
778    echo
779    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
780    echo "AC_OUTPUT([Makefile])"
781   ) > "$megatestdir/configure.ac"
782
783   # Create autogenerated files.
784   (cd "$megatestdir"
785    echo "executing ${AUTORECONF} --force --install"
786    ${AUTORECONF} --force --install
787   )
788 }
789
790 case $mode in
791   "" )
792     func_fatal_error "no mode specified" ;;
793
794   list )
795     func_all_modules
796     ;;
797
798   import )
799     # Where to import.
800     if test -z "$destdir"; then
801       destdir=.
802     fi
803     test -d "$destdir" \
804       || func_fatal_error "destination directory does not exist: $destdir"
805
806     # Prefer configure.ac to configure.in
807     test -f $destdir/configure.in && configure_ac=$destdir/configure.in
808     test -f $destdir/configure.ac && configure_ac=$destdir/configure.ac
809     test -f "$configure_ac" \
810       || func_fatal_error "cannot find $destdir/configure.ac"
811
812     # Get settings.
813     my_sed_traces='s,#.*$,,; s,^dnl .*$,,; s, dnl .*$,,;
814       /gl_MODULES[^_]/  {
815         s,^.*gl_MODULES([[   ]*\([^])]*\).*$,ac_modules="\1",; p;
816       };
817       /gl_SOURCE_BASE/   {
818         s,^.*gl_SOURCE_BASE([[         ]*\([^])]*\).*$,ac_sourcebase="\1",; p;
819       };
820       /gl_M4_BASE/   {
821         s,^.*gl_M4_BASE([[         ]*\([^])]*\).*$,ac_m4base="\1",; p;
822       };
823       /gl_LIB/   {
824         s,^.*gl_LIB([[         ]*\([^])]*\).*$,ac_libname="\1",; p;
825       };
826       /A[CM]_PROG_LIBTOOL/ { s,^.*$,seen_libtool=:,; p; };
827       /LT_INIT/            { s,^.*$,seen_libtool=:,; p; };
828       /gl_LGPL/            { s,^.*$,lgpl=true,; p; };
829       d;'
830     eval `cat $configure_ac | sed "$my_sed_traces"`
831
832     # Override libname?
833     if test -z "$supplied_libname" && test -n "$ac_libname"; then
834       libname="$ac_libname"
835     fi
836
837     # Set up source base.
838     test -z "$sourcebase" && sourcebase="$ac_sourcebase"
839     test -z "$sourcebase" && sourcebase="lib"
840     test -d "$destdir/$sourcebase" \
841       || (test -z "$dry_run" && mkdir "$destdir/$sourcebase") \
842       || func_fatal_error "source base $destdir/$sourcebase doesn't exist"
843
844     # Set up m4 base.
845     test -z "$m4base" && m4base="$ac_m4base"
846     test -z "$m4base" && m4base="m4"
847     test -d "$destdir/$m4base" \
848       || (test -z "$dry_run" && mkdir "$destdir/$m4base") \
849       || func_fatal_error "m4 base $destdir/$m4base doesn't exist"
850
851     # Using libtool?
852     if test -n "$seen_libtool"; then
853       libtool=true
854     fi
855
856     # What modules to extract.
857     if test $# = 0; then
858       modules="$ac_modules"
859     else
860       modules="$*"
861     fi
862
863     func_import "$modules"
864     ;;
865
866   create-testdir )
867     if test -z "$destdir"; then
868       func_fatal_error "please specify --dir option"
869     fi
870     mkdir "$destdir"
871     test -d "$destdir" \
872       || func_fatal_error "could not create destination directory"
873     func_create_testdir "$destdir" "$*"
874     ;;
875
876   create-megatestdir )
877     if test -z "$destdir"; then
878       func_fatal_error "please specify --dir option"
879     fi
880     mkdir "$destdir" || func_fatal_error "could not create destination directory"
881     func_create_megatestdir "$destdir" "$*"
882     ;;
883
884   test )
885     test -n "$destdir" || destdir=testdir$$
886     mkdir "$destdir" || func_fatal_error "could not create destination directory"
887     func_create_testdir "$destdir" "$*"
888     cd "$destdir"
889       mkdir build
890       cd build
891         ../configure
892         make
893         make check
894         make distclean
895         remaining=`find . -type f -print`
896         if test -n "$remaining"; then
897           echo "Remaining files:" $remaining 1>&2
898           echo "gnulib-tool: *** Stop." 1>&2
899           exit 1
900         fi
901       cd ..
902     cd ..
903     rm -rf "$destdir"
904     ;;
905
906   megatest )
907     test -n "$destdir" || destdir=testdir$$
908     mkdir "$destdir" || func_fatal_error "could not create destination directory"
909     func_create_megatestdir "$destdir" "$*"
910     cd "$destdir"
911       mkdir build
912       cd build
913         ../configure
914         make
915         make check
916         make distclean
917         remaining=`find . -type f -print`
918         if test -n "$remaining"; then
919           echo "Remaining files:" $remaining 1>&2
920           echo "gnulib-tool: *** Stop." 1>&2
921           exit 1
922         fi
923       cd ..
924     cd ..
925     rm -rf "$destdir"
926     ;;
927
928   extract-description )
929     for module
930     do
931       func_verify_module
932       if test -n "$module"; then
933         func_get_description "$module"
934       fi
935     done
936     ;;
937
938   extract-filelist )
939     for module
940     do
941       func_verify_module
942       if test -n "$module"; then
943         func_get_filelist "$module"
944       fi
945     done
946     ;;
947
948   extract-dependencies )
949     for module
950     do
951       func_verify_module
952       if test -n "$module"; then
953         func_get_dependencies "$module"
954       fi
955     done
956     ;;
957
958   extract-autoconf-snippet )
959     for module
960     do
961       func_verify_module
962       if test -n "$module"; then
963         func_get_autoconf_snippet "$module"
964       fi
965     done
966     ;;
967
968   extract-automake-snippet )
969     for module
970     do
971       func_verify_module
972       if test -n "$module"; then
973         func_get_automake_snippet "$module"
974       fi
975     done
976     ;;
977
978   extract-include-directive )
979     for module
980     do
981       func_verify_module
982       if test -n "$module"; then
983         func_get_include_directive "$module"
984       fi
985     done
986     ;;
987
988   extract-license )
989     for module
990     do
991       func_verify_module
992       if test -n "$module"; then
993         func_get_license "$module"
994       fi
995     done
996     ;;
997
998   extract-maintainer )
999     for module
1000     do
1001       func_verify_module
1002       if test -n "$module"; then
1003         func_get_maintainer "$module"
1004       fi
1005     done
1006     ;;
1007
1008   * )
1009     func_fatal_error "unknown operation mode --$mode" ;;
1010 esac
1011
1012 exit 0