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