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