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