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