Move a lot of code into function func_import().
[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:28:43 $'
26 last_checkin_date=`echo "$cvsdatestamp" | sed -e 's,^\$[D]ate: ,,'`
27 version=`echo "$last_checkin_date" | sed -e 's/ .*$//' -e 's,/,-,g'`
28
29 # You can set AUTOCONFPATH to empty if autoconf 2.57 is already in your PATH.
30 AUTOCONFPATH=
31 #case $USER in
32 #  bruno )
33 #    AUTOCONFBINDIR=/packages/gnu-inst-autoconf/2.57/bin
34 #    AUTOCONFPATH="eval env PATH=${AUTOCONFBINDIR}:\$PATH "
35 #    ;;
36 #esac
37
38 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_import modules
361 # Uses also the variables
362 # - destdir         target directory
363 # - libname         library name
364 # - sourcebase      directory relative to destdir where to place source code
365 # - m4base          directory relative to destdir where to place *.m4 macros
366 # - lgpl            true if library's license shall be LGPL, blank otherwise
367 # - dry_run         true if actions shall only be printed, blank otherwise
368 # - supplied_opts   all options passed to gnulib-tool
369 func_import ()
370 {
371   modules="$1"
372   modules=`for m in $modules; do echo $m; done | sort | uniq`
373
374   # Determine final module list.
375   while true; do
376     xmodules=
377     for module in $modules; do
378       func_verify_module
379       if test -n "$module"; then
380         # Duplicate dependenies are harmless, but Jim wants a warning.
381         duplicated_deps=`func_get_dependencies $module | sort | uniq -d`
382         if test -n "$duplicated_deps"; then
383           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
384         fi
385         xmodules="$xmodules $module "`func_get_dependencies $module`
386       fi
387     done
388     xmodules=`for m in $xmodules; do echo $m; done | sort | uniq`
389     if test "$xmodules" = "$modules"; then
390       break
391     fi
392     modules="$xmodules"
393   done
394   echo "Module list with included dependencies:"
395   echo "$modules" | sed -e 's/^/  /'
396
397   # If --lgpl, check the license of modules are compatible.
398   if test -n "$lgpl"; then
399     for module in $modules; do
400       license=`sed -n -e "/^License$sed_extract_prog" < "$gnulib_dir/modules/$module"`
401       if test $license != LGPL; then
402         func_fatal_error "incompatible license on module \`$module\`: $license"
403       fi
404     done
405   fi
406
407   # Determine final file list.
408   files=
409   for module in $modules; do
410     func_verify_module
411     if test -n "$module"; then
412       files="$files "`func_get_filelist $module`
413     fi
414   done
415   files=`for f in $files; do echo $f; done | sort | uniq`
416   echo "File list:"
417   echo "$files" | sed -e 's/^/  /'
418
419   test -n "$files" \
420     || func_fatal_error "refusing to do nothing"
421
422   # Copy files.
423   for f in $files; do
424     source=
425     case "$f" in
426       config/*) g=`echo "$f" | sed -e 's,^config/,,'` ;;
427       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"`; source=true ;;
428       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
429       *) g="$f" ;;
430     esac
431     test -n "$dry_run" && dry=echo
432     $dry cp -p "$gnulib_dir/$f" "$destdir/$g"
433     # Update license.
434     if test -z "$dry_run" -a -n "$lgpl" -a -n "$source"; then
435       perl -pi -e 's/GNU General/GNU Lesser General/g;' \
436                -e 's/version 2([ ,])/version 2.1\1/g' \
437                $destdir/$g
438     fi
439   done
440
441   # Commands printed in a comment in generated files.
442   cmd="gnulib-tool $supplied_opts"
443   opt_libtool=
444   if test -n "$libtool"; then
445     opt_libtool="--libtool"
446   fi
447   if test -n "$lgpl"; then
448     opt_lgpl="--lgpl"
449   fi
450   actioncmd="gnulib-tool --import --dir=$destdir --lib=$libname --source-base=$sourcebase --m4-base=$m4base $opt_libtool $opt_lgpl `echo $modules`"
451
452   # Create lib/Makefile.am.
453   echo "Creating $destdir/$sourcebase/Makefile.am..."
454   if test -n "$libtool"; then
455     libext=la
456     perhapsLT=LT
457   else
458     libext=a
459     perhapsLT=
460   fi
461   (
462     if test -z "$dry_run"; then
463       exec > $destdir/$sourcebase/Makefile.am
464     else
465       echo "# $destdir/$sourcebase/Makefile.am"
466     fi
467     echo "## Process this file with automake to produce Makefile.in."
468     echo "# Copyright (C) 2004 Free Software Foundation, Inc."
469     echo "#"
470     echo "# This file is free software, distributed under the terms of the GNU"
471     echo "# General Public License.  As a special exception to the GNU General"
472     echo "# Public License, this file may be distributed as part of a program"
473     echo "# that contains a configuration script generated by Automake, under"
474     echo "# the same distribution terms as the rest of that program."
475     echo "#"
476     echo "# Generated by gnulib-tool."
477     echo "#"
478     echo "# Invoked as: $cmd"
479     echo "# Reproduce by: $actioncmd"
480     echo
481     echo "AUTOMAKE_OPTIONS = 1.8 gnits"
482     echo
483     echo "noinst_${perhapsLT}LIBRARIES = $libname.$libext"
484     echo
485     echo "${libname}_${libext}_SOURCES ="
486     echo "${libname}_${libext}_LIBADD = @${perhapsLT}LIBOBJS@"
487     echo "EXTRA_DIST ="
488     echo "BUILT_SOURCES ="
489     echo "SUFFIXES ="
490     echo "MOSTLYCLEANFILES ="
491     echo "CLEANFILES ="
492     echo "DISTCLEANFILES ="
493     echo "MAINTAINERCLEANFILES ="
494     for module in $modules; do
495       func_verify_module
496       if test -n "$module"; then
497         func_get_automake_snippet "$module" | sed -e "s,lib_SOURCES,${libname}_${libext}_SOURCES,g" -e "s,lib_OBJECTS,${libname}_${libext}_OBJECTS,g"
498         if test "$module" = 'alloca'; then
499           echo "${libname}_${libext}_LIBADD += @ALLOCA@"
500         fi
501       fi
502     done
503     echo
504     echo "# Makefile.am ends here"
505   )
506
507   # Create gnulib.m4.
508   echo "Creating $destdir/$m4base/gnulib.m4..."
509   (
510     if test -z "$dry_run"; then
511       exec > $destdir/$m4base/gnulib.m4
512     else
513       echo "# $destdir/$m4base/gnulib.m4"
514     fi
515     echo "# Copyright (C) 2004 Free Software Foundation, Inc."
516     echo "# This file is free software, distributed under the terms of the GNU"
517     echo "# General Public License.  As a special exception to the GNU General"
518     echo "# Public License, this file may be distributed as part of a program"
519     echo "# that contains a configuration script generated by Autoconf, under"
520     echo "# the same distribution terms as the rest of that program."
521     echo "#"
522     echo "# Generated by gnulib-tool."
523     echo "#"
524     echo "# Invoked as: $cmd"
525     echo "# Reproduce by: $actioncmd"
526     echo
527     echo "AC_DEFUN([gl_EARLY],"
528     echo "["
529     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 > /dev/null; then
530       echo "  AC_GNU_SOURCE"
531     fi
532     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 > /dev/null; then
533       echo "  gl_USE_SYSTEM_EXTENSIONS"
534     fi
535     echo "])"
536     echo
537     echo "AC_DEFUN([gl_INIT],"
538     echo "["
539     for module in $modules; do
540       func_verify_module
541       if test -n "$module"; then
542         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./'
543       fi
544     done
545     echo "])"
546     echo
547     echo "dnl Usage: gl_MODULES(module1 module2 ...)"
548     echo "AC_DEFUN([gl_MODULES], [])"
549     echo
550     echo "dnl Usage: gl_SOURCE_BASE(DIR)"
551     echo "AC_DEFUN([gl_SOURCE_BASE], [])"
552     echo
553     echo "dnl Usage: gl_M4_BASE(DIR)"
554     echo "AC_DEFUN([gl_M4_BASE], [])"
555     echo
556     echo "dnl Usage: gl_LIB(LIBNAME)"
557     echo "AC_DEFUN([gl_LIB], [])"
558     echo
559     echo "dnl Usage: gl_LGPL"
560     echo "AC_DEFUN([gl_LGPL], [])"
561     echo
562     echo "# gnulib.m4 ends here"
563   )
564   echo "Finished."
565   echo
566   echo "Don't forget to add \"$sourcebase/Makefile\""
567   echo "to AC_CONFIG_FILES in \"$configure_ac\" and to mention"
568   echo "\"`basename $sourcebase`\" in SUBDIRS in some Makefile.am."
569 }
570
571 # func_create_testdir testdir modules
572 func_create_testdir ()
573 {
574   testdir="$1"
575   modules="$2"
576   modules=`for m in $modules; do echo $m; done | sort | uniq`
577
578   # Determine final module list.
579   while true; do
580     xmodules=
581     for module in $modules; do
582       func_verify_module
583       if test -n "$module"; then
584         # Duplicate dependenies are harmless, but Jim wants a warning.
585         duplicated_deps=`func_get_dependencies $module | sort | uniq -d`
586         if test -n "$duplicated_deps"; then
587           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
588         fi
589         xmodules="$xmodules $module "`func_get_dependencies $module`
590       fi
591     done
592     xmodules=`for m in $xmodules; do echo $m; done | sort | uniq`
593     if test "$xmodules" = "$modules"; then
594       break
595     fi
596     modules="$xmodules"
597   done
598   echo "Module list with included dependencies:"
599   echo "$modules" | sed -e 's/^/  /'
600
601   # Determine final file list.
602   files=
603   for module in $modules; do
604     func_verify_module
605     if test -n "$module"; then
606       files="$files "`func_get_filelist $module`
607     fi
608   done
609   files=`for f in $files; do echo $f; done | sort | uniq`
610   echo "File list:"
611   echo "$files" | sed -e 's/^/  /'
612
613   # Create directories.
614   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
615     if test "$d" != config; then
616       mkdir -p "$testdir/$d"
617     fi
618   done
619
620   # Copy files.
621   for f in $files; do
622     case "$f" in
623       config/*) g=`echo "$f" | sed -e 's,^config/,,'` ;;
624       *) g="$f" ;;
625     esac
626     ln "$gnulib_dir/$f" "$testdir/$g" 2>/dev/null ||
627     cp -p "$gnulib_dir/$f" "$testdir/$g"
628   done
629
630   # Create lib/Makefile.am.
631   mkdir -p "$testdir/lib"
632   (echo "## Process this file with automake to produce Makefile.in."
633    echo
634    echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
635    echo
636    echo "noinst_LIBRARIES = $libname.a"
637    echo
638    echo "$libname"'_a_SOURCES ='
639    echo "$libname"'_a_LIBADD = @LIBOBJS@'
640    echo '#'"$libname"'_la_LIBADD = @LTLIBOBJS@'
641    echo "EXTRA_DIST ="
642    echo "BUILT_SOURCES ="
643    echo "SUFFIXES ="
644    echo "MOSTLYCLEANFILES ="
645    echo "CLEANFILES ="
646    echo "DISTCLEANFILES ="
647    echo "MAINTAINERCLEANFILES ="
648    for module in $modules; do
649      func_verify_module
650      if test -n "$module"; then
651        func_get_automake_snippet "$module" | sed -e "s,lib_SOURCES,$libname"'_a_SOURCES,g' -e "s,lib_OBJECTS,$libname"'_a_OBJECTS,g'
652        if test "$module" = 'alloca'; then
653          echo "$libname"'_a_LIBADD += @ALLOCA@'
654          echo '#'"$libname"'_la_LIBADD += @LTALLOCA@'
655        fi
656      fi
657    done
658   ) > "$testdir/lib/Makefile.am"
659
660   # Create m4/Makefile.am.
661   mkdir -p "$testdir/m4"
662   (echo "## Process this file with automake to produce Makefile.in."
663    echo
664    echo "EXTRA_DIST ="
665    for f in $files; do
666      case "$f" in
667        m4/* )
668          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
669      esac
670    done
671   ) > "$testdir/m4/Makefile.am"
672
673   subdirs="lib m4"
674
675   if test -f "$testdir"/m4/gettext.m4; then
676     # Avoid stupid error message from automake:
677     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
678     mkdir -p "$testdir/po"
679     (echo "## Process this file with automake to produce Makefile.in."
680     ) > "$testdir/po/Makefile.am"
681     subdirs="$subdirs po"
682   fi
683
684   # Create Makefile.am.
685   (echo "## Process this file with automake to produce Makefile.in."
686    echo
687    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
688    echo
689    echo "SUBDIRS = $subdirs"
690    echo
691    echo "ACLOCAL_AMFLAGS = -I m4"
692   ) > "$testdir/Makefile.am"
693
694   # Create configure.ac.
695   (echo "# Process this file with autoconf to produce a configure script."
696    echo "AC_INIT(dummy,0)"
697    echo "AM_INIT_AUTOMAKE"
698    echo
699    echo "AM_CONFIG_HEADER(config.h)"
700    echo
701    echo "AC_PROG_CC"
702    echo "AC_PROG_INSTALL"
703    echo "AC_PROG_MAKE_SET"
704    echo "AC_PROG_RANLIB"
705    echo
706    if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 > /dev/null; then
707      echo "AC_GNU_SOURCE"
708      echo
709    fi
710    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 > /dev/null; then
711      echo "gl_USE_SYSTEM_EXTENSIONS"
712      echo
713    fi
714    for module in $modules; do
715      func_verify_module
716      if test -n "$module"; then
717        func_get_autoconf_snippet "$module"
718      fi
719    done
720    echo
721    makefiles="Makefile"
722    for d in $subdirs; do
723      makefiles="$makefiles $d/Makefile"
724    done
725    echo "AC_OUTPUT([$makefiles])"
726   ) > "$testdir/configure.ac"
727
728   # Create autogenerated files.
729   (cd "$testdir"
730    echo "executing ${AUTORECONF} --force --install"
731    ${AUTORECONF} --force --install
732   )
733   if grep '^BUILT_SOURCES *+=' "$testdir/lib/Makefile.am" > /dev/null; then
734     (cd "$testdir"
735      ./configure
736        cd lib
737        built_sources=`grep '^BUILT_SOURCES *=' Makefile.in | sed -e 's/^BUILT_SOURCES *=//'`
738        if test -n "$built_sources"; then
739          make $built_sources
740        fi
741        cd ..
742      make distclean
743     )
744   fi
745 }
746
747 # func_create_megatestdir megatestdir allmodules
748 func_create_megatestdir ()
749 {
750   megatestdir="$1"
751   allmodules="$2"
752   if test -z "$allmodules"; then
753     allmodules=`func_all_modules`
754   fi
755
756   megasubdirs=
757   # First, all modules one by one.
758   for onemodule in $allmodules; do
759     func_create_testdir "$megatestdir/$onemodule" $onemodule
760     megasubdirs="${megasubdirs}$onemodule "
761   done
762   # Then, all modules all together.
763   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
764   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
765   func_create_testdir "$megatestdir/ALL" "$allmodules"
766   megasubdirs="${megasubdirs}ALL"
767
768   # Create Makefile.am.
769   (echo "## Process this file with automake to produce Makefile.in."
770    echo
771    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
772    echo
773    echo "SUBDIRS = $megasubdirs"
774   ) > "$megatestdir/Makefile.am"
775
776   # Create configure.ac.
777   (echo "# Process this file with autoconf to produce a configure script."
778    echo "AC_INIT(dummy,0)"
779    echo "AM_INIT_AUTOMAKE"
780    echo
781    echo "AC_PROG_MAKE_SET"
782    echo
783    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
784    echo "AC_OUTPUT([Makefile])"
785   ) > "$megatestdir/configure.ac"
786
787   # Create autogenerated files.
788   (cd "$megatestdir"
789    echo "executing ${AUTORECONF} --force --install"
790    ${AUTORECONF} --force --install
791   )
792 }
793
794 case $mode in
795   "" )
796     func_fatal_error "no mode specified" ;;
797
798   list )
799     func_all_modules
800     ;;
801
802   import )
803     # Where to import.
804     if test -z "$destdir"; then
805       destdir=.
806     fi
807     test -d "$destdir" \
808       || func_fatal_error "destination directory does not exist: $destdir"
809
810     # Prefer configure.ac to configure.in
811     test -f $destdir/configure.in && configure_ac=$destdir/configure.in
812     test -f $destdir/configure.ac && configure_ac=$destdir/configure.ac
813     test -f "$configure_ac" \
814       || func_fatal_error "cannot find $destdir/configure.ac"
815
816     # Get settings.
817     my_sed_traces='s,#.*$,,; s,^dnl .*$,,; s, dnl .*$,,;
818       /gl_MODULES[^_]/  {
819         s,^.*gl_MODULES([[   ]*\([^])]*\).*$,ac_modules="\1",; p;
820       };
821       /gl_SOURCE_BASE/   {
822         s,^.*gl_SOURCE_BASE([[         ]*\([^])]*\).*$,ac_sourcebase="\1",; p;
823       };
824       /gl_M4_BASE/   {
825         s,^.*gl_M4_BASE([[         ]*\([^])]*\).*$,ac_m4base="\1",; p;
826       };
827       /gl_LIB/   {
828         s,^.*gl_LIB([[         ]*\([^])]*\).*$,ac_libname="\1",; p;
829       };
830       /A[CM]_PROG_LIBTOOL/ { s,^.*$,seen_libtool=:,; p; };
831       /LT_INIT/            { s,^.*$,seen_libtool=:,; p; };
832       /gl_LGPL/            { s,^.*$,lgpl=true,; p; };
833       d;'
834     eval `cat $configure_ac | sed "$my_sed_traces"`
835
836     # Override libname?
837     if test -z "$supplied_libname" && test -n "$ac_libname"; then
838       libname="$ac_libname"
839     fi
840
841     # Set up source base.
842     test -z "$sourcebase" && sourcebase="$ac_sourcebase"
843     test -z "$sourcebase" && sourcebase="lib"
844     test -d "$destdir/$sourcebase" \
845       || (test -z "$dry_run" && mkdir "$destdir/$sourcebase") \
846       || func_fatal_error "source base $destdir/$sourcebase doesn't exist"
847
848     # Set up m4 base.
849     test -z "$m4base" && m4base="$ac_m4base"
850     test -z "$m4base" && m4base="m4"
851     test -d "$destdir/$m4base" \
852       || (test -z "$dry_run" && mkdir "$destdir/$m4base") \
853       || func_fatal_error "m4 base $destdir/$m4base doesn't exist"
854
855     # Using libtool?
856     if test -n "$seen_libtool"; then
857       libtool=true
858     fi
859
860     # What modules to extract.
861     if test $# = 0; then
862       modules="$ac_modules"
863     else
864       modules="$*"
865     fi
866
867     func_import "$modules"
868     ;;
869
870   create-testdir )
871     if test -z "$destdir"; then
872       func_fatal_error "please specify --dir option"
873     fi
874     mkdir "$destdir"
875     test -d "$destdir" \
876       || func_fatal_error "could not create destination directory"
877     func_create_testdir "$destdir" "$*"
878     ;;
879
880   create-megatestdir )
881     if test -z "$destdir"; then
882       func_fatal_error "please specify --dir option"
883     fi
884     mkdir "$destdir" || func_fatal_error "could not create destination directory"
885     func_create_megatestdir "$destdir" "$*"
886     ;;
887
888   test )
889     test -n "$destdir" || destdir=testdir$$
890     mkdir "$destdir" || func_fatal_error "could not create destination directory"
891     func_create_testdir "$destdir" "$*"
892     cd "$destdir"
893       mkdir build
894       cd build
895         ../configure
896         make
897         make check
898         make distclean
899         remaining=`find . -type f -print`
900         if test -n "$remaining"; then
901           echo "Remaining files:" $remaining 1>&2
902           echo "gnulib-tool: *** Stop." 1>&2
903           exit 1
904         fi
905       cd ..
906     cd ..
907     rm -rf "$destdir"
908     ;;
909
910   megatest )
911     test -n "$destdir" || destdir=testdir$$
912     mkdir "$destdir" || func_fatal_error "could not create destination directory"
913     func_create_megatestdir "$destdir" "$*"
914     cd "$destdir"
915       mkdir build
916       cd build
917         ../configure
918         make
919         make check
920         make distclean
921         remaining=`find . -type f -print`
922         if test -n "$remaining"; then
923           echo "Remaining files:" $remaining 1>&2
924           echo "gnulib-tool: *** Stop." 1>&2
925           exit 1
926         fi
927       cd ..
928     cd ..
929     rm -rf "$destdir"
930     ;;
931
932   extract-description )
933     for module
934     do
935       func_verify_module
936       if test -n "$module"; then
937         func_get_description "$module"
938       fi
939     done
940     ;;
941
942   extract-filelist )
943     for module
944     do
945       func_verify_module
946       if test -n "$module"; then
947         func_get_filelist "$module"
948       fi
949     done
950     ;;
951
952   extract-dependencies )
953     for module
954     do
955       func_verify_module
956       if test -n "$module"; then
957         func_get_dependencies "$module"
958       fi
959     done
960     ;;
961
962   extract-autoconf-snippet )
963     for module
964     do
965       func_verify_module
966       if test -n "$module"; then
967         func_get_autoconf_snippet "$module"
968       fi
969     done
970     ;;
971
972   extract-automake-snippet )
973     for module
974     do
975       func_verify_module
976       if test -n "$module"; then
977         func_get_automake_snippet "$module"
978       fi
979     done
980     ;;
981
982   extract-include-directive )
983     for module
984     do
985       func_verify_module
986       if test -n "$module"; then
987         func_get_include_directive "$module"
988       fi
989     done
990     ;;
991
992   extract-license )
993     for module
994     do
995       func_verify_module
996       if test -n "$module"; then
997         func_get_license "$module"
998       fi
999     done
1000     ;;
1001
1002   extract-maintainer )
1003     for module
1004     do
1005       func_verify_module
1006       if test -n "$module"; then
1007         func_get_maintainer "$module"
1008       fi
1009     done
1010     ;;
1011
1012   * )
1013     func_fatal_error "unknown operation mode --$mode" ;;
1014 esac
1015
1016 exit 0