ac9b0bf247122c71a2b489a6defb1a766aa25b58
[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-07-11 11:37:09 $'
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') ;;
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_SOURCE_BASE(DIR)"
724     echo "AC_DEFUN([gl_SOURCE_BASE], [])"
725     echo
726     echo "dnl Usage: gl_M4_BASE(DIR)"
727     echo "AC_DEFUN([gl_M4_BASE], [])"
728     echo
729     echo "dnl Usage: gl_LIB(LIBNAME)"
730     echo "AC_DEFUN([gl_LIB], [])"
731     echo
732     echo "dnl Usage: gl_LGPL"
733     echo "AC_DEFUN([gl_LGPL], [])"
734     echo
735     echo "# gnulib.m4 ends here"
736   )
737   func_mv_if_changed $destdir/$m4base/gnulib.m4.new $destdir/$m4base/gnulib.m4
738   echo "Finished."
739   echo
740   echo "You may need to add #include directives for the following .h files."
741   for module in $modules; do
742     func_get_include_directive "$module"
743   done | LC_ALL=C sort -u | sed -e '/^$/d;' -e 's/^/  /'
744   echo
745   echo "Don't forget to add \"$sourcebase/Makefile\""
746   echo "to AC_CONFIG_FILES in \"$configure_ac\" and to mention"
747   echo "\"`basename $sourcebase`\" in SUBDIRS in some Makefile.am."
748 }
749
750 # func_create_testdir testdir modules
751 func_create_testdir ()
752 {
753   testdir="$1"
754   modules="$2"
755   modules=`for m in $modules; do echo $m; done | sort | uniq`
756
757   # Determine final module list.
758   func_modules_transitive_closure
759   echo "Module list with included dependencies:"
760   echo "$modules" | sed -e 's/^/  /'
761
762   # Determine final file list.
763   func_modules_to_filelist
764   echo "File list:"
765   echo "$files" | sed -e 's/^/  /'
766
767   # Create directories.
768   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
769     if test "$d" = build-aux; then
770       mkdir -p "$testdir/$auxdir"
771     else
772       mkdir -p "$testdir/$d"
773     fi
774   done
775
776   # Copy files or make symbolic links.
777   for f in $files; do
778     case "$f" in
779       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
780       *) g="$f" ;;
781     esac
782     ln "$gnulib_dir/$f" "$testdir/$g" 2>/dev/null ||
783     if test -z "$symbolic"; then
784       cp -p "$gnulib_dir/$f" "$testdir/$g"
785     else
786       ln -s "$gnulib_dir/$f" "$testdir/$g"
787     fi
788   done
789
790   # Create lib/Makefile.am.
791   mkdir -p "$testdir/lib"
792   func_emit_lib_Makefile_am > "$testdir/lib/Makefile.am"
793
794   # Create m4/Makefile.am.
795   mkdir -p "$testdir/m4"
796   (echo "## Process this file with automake to produce Makefile.in."
797    echo
798    echo "EXTRA_DIST ="
799    for f in $files; do
800      case "$f" in
801        m4/* )
802          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
803      esac
804    done
805   ) > "$testdir/m4/Makefile.am"
806
807   subdirs="lib m4"
808
809   if test -f "$testdir"/m4/gettext.m4; then
810     # Avoid stupid error message from automake:
811     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
812     mkdir -p "$testdir/po"
813     (echo "## Process this file with automake to produce Makefile.in."
814     ) > "$testdir/po/Makefile.am"
815     subdirs="$subdirs po"
816   fi
817
818   # Create Makefile.am.
819   (echo "## Process this file with automake to produce Makefile.in."
820    echo
821    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
822    echo
823    echo "SUBDIRS = $subdirs"
824    echo
825    echo "ACLOCAL_AMFLAGS = -I m4"
826   ) > "$testdir/Makefile.am"
827
828   # Create configure.ac.
829   (echo "# Process this file with autoconf to produce a configure script."
830    echo "AC_INIT(dummy,0)"
831    echo "AM_INIT_AUTOMAKE"
832    echo
833    echo "AM_CONFIG_HEADER(config.h)"
834    echo
835    echo "AC_PROG_CC"
836    echo "AC_PROG_INSTALL"
837    echo "AC_PROG_MAKE_SET"
838    echo "AC_PROG_RANLIB"
839    echo
840    if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 > /dev/null; then
841      echo "AC_GNU_SOURCE"
842      echo
843    fi
844    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 > /dev/null; then
845      echo "gl_USE_SYSTEM_EXTENSIONS"
846      echo
847    fi
848    for module in $modules; do
849      func_verify_module
850      if test -n "$module"; then
851        func_get_autoconf_snippet "$module"
852      fi
853    done
854    echo
855    makefiles="Makefile"
856    for d in $subdirs; do
857      makefiles="$makefiles $d/Makefile"
858    done
859    echo "AC_OUTPUT([$makefiles])"
860   ) > "$testdir/configure.ac"
861
862   # Create autogenerated files.
863   (cd "$testdir"
864    echo "executing ${AUTORECONF} --force --install"
865    ${AUTORECONF} --force --install
866   )
867   if grep '^BUILT_SOURCES *+=' "$testdir/lib/Makefile.am" > /dev/null; then
868     (cd "$testdir"
869      ./configure
870        cd lib
871        built_sources=`grep '^BUILT_SOURCES *=' Makefile.in | sed -e 's/^BUILT_SOURCES *=//'`
872        if test -n "$built_sources"; then
873          make $built_sources
874        fi
875        cd ..
876      make distclean
877     )
878   fi
879 }
880
881 # func_create_megatestdir megatestdir allmodules
882 func_create_megatestdir ()
883 {
884   megatestdir="$1"
885   allmodules="$2"
886   if test -z "$allmodules"; then
887     allmodules=`func_all_modules`
888   fi
889
890   megasubdirs=
891   # First, all modules one by one.
892   for onemodule in $allmodules; do
893     func_create_testdir "$megatestdir/$onemodule" $onemodule
894     megasubdirs="${megasubdirs}$onemodule "
895   done
896   # Then, all modules all together.
897   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
898   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
899   func_create_testdir "$megatestdir/ALL" "$allmodules"
900   megasubdirs="${megasubdirs}ALL"
901
902   # Create Makefile.am.
903   (echo "## Process this file with automake to produce Makefile.in."
904    echo
905    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
906    echo
907    echo "SUBDIRS = $megasubdirs"
908   ) > "$megatestdir/Makefile.am"
909
910   # Create configure.ac.
911   (echo "# Process this file with autoconf to produce a configure script."
912    echo "AC_INIT(dummy,0)"
913    echo "AM_INIT_AUTOMAKE"
914    echo
915    echo "AC_PROG_MAKE_SET"
916    echo
917    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
918    echo "AC_OUTPUT([Makefile])"
919   ) > "$megatestdir/configure.ac"
920
921   # Create autogenerated files.
922   (cd "$megatestdir"
923    echo "executing ${AUTORECONF} --force --install"
924    ${AUTORECONF} --force --install
925   )
926 }
927
928 case $mode in
929   "" )
930     func_fatal_error "no mode specified" ;;
931
932   list )
933     func_all_modules
934     ;;
935
936   import )
937     # Where to import.
938     if test -z "$destdir"; then
939       destdir=.
940     fi
941     test -d "$destdir" \
942       || func_fatal_error "destination directory does not exist: $destdir"
943
944     # Prefer configure.ac to configure.in
945     test -f $destdir/configure.in && configure_ac=$destdir/configure.in
946     test -f $destdir/configure.ac && configure_ac=$destdir/configure.ac
947     test -f "$configure_ac" \
948       || func_fatal_error "cannot find $destdir/configure.ac"
949
950     # Get settings.
951     my_sed_traces='s,#.*$,,; s,^dnl .*$,,; s, dnl .*$,,;
952       /gl_MODULES[^_]/  {
953         s,^.*gl_MODULES([[   ]*\([^])]*\).*$,ac_modules="\1",; p;
954       };
955       /gl_SOURCE_BASE/   {
956         s,^.*gl_SOURCE_BASE([[         ]*\([^])]*\).*$,ac_sourcebase="\1",; p;
957       };
958       /gl_M4_BASE/   {
959         s,^.*gl_M4_BASE([[         ]*\([^])]*\).*$,ac_m4base="\1",; p;
960       };
961       /gl_LIB/   {
962         s,^.*gl_LIB([[         ]*\([^])]*\).*$,ac_libname="\1",; p;
963       };
964       /AC_CONFIG_AUX_DIR/  {
965         s,^.*AC_CONFIG_AUX_DIR([[         ]*\([^])]*\).*$,ac_auxdir="\1",; p;
966       }
967       /A[CM]_PROG_LIBTOOL/ { s,^.*$,seen_libtool=:,; p; };
968       /LT_INIT/            { s,^.*$,seen_libtool=:,; p; };
969       /gl_LGPL/            { s,^.*$,lgpl=true,; p; };
970       d;'
971     eval `cat $configure_ac | sed "$my_sed_traces"`
972
973     # Override libname?
974     if test -z "$supplied_libname" && test -n "$ac_libname"; then
975       libname="$ac_libname"
976     fi
977
978     # Set up source base.
979     test -z "$sourcebase" && sourcebase="$ac_sourcebase"
980     test -z "$sourcebase" && sourcebase="lib"
981     test -d "$destdir/$sourcebase" \
982       || (test -z "$dry_run" && mkdir "$destdir/$sourcebase") \
983       || func_fatal_error "source base $destdir/$sourcebase doesn't exist"
984
985     # Set up m4 base.
986     test -z "$m4base" && m4base="$ac_m4base"
987     test -z "$m4base" && m4base="m4"
988     test -d "$destdir/$m4base" \
989       || (test -z "$dry_run" && mkdir "$destdir/$m4base") \
990       || func_fatal_error "m4 base $destdir/$m4base doesn't exist"
991
992     # Set up auxiliary directory.
993     test -z "$auxdir" && auxdir="$ac_auxdir"
994     test -z "$auxdir" && auxdir="."
995     test -d "$destdir/$auxdir" \
996       || (test -z "$dry_run" && mkdir "$destdir/$auxdir") \
997       || func_fatal_error "aux directory $destdir/$auxdir doesn't exist"
998
999     # Using libtool?
1000     if test -n "$seen_libtool"; then
1001       libtool=true
1002     fi
1003
1004     # What modules to extract.
1005     if test $# = 0; then
1006       modules="$ac_modules"
1007     else
1008       modules="$*"
1009     fi
1010
1011     func_import "$modules"
1012     ;;
1013
1014   create-testdir )
1015     if test -z "$destdir"; then
1016       func_fatal_error "please specify --dir option"
1017     fi
1018     mkdir "$destdir"
1019     test -d "$destdir" \
1020       || func_fatal_error "could not create destination directory"
1021     func_create_testdir "$destdir" "$*"
1022     ;;
1023
1024   create-megatestdir )
1025     if test -z "$destdir"; then
1026       func_fatal_error "please specify --dir option"
1027     fi
1028     mkdir "$destdir" || func_fatal_error "could not create destination directory"
1029     func_create_megatestdir "$destdir" "$*"
1030     ;;
1031
1032   test )
1033     test -n "$destdir" || destdir=testdir$$
1034     mkdir "$destdir" || func_fatal_error "could not create destination directory"
1035     func_create_testdir "$destdir" "$*"
1036     cd "$destdir"
1037       mkdir build
1038       cd build
1039         ../configure
1040         make
1041         make check
1042         make distclean
1043         remaining=`find . -type f -print`
1044         if test -n "$remaining"; then
1045           echo "Remaining files:" $remaining 1>&2
1046           echo "gnulib-tool: *** Stop." 1>&2
1047           exit 1
1048         fi
1049       cd ..
1050     cd ..
1051     rm -rf "$destdir"
1052     ;;
1053
1054   megatest )
1055     test -n "$destdir" || destdir=testdir$$
1056     mkdir "$destdir" || func_fatal_error "could not create destination directory"
1057     func_create_megatestdir "$destdir" "$*"
1058     cd "$destdir"
1059       mkdir build
1060       cd build
1061         ../configure
1062         make
1063         make check
1064         make distclean
1065         remaining=`find . -type f -print`
1066         if test -n "$remaining"; then
1067           echo "Remaining files:" $remaining 1>&2
1068           echo "gnulib-tool: *** Stop." 1>&2
1069           exit 1
1070         fi
1071       cd ..
1072     cd ..
1073     rm -rf "$destdir"
1074     ;;
1075
1076   extract-description )
1077     for module
1078     do
1079       func_verify_module
1080       if test -n "$module"; then
1081         func_get_description "$module"
1082       fi
1083     done
1084     ;;
1085
1086   extract-filelist )
1087     for module
1088     do
1089       func_verify_module
1090       if test -n "$module"; then
1091         func_get_filelist "$module"
1092       fi
1093     done
1094     ;;
1095
1096   extract-dependencies )
1097     for module
1098     do
1099       func_verify_module
1100       if test -n "$module"; then
1101         func_get_dependencies "$module"
1102       fi
1103     done
1104     ;;
1105
1106   extract-autoconf-snippet )
1107     for module
1108     do
1109       func_verify_module
1110       if test -n "$module"; then
1111         func_get_autoconf_snippet "$module"
1112       fi
1113     done
1114     ;;
1115
1116   extract-automake-snippet )
1117     for module
1118     do
1119       func_verify_module
1120       if test -n "$module"; then
1121         func_get_automake_snippet "$module"
1122       fi
1123     done
1124     ;;
1125
1126   extract-include-directive )
1127     for module
1128     do
1129       func_verify_module
1130       if test -n "$module"; then
1131         func_get_include_directive "$module"
1132       fi
1133     done
1134     ;;
1135
1136   extract-license )
1137     for module
1138     do
1139       func_verify_module
1140       if test -n "$module"; then
1141         func_get_license "$module"
1142       fi
1143     done
1144     ;;
1145
1146   extract-maintainer )
1147     for module
1148     do
1149       func_verify_module
1150       if test -n "$module"; then
1151         func_get_maintainer "$module"
1152       fi
1153     done
1154     ;;
1155
1156   * )
1157     func_fatal_error "unknown operation mode --$mode" ;;
1158 esac
1159
1160 exit 0