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