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