01819009c147db6c91a5862199e8824a6a02b7bc
[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-09-19 15:32:51 $'
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        gnulib-tool --extract-tests-module module
60
61 Operation modes:
62       --list                print the available module names
63       --import              import the given modules into the current package
64       --create-testdir      create a scratch package with the given modules
65       --create-megatestdir  create a mega scratch package with the given modules
66                             one by one and all together
67       --test                test the combination of the given modules
68                             (recommended to use CC=\"gcc -Wall\" here)
69       --megatest            test the given modules one by one and all together
70                             (recommended to use CC=\"gcc -Wall\" here)
71       --extract-description        extract the description
72       --extract-filelist           extract the list of files
73       --extract-dependencies       extract the dependencies
74       --extract-autoconf-snippet   extract the snippet for configure.ac
75       --extract-automake-snippet   extract the snippet for lib/Makefile.am
76       --extract-include-directive  extract the #include directive
77       --extract-license            report the license terms of the source files
78                                    under lib/
79       --extract-maintainer         report the maintainer(s) inside gnulib
80       --extract-tests-module       report the unit test module, if it exists
81
82 Options:
83       --dir=DIRECTORY       specify the target directory
84                             For --import, this specifies where your
85                             configure.ac can be found.  Defaults to current
86                             directory.
87       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
88       --source-base=DIRECTORY
89                             Directory relative --dir where source code is
90                             placed (default \"lib\"), for --import.
91       --m4-base=DIRECTORY   Directory relative --dir where *.m4 macros are
92                             placed (default \"m4\"), for --import.
93       --tests-base=DIRECTORY
94                             Directory relative --dir where unit tests are
95                             placed (default \"tests\"), for --import.
96       --aux-dir=DIRECTORY   Directory relative --dir where auxiliary build
97                             tools are placed (default \"build-aux\").
98       --with-tests          Include unit tests for the included modules.
99       --avoid=MODULE        Avoid including the given MODULE. Useful if you
100                             have code that provides equivalent functionality.
101                             This option can be repeated.
102       --lgpl                Abort if modules aren't available under the LGPL.
103                             Also modify license template from GPL to LGPL.
104       --libtool             Use libtool rules, for --import.
105       --macro-prefix=PREFIX  Specify the prefix of the macros 'gl_EARLY' and
106                             'gl_INIT'. Default is 'gl'.
107       --no-changelog        don't update or create ChangeLog files
108       --dry-run             For --import, only print what would have been done.
109   -s, --symbolic, --symlink Make symbolic links instead of copying files.
110
111 Report bugs to <bug-gnulib@gnu.org>."
112 }
113
114 # func_version
115 # outputs to stdout the --version message.
116 func_version ()
117 {
118   echo "$progname (GNU $package) $version"
119   echo "Copyright (C) 2002-2005 Free Software Foundation, Inc.
120 This is free software; see the source for copying conditions.  There is NO
121 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
122   echo "Written by" "Bruno Haible" "and" "Simon Josefsson"
123 }
124
125 # func_tmpdir
126 # creates a temporary directory.
127 # Sets variable
128 # - tmp             pathname of freshly created temporary directory
129 func_tmpdir ()
130 {
131   # Use the environment variable TMPDIR, falling back to /tmp. This allows
132   # users to specify a different temporary directory, for example, if their
133   # /tmp is filled up or too small.
134   : ${TMPDIR=/tmp}
135   {
136     # Use the mktemp program if available. If not available, hide the error
137     # message.
138     tmp=`(umask 077 && mktemp -d -q "$TMPDIR/glXXXXXX") 2>/dev/null` &&
139     test -n "$tmp" && test -d "$tmp"
140   } ||
141   {
142     # Use a simple mkdir command. It is guaranteed to fail if the directory
143     # already exists.  $RANDOM is bash specific and expands to empty in shells
144     # other than bash, ksh and zsh.  Its use does not increase security;
145     # rather, it minimizes the probability of failure in a very cluttered /tmp
146     # directory.
147     tmp=$TMPDIR/gl$$-$RANDOM
148     (umask 077 && mkdir "$tmp")
149   } ||
150   {
151     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
152     { (exit 1); exit 1; }
153   }
154 }
155
156 # func_fatal_error message
157 # outputs to stderr a fatal error message, and terminates the program.
158 func_fatal_error ()
159 {
160   echo "gnulib-tool: *** $1" 1>&2
161   echo "gnulib-tool: *** Stop." 1>&2
162   exit 1
163 }
164
165 # func_readlink SYMLINK
166 # outputs the target of the given symlink.
167 if (type -p readlink) > /dev/null 2>&1; then
168   func_readlink ()
169   {
170     # Use the readlink program from GNU coreutils.
171     readlink "$1"
172   }
173 else
174   func_readlink ()
175   {
176     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
177     # would do the wrong link if the link target contains " -> ".
178     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
179   }
180 fi
181
182 # func_ln_if_changed SRC DEST
183 # Like ln -s, but avoids munging timestamps if the link is correct.
184 func_ln_if_changed ()
185 {
186   if test $# -ne 2; then
187     echo "usage: func_ln_if_changed SRC DEST" >&2
188   fi
189   if test -L "$2" && test "$1" = "`func_readlink "$2"`"; then
190     :
191   else
192     rm -f "$2"
193     ln -s "$1" "$2"
194   fi
195 }
196
197 # Command-line option processing.
198 # Removes the OPTIONS from the arguments. Sets the variables:
199 # - mode            list or import or create-testdir or create-megatestdir
200 # - destdir         from --dir
201 # - libname, supplied_libname  from --lib
202 # - sourcebase      from --source-base
203 # - m4base          from --m4-base
204 # - testsbase       from --tests-base
205 # - auxdir          from --aux-dir
206 # - inctests        true if --with-tests was given, blank otherwise
207 # - avoidlist       list of modules to avoid, from --avoid
208 # - lgpl            true if --lgpl was given, blank otherwise
209 # - libtool         true if --libtool was given, blank otherwise
210 # - do_changelog    false if --no-changelog was given, : otherwise
211 # - doit            : if actions shall be executed, false if only to be printed
212 {
213   mode=
214   destdir=
215   libname=libgnu
216   supplied_libname=
217   sourcebase=
218   m4base=
219   testsbase=
220   auxdir=
221   inctests=
222   avoidlist=
223   lgpl=
224   libtool=
225   macro_prefix=
226   do_changelog=:
227   doit=:
228   symbolic=
229
230   supplied_opts="$@"
231
232   while test $# -gt 0; do
233     case "$1" in
234       --list | --lis )
235         mode=list
236         shift ;;
237       --import | --impor | --impo | --imp | --im | --i )
238         mode=import
239         shift ;;
240       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
241         mode=create-testdir
242         shift ;;
243       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
244         mode=create-megatestdir
245         shift ;;
246       --test | --tes | --te | --t )
247         mode=test
248         shift ;;
249       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
250         mode=megatest
251         shift ;;
252       --extract-* )
253         mode=`echo "X$1" | sed -e 's/^X--//'`
254         shift ;;
255       --dir )
256         shift
257         if test $# = 0; then
258           func_fatal_error "missing argument for --dir"
259         fi
260         destdir=$1
261         shift ;;
262       --dir=* )
263         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
264         shift ;;
265       --lib )
266         shift
267         if test $# = 0; then
268           func_fatal_error "missing argument for --lib"
269         fi
270         libname=$1
271         supplied_libname=true
272         shift ;;
273       --lib=* )
274         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
275         supplied_libname=true
276         shift ;;
277       --source-base )
278         shift
279         if test $# = 0; then
280           func_fatal_error "missing argument for --source-base"
281         fi
282         sourcebase=$1
283         shift ;;
284       --source-base=* )
285         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
286         shift ;;
287       --m4-base )
288         shift
289         if test $# = 0; then
290           func_fatal_error "missing argument for --m4-base"
291         fi
292         m4base=$1
293         shift ;;
294       --m4-base=* )
295         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
296         shift ;;
297       --tests-base )
298         shift
299         if test $# = 0; then
300           func_fatal_error "missing argument for --tests-base"
301         fi
302         testsbase=$1
303         shift ;;
304       --tests-base=* )
305         testsbase=`echo "X$1" | sed -e 's/^X--tests-base=//'`
306         shift ;;
307       --aux-dir )
308         shift
309         if test $# = 0; then
310           func_fatal_error "missing argument for --aux-dir"
311         fi
312         auxdir=$1
313         shift ;;
314       --aux-dir=* )
315         auxdir=`echo "X$1" | sed -e 's/^X--aux-dir=//'`
316         shift ;;
317       --with-tests )
318         inctests=true
319         shift ;;
320       --avoid )
321         shift
322         if test $# = 0; then
323           func_fatal_error "missing argument for --avoid"
324         fi
325         avoidlist="$avoidlist $1"
326         shift ;;
327       --avoid=* )
328         avoidlist="$avoidlist "`echo "X$1" | sed -e 's/^X--avoid=//'`
329         shift ;;
330       --lgpl )
331         lgpl=true
332         shift ;;
333       --libtool )
334         libtool=true
335         shift ;;
336       --macro-prefix )
337         shift
338         if test $# = 0; then
339           func_fatal_error "missing argument for --macro-prefix"
340         fi
341         macro_prefix="$1"
342         shift ;;
343       --macro-prefix=* )
344         macro_prefix=`echo "X$1" | sed -e 's/^X--macro-prefix=//'`
345         shift ;;
346       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
347         do_changelog=false
348         shift ;;
349       --dry-run )
350         doit=false
351         shift ;;
352       -s | --symbolic | --symlink )
353         symbolic=true
354         shift ;;
355       --help | --hel | --he | --h )
356         func_usage
357         exit 0 ;;
358       --version | --versio | --versi | --vers | --ver | --ve | --v )
359         func_version
360         exit 0 ;;
361       -- )
362         # Stop option prcessing
363         shift
364         break ;;
365       -* )
366         echo "gnulib-tool: unknown option $1" 1>&2
367         echo "Try 'gnulib-tool --help' for more information." 1>&2
368         exit 1 ;;
369       * )
370         break ;;
371     esac
372   done
373 }
374
375 case "$0" in
376   /*) self_abspathname="$0" ;;
377   */*) self_abspathname=`pwd`/"$0" ;;
378   *) for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`; do
379        if test -x "$d/$0" && test ! -d "$d/$0"; then
380          self_abspathname="$d/$0"
381          break
382        fi
383      done
384      if test -z "$self_abspathname"; then
385        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
386      fi
387      ;;
388 esac
389 while test -h "$self_abspathname"; do
390   # Resolve symbolic link.
391   linkval=`func_readlink "$self_abspathname"`
392   test -n "$linkval" || break
393   case "$linkval" in
394     /* ) self_abspathname="$linkval" ;;
395     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
396   esac
397 done
398 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
399
400 # func_all_modules
401 func_all_modules ()
402 {
403   # Filter out metainformation files like README, which are not modules.
404   # Filter out unit test modules; they can be retrieved through
405   # --extract-tests-module if desired.
406   (cd "$gnulib_dir/modules" && ls -1) \
407       | sed -e '/^CVS$/d' -e '/^ChangeLog$/d' -e '/^README$/d' -e '/^TEMPLATE$/d' -e '/^TEMPLATE-TESTS$/d' -e '/~$/d' \
408       | sed -e '/-tests$/d' \
409       | sort
410 }
411
412 # func_verify_module
413 # verifies a module name
414 func_verify_module ()
415 {
416   if test ! -f "$gnulib_dir/modules/$module" \
417      || test "CVS" = "$module" \
418      || test "ChangeLog" = "$module" \
419      || test "README" = "$module" \
420      || test "TEMPLATE" = "$module" \
421      || test "TEMPLATE-TESTS" = "$module"; then
422     echo "gnulib-tool: module $module doesn't exist" 1>&2
423     module=
424   fi
425 }
426
427 # func_verify_nontests_module
428 # verifies a module name, excluding tests modules
429 func_verify_nontests_module ()
430 {
431   case "$module" in
432     *-tests ) module= ;;
433     * ) func_verify_module ;;
434   esac
435 }
436
437 # func_verify_tests_module
438 # verifies a module name, considering only tests modules
439 func_verify_tests_module ()
440 {
441   case "$module" in
442     *-tests ) func_verify_module ;;
443     * ) module= ;;
444   esac
445 }
446
447 sed_extract_prog=':[    ]*$/ {
448   :a
449     n
450     s/^Description:[    ]*$//
451     s/^Files:[  ]*$//
452     s/^Depends-on:[     ]*$//
453     s/^configure\.ac:[  ]*$//
454     s/^Makefile\.am:[   ]*$//
455     s/^Include:[        ]*$//
456     s/^License:[        ]*$//
457     s/^Maintainer:[     ]*$//
458     tb
459     p
460     ba
461   :b
462 }'
463
464 # func_get_description module
465 func_get_description ()
466 {
467   sed -n -e "/^Description$sed_extract_prog" < "$gnulib_dir/modules/$1"
468 }
469
470 # func_get_filelist module
471 func_get_filelist ()
472 {
473   sed -n -e "/^Files$sed_extract_prog" < "$gnulib_dir/modules/$1"
474   #echo m4/onceonly.m4
475   echo m4/onceonly_2_57.m4
476 }
477
478 # func_get_dependencies module
479 func_get_dependencies ()
480 {
481   # ${module}-tests always implicitly depends on ${module}.
482   echo "$1" | sed -n -e 's/-tests//p'
483   # Then the explicit dependencies listed in the module description.
484   sed -n -e "/^Depends-on$sed_extract_prog" < "$gnulib_dir/modules/$1"
485 }
486
487 # func_get_autoconf_snippet module
488 func_get_autoconf_snippet ()
489 {
490   sed -n -e "/^configure\.ac$sed_extract_prog" < "$gnulib_dir/modules/$1"
491 }
492
493 # func_get_automake_snippet module
494 func_get_automake_snippet ()
495 {
496   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$gnulib_dir/modules/$1"
497 }
498
499 # func_get_include_directive module
500 func_get_include_directive ()
501 {
502   sed -n -e "/^Include$sed_extract_prog" < "$gnulib_dir/modules/$1" | \
503   sed -e 's/^\(["<]\)/#include \1/'
504 }
505
506 # func_get_license module
507 func_get_license ()
508 {
509   sed -n -e "/^License$sed_extract_prog" < "$gnulib_dir/modules/$1"
510 }
511
512 # func_get_maintainer module
513 func_get_maintainer ()
514 {
515   sed -n -e "/^Maintainer$sed_extract_prog" < "$gnulib_dir/modules/$1"
516 }
517
518 # func_get_tests_module module
519 func_get_tests_module ()
520 {
521   # The naming convention for tests modules is hardwired: ${module}-tests.
522   if test -f modules/"$1"-tests; then
523     echo "$1"-tests
524   fi
525 }
526
527 # func_acceptable module
528 # tests whether a module is acceptable.
529 # Input:
530 # - avoidlist       list of modules to avoid
531 func_acceptable ()
532 {
533   for avoid in $avoidlist; do
534     if test "$avoid" = "$1"; then
535       return 1
536     fi
537   done
538   return 0
539 }
540
541 # func_modules_transitive_closure
542 # Input:
543 # - modules         list of specified modules
544 # - inctests        true if tests should be included, blank otherwise
545 # - avoidlist       list of modules to avoid
546 # Output:
547 # - modules         list of modules, including dependencies
548 func_modules_transitive_closure ()
549 {
550   while true; do
551     xmodules=
552     for module in $modules; do
553       func_verify_module
554       if test -n "$module"; then
555         # Duplicate dependencies are harmless, but Jim wants a warning.
556         duplicated_deps=`func_get_dependencies $module | sort | uniq -d`
557         if test -n "$duplicated_deps"; then
558           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
559         fi
560         if func_acceptable $module; then
561           xmodules="$xmodules $module"
562           for depmodule in `func_get_dependencies $module`; do
563             if func_acceptable $depmodule; then
564               xmodules="$xmodules $depmodule"
565             fi
566           done
567           if test -n "$inctests"; then
568             testsmodule=`func_get_tests_module $module`
569             if test -n "$testsmodule"; then
570               if func_acceptable $testsmodule; then
571                 xmodules="$xmodules $testsmodule"
572                 for depmodule in `func_get_dependencies $testsmodule`; do
573                   if func_acceptable $depmodule; then
574                     xmodules="$xmodules $depmodule"
575                   fi
576                 done
577               fi
578             fi
579           fi
580         fi
581       fi
582     done
583     xmodules=`for m in $xmodules; do echo $m; done | sort | uniq`
584     if test "$xmodules" = "$modules"; then
585       break
586     fi
587     modules="$xmodules"
588   done
589 }
590
591 # func_modules_to_filelist
592 # Input:
593 # - modules         list of modules, including dependencies
594 # Output:
595 # - files           list of files
596 func_modules_to_filelist ()
597 {
598   files=
599   for module in $modules; do
600     func_verify_module
601     if test -n "$module"; then
602       files="$files "`func_get_filelist $module`
603     fi
604   done
605   files=`for f in $files; do echo $f; done | sort | uniq`
606 }
607
608 # func_emit_lib_Makefile_am
609 # emits the contents of lib/Makefile.am to standard output.
610 # Input:
611 # - modules         list of modules, including dependencies
612 # - libname         library name
613 # - libtool         true if libtool will be used, blank otherwise
614 # - actioncmd       (optional) command that will reproduce this invocation
615 func_emit_lib_Makefile_am ()
616 {
617   if test -n "$libtool"; then
618     libext=la
619     perhapsLT=LT
620   else
621     libext=a
622     perhapsLT=
623   fi
624   echo "## Process this file with automake to produce Makefile.in."
625   echo "# Copyright (C) 2004 Free Software Foundation, Inc."
626   echo "#"
627   echo "# This file is free software, distributed under the terms of the GNU"
628   echo "# General Public License.  As a special exception to the GNU General"
629   echo "# Public License, this file may be distributed as part of a program"
630   echo "# that contains a configuration script generated by Automake, under"
631   echo "# the same distribution terms as the rest of that program."
632   echo "#"
633   echo "# Generated by gnulib-tool."
634   if test -n "$actioncmd"; then
635     echo "# Reproduce by: $actioncmd"
636   fi
637   echo
638   # No need to generate dependencies since the sources are in gnulib, not here.
639   echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
640   echo
641   echo "noinst_${perhapsLT}LIBRARIES = $libname.$libext"
642   echo
643   echo "${libname}_${libext}_SOURCES ="
644   echo "${libname}_${libext}_LIBADD = @${perhapsLT}LIBOBJS@"
645   echo "EXTRA_DIST ="
646   echo "BUILT_SOURCES ="
647   echo "SUFFIXES ="
648   echo "MOSTLYCLEANFILES ="
649   echo "CLEANFILES ="
650   echo "DISTCLEANFILES ="
651   echo "MAINTAINERCLEANFILES ="
652   echo
653   for module in $modules; do
654     func_verify_nontests_module
655     if test -n "$module"; then
656       {
657         func_get_automake_snippet "$module" |
658           sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g'
659         if test "$module" = 'alloca'; then
660           echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
661         fi
662       } > amsnippet.tmp
663       # Skip the contents if its entirely empty.
664       if grep '[^       ]' amsnippet.tmp > /dev/null ; then
665         echo "## begin gnulib module $module"
666         echo
667         cat amsnippet.tmp
668         echo "## end   gnulib module $module"
669         echo
670       fi
671       rm -f amsnippet.tmp
672     fi
673   done
674   echo
675   echo "# Makefile.am ends here"
676 }
677
678 # func_emit_tests_Makefile_am
679 # emits the contents of tests/Makefile.am to standard output.
680 # Input:
681 # - modules         list of modules, including dependencies
682 # - libname         library name
683 # - libtool         true if libtool will be used, blank otherwise
684 # - sourcebase      relative directory containing lib source code
685 func_emit_tests_Makefile_am ()
686 {
687   if test -n "$libtool"; then
688     libext=la
689   else
690     libext=a
691   fi
692   echo "## Process this file with automake to produce Makefile.in."
693   echo "# Copyright (C) 2004-2005 Free Software Foundation, Inc."
694   echo "#"
695   echo "# This file is free software, distributed under the terms of the GNU"
696   echo "# General Public License.  As a special exception to the GNU General"
697   echo "# Public License, this file may be distributed as part of a program"
698   echo "# that contains a configuration script generated by Automake, under"
699   echo "# the same distribution terms as the rest of that program."
700   echo "#"
701   echo "# Generated by gnulib-tool."
702   echo
703   # Generate dependencies here, since it eases the debugging of test failures.
704   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
705   echo
706   echo "ACLOCAL_AMFLAGS = -I ../m4"
707   echo
708   echo "TESTS ="
709   echo "noinst_PROGRAMS ="
710   echo "EXTRA_DIST ="
711   echo "BUILT_SOURCES ="
712   echo "SUFFIXES ="
713   echo "MOSTLYCLEANFILES ="
714   echo "CLEANFILES ="
715   echo "DISTCLEANFILES ="
716   echo "MAINTAINERCLEANFILES ="
717   echo
718   echo "AM_CPPFLAGS = \\"
719   echo "  -I. -I\$(srcdir) \\"
720   echo "  -I.. -I\$(srcdir)/.. \\"
721   echo "  -I../${sourcebase-lib} -I\$(srcdir)/../${sourcebase-lib}"
722   echo
723   echo "LDADD = ../${sourcebase-lib}/${libname}.${libext}"
724   echo
725   for module in $modules; do
726     func_verify_tests_module
727     if test -n "$module"; then
728       func_get_automake_snippet "$module" > amsnippet.tmp
729       # Skip the contents if its entirely empty.
730       if grep '[^       ]' amsnippet.tmp > /dev/null ; then
731         echo "## begin gnulib module $module"
732         echo
733         cat amsnippet.tmp
734         echo "## end   gnulib module $module"
735         echo
736       fi
737       rm -f amsnippet.tmp
738     fi
739   done
740   echo "# Clean up after Solaris cc."
741   echo "clean-local:"
742   echo "        rm -rf SunWS_cache"
743   echo
744   echo "# Makefile.am ends here"
745 }
746
747 # func_import modules
748 # Uses also the variables
749 # - destdir         target directory
750 # - libname         library name
751 # - sourcebase      directory relative to destdir where to place source code
752 # - m4base          directory relative to destdir where to place *.m4 macros
753 # - auxdir          directory relative to destdir where to place build aux files
754 # - avoidlist       list of modules to avoid, from --avoid
755 # - lgpl            true if library's license shall be LGPL, blank otherwise
756 # - libtool         true if libtool will be used, blank otherwise
757 # - guessed_libtool true if the configure.ac file uses libtool, blank otherwise
758 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
759 # - doit            : if actions shall be executed, false if only to be printed
760 # - symbolic        true if files should be symlinked, copied otherwise
761 func_import ()
762 {
763   # Get the cached settings.
764   cached_specified_modules=
765   cached_avoidlist=
766   cached_sourcebase=
767   cached_m4base=
768   cached_libname=
769   cached_lgpl=
770   cached_libtool=
771   cached_macro_prefix=
772   cached_files=
773   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
774     my_sed_traces='
775       s,#.*$,,
776       s,^dnl .*$,,
777       s, dnl .*$,,
778       /gl_MODULES(/ {
779         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
780       }
781       /gl_AVOID(/ {
782         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
783       }
784       /gl_SOURCE_BASE(/ {
785         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
786       }
787       /gl_M4_BASE(/ {
788         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
789       }
790       /gl_LIB(/ {
791         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
792       }
793       /gl_LGPL/ {
794         s,^.*$,cached_lgpl=true,p
795       }
796       /gl_LIBTOOL/ {
797         s,^.*$,cached_libtool=true,p
798       }
799       /gl_MACRO_PREFIX(/ {
800         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
801       }'
802     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
803     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
804       my_sed_traces='
805         s,#.*$,,
806         s,^dnl .*$,,
807         s, dnl .*$,,
808         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
809           s,^.*$,cached_files=",p
810           n
811           ta
812           :a
813           s,^\]).*$,",
814           tb
815           p
816           n
817           ba
818           :b
819           p
820         }'
821       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
822     fi
823   fi
824
825   # Merge the cached settings with the specified ones.
826   # The m4base must be the same as expected from the pathname.
827   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
828     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
829   fi
830   # Append the cached and the specified module names. So that
831   # "gnulib-tool --import foo" means to add the module foo.
832   specified_modules="$cached_specified_modules $1"
833   # Append the cached and the specified avoidlist. This is probably better
834   # than dropping the cached one when --avoid is specified at least once.
835   avoidlist=`echo $cached_avoidlist $avoidlist`
836   # The sourcebase defaults to the cached one.
837   if test -z "$sourcebase"; then
838     sourcebase="$cached_sourcebase"
839     if test -z "$sourcebase"; then
840       func_fatal_error "missing --source-base option"
841     fi
842   fi
843   # The libname defaults to the cached one.
844   if test -z "$supplied_libname"; then
845     libname="$cached_libname"
846     if test -z "$libname"; then
847       func_fatal_error "missing --lib option"
848     fi
849   fi
850   # Require LGPL if specified either way.
851   if test -z "$lgpl"; then
852     lgpl="$cached_lgpl"
853   fi
854   # Use libtool if specified either way, or if guessed.
855   if test -z "$libtool"; then
856     if test -n "$cached_m4base"; then
857       libtool="$cached_libtool"
858     else
859       libtool="$guessed_libtool"
860     fi
861   fi
862   # The macro_prefix defaults to the cached one.
863   if test -z "$macro_prefix"; then
864     macro_prefix="$cached_macro_prefix"
865     if test -z "$macro_prefix"; then
866       func_fatal_error "missing --macro-prefix option"
867     fi
868   fi
869
870   # Canonicalize the list of specified modules.
871   specified_modules=`for m in $specified_modules; do echo $m; done | sort | uniq`
872
873   # Determine final module list.
874   modules="$specified_modules"
875   func_modules_transitive_closure
876   echo "Module list with included dependencies:"
877   echo "$modules" | sed -e 's/^/  /'
878
879   # If --lgpl, check the license of modules are compatible.
880   if test -n "$lgpl"; then
881     for module in $modules; do
882       license=`func_get_license $module`
883       case $license in
884         LGPL | 'public domain' | 'unlimited') ;;
885         *) func_fatal_error "incompatible license on module $module: $license" ;;
886       esac
887     done
888   fi
889
890   # Determine final file list.
891   func_modules_to_filelist
892   echo "File list:"
893   echo "$files" | sed -e 's/^/  /'
894
895   test -n "$files" \
896     || func_fatal_error "refusing to do nothing"
897
898   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
899   new_files="$files m4/gnulib-tool.m4"
900   old_files="$cached_files"
901   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
902     old_files="$old_files m4/gnulib-tool.m4"
903   fi
904
905   # Create directories.
906   if test ! -d "$destdir/$sourcebase"; then
907     if $doit; then
908       echo "Creating directory $destdir/$sourcebase"
909       mkdir "$destdir/$sourcebase" || func_fatal_error "failed"
910     else
911       echo "Create directory $destdir/$sourcebase"
912     fi
913   fi
914   if test ! -d "$destdir/$m4base"; then
915     if $doit; then
916       echo "Creating directory $destdir/$m4base"
917       mkdir "$destdir/$m4base" || func_fatal_error "failed"
918     else
919       echo "Create directory $destdir/$m4base"
920     fi
921   fi
922   if test ! -d "$destdir/$auxdir"; then
923     if $doit; then
924       echo "Creating directory $destdir/$auxdir"
925       mkdir "$destdir/$auxdir" || func_fatal_error "failed"
926     else
927       echo "Create directory $destdir/$auxdir"
928     fi
929   fi
930
931   # Copy files or make symbolic links. Remove obsolete files.
932   func_tmpdir
933   trap 'rm -rf "$tmp"' 0 1 2 3 15
934   delimiter='   '
935   for f in $old_files; do
936     case "$f" in
937       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
938       lib/*) g=`echo "$f" | sed -e "s,^lib/,$cached_sourcebase/,"` ;;
939       m4/*) g=`echo "$f" | sed -e "s,^m4/,$cached_m4base/,"` ;;
940       *) g="$f" ;;
941     esac
942     echo "$g""$delimiter""$f"
943   done | LC_ALL=C sort > "$tmp"/old-files
944   for f in $new_files; do
945     case "$f" in
946       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
947       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
948       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
949       *) g="$f" ;;
950     esac
951     echo "$g""$delimiter""$f"
952   done | LC_ALL=C sort > "$tmp"/new-files
953   # First the files that are in old-files, but not in new-files:
954   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e 's,'"$delimiter"'.*,,'`; do
955     # Remove the file. Do nothing if the user already removed it.
956     if test -f "$destdir/$g"; then
957       if $doit; then
958         echo "Removing file $g (backup in ${g}~)"
959         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
960       else
961         echo "Remove file $g (backup in ${g}~)"
962       fi
963     fi
964   done
965   # func_add_or_update handles a file that ought to be present afterwards.
966   # Uses parameters f, g, already_present.
967   func_add_or_update ()
968   {
969     cp "$gnulib_dir/$f" "$destdir/$g.tmp" || func_fatal_error "failed"
970     if test -n "$lgpl"; then
971       # Update license.
972       case "$f" in
973         lib/*)
974           sed -e 's/GNU General/GNU Lesser General/g' \
975               -e 's/version 2\([ ,]\)/version 2.1\1/g' \
976             < "$gnulib_dir/$f" > "$destdir/$g.tmp" || func_fatal_error "failed"
977           ;;
978       esac
979     fi
980     if test -f "$destdir/$g"; then
981       # The file already exists.
982       if cmp "$destdir/$g" "$destdir/$g.tmp" > /dev/null; then
983         : # The file has not changed.
984       else
985         # Replace the file.
986         if $doit; then
987           if test -n "$already_present"; then
988             echo "Updating file $g (backup in ${g}~)"
989           else
990             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
991           fi
992           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
993           if test -n "$symbolic" && cmp "$gnulib_dir/$f" "$destdir/$g.tmp" > /dev/null; then
994             func_ln_if_changed "$gnulib_dir/$f" "$destdir/$g"
995           else
996             mv -f "$destdir/$g.tmp" "$destdir/${g}" || func_fatal_error "failed"
997           fi
998         else
999           if test -n "$already_present"; then
1000             echo "Update file $g (backup in ${g}~)"
1001           else
1002             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1003           fi
1004         fi
1005       fi
1006     else
1007       # Install the file.
1008       # Don't protest if the file should be there but isn't: it happens
1009       # frequently that developers don't put autogenerated files into CVS.
1010       if $doit; then
1011         echo "Copying file $g"
1012         if test -n "$symbolic" && cmp "$gnulib_dir/$f" "$destdir/$g.tmp" > /dev/null; then
1013           func_ln_if_changed "$gnulib_dir/$f" "$destdir/$g"
1014         else
1015           mv -f "$destdir/$g.tmp" "$destdir/${g}" || func_fatal_error "failed"
1016         fi
1017       else
1018         echo "Copy file $g"
1019       fi
1020     fi
1021     rm -f "$destdir/$g.tmp"
1022   }
1023   # Then the files that are in new-files, but not in old-files:
1024   already_present=
1025   for f in `LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files | sed -e 's,'^.*"$delimiter"',,'`; do
1026     case "$f" in
1027       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1028       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1029       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1030       *) g="$f" ;;
1031     esac
1032     func_add_or_update
1033   done
1034   # Then the files that are in new-files and in old-files:
1035   already_present=true
1036   for f in `LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files | sed -e 's,'^.*"$delimiter"',,'`; do
1037     case "$f" in
1038       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1039       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1040       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1041       *) g="$f" ;;
1042     esac
1043     func_add_or_update
1044   done
1045   rm -rf "$tmp"
1046   trap - 0 1 2 3 15
1047
1048   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1049   actioncmd="gnulib-tool --import"
1050   actioncmd="$actioncmd --dir=$destdir"
1051   actioncmd="$actioncmd --lib=$libname"
1052   actioncmd="$actioncmd --source-base=$sourcebase"
1053   actioncmd="$actioncmd --m4-base=$m4base"
1054   actioncmd="$actioncmd --aux-dir=$auxdir"
1055   for module in $avoidlist; do
1056     actioncmd="$actioncmd --avoid=$module"
1057   done
1058   if test -n "$lgpl"; then
1059     actioncmd="$actioncmd --lgpl"
1060   fi
1061   if test -n "$libtool"; then
1062     actioncmd="$actioncmd --libtool"
1063   fi
1064   actioncmd="$actioncmd --macro-prefix=$macro_prefix"
1065   actioncmd="$actioncmd `echo $specified_modules`"
1066
1067   # Create lib/Makefile.am.
1068   func_emit_lib_Makefile_am > "$destdir"/$sourcebase/Makefile.am.tmp
1069   if test -f "$destdir"/$sourcebase/Makefile.am; then
1070     if cmp "$destdir"/$sourcebase/Makefile.am "$destdir"/$sourcebase/Makefile.am.tmp > /dev/null; then
1071       rm -f "$destdir"/$sourcebase/Makefile.am.tmp
1072     else
1073       if $doit; then
1074         echo "Updating $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1075         mv -f "$destdir"/$sourcebase/Makefile.am "$destdir"/$sourcebase/Makefile.am~
1076         mv -f "$destdir"/$sourcebase/Makefile.am.tmp "$destdir"/$sourcebase/Makefile.am
1077       else
1078         echo "Update $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1079         rm -f "$destdir"/$sourcebase/Makefile.am.tmp
1080       fi
1081     fi
1082   else
1083     if $doit; then
1084       echo "Creating $sourcebase/Makefile.am"
1085       mv -f "$destdir"/$sourcebase/Makefile.am.tmp "$destdir"/$sourcebase/Makefile.am
1086     else
1087       echo "Create $sourcebase/Makefile.am"
1088       rm -f "$destdir"/$sourcebase/Makefile.am.tmp
1089     fi
1090   fi
1091
1092   # Create m4/gnulib-cache.m4.
1093   (
1094     echo "# Copyright (C) 2004 Free Software Foundation, Inc."
1095     echo "# This file is free software, distributed under the terms of the GNU"
1096     echo "# General Public License.  As a special exception to the GNU General"
1097     echo "# Public License, this file may be distributed as part of a program"
1098     echo "# that contains a configuration script generated by Autoconf, under"
1099     echo "# the same distribution terms as the rest of that program."
1100     echo "#"
1101     echo "# Generated by gnulib-tool."
1102     echo "#"
1103     echo "# This file represents the specification of how gnulib-tool is used."
1104     echo "# It acts as a cache: It is written and read by gnulib-tool."
1105     echo "# In projects using CVS, this file is meant to be stored in CVS,"
1106     echo "# like the configure.ac and various Makefile.am files."
1107     echo
1108     echo
1109     echo "# Specification in the form of a command-line invocation:"
1110     echo "#   $actioncmd"
1111     echo
1112     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
1113     echo "gl_MODULES(["`echo $specified_modules`"])"
1114     echo "gl_AVOID([$avoidlist])"
1115     echo "gl_SOURCE_BASE([$sourcebase])"
1116     echo "gl_M4_BASE([$m4base])"
1117     echo "gl_LIB([$libname])"
1118     test -z "$lgpl" || echo "gl_LGPL"
1119     test -z "$libtool" || echo "gl_LIBTOOL"
1120     echo "gl_MACRO_PREFIX([$macro_prefix])"
1121   ) > "$destdir"/$m4base/gnulib-cache.m4.tmp
1122   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1123     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4.tmp > /dev/null; then
1124       rm -f "$destdir"/$m4base/gnulib-cache.m4.tmp
1125     else
1126       if $doit; then
1127         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1128         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
1129         mv -f "$destdir"/$m4base/gnulib-cache.m4.tmp "$destdir"/$m4base/gnulib-cache.m4
1130       else
1131         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1132         if false; then
1133           cat "$destdir"/$m4base/gnulib-cache.m4.tmp
1134           echo
1135           echo "# gnulib-cache.m4 ends here"
1136         fi
1137         rm -f "$destdir"/$m4base/gnulib-cache.m4.tmp
1138       fi
1139     fi
1140   else
1141     if $doit; then
1142       echo "Creating $m4base/gnulib-cache.m4"
1143       mv -f "$destdir"/$m4base/gnulib-cache.m4.tmp "$destdir"/$m4base/gnulib-cache.m4
1144     else
1145       echo "Create $m4base/gnulib-cache.m4"
1146       cat "$destdir"/$m4base/gnulib-cache.m4.tmp
1147       rm -f "$destdir"/$m4base/gnulib-cache.m4.tmp
1148     fi
1149   fi
1150
1151   # Create m4/gnulib-comp.m4.
1152   (
1153     echo "# Copyright (C) 2004 Free Software Foundation, Inc."
1154     echo "# This file is free software, distributed under the terms of the GNU"
1155     echo "# General Public License.  As a special exception to the GNU General"
1156     echo "# Public License, this file may be distributed as part of a program"
1157     echo "# that contains a configuration script generated by Autoconf, under"
1158     echo "# the same distribution terms as the rest of that program."
1159     echo "#"
1160     echo "# Generated by gnulib-tool."
1161     echo "#"
1162     echo "# This file represents the compiled summary of the specification in"
1163     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
1164     echo "# to be invoked from configure.ac."
1165     echo "# In projects using CVS, this file can be treated like other built files."
1166     echo
1167     echo
1168     echo "# This macro should be invoked from $configure_ac, in the section"
1169     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
1170     echo "# any checks for libraries, header files, types and library functions."
1171     echo "AC_DEFUN([${macro_prefix}_EARLY],"
1172     echo "["
1173     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 > /dev/null; then
1174       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
1175     fi
1176     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 > /dev/null; then
1177       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
1178     fi
1179     echo "])"
1180     echo
1181     echo "# This macro should be invoked from $configure_ac, in the section"
1182     echo "# \"Check for header files, types and library functions\"."
1183     echo "AC_DEFUN([${macro_prefix}_INIT],"
1184     echo "["
1185     for module in $modules; do
1186       func_verify_module
1187       if test -n "$module"; then
1188         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./'
1189         if test "$module" = 'alloca' && test -n "$libtool"; then
1190           echo 'changequote(,)dnl'
1191           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
1192           echo 'changequote([, ])dnl'
1193           echo 'AC_SUBST([LTALLOCA])'
1194         fi
1195       fi
1196     done
1197     echo "])"
1198     echo
1199     echo "# This macro records the list of files which have been installed by"
1200     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
1201     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
1202     echo "$files" | sed -e 's,^,  ,'
1203     echo "])"
1204   ) > "$destdir"/$m4base/gnulib-comp.m4.tmp
1205   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1206     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4.tmp > /dev/null; then
1207       rm -f "$destdir"/$m4base/gnulib-comp.m4.tmp
1208     else
1209       if $doit; then
1210         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1211         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
1212         mv -f "$destdir"/$m4base/gnulib-comp.m4.tmp "$destdir"/$m4base/gnulib-comp.m4
1213       else
1214         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1215         if false; then
1216           cat "$destdir"/$m4base/gnulib-comp.m4.tmp
1217           echo
1218           echo "# gnulib-comp.m4 ends here"
1219         fi
1220         rm -f "$destdir"/$m4base/gnulib-comp.m4.tmp
1221       fi
1222     fi
1223   else
1224     if $doit; then
1225       echo "Creating $m4base/gnulib-comp.m4"
1226       mv -f "$destdir"/$m4base/gnulib-comp.m4.tmp "$destdir"/$m4base/gnulib-comp.m4
1227     else
1228       echo "Create $m4base/gnulib-comp.m4"
1229       cat "$destdir"/$m4base/gnulib-comp.m4.tmp
1230       rm -f "$destdir"/$m4base/gnulib-comp.m4.tmp
1231     fi
1232   fi
1233
1234   echo "Finished."
1235   echo
1236   echo "You may need to add #include directives for the following .h files."
1237   for module in $modules; do
1238     func_get_include_directive "$module"
1239   done | LC_ALL=C sort -u | sed -e '/^$/d;' -e 's/^/  /'
1240   echo
1241   echo "Don't forget to"
1242   echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1243   sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
1244   sourcebase_base=`basename "$sourcebase"`
1245   echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
1246   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
1247   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
1248   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
1249 }
1250
1251 # func_create_testdir testdir modules
1252 # Input:
1253 # - auxdir          directory relative to destdir where to place build aux files
1254 func_create_testdir ()
1255 {
1256   testdir="$1"
1257   modules="$2"
1258   modules=`for m in $modules; do echo $m; done | sort | uniq`
1259
1260   # Determine final module list.
1261   func_modules_transitive_closure
1262   echo "Module list with included dependencies:"
1263   echo "$modules" | sed -e 's/^/  /'
1264
1265   # Determine final file list.
1266   func_modules_to_filelist
1267   echo "File list:"
1268   echo "$files" | sed -e 's/^/  /'
1269
1270   # Create directories.
1271   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
1272     if test "$d" = build-aux; then
1273       mkdir -p "$testdir/$auxdir"
1274     else
1275       mkdir -p "$testdir/$d"
1276     fi
1277   done
1278
1279   # Copy files or make symbolic links.
1280   for f in $files; do
1281     case "$f" in
1282       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1283       *) g="$f" ;;
1284     esac
1285     ln "$gnulib_dir/$f" "$testdir/$g" 2>/dev/null ||
1286     if test -z "$symbolic"; then
1287       cp -p "$gnulib_dir/$f" "$testdir/$g"
1288     else
1289       ln -s "$gnulib_dir/$f" "$testdir/$g"
1290     fi
1291   done
1292
1293   # Create lib/Makefile.am.
1294   mkdir -p "$testdir/lib"
1295   func_emit_lib_Makefile_am > "$testdir/lib/Makefile.am"
1296
1297   # Create m4/Makefile.am.
1298   mkdir -p "$testdir/m4"
1299   (echo "## Process this file with automake to produce Makefile.in."
1300    echo
1301    echo "EXTRA_DIST ="
1302    for f in $files; do
1303      case "$f" in
1304        m4/* )
1305          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
1306      esac
1307    done
1308   ) > "$testdir/m4/Makefile.am"
1309
1310   subdirs="lib m4"
1311   subdirs_with_configure_ac=""
1312
1313   if test -f "$testdir"/m4/gettext.m4; then
1314     # Avoid stupid error message from automake:
1315     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
1316     mkdir -p "$testdir/po"
1317     (echo "## Process this file with automake to produce Makefile.in."
1318     ) > "$testdir/po/Makefile.am"
1319     subdirs="$subdirs po"
1320   fi
1321
1322   if test -n "$inctests"; then
1323     test -d "$testdir/tests" || mkdir "$testdir/tests"
1324     # Create tests/Makefile.am.
1325     sourcebase=lib
1326     func_emit_tests_Makefile_am > "$testdir/tests/Makefile.am"
1327     # Create tests/configure.ac.
1328     (echo "# Process this file with autoconf to produce a configure script."
1329      echo "AC_INIT([dummy], [0])"
1330      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
1331      echo "AM_INIT_AUTOMAKE"
1332      echo
1333      echo "AM_CONFIG_HEADER([config.h])"
1334      echo
1335      echo "AC_PROG_CC"
1336      echo "AC_PROG_INSTALL"
1337      echo "AC_PROG_MAKE_SET"
1338      echo "AC_PROG_RANLIB"
1339      echo
1340      if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 > /dev/null; then
1341        echo "AC_GNU_SOURCE"
1342        echo
1343      fi
1344      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 > /dev/null; then
1345        echo "gl_USE_SYSTEM_EXTENSIONS"
1346        echo
1347      fi
1348      # We don't have explicit ordering constraints between the various
1349      # autoconf snippets. It's cleanest to put those of the library before
1350      # those of the tests.
1351      for module in $modules; do
1352        func_verify_nontests_module
1353        if test -n "$module"; then
1354          func_get_autoconf_snippet "$module"
1355        fi
1356      done
1357      for module in $modules; do
1358        func_verify_tests_module
1359        if test -n "$module"; then
1360          func_get_autoconf_snippet "$module"
1361        fi
1362      done
1363      echo
1364      # Usually tests/config.h will be a superset of config.h. Verify this by
1365      # "merging" config.h into tests/config.h; look out for gcc warnings.
1366      echo "AH_TOP([#include \"../config.h\"])"
1367      echo
1368      echo "AC_OUTPUT([Makefile])"
1369     ) > "$testdir/tests/configure.ac"
1370     subdirs="$subdirs tests"
1371     subdirs_with_configure_ac="$subdirs_with_configure_ac tests"
1372   fi
1373
1374   # Create Makefile.am.
1375   (echo "## Process this file with automake to produce Makefile.in."
1376    echo
1377    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1378    echo
1379    echo "SUBDIRS = $subdirs"
1380    echo
1381    echo "ACLOCAL_AMFLAGS = -I m4"
1382   ) > "$testdir/Makefile.am"
1383
1384   # Create configure.ac.
1385   (echo "# Process this file with autoconf to produce a configure script."
1386    echo "AC_INIT([dummy], [0])"
1387    if test "$auxdir" != "."; then
1388      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1389    fi
1390    echo "AM_INIT_AUTOMAKE"
1391    echo
1392    echo "AM_CONFIG_HEADER([config.h])"
1393    echo
1394    echo "AC_PROG_CC"
1395    echo "AC_PROG_INSTALL"
1396    echo "AC_PROG_MAKE_SET"
1397    echo "AC_PROG_RANLIB"
1398    echo
1399    if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 > /dev/null; then
1400      echo "AC_GNU_SOURCE"
1401      echo
1402    fi
1403    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 > /dev/null; then
1404      echo "gl_USE_SYSTEM_EXTENSIONS"
1405      echo
1406    fi
1407    for module in $modules; do
1408      func_verify_nontests_module
1409      if test -n "$module"; then
1410        func_get_autoconf_snippet "$module"
1411      fi
1412    done
1413    echo
1414    if test -n "$subdirs_with_configure_ac"; then
1415      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
1416    fi
1417    makefiles="Makefile"
1418    for d in $subdirs; do
1419      # For subdirs that have a configure.ac by their own, it's the subdir's
1420      # configure.ac which creates the subdir's Makefile.am, not this one.
1421      case " $subdirs_with_configure_ac " in
1422        *" $d "*) ;;
1423        *) makefiles="$makefiles $d/Makefile" ;;
1424      esac
1425    done
1426    echo "AC_OUTPUT([$makefiles])"
1427   ) > "$testdir/configure.ac"
1428
1429   # Create autogenerated files.
1430   (cd "$testdir"
1431    echo "executing ${AUTORECONF} --force --install"
1432    ${AUTORECONF} --force --install
1433   )
1434   if grep '^BUILT_SOURCES *+=' "$testdir/lib/Makefile.am" > /dev/null; then
1435     (cd "$testdir"
1436      ./configure
1437        cd lib
1438        built_sources=`grep '^BUILT_SOURCES *=' Makefile.in | sed -e 's/^BUILT_SOURCES *=//'`
1439        if test -n "$built_sources"; then
1440          make $built_sources
1441        fi
1442        cd ..
1443      make distclean
1444     )
1445   fi
1446 }
1447
1448 # func_create_megatestdir megatestdir allmodules
1449 # Input:
1450 # - auxdir          directory relative to destdir where to place build aux files
1451 func_create_megatestdir ()
1452 {
1453   megatestdir="$1"
1454   allmodules="$2"
1455   if test -z "$allmodules"; then
1456     allmodules=`func_all_modules`
1457   fi
1458
1459   megasubdirs=
1460   # First, all modules one by one.
1461   for onemodule in $allmodules; do
1462     func_create_testdir "$megatestdir/$onemodule" $onemodule
1463     megasubdirs="${megasubdirs}$onemodule "
1464   done
1465   # Then, all modules all together.
1466   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
1467   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
1468   func_create_testdir "$megatestdir/ALL" "$allmodules"
1469   megasubdirs="${megasubdirs}ALL"
1470
1471   # Create Makefile.am.
1472   (echo "## Process this file with automake to produce Makefile.in."
1473    echo
1474    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1475    echo
1476    echo "SUBDIRS = $megasubdirs"
1477   ) > "$megatestdir/Makefile.am"
1478
1479   # Create configure.ac.
1480   (echo "# Process this file with autoconf to produce a configure script."
1481    echo "AC_INIT([dummy], [0])"
1482    if test "$auxdir" != "."; then
1483      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1484    fi
1485    echo "AM_INIT_AUTOMAKE"
1486    echo
1487    echo "AC_PROG_MAKE_SET"
1488    echo
1489    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
1490    echo "AC_OUTPUT([Makefile])"
1491   ) > "$megatestdir/configure.ac"
1492
1493   # Create autogenerated files.
1494   (cd "$megatestdir"
1495    echo "executing ${AUTORECONF} --install"
1496    ${AUTORECONF} --install
1497   )
1498 }
1499
1500 case $mode in
1501   "" )
1502     func_fatal_error "no mode specified" ;;
1503
1504   list )
1505     func_all_modules
1506     ;;
1507
1508   import )
1509     # Where to import.
1510     if test -z "$destdir"; then
1511       destdir=.
1512     fi
1513     test -d "$destdir" \
1514       || func_fatal_error "destination directory does not exist: $destdir"
1515
1516     # Prefer configure.ac to configure.in.
1517     if test -f "$destdir"/configure.ac; then
1518       configure_ac="$destdir/configure.ac"
1519     else
1520       if test -f "$destdir"/configure.in; then
1521         configure_ac="$destdir/configure.in"
1522       else
1523         func_fatal_error "cannot find $destdir/configure.ac"
1524       fi
1525     fi
1526
1527     test -f "$destdir"/Makefile.am \
1528       || func_fatal_error "cannot find $destdir/Makefile.am"
1529
1530     # Analyze configure.ac.
1531     guessed_auxdir="."
1532     guessed_libtool=
1533     my_sed_traces='
1534       s,#.*$,,
1535       s,^dnl .*$,,
1536       s, dnl .*$,,
1537       /AC_CONFIG_AUX_DIR/ {
1538         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
1539       }
1540       /A[CM]_PROG_LIBTOOL/ {
1541         s,^.*$,guessed_libtool=true,p
1542       }'
1543     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
1544
1545     if test -z "$auxdir"; then
1546       auxdir="$guessed_auxdir"
1547     fi
1548
1549     # Determine where to apply func_import.
1550     if test -n "$m4base"; then
1551       # Apply func_import to a particular gnulib directory.
1552       # Any number of additional modules can be given.
1553       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
1554         # First use of gnulib in the given m4base.
1555         test -n "$supplied_libname" || supplied_libname=true
1556         test -n "$sourcebase" || sourcebase="lib"
1557         test -n "$macro_prefix" || macro_prefix="gl"
1558       fi
1559       func_import "$*"
1560     else
1561       # Apply func_import to all gnulib directories.
1562       # To get this list of directories, look at Makefile.am. (Not at
1563       # configure, because it may be omitted from CVS. Also, don't run
1564       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
1565       aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[  ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
1566       m4dirs=
1567       m4dirs_count=0
1568       m4dir_is_next=
1569       for arg in $aclocal_amflags; do
1570         if test -n "$m4dir_is_next"; then
1571           # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
1572           case "$arg" in
1573             /*) ;;
1574             *)
1575               if test -f "$destdir/$arg"/gnulib-cache.m4; then
1576                 m4dirs="$m4dirs $arg"
1577                 m4dirs_count=`expr $m4dirs_count + 1`
1578               fi
1579               ;;
1580           esac
1581         else
1582           if test "X$arg" = "X-I"; then
1583             m4dir_is_next=yes
1584           else
1585             m4dir_is_next=
1586           fi
1587         fi
1588       done
1589       if test $m4dirs_count = 0; then
1590         # First use of gnulib in a package.
1591         # Any number of additional modules can be given.
1592         test -n "$supplied_libname" || supplied_libname=true
1593         test -n "$sourcebase" || sourcebase="lib"
1594         m4base="m4"
1595         test -n "$macro_prefix" || macro_prefix="gl"
1596         func_import "$*"
1597       else
1598         if test $m4dirs_count = 1; then
1599           # There's only one use of gnulib here. Assume the user means it.
1600           # Any number of additional modules can be given.
1601           for m4base in $m4dirs; do
1602             func_import "$*"
1603           done
1604         else
1605           # Ambiguous - guess what the user meant.
1606           if test $# = 0; then
1607             # No further arguments. Guess the user wants to update all of them.
1608             for m4base in $m4dirs; do
1609               func_import
1610             done
1611           else
1612             # Really ambiguous.
1613             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
1614           fi
1615         fi
1616       fi
1617     fi
1618     ;;
1619
1620   create-testdir )
1621     if test -z "$destdir"; then
1622       func_fatal_error "please specify --dir option"
1623     fi
1624     mkdir "$destdir"
1625     test -d "$destdir" \
1626       || func_fatal_error "could not create destination directory"
1627     test -n "$auxdir" || auxdir="build-aux"
1628     func_create_testdir "$destdir" "$*"
1629     ;;
1630
1631   create-megatestdir )
1632     if test -z "$destdir"; then
1633       func_fatal_error "please specify --dir option"
1634     fi
1635     mkdir "$destdir" || func_fatal_error "could not create destination directory"
1636     test -n "$auxdir" || auxdir="build-aux"
1637     func_create_megatestdir "$destdir" "$*"
1638     ;;
1639
1640   test )
1641     test -n "$destdir" || destdir=testdir$$
1642     mkdir "$destdir" || func_fatal_error "could not create destination directory"
1643     test -n "$auxdir" || auxdir="build-aux"
1644     func_create_testdir "$destdir" "$*"
1645     cd "$destdir"
1646       mkdir build
1647       cd build
1648         ../configure
1649         make
1650         make check
1651         make distclean
1652         remaining=`find . -type f -print`
1653         if test -n "$remaining"; then
1654           echo "Remaining files:" $remaining 1>&2
1655           echo "gnulib-tool: *** Stop." 1>&2
1656           exit 1
1657         fi
1658       cd ..
1659     cd ..
1660     rm -rf "$destdir"
1661     ;;
1662
1663   megatest )
1664     test -n "$destdir" || destdir=testdir$$
1665     mkdir "$destdir" || func_fatal_error "could not create destination directory"
1666     test -n "$auxdir" || auxdir="build-aux"
1667     func_create_megatestdir "$destdir" "$*"
1668     cd "$destdir"
1669       mkdir build
1670       cd build
1671         ../configure
1672         make
1673         make check
1674         make distclean
1675         remaining=`find . -type f -print`
1676         if test -n "$remaining"; then
1677           echo "Remaining files:" $remaining 1>&2
1678           echo "gnulib-tool: *** Stop." 1>&2
1679           exit 1
1680         fi
1681       cd ..
1682     cd ..
1683     rm -rf "$destdir"
1684     ;;
1685
1686   extract-description )
1687     for module
1688     do
1689       func_verify_module
1690       if test -n "$module"; then
1691         func_get_description "$module"
1692       fi
1693     done
1694     ;;
1695
1696   extract-filelist )
1697     for module
1698     do
1699       func_verify_module
1700       if test -n "$module"; then
1701         func_get_filelist "$module"
1702       fi
1703     done
1704     ;;
1705
1706   extract-dependencies )
1707     for module
1708     do
1709       func_verify_module
1710       if test -n "$module"; then
1711         func_get_dependencies "$module"
1712       fi
1713     done
1714     ;;
1715
1716   extract-autoconf-snippet )
1717     for module
1718     do
1719       func_verify_module
1720       if test -n "$module"; then
1721         func_get_autoconf_snippet "$module"
1722       fi
1723     done
1724     ;;
1725
1726   extract-automake-snippet )
1727     for module
1728     do
1729       func_verify_module
1730       if test -n "$module"; then
1731         func_get_automake_snippet "$module"
1732       fi
1733     done
1734     ;;
1735
1736   extract-include-directive )
1737     for module
1738     do
1739       func_verify_module
1740       if test -n "$module"; then
1741         func_get_include_directive "$module"
1742       fi
1743     done
1744     ;;
1745
1746   extract-license )
1747     for module
1748     do
1749       func_verify_module
1750       if test -n "$module"; then
1751         func_get_license "$module"
1752       fi
1753     done
1754     ;;
1755
1756   extract-maintainer )
1757     for module
1758     do
1759       func_verify_module
1760       if test -n "$module"; then
1761         func_get_maintainer "$module"
1762       fi
1763     done
1764     ;;
1765
1766   extract-tests-module )
1767     for module
1768     do
1769       func_verify_module
1770       if test -n "$module"; then
1771         func_get_tests_module "$module"
1772       fi
1773     done
1774     ;;
1775
1776   * )
1777     func_fatal_error "unknown operation mode --$mode" ;;
1778 esac
1779
1780 exit 0