New config-h module, so that "make" output needn't be cluttered
[pspp] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2006 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 Foundation,
17 # 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: 2006-08-26 06:39:32 $'
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 # You can set AUTOMAKEPATH to empty if automake 1.9.x is already in your PATH.
39 AUTOMAKEPATH=
40
41 # If you didn't set AUTOCONFPATH and AUTOMAKEPATH, you can also set the
42 # variables AUTOCONF, ACLOCAL, AUTOMAKE, AUTORECONF individually.
43 if test -z "${AUTOCONF}" || test -n "${AUTOCONFPATH}"; then
44   AUTOCONF="${AUTOCONFPATH}autoconf"
45 fi
46 if test -z "${ACLOCAL}" || test -n "${AUTOMAKEPATH}"; then
47   ACLOCAL="${AUTOMAKEPATH}aclocal"
48 fi
49 if test -z "${AUTOMAKE}" || test -n "${AUTOMAKEPATH}"; then
50   AUTOMAKE="${AUTOMAKEPATH}automake"
51 fi
52 if test -z "${AUTORECONF}" || test -n "${AUTOCONFPATH}"; then
53   AUTORECONF="${AUTOCONFPATH}autoreconf"
54 fi
55
56 # GNU sort is needed. Set SORT to its location (not needed if it's called
57 # 'sort' and already in the PATH).
58 if test -z "$SORT"; then
59   SORT=sort
60 fi
61
62 nl='
63 '
64
65 # func_usage
66 # outputs to stdout the --help usage message.
67 func_usage ()
68 {
69   echo "\
70 Usage: gnulib-tool --list
71        gnulib-tool --import [module1 ... moduleN]
72        gnulib-tool --update
73        gnulib-tool --create-testdir --dir=directory module1 ... moduleN
74        gnulib-tool --create-megatestdir --dir=directory [module1 ... moduleN]
75        gnulib-tool --test --dir=directory module1 ... moduleN
76        gnulib-tool --megatest --dir=directory [module1 ... moduleN]
77        gnulib-tool --extract-description module
78        gnulib-tool --extract-filelist module
79        gnulib-tool --extract-dependencies module
80        gnulib-tool --extract-autoconf-snippet module
81        gnulib-tool --extract-automake-snippet module
82        gnulib-tool --extract-include-directive module
83        gnulib-tool --extract-license module
84        gnulib-tool --extract-maintainer module
85        gnulib-tool --extract-tests-module module
86
87 Operation modes:
88       --list                print the available module names
89       --import              import the given modules into the current package;
90                             if no modules are specified, update the current
91                             package from the current gnulib
92       --update              update the current package, restore files omitted
93                             from CVS
94       --create-testdir      create a scratch package with the given modules
95       --create-megatestdir  create a mega scratch package with the given modules
96                             one by one and all together
97       --test                test the combination of the given modules
98                             (recommended to use CC=\"gcc -Wall\" here)
99       --megatest            test the given modules one by one and all together
100                             (recommended to use CC=\"gcc -Wall\" here)
101       --extract-description        extract the description
102       --extract-filelist           extract the list of files
103       --extract-dependencies       extract the dependencies
104       --extract-autoconf-snippet   extract the snippet for configure.ac
105       --extract-automake-snippet   extract the snippet for lib/Makefile.am
106       --extract-include-directive  extract the #include directive
107       --extract-license            report the license terms of the source files
108                                    under lib/
109       --extract-maintainer         report the maintainer(s) inside gnulib
110       --extract-tests-module       report the unit test module, if it exists
111
112 General options:
113       --dir=DIRECTORY       Specify the target directory.
114                             For --import, this specifies where your
115                             configure.ac can be found.  Defaults to current
116                             directory.
117       --local-dir=DIRECTORY  Specify a local override directory where to look
118                             up files before looking in gnulib's directory.
119
120 Options for --import:
121       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
122       --source-base=DIRECTORY
123                             Directory relative to --dir where source code is
124                             placed (default \"lib\").
125       --m4-base=DIRECTORY   Directory relative to --dir where *.m4 macros are
126                             placed (default \"m4\").
127       --doc-base=DIRECTORY  Directory relative to --dir where doc files are
128                             placed (default \"doc\").
129       --tests-base=DIRECTORY
130                             Directory relative to --dir where unit tests are
131                             placed (default \"tests\").
132       --aux-dir=DIRECTORY   Directory relative to --dir where auxiliary build
133                             tools are placed (default \"build-aux\").
134       --with-tests          Include unit tests for the included modules.
135       --avoid=MODULE        Avoid including the given MODULE. Useful if you
136                             have code that provides equivalent functionality.
137                             This option can be repeated.
138       --lgpl                Abort if modules aren't available under the LGPL.
139                             Also modify license template from GPL to LGPL.
140       --libtool             Use libtool rules.
141       --no-libtool          Don't use libtool rules.
142       --macro-prefix=PREFIX  Specify the prefix of the macros 'gl_EARLY' and
143                             'gl_INIT'. Default is 'gl'.
144       --no-changelog        don't update or create ChangeLog files
145
146 Options for --import and --update:
147       --dry-run             For --import, only print what would have been done.
148   -s, --symbolic, --symlink Make symbolic links instead of copying files.
149
150 Report bugs to <bug-gnulib@gnu.org>."
151 }
152
153 # func_version
154 # outputs to stdout the --version message.
155 func_version ()
156 {
157   year=`echo "$last_checkin_date" | sed -e 's,/.*$,,'`
158   echo "$progname (GNU $package) $version"
159   echo "Copyright (C) $year Free Software Foundation, Inc.
160 This is free software; see the source for copying conditions.  There is NO
161 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
162   echo "Written by" "Bruno Haible" "and" "Simon Josefsson"
163 }
164
165 # func_emit_copyright_notice
166 # outputs to stdout a header for a generated file.
167 func_emit_copyright_notice ()
168 {
169   echo "# Copyright (C) 2004-2006 Free Software Foundation, Inc."
170   echo "#"
171   echo "# This file is free software, distributed under the terms of the GNU"
172   echo "# General Public License.  As a special exception to the GNU General"
173   echo "# Public License, this file may be distributed as part of a program"
174   echo "# that contains a configuration script generated by Autoconf, under"
175   echo "# the same distribution terms as the rest of that program."
176   echo "#"
177   echo "# Generated by gnulib-tool."
178 }
179
180 # func_tmpdir
181 # creates a temporary directory.
182 # Sets variable
183 # - tmp             pathname of freshly created temporary directory
184 func_tmpdir ()
185 {
186   # Use the environment variable TMPDIR, falling back to /tmp. This allows
187   # users to specify a different temporary directory, for example, if their
188   # /tmp is filled up or too small.
189   : ${TMPDIR=/tmp}
190   {
191     # Use the mktemp program if available. If not available, hide the error
192     # message.
193     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
194     test -n "$tmp" && test -d "$tmp"
195   } ||
196   {
197     # Use a simple mkdir command. It is guaranteed to fail if the directory
198     # already exists.  $RANDOM is bash specific and expands to empty in shells
199     # other than bash, ksh and zsh.  Its use does not increase security;
200     # rather, it minimizes the probability of failure in a very cluttered /tmp
201     # directory.
202     tmp=$TMPDIR/gl$$-$RANDOM
203     (umask 077 && mkdir "$tmp")
204   } ||
205   {
206     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
207     { (exit 1); exit 1; }
208   }
209 }
210
211 # func_fatal_error message
212 # outputs to stderr a fatal error message, and terminates the program.
213 func_fatal_error ()
214 {
215   echo "gnulib-tool: *** $1" 1>&2
216   echo "gnulib-tool: *** Stop." 1>&2
217   exit 1
218 }
219
220 # func_readlink SYMLINK
221 # outputs the target of the given symlink.
222 if (type -p readlink) > /dev/null 2>&1; then
223   func_readlink ()
224   {
225     # Use the readlink program from GNU coreutils.
226     readlink "$1"
227   }
228 else
229   func_readlink ()
230   {
231     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
232     # would do the wrong thing if the link target contains " -> ".
233     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
234   }
235 fi
236
237 # func_ln_if_changed SRC DEST
238 # Like ln -s, but avoids munging timestamps if the link is correct.
239 func_ln_if_changed ()
240 {
241   if test $# -ne 2; then
242     echo "usage: func_ln_if_changed SRC DEST" >&2
243   fi
244   if test -L "$2" && test "$1" = "`func_readlink "$2"`"; then
245     :
246   else
247     rm -f "$2"
248     ln -s "$1" "$2"
249   fi
250 }
251
252 # Command-line option processing.
253 # Removes the OPTIONS from the arguments. Sets the variables:
254 # - mode            list or import or create-testdir or create-megatestdir
255 # - destdir         from --dir
256 # - libname, supplied_libname  from --lib
257 # - sourcebase      from --source-base
258 # - m4base          from --m4-base
259 # - docbase         from --doc-base
260 # - testsbase       from --tests-base
261 # - auxdir          from --aux-dir
262 # - inctests        true if --with-tests was given, blank otherwise
263 # - avoidlist       list of modules to avoid, from --avoid
264 # - lgpl            true if --lgpl was given, blank otherwise
265 # - libtool         true if --libtool was given, false if --no-libtool was
266 #                   given, blank otherwise
267 # - macro_prefix    from --macro-prefix
268 # - autoconf_minversion  minimum supported autoconf version
269 # - do_changelog    false if --no-changelog was given, : otherwise
270 # - doit            : if actions shall be executed, false if only to be printed
271 # - local_gnulib_dir  from --local-dir
272 # - symbolic        true if --symbolic was given, blank otherwise
273 {
274   mode=
275   destdir=
276   libname=libgnu
277   supplied_libname=
278   sourcebase=
279   m4base=
280   docbase=
281   testsbase=
282   auxdir=
283   inctests=
284   avoidlist=
285   lgpl=
286   libtool=
287   macro_prefix=
288   do_changelog=:
289   doit=:
290   local_gnulib_dir=
291   symbolic=
292
293   supplied_opts="$@"
294
295   while test $# -gt 0; do
296     case "$1" in
297       --list | --lis )
298         mode=list
299         shift ;;
300       --import | --impor | --impo | --imp | --im | --i )
301         mode=import
302         shift ;;
303       --update | --updat | --upda | --upd | --up | --u )
304         mode=update
305         shift ;;
306       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
307         mode=create-testdir
308         shift ;;
309       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
310         mode=create-megatestdir
311         shift ;;
312       --test | --tes | --te | --t )
313         mode=test
314         shift ;;
315       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
316         mode=megatest
317         shift ;;
318       --extract-* )
319         mode=`echo "X$1" | sed -e 's/^X--//'`
320         shift ;;
321       --dir )
322         shift
323         if test $# = 0; then
324           func_fatal_error "missing argument for --dir"
325         fi
326         destdir=$1
327         shift ;;
328       --dir=* )
329         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
330         shift ;;
331       --lib )
332         shift
333         if test $# = 0; then
334           func_fatal_error "missing argument for --lib"
335         fi
336         libname=$1
337         supplied_libname=true
338         shift ;;
339       --lib=* )
340         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
341         supplied_libname=true
342         shift ;;
343       --source-base )
344         shift
345         if test $# = 0; then
346           func_fatal_error "missing argument for --source-base"
347         fi
348         sourcebase=$1
349         shift ;;
350       --source-base=* )
351         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
352         shift ;;
353       --m4-base )
354         shift
355         if test $# = 0; then
356           func_fatal_error "missing argument for --m4-base"
357         fi
358         m4base=$1
359         shift ;;
360       --m4-base=* )
361         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
362         shift ;;
363       --doc-base )
364         shift
365         if test $# = 0; then
366           func_fatal_error "missing argument for --doc-base"
367         fi
368         docbase=$1
369         shift ;;
370       --doc-base=* )
371         docbase=`echo "X$1" | sed -e 's/^X--doc-base=//'`
372         shift ;;
373       --tests-base )
374         shift
375         if test $# = 0; then
376           func_fatal_error "missing argument for --tests-base"
377         fi
378         testsbase=$1
379         shift ;;
380       --tests-base=* )
381         testsbase=`echo "X$1" | sed -e 's/^X--tests-base=//'`
382         shift ;;
383       --aux-dir )
384         shift
385         if test $# = 0; then
386           func_fatal_error "missing argument for --aux-dir"
387         fi
388         auxdir=$1
389         shift ;;
390       --aux-dir=* )
391         auxdir=`echo "X$1" | sed -e 's/^X--aux-dir=//'`
392         shift ;;
393       --with-tests )
394         inctests=true
395         shift ;;
396       --avoid )
397         shift
398         if test $# = 0; then
399           func_fatal_error "missing argument for --avoid"
400         fi
401         avoidlist="$avoidlist $1"
402         shift ;;
403       --avoid=* )
404         avoidlist="$avoidlist "`echo "X$1" | sed -e 's/^X--avoid=//'`
405         shift ;;
406       --lgpl )
407         lgpl=true
408         shift ;;
409       --libtool )
410         libtool=true
411         shift ;;
412       --no-libtool )
413         libtool=false
414         shift ;;
415       --macro-prefix )
416         shift
417         if test $# = 0; then
418           func_fatal_error "missing argument for --macro-prefix"
419         fi
420         macro_prefix="$1"
421         shift ;;
422       --macro-prefix=* )
423         macro_prefix=`echo "X$1" | sed -e 's/^X--macro-prefix=//'`
424         shift ;;
425       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
426         do_changelog=false
427         shift ;;
428       --dry-run )
429         doit=false
430         shift ;;
431       --local-dir )
432         shift
433         if test $# = 0; then
434           func_fatal_error "missing argument for --local-dir"
435         fi
436         local_gnulib_dir=$1
437         shift ;;
438       --local-dir=* )
439         local_gnulib_dir=`echo "X$1" | sed -e 's/^X--local-dir=//'`
440         shift ;;
441       -s | --symbolic | --symboli | --symbol | --symbo | --symb | --symlink | --symlin | --symli | --syml | --sym | --sy )
442         symbolic=true
443         shift ;;
444       --help | --hel | --he | --h )
445         func_usage
446         exit 0 ;;
447       --version | --versio | --versi | --vers | --ver | --ve | --v )
448         func_version
449         exit 0 ;;
450       -- )
451         # Stop option processing
452         shift
453         break ;;
454       -* )
455         echo "gnulib-tool: unknown option $1" 1>&2
456         echo "Try 'gnulib-tool --help' for more information." 1>&2
457         exit 1 ;;
458       * )
459         break ;;
460     esac
461   done
462
463   if test "$mode" = update; then
464     if test $# != 0; then
465       echo "gnulib-tool: too many arguments in 'update' mode" 1>&2
466       echo "Try 'gnulib-tool --help' for more information." 1>&2
467       echo "If you really want to modify the gnulib configuration of your project," 1>&2
468       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
469       exit 1
470     fi
471     if test -n "$supplied_libname" || test -n "$sourcebase" || test -n "$m4base" \
472        || test -n "$docbase" || test -n "$testsbase" || test -n "$auxdir" \
473        || test -n "$inctests" || test -n "$avoidlist" || test -n "$lgpl" \
474        || test -n "$macro_prefix"; then
475       echo "gnulib-tool: invalid options for 'update' mode" 1>&2
476       echo "Try 'gnulib-tool --help' for more information." 1>&2
477       echo "If you really want to modify the gnulib configuration of your project," 1>&2
478       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
479       exit 1
480     fi
481     do_changelog=false
482   fi
483
484   # Determine the minimum supported autoconf version from the project's
485   # configure.ac.
486   DEFAULT_AUTOCONF_MINVERSION="2.59"
487   autoconf_minversion=
488   configure_ac=
489   if { test "$mode" = import || test "$mode" = update; } && test -n "$destdir"; then
490     if test -f "$destdir"/configure.ac; then
491       configure_ac="$destdir/configure.ac"
492     else
493       if test -f "$destdir"/configure.in; then
494         configure_ac="$destdir/configure.in"
495       fi
496     fi
497   else
498     if test -f configure.ac; then
499       configure_ac="configure.ac"
500     else
501       if test -f configure.in; then
502         configure_ac="configure.in"
503       fi
504     fi
505   fi
506   if test -n "$configure_ac"; then
507     # Use sed, not autoconf --trace, to look for the AC_PREREQ invocation,
508     # because when some m4 files are omitted from a CVS repository,
509     # "autoconf --trace=AC_PREREQ" fails with an error message like this:
510     #   m4: aclocal.m4:851: Cannot open m4/absolute-header.m4: No such file or directory
511     #   autom4te: m4 failed with exit status: 1
512     prereqs=
513     my_sed_traces='
514       s,#.*$,,
515       s,^dnl .*$,,
516       s, dnl .*$,,
517       /AC_PREREQ/ {
518         s,^.*AC_PREREQ([[ ]*\([^])]*\).*$,prereqs="$prereqs \1",p
519       }'
520     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
521     if test -n "$prereqs"; then
522       autoconf_minversion=`for version in $prereqs; do echo $version; done | $SORT -g | uniq | tail -1`
523     fi
524   fi
525   if test -z "$autoconf_minversion"; then
526     autoconf_minversion=$DEFAULT_AUTOCONF_MINVERSION
527   fi
528   case "$autoconf_minversion" in
529     1.* | 2.[0-4]* | 2.5[0-8]*)
530       func_fatal_error "minimum supported autoconf version is 2.59. Try adding AC_PREREQ([$DEFAULT_AUTOCONF_MINVERSION]) to your configure.ac." ;;
531   esac
532
533   # Remove trailing slashes from the directory names. This is necessary for
534   # m4base (to avoid an error in func_import) and optional for the others.
535   sed_trimtrailingslashes='s,\([^/]\)//*$,\1,'
536   case "$sourcebase" in
537     */ ) sourcebase=`echo "$sourcebase" | sed -e "$sed_trimtrailingslashes"` ;;
538   esac
539   case "$m4base" in
540     */ ) m4base=`echo "$m4base" | sed -e "$sed_trimtrailingslashes"` ;;
541   esac
542   case "$docbase" in
543     */ ) docbase=`echo "$docbase" | sed -e "$sed_trimtrailingslashes"` ;;
544   esac
545   case "$testsbase" in
546     */ ) testsbase=`echo "$testsbase" | sed -e "$sed_trimtrailingslashes"` ;;
547   esac
548   case "$auxdir" in
549     */ ) auxdir=`echo "$auxdir" | sed -e "$sed_trimtrailingslashes"` ;;
550   esac
551 }
552
553 case "$0" in
554   /*) self_abspathname="$0" ;;
555   */*) self_abspathname=`pwd`/"$0" ;;
556   *) for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`; do
557        if test -x "$d/$0" && test ! -d "$d/$0"; then
558          self_abspathname="$d/$0"
559          break
560        fi
561      done
562      if test -z "$self_abspathname"; then
563        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
564      fi
565      ;;
566 esac
567 while test -h "$self_abspathname"; do
568   # Resolve symbolic link.
569   linkval=`func_readlink "$self_abspathname"`
570   test -n "$linkval" || break
571   case "$linkval" in
572     /* ) self_abspathname="$linkval" ;;
573     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
574   esac
575 done
576 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
577
578 func_tmpdir
579 trap 'rm -rf "$tmp"' 0 1 2 3 15
580
581 # func_lookup_file file
582 # looks up a file in $local_gnulib_dir or $gnulib_dir, or combines it through
583 # 'patch'.
584 # Output:
585 # - lookedup_file   name of the merged (combined) file
586 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
587 func_lookup_file ()
588 {
589   lkfile="$1"
590   if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile"; then
591     lookedup_file="$local_gnulib_dir/$lkfile"
592     lookedup_tmp=
593   else
594     if test -f "$gnulib_dir/$lkfile"; then
595       if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile.diff"; then
596         lkbase=`echo "$lkfile" | sed -e 's,^.*/,,'`
597         rm -f "$tmp/$lkbase"
598         cp "$gnulib_dir/$lkfile" "$tmp/$lkbase"
599         patch -s "$tmp/$lkbase" < "$local_gnulib_dir/$lkfile.diff" \
600           || func_fatal_error "patch file $local_gnulib_dir/$lkfile.diff didn't apply cleanly"
601         lookedup_file="$tmp/$lkbase"
602         lookedup_tmp=true
603       else
604         lookedup_file="$gnulib_dir/$lkfile"
605         lookedup_tmp=
606       fi
607     else
608       func_fatal_error "file $gnulib_dir/$lkfile not found"
609     fi
610   fi
611 }
612
613 # func_all_modules
614 func_all_modules ()
615 {
616   # Filter out metainformation files like README, which are not modules.
617   # Filter out unit test modules; they can be retrieved through
618   # --extract-tests-module if desired.
619   {
620     (cd "$gnulib_dir/modules" && ls -1)
621     if test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules"; then
622       (cd "$local_gnulib_dir/modules" && ls -1 | sed -e 's,\.diff$,,')
623     fi
624   } \
625       | sed -e '/^CVS$/d' -e '/^ChangeLog$/d' -e '/^COPYING$/d' -e '/^README$/d' -e '/^TEMPLATE$/d' -e '/^TEMPLATE-TESTS$/d' -e '/~$/d' \
626       | sed -e '/-tests$/d' \
627       | LC_ALL=C sort \
628       | LC_ALL=C uniq
629 }
630
631 # func_verify_module
632 # verifies a module name
633 func_verify_module ()
634 {
635   if { test -f "$gnulib_dir/modules/$module" \
636        || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
637             && test -f "$local_gnulib_dir/modules/$module"; }; } \
638      && test "CVS" != "$module" \
639      && test "ChangeLog" != "$module" \
640      && test "COPYING" != "$module" \
641      && test "README" != "$module" \
642      && test "TEMPLATE" != "$module" \
643      && test "TEMPLATE-TESTS" != "$module"; then
644     # OK, $module is a correct module name.
645     :
646   else
647     echo "gnulib-tool: module $module doesn't exist" 1>&2
648     module=
649   fi
650 }
651
652 # func_verify_nontests_module
653 # verifies a module name, excluding tests modules
654 func_verify_nontests_module ()
655 {
656   case "$module" in
657     *-tests ) module= ;;
658     * ) func_verify_module ;;
659   esac
660 }
661
662 # func_verify_tests_module
663 # verifies a module name, considering only tests modules
664 func_verify_tests_module ()
665 {
666   case "$module" in
667     *-tests ) func_verify_module ;;
668     * ) module= ;;
669   esac
670 }
671
672 sed_extract_prog=':[     ]*$/ {
673   :a
674     n
675     s/^Description:[     ]*$//
676     s/^Files:[   ]*$//
677     s/^Depends-on:[      ]*$//
678     s/^configure\.ac:[   ]*$//
679     s/^Makefile\.am:[    ]*$//
680     s/^Include:[         ]*$//
681     s/^License:[         ]*$//
682     s/^Maintainer:[      ]*$//
683     tb
684     p
685     ba
686   :b
687 }'
688
689 # func_get_description module
690 func_get_description ()
691 {
692   func_lookup_file "modules/$1"
693   sed -n -e "/^Description$sed_extract_prog" < "$lookedup_file"
694 }
695
696 # func_get_filelist module
697 func_get_filelist ()
698 {
699   func_lookup_file "modules/$1"
700   sed -n -e "/^Files$sed_extract_prog" < "$lookedup_file"
701   case "$autoconf_minversion" in
702     2.59)
703       #echo m4/onceonly.m4
704       echo m4/onceonly_2_57.m4
705       ;;
706   esac
707 }
708
709 # func_get_dependencies module
710 func_get_dependencies ()
711 {
712   # ${module}-tests always implicitly depends on ${module}.
713   echo "$1" | sed -n -e 's/-tests//p'
714   # Then the explicit dependencies listed in the module description.
715   func_lookup_file "modules/$1"
716   sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
717 }
718
719 # func_get_autoconf_snippet module
720 func_get_autoconf_snippet ()
721 {
722   func_lookup_file "modules/$1"
723   sed -n -e "/^configure\.ac$sed_extract_prog" < "$lookedup_file"
724 }
725
726 # func_get_automake_snippet module
727 func_get_automake_snippet ()
728 {
729   func_lookup_file "modules/$1"
730   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file"
731 }
732
733 # func_get_include_directive module
734 func_get_include_directive ()
735 {
736   func_lookup_file "modules/$1"
737   sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
738   sed -e 's/^\(["<]\)/#include \1/'
739 }
740
741 # func_get_license module
742 func_get_license ()
743 {
744   func_lookup_file "modules/$1"
745   sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
746 }
747
748 # func_get_maintainer module
749 func_get_maintainer ()
750 {
751   func_lookup_file "modules/$1"
752   sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
753 }
754
755 # func_get_tests_module module
756 func_get_tests_module ()
757 {
758   # The naming convention for tests modules is hardwired: ${module}-tests.
759   if test -f modules/"$1"-tests; then
760     echo "$1"-tests
761   fi
762 }
763
764 # func_acceptable module
765 # tests whether a module is acceptable.
766 # Input:
767 # - avoidlist       list of modules to avoid
768 func_acceptable ()
769 {
770   for avoid in $avoidlist; do
771     if test "$avoid" = "$1"; then
772       return 1
773     fi
774   done
775   return 0
776 }
777
778 # func_modules_transitive_closure
779 # Input:
780 # - modules         list of specified modules
781 # - inctests        true if tests should be included, blank otherwise
782 # - avoidlist       list of modules to avoid
783 # Output:
784 # - modules         list of modules, including dependencies
785 func_modules_transitive_closure ()
786 {
787   while true; do
788     xmodules=
789     for module in $modules; do
790       func_verify_module
791       if test -n "$module"; then
792         # Duplicate dependencies are harmless, but Jim wants a warning.
793         duplicated_deps=`func_get_dependencies $module | LC_ALL=C sort | LC_ALL=C uniq -d`
794         if test -n "$duplicated_deps"; then
795           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
796         fi
797         if func_acceptable $module; then
798           xmodules="$xmodules $module"
799           for depmodule in `func_get_dependencies $module`; do
800             if func_acceptable $depmodule; then
801               xmodules="$xmodules $depmodule"
802             fi
803           done
804           if test -n "$inctests"; then
805             testsmodule=`func_get_tests_module $module`
806             if test -n "$testsmodule"; then
807               if func_acceptable $testsmodule; then
808                 xmodules="$xmodules $testsmodule"
809                 for depmodule in `func_get_dependencies $testsmodule`; do
810                   if func_acceptable $depmodule; then
811                     xmodules="$xmodules $depmodule"
812                   fi
813                 done
814               fi
815             fi
816           fi
817         fi
818       fi
819     done
820     xmodules=`for m in $xmodules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
821     if test "$xmodules" = "$modules"; then
822       break
823     fi
824     modules="$xmodules"
825   done
826 }
827
828 # func_modules_add_dummy
829 # Input:
830 # - modules         list of modules, including dependencies
831 # Output:
832 # - modules         list of modules, including 'dummy' if needed
833 func_modules_add_dummy ()
834 {
835   have_lib_SOURCES=
836   sed_remove_backslash_newline=':a
837 /\\$/{
838 s/\\$//
839 N
840 s/\n//
841 ba
842 }'
843   for module in $modules; do
844     func_verify_nontests_module
845     if test -n "$module"; then
846       # Extract the value of "lib_SOURCES += ...".
847       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
848         # Ignore .h files since they are not compiled.
849         case "$file" in
850           *.h) ;;
851           *) have_lib_SOURCES=yes ;;
852         esac
853       done
854     fi
855   done
856   # Add the dummy module, to make sure the library will be non-empty.
857   if test -z "$have_lib_SOURCES"; then
858     modules="$modules dummy"
859   fi
860 }
861
862 # func_modules_to_filelist
863 # Input:
864 # - modules         list of modules, including dependencies
865 # Output:
866 # - files           list of files
867 func_modules_to_filelist ()
868 {
869   files=
870   for module in $modules; do
871     func_verify_module
872     if test -n "$module"; then
873       files="$files "`func_get_filelist $module`
874     fi
875   done
876   files=`for f in $files; do echo $f; done | LC_ALL=C sort | LC_ALL=C uniq`
877 }
878
879 # func_emit_lib_Makefile_am
880 # emits the contents of lib/Makefile.am to standard output.
881 # Input:
882 # - modules         list of modules, including dependencies
883 # - libname         library name
884 # - libtool         true if libtool will be used, false or blank otherwise
885 # - actioncmd       (optional) command that will reproduce this invocation
886 func_emit_lib_Makefile_am ()
887 {
888   if test "$libtool" = true; then
889     libext=la
890     perhapsLT=LT
891   else
892     libext=a
893     perhapsLT=
894   fi
895   echo "## Process this file with automake to produce Makefile.in."
896   func_emit_copyright_notice
897   if test -n "$actioncmd"; then
898     echo "# Reproduce by: $actioncmd"
899   fi
900   echo
901   # No need to generate dependencies since the sources are in gnulib, not here.
902   echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
903   echo
904   (
905     for module in $modules; do
906       func_verify_nontests_module
907       if test -n "$module"; then
908         {
909           func_get_automake_snippet "$module" |
910             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
911                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
912             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
913             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
914                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g'
915           if test "$module" = 'alloca'; then
916             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
917           fi
918         } > amsnippet.tmp
919         # Skip the contents if its entirely empty.
920         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
921           echo "## begin gnulib module $module"
922           echo
923           cat amsnippet.tmp
924           echo "## end   gnulib module $module"
925           echo
926         fi
927         rm -f amsnippet.tmp
928       fi
929     done
930   ) > allsnippets.tmp
931   if grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *= *$libname\\.$libext\$" allsnippets.tmp > /dev/null; then
932     # One of the snippets already specifies an installation location for the
933     # library. Don't confuse automake by saying it should not be installed.
934     :
935   else
936     # By default, the generated library should not be installed.
937     echo "noinst_${perhapsLT}LIBRARIES = $libname.$libext"
938   fi
939   echo
940   echo "${libname}_${libext}_SOURCES ="
941   echo "${libname}_${libext}_LIBADD = @${perhapsLT}LIBOBJS@"
942   echo "noinst_HEADERS ="
943   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
944     echo "pkgdata_DATA ="
945   fi
946   echo "EXTRA_DIST ="
947   echo "BUILT_SOURCES ="
948   echo "SUFFIXES ="
949   echo "MOSTLYCLEANFILES = core *.stackdump"
950   echo "MOSTLYCLEANDIRS ="
951   echo "CLEANFILES ="
952   echo "DISTCLEANFILES ="
953   echo "MAINTAINERCLEANFILES ="
954   echo
955   echo "AM_CPPFLAGS ="
956   echo
957   cat allsnippets.tmp
958   echo
959   echo "mostlyclean-local: mostlyclean-generic"
960   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
961   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
962   echo "            if test -d \$\$dir; then \\"
963   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
964   echo "            fi; \\"
965   echo "          done"
966   echo
967   echo "# Makefile.am ends here"
968   rm -f allsnippets.tmp
969 }
970
971 # func_emit_tests_Makefile_am
972 # emits the contents of tests/Makefile.am to standard output.
973 # Input:
974 # - modules         list of modules, including dependencies
975 # - libname         library name
976 # - libtool         true if libtool will be used, false or blank otherwise
977 # - sourcebase      relative directory containing lib source code
978 # - m4base          relative directory containing autoconf macros
979 # - testsbase       relative directory containing unit test code
980 func_emit_tests_Makefile_am ()
981 {
982   if test "$libtool" = true; then
983     libext=la
984   else
985     libext=a
986   fi
987   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
988   echo "## Process this file with automake to produce Makefile.in."
989   func_emit_copyright_notice
990   echo
991   # Generate dependencies here, since it eases the debugging of test failures.
992   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
993   echo
994   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
995   echo
996   (
997     for module in $modules; do
998       func_verify_tests_module
999       if test -n "$module"; then
1000         func_get_automake_snippet "$module" > amsnippet.tmp
1001         # Skip the contents if its entirely empty.
1002         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1003           echo "## begin gnulib module $module"
1004           echo
1005           cat amsnippet.tmp
1006           echo "## end   gnulib module $module"
1007           echo
1008         fi
1009         rm -f amsnippet.tmp
1010       fi
1011     done
1012   ) > allsnippets.tmp
1013   # Nothing is being added to SUBDIRS; nevertheless the existence of this
1014   # variable is needed to avoid an error from automake:
1015   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
1016   echo "SUBDIRS ="
1017   echo "TESTS ="
1018   echo "TESTS_ENVIRONMENT ="
1019   echo "noinst_PROGRAMS ="
1020   echo "check_PROGRAMS ="
1021   echo "noinst_HEADERS ="
1022   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1023     echo "pkgdata_DATA ="
1024   fi
1025   echo "EXTRA_DIST ="
1026   echo "BUILT_SOURCES ="
1027   echo "SUFFIXES ="
1028   echo "MOSTLYCLEANFILES = core *.stackdump"
1029   echo "MOSTLYCLEANDIRS ="
1030   echo "CLEANFILES ="
1031   echo "DISTCLEANFILES ="
1032   echo "MAINTAINERCLEANFILES ="
1033   echo
1034   echo "AM_CPPFLAGS = \\"
1035   echo "  -I. -I\$(srcdir) \\"
1036   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
1037   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
1038   echo
1039   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
1040   echo
1041   cat allsnippets.tmp
1042   echo "# Clean up after Solaris cc."
1043   echo "clean-local:"
1044   echo "        rm -rf SunWS_cache"
1045   echo
1046   echo "mostlyclean-local: mostlyclean-generic"
1047   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
1048   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
1049   echo "            if test -d \$\$dir; then \\"
1050   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1051   echo "            fi; \\"
1052   echo "          done"
1053   echo
1054   echo "# Makefile.am ends here"
1055   rm -f allsnippets.tmp
1056 }
1057
1058 # func_import modules
1059 # Uses also the variables
1060 # - destdir         target directory
1061 # - libname         library name
1062 # - sourcebase      directory relative to destdir where to place source code
1063 # - m4base          directory relative to destdir where to place *.m4 macros
1064 # - docbase         directory relative to destdir where to place doc files
1065 # - testsbase       directory relative to destdir where to place unit test code
1066 # - auxdir          directory relative to destdir where to place build aux files
1067 # - inctests        true if --with-tests was given, blank otherwise
1068 # - avoidlist       list of modules to avoid, from --avoid
1069 # - lgpl            true if library's license shall be LGPL, blank otherwise
1070 # - libtool         true if --libtool was given, false if --no-libtool was
1071 #                   given, blank otherwise
1072 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
1073 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1074 # - autoconf_minversion  minimum supported autoconf version
1075 # - doit            : if actions shall be executed, false if only to be printed
1076 # - symbolic        true if files should be symlinked, copied otherwise
1077 func_import ()
1078 {
1079   # Get the cached settings.
1080   cached_specified_modules=
1081   cached_avoidlist=
1082   cached_sourcebase=
1083   cached_m4base=
1084   cached_docbase=
1085   cached_testsbase=
1086   cached_libname=
1087   cached_lgpl=
1088   cached_libtool=
1089   cached_macro_prefix=
1090   cached_files=
1091   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1092     cached_libtool=false
1093     my_sed_traces='
1094       s,#.*$,,
1095       s,^dnl .*$,,
1096       s, dnl .*$,,
1097       /gl_MODULES(/ {
1098         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
1099       }
1100       /gl_AVOID(/ {
1101         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
1102       }
1103       /gl_SOURCE_BASE(/ {
1104         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
1105       }
1106       /gl_M4_BASE(/ {
1107         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
1108       }
1109       /gl_DOC_BASE(/ {
1110         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
1111       }
1112       /gl_TESTS_BASE(/ {
1113         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
1114       }
1115       /gl_LIB(/ {
1116         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
1117       }
1118       /gl_LGPL/ {
1119         s,^.*$,cached_lgpl=true,p
1120       }
1121       /gl_LIBTOOL/ {
1122         s,^.*$,cached_libtool=true,p
1123       }
1124       /gl_MACRO_PREFIX(/ {
1125         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
1126       }'
1127     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
1128     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1129       my_sed_traces='
1130         s,#.*$,,
1131         s,^dnl .*$,,
1132         s, dnl .*$,,
1133         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
1134           s,^.*$,cached_files=",p
1135           n
1136           ta
1137           :a
1138           s,^\]).*$,",
1139           tb
1140           p
1141           n
1142           ba
1143           :b
1144           p
1145         }'
1146       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1147     fi
1148   fi
1149
1150   # Merge the cached settings with the specified ones.
1151   # The m4base must be the same as expected from the pathname.
1152   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1153     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1154   fi
1155   # Append the cached and the specified module names. So that
1156   # "gnulib-tool --import foo" means to add the module foo.
1157   specified_modules="$cached_specified_modules $1"
1158   # Append the cached and the specified avoidlist. This is probably better
1159   # than dropping the cached one when --avoid is specified at least once.
1160   avoidlist=`echo $cached_avoidlist $avoidlist`
1161   # The sourcebase defaults to the cached one.
1162   if test -z "$sourcebase"; then
1163     sourcebase="$cached_sourcebase"
1164     if test -z "$sourcebase"; then
1165       func_fatal_error "missing --source-base option"
1166     fi
1167   fi
1168   # The docbase defaults to the cached one.
1169   if test -z "$docbase"; then
1170     docbase="$cached_docbase"
1171     if test -z "$docbase"; then
1172       func_fatal_error "missing --doc-base option. --doc-base has been introduced on 2006-07-11; if your last invocation of 'gnulib-tool --import' is before that date, you need to run 'gnulib-tool --import' once, with a --doc-base option."
1173     fi
1174   fi
1175   # The testsbase defaults to the cached one.
1176   if test -z "$testsbase"; then
1177     testsbase="$cached_testsbase"
1178     if test -z "$testsbase"; then
1179       func_fatal_error "missing --tests-base option"
1180     fi
1181   fi
1182   # The libname defaults to the cached one.
1183   if test -z "$supplied_libname"; then
1184     libname="$cached_libname"
1185     if test -z "$libname"; then
1186       func_fatal_error "missing --lib option"
1187     fi
1188   fi
1189   # Require LGPL if specified either way.
1190   if test -z "$lgpl"; then
1191     lgpl="$cached_lgpl"
1192   fi
1193   # Use libtool if specified either way, or if guessed.
1194   if test -z "$libtool"; then
1195     if test -n "$cached_m4base"; then
1196       libtool="$cached_libtool"
1197     else
1198       libtool="$guessed_libtool"
1199     fi
1200   fi
1201   # The macro_prefix defaults to the cached one.
1202   if test -z "$macro_prefix"; then
1203     macro_prefix="$cached_macro_prefix"
1204     if test -z "$macro_prefix"; then
1205       func_fatal_error "missing --macro-prefix option"
1206     fi
1207   fi
1208
1209   # Canonicalize the list of specified modules.
1210   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
1211
1212   # Determine final module list.
1213   modules="$specified_modules"
1214   func_modules_transitive_closure
1215   echo "Module list with included dependencies:"
1216   echo "$modules" | sed -e 's/^/  /'
1217
1218   # Add the dummy module if needed.
1219   func_modules_add_dummy
1220
1221   # If --lgpl, check the license of modules are compatible.
1222   if test -n "$lgpl"; then
1223     for module in $modules; do
1224       license=`func_get_license $module`
1225       case $license in
1226         LGPL | 'GPLed build tool' | 'public domain' | 'unlimited') ;;
1227         *) func_fatal_error "incompatible license on module $module: $license" ;;
1228       esac
1229     done
1230   fi
1231
1232   # Determine script to apply to imported library files.
1233   sed_transform_lib_file=
1234   case $nl$modules$nl in
1235     *"${nl}config-h$nl"*)
1236       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
1237       sed_transform_lib_file=$sed_transform_lib_file'
1238         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
1239       ' ;;
1240   esac
1241   if test -n "$lgpl"; then
1242     # Update license.
1243     sed_transform_lib_file=$sed_transform_lib_file'
1244       s/GNU General/GNU Lesser General/g
1245       s/version 2\([ ,]\)/version 2.1\1/g
1246     '
1247   fi
1248
1249   # Determine final file list.
1250   func_modules_to_filelist
1251   echo "File list:"
1252   echo "$files" | sed -e 's/^/  /'
1253
1254   test -n "$files" \
1255     || func_fatal_error "refusing to do nothing"
1256
1257   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1258   new_files="$files m4/gnulib-tool.m4"
1259   old_files="$cached_files"
1260   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1261     old_files="$old_files m4/gnulib-tool.m4"
1262   fi
1263
1264   # Create directories.
1265   if test ! -d "$destdir/$sourcebase"; then
1266     if $doit; then
1267       echo "Creating directory $destdir/$sourcebase"
1268       mkdir "$destdir/$sourcebase" || func_fatal_error "failed"
1269     else
1270       echo "Create directory $destdir/$sourcebase"
1271     fi
1272   fi
1273   if test ! -d "$destdir/$m4base"; then
1274     if $doit; then
1275       echo "Creating directory $destdir/$m4base"
1276       mkdir "$destdir/$m4base" || func_fatal_error "failed"
1277     else
1278       echo "Create directory $destdir/$m4base"
1279     fi
1280   fi
1281   docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1282   if test -n "$docfiles"; then
1283     if test ! -d "$destdir/$docbase"; then
1284       if $doit; then
1285         echo "Creating directory $destdir/$docbase"
1286         mkdir "$destdir/$docbase" || func_fatal_error "failed"
1287       else
1288         echo "Create directory $destdir/$docbase"
1289       fi
1290     fi
1291   fi
1292   if test -n "$inctests"; then
1293     if test ! -d "$destdir/$testsbase"; then
1294       if $doit; then
1295         echo "Creating directory $destdir/$testsbase"
1296         mkdir "$destdir/$testsbase" || func_fatal_error "failed"
1297       else
1298         echo "Create directory $destdir/$testsbase"
1299       fi
1300     fi
1301   fi
1302   if test ! -d "$destdir/$auxdir"; then
1303     if $doit; then
1304       echo "Creating directory $destdir/$auxdir"
1305       mkdir "$destdir/$auxdir" || func_fatal_error "failed"
1306     else
1307       echo "Create directory $destdir/$auxdir"
1308     fi
1309   fi
1310
1311   # func_dest_tmpfilename file
1312   # determines the name of a temporary file (file is relative to destdir).
1313   # Sets variable:
1314   #   - tmpfile       absolute filename of the temporary file
1315   func_dest_tmpfilename ()
1316   {
1317     if $doit; then
1318       # Put the new contents of $file in a file in the same directory (needed
1319       # to guarantee that an 'mv' to "$destdir/$file" works).
1320       tmpfile="$destdir/$1.tmp"
1321     else
1322       # Put the new contents of $file in a file in a temporary directory
1323       # (because the directory of "$file" might not exist).
1324       tmpfile="$tmp"/`basename "$1"`.tmp
1325     fi
1326   }
1327
1328   # Copy files or make symbolic links. Remove obsolete files.
1329   delimiter='   '
1330   for f in $old_files; do
1331     case "$f" in
1332       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1333       doc/*) g=`echo "$f" | sed -e "s,^doc/,$cached_docbase/,"` ;;
1334       lib/*) g=`echo "$f" | sed -e "s,^lib/,$cached_sourcebase/,"` ;;
1335       m4/*) g=`echo "$f" | sed -e "s,^m4/,$cached_m4base/,"` ;;
1336       tests/*) g=`echo "$f" | sed -e "s,^tests/,$cached_testsbase/,"` ;;
1337       *) g="$f" ;;
1338     esac
1339     echo "$g""$delimiter""$f"
1340   done | LC_ALL=C sort > "$tmp"/old-files
1341   for f in $new_files; do
1342     case "$f" in
1343       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1344       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1345       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1346       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1347       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1348       *) g="$f" ;;
1349     esac
1350     echo "$g""$delimiter""$f"
1351   done | LC_ALL=C sort > "$tmp"/new-files
1352   # First the files that are in old-files, but not in new-files:
1353   sed_take_first_column='s,'"$delimiter"'.*,,'
1354   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1355     # Remove the file. Do nothing if the user already removed it.
1356     if test -f "$destdir/$g"; then
1357       if $doit; then
1358         echo "Removing file $g (backup in ${g}~)"
1359         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1360       else
1361         echo "Remove file $g (backup in ${g}~)"
1362       fi
1363     fi
1364   done
1365   # func_add_or_update handles a file that ought to be present afterwards.
1366   # Uses parameters f, g, already_present.
1367   func_add_or_update ()
1368   {
1369     func_dest_tmpfilename "$g"
1370     func_lookup_file "$f"
1371     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
1372     if test -n "sed_transform_lib_file"; then
1373       case "$f" in
1374         lib/*)
1375           sed -e "$sed_transform_lib_file" \
1376             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
1377           ;;
1378       esac
1379     fi
1380     if test -f "$destdir/$g"; then
1381       # The file already exists.
1382       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1383         : # The file has not changed.
1384       else
1385         # Replace the file.
1386         if $doit; then
1387           if test -n "$already_present"; then
1388             echo "Updating file $g (backup in ${g}~)"
1389           else
1390             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1391           fi
1392           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1393           if test -n "$symbolic" && test -z "$lookedup_tmp" \
1394              && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1395             func_ln_if_changed "$lookedup_file" "$destdir/$g"
1396           else
1397             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1398           fi
1399         else
1400           if test -n "$already_present"; then
1401             echo "Update file $g (backup in ${g}~)"
1402           else
1403             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1404           fi
1405         fi
1406       fi
1407     else
1408       # Install the file.
1409       # Don't protest if the file should be there but isn't: it happens
1410       # frequently that developers don't put autogenerated files into CVS.
1411       if $doit; then
1412         echo "Copying file $g"
1413         if test -n "$symbolic" && test -z "$lookedup_tmp" \
1414            && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1415           func_ln_if_changed "$lookedup_file" "$destdir/$g"
1416         else
1417           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1418         fi
1419       else
1420         echo "Copy file $g"
1421       fi
1422     fi
1423     rm -f "$tmpfile"
1424   }
1425   # Then the files that are in new-files, but not in old-files:
1426   sed_take_last_column='s,^.*'"$delimiter"',,'
1427   already_present=
1428   for f in `LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1429     case "$f" in
1430       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1431       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1432       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1433       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1434       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1435       *) g="$f" ;;
1436     esac
1437     func_add_or_update
1438   done
1439   # Then the files that are in new-files and in old-files:
1440   already_present=true
1441   for f in `LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1442     case "$f" in
1443       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1444       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1445       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1446       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1447       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1448       *) g="$f" ;;
1449     esac
1450     func_add_or_update
1451   done
1452
1453   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1454   actioncmd="gnulib-tool --import"
1455   actioncmd="$actioncmd --dir=$destdir"
1456   actioncmd="$actioncmd --lib=$libname"
1457   actioncmd="$actioncmd --source-base=$sourcebase"
1458   actioncmd="$actioncmd --m4-base=$m4base"
1459   actioncmd="$actioncmd --doc-base=$docbase"
1460   actioncmd="$actioncmd --aux-dir=$auxdir"
1461   for module in $avoidlist; do
1462     actioncmd="$actioncmd --avoid=$module"
1463   done
1464   if test -n "$lgpl"; then
1465     actioncmd="$actioncmd --lgpl"
1466   fi
1467   if test "$libtool" = true; then
1468     actioncmd="$actioncmd --libtool"
1469   else
1470     actioncmd="$actioncmd --no-libtool"
1471   fi
1472   actioncmd="$actioncmd --macro-prefix=$macro_prefix"
1473   actioncmd="$actioncmd `echo $specified_modules`"
1474
1475   # Create lib/Makefile.am.
1476   func_dest_tmpfilename $sourcebase/Makefile.am
1477   func_emit_lib_Makefile_am > "$tmpfile"
1478   if test -f "$destdir"/$sourcebase/Makefile.am; then
1479     if cmp "$destdir"/$sourcebase/Makefile.am "$tmpfile" > /dev/null; then
1480       rm -f "$tmpfile"
1481     else
1482       if $doit; then
1483         echo "Updating $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1484         mv -f "$destdir"/$sourcebase/Makefile.am "$destdir"/$sourcebase/Makefile.am~
1485         mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1486       else
1487         echo "Update $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1488         rm -f "$tmpfile"
1489       fi
1490     fi
1491   else
1492     if $doit; then
1493       echo "Creating $sourcebase/Makefile.am"
1494       mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1495     else
1496       echo "Create $sourcebase/Makefile.am"
1497       rm -f "$tmpfile"
1498     fi
1499   fi
1500
1501   # Create m4/gnulib-cache.m4.
1502   func_dest_tmpfilename $m4base/gnulib-cache.m4
1503   (
1504     func_emit_copyright_notice
1505     echo "#"
1506     echo "# This file represents the specification of how gnulib-tool is used."
1507     echo "# It acts as a cache: It is written and read by gnulib-tool."
1508     echo "# In projects using CVS, this file is meant to be stored in CVS,"
1509     echo "# like the configure.ac and various Makefile.am files."
1510     echo
1511     echo
1512     echo "# Specification in the form of a command-line invocation:"
1513     echo "#   $actioncmd"
1514     echo
1515     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
1516     echo "gl_MODULES(["`echo $specified_modules`"])"
1517     echo "gl_AVOID([$avoidlist])"
1518     echo "gl_SOURCE_BASE([$sourcebase])"
1519     echo "gl_M4_BASE([$m4base])"
1520     echo "gl_DOC_BASE([$docbase])"
1521     echo "gl_TESTS_BASE([$testsbase])"
1522     echo "gl_LIB([$libname])"
1523     test -z "$lgpl" || echo "gl_LGPL"
1524     if test "$libtool" = true; then
1525       echo "gl_LIBTOOL"
1526     fi
1527     echo "gl_MACRO_PREFIX([$macro_prefix])"
1528   ) > "$tmpfile"
1529   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1530     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
1531       rm -f "$tmpfile"
1532     else
1533       if $doit; then
1534         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1535         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
1536         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1537       else
1538         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1539         if false; then
1540           cat "$tmpfile"
1541           echo
1542           echo "# gnulib-cache.m4 ends here"
1543         fi
1544         rm -f "$tmpfile"
1545       fi
1546     fi
1547   else
1548     if $doit; then
1549       echo "Creating $m4base/gnulib-cache.m4"
1550       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1551     else
1552       echo "Create $m4base/gnulib-cache.m4"
1553       cat "$tmpfile"
1554       rm -f "$tmpfile"
1555     fi
1556   fi
1557
1558   # Create m4/gnulib-comp.m4.
1559   func_dest_tmpfilename $m4base/gnulib-comp.m4
1560   (
1561     func_emit_copyright_notice
1562     echo "#"
1563     echo "# This file represents the compiled summary of the specification in"
1564     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
1565     echo "# to be invoked from configure.ac."
1566     echo "# In projects using CVS, this file can be treated like other built files."
1567     echo
1568     echo
1569     echo "# This macro should be invoked from $configure_ac, in the section"
1570     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
1571     echo "# any checks for libraries, header files, types and library functions."
1572     echo "AC_DEFUN([${macro_prefix}_EARLY],"
1573     echo "["
1574     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
1575     echo "  m4_pattern_allow([^gl_ES$])dnl a valid locale name"
1576     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
1577     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1578       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
1579     fi
1580     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1581       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
1582     fi
1583     if grep gl_LOCK "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1584       echo "  AC_REQUIRE([gl_LOCK_EARLY])"
1585     fi
1586     echo "])"
1587     echo
1588     echo "# This macro should be invoked from $configure_ac, in the section"
1589     echo "# \"Check for header files, types and library functions\"."
1590     echo "AC_DEFUN([${macro_prefix}_INIT],"
1591     echo "["
1592     if test "$libtool" = true; then
1593       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1594     else
1595       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1596     fi
1597     if test "$auxdir" != "build-aux"; then
1598       sed_replace_build_aux='
1599         :a
1600         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1601           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1602           ba
1603         }'
1604       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1605     else
1606       sed_replace_build_aux=
1607     fi
1608     for module in $modules; do
1609       func_verify_module
1610       if test -n "$module"; then
1611         func_get_autoconf_snippet "$module" \
1612           | sed -e '/^$/d;' -e 's/^/  /' \
1613                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
1614                 -e "$sed_replace_build_aux"
1615         if test "$module" = 'alloca' && test "$libtool" = true; then
1616           echo 'changequote(,)dnl'
1617           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
1618           echo 'changequote([, ])dnl'
1619           echo 'AC_SUBST([LTALLOCA])'
1620         fi
1621       fi
1622     done
1623     echo "])"
1624     echo
1625     echo "# This macro records the list of files which have been installed by"
1626     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
1627     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
1628     echo "$files" | sed -e 's,^,  ,'
1629     echo "])"
1630   ) > "$tmpfile"
1631   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1632     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
1633       rm -f "$tmpfile"
1634     else
1635       if $doit; then
1636         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1637         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
1638         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1639       else
1640         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1641         if false; then
1642           cat "$tmpfile"
1643           echo
1644           echo "# gnulib-comp.m4 ends here"
1645         fi
1646         rm -f "$tmpfile"
1647       fi
1648     fi
1649   else
1650     if $doit; then
1651       echo "Creating $m4base/gnulib-comp.m4"
1652       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1653     else
1654       echo "Create $m4base/gnulib-comp.m4"
1655       cat "$tmpfile"
1656       rm -f "$tmpfile"
1657     fi
1658   fi
1659
1660   if test -n "$inctests"; then
1661     # Create tests/Makefile.am.
1662     func_dest_tmpfilename $testsbase/Makefile.am
1663     func_emit_tests_Makefile_am > "$tmpfile"
1664     if test -f "$destdir"/$testsbase/Makefile.am; then
1665       if cmp "$destdir"/$testsbase/Makefile.am "$tmpfile" > /dev/null; then
1666         rm -f "$tmpfile"
1667       else
1668         if $doit; then
1669           echo "Updating $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1670           mv -f "$destdir"/$testsbase/Makefile.am "$destdir"/$testsbase/Makefile.am~
1671           mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1672         else
1673           echo "Update $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1674           rm -f "$tmpfile"
1675         fi
1676       fi
1677     else
1678       if $doit; then
1679         echo "Creating $testsbase/Makefile.am"
1680         mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1681       else
1682         echo "Create $testsbase/Makefile.am"
1683         rm -f "$tmpfile"
1684       fi
1685     fi
1686   fi
1687
1688   echo "Finished."
1689   echo
1690   echo "You may need to add #include directives for the following .h files."
1691   (
1692    # First the #include <...> directives without #ifs, sorted for convenience.
1693    for module in $modules; do
1694      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1695        :
1696      else
1697        func_get_include_directive "$module" | grep -v 'include "'
1698      fi
1699    done | LC_ALL=C sort -u
1700    # Then the #include "..." directives without #ifs, sorted for convenience.
1701    for module in $modules; do
1702      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1703        :
1704      else
1705        func_get_include_directive "$module" | grep 'include "'
1706      fi
1707    done | LC_ALL=C sort -u
1708    # Then the #include directives that are surrounded by #ifs. Not sorted.
1709    for module in $modules; do
1710      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1711        func_get_include_directive "$module"
1712      fi
1713    done
1714   ) | sed -e '/^$/d;' -e 's/^/  /'
1715   echo
1716   echo "Don't forget to"
1717   echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1718   if test -n "$inctests"; then
1719     echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1720   fi
1721   sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
1722   sourcebase_base=`basename "$sourcebase"`
1723   echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
1724   if test -n "$inctests"; then
1725     testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
1726     testsbase_base=`basename "$testsbase"`
1727     echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
1728   fi
1729   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
1730   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
1731   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
1732 }
1733
1734 # func_create_testdir testdir modules
1735 # Input:
1736 # - auxdir          directory relative to destdir where to place build aux files
1737 func_create_testdir ()
1738 {
1739   testdir="$1"
1740   modules="$2"
1741   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
1742
1743   # Subdirectory names.
1744   sourcebase=gllib
1745   m4base=glm4
1746   testsbase=gltests
1747
1748   # Determine final module list.
1749   func_modules_transitive_closure
1750   echo "Module list with included dependencies:"
1751   echo "$modules" | sed -e 's/^/  /'
1752
1753   # Add the dummy module if needed.
1754   func_modules_add_dummy
1755
1756   # Determine final file list.
1757   func_modules_to_filelist
1758   echo "File list:"
1759   echo "$files" | sed -e 's/^/  /'
1760
1761   # Create directories.
1762   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
1763     case "$d" in
1764       build-aux) mkdir -p "$testdir/$auxdir" ;;
1765       lib)       mkdir -p "$testdir/$sourcebase" ;;
1766       m4)        mkdir -p "$testdir/$m4base" ;;
1767       tests)     mkdir -p "$testdir/$testsbase" ;;
1768       *)         mkdir -p "$testdir/$d" ;;
1769     esac
1770   done
1771
1772   # Copy files or make symbolic links.
1773   for f in $files; do
1774     func_lookup_file "$f"
1775     case "$f" in
1776       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1777       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1778       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1779       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1780       *) g="$f" ;;
1781     esac
1782     if test -n "$lookedup_tmp"; then
1783       cp -p "$lookedup_file" "$testdir/$g"
1784     else
1785       ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
1786       if test -z "$symbolic"; then
1787         cp -p "$lookedup_file" "$testdir/$g"
1788       else
1789         ln -s "$lookedup_file" "$testdir/$g"
1790       fi
1791     fi
1792   done
1793
1794   # Create $sourcebase/Makefile.am.
1795   mkdir -p "$testdir/$sourcebase"
1796   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
1797
1798   # Create $m4base/Makefile.am.
1799   mkdir -p "$testdir/$m4base"
1800   (echo "## Process this file with automake to produce Makefile.in."
1801    echo
1802    echo "EXTRA_DIST ="
1803    for f in $files; do
1804      case "$f" in
1805        m4/* )
1806          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
1807      esac
1808    done
1809   ) > "$testdir/$m4base/Makefile.am"
1810
1811   subdirs="$sourcebase $m4base"
1812   subdirs_with_configure_ac=""
1813
1814   if test -f "$testdir"/$m4base/gettext.m4; then
1815     # Avoid stupid error message from automake:
1816     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
1817     mkdir -p "$testdir/po"
1818     (echo "## Process this file with automake to produce Makefile.in."
1819     ) > "$testdir/po/Makefile.am"
1820     subdirs="$subdirs po"
1821   fi
1822
1823   if test -n "$inctests"; then
1824     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
1825     # Create $testsbase/Makefile.am.
1826     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
1827     # Create $testsbase/configure.ac.
1828     (echo "# Process this file with autoconf to produce a configure script."
1829      echo "AC_INIT([dummy], [0])"
1830      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
1831      echo "AM_INIT_AUTOMAKE"
1832      echo
1833      echo "AM_CONFIG_HEADER([config.h])"
1834      echo
1835      echo "AC_PROG_CC"
1836      echo "AC_PROG_INSTALL"
1837      echo "AC_PROG_MAKE_SET"
1838      echo "AC_PROG_RANLIB"
1839      echo
1840      if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1841        echo "AC_GNU_SOURCE"
1842        echo
1843      fi
1844      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1845        echo "gl_USE_SYSTEM_EXTENSIONS"
1846        echo
1847      fi
1848      if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1849        echo "gl_LOCK_EARLY"
1850        echo
1851      fi
1852      if test "$libtool" = true; then
1853        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1854      else
1855        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1856      fi
1857      # Wrap the set of autoconf snippets into an autoconf macro that is then
1858      # invoked. This is needed because autoconf does not support AC_REQUIRE
1859      # at the top level:
1860      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
1861      # but we want the AC_REQUIRE to have its normal meaning (provide one
1862      # expansion of the required macro before the current point, and only one
1863      # expansion total).
1864      echo "AC_DEFUN([gl_INIT], ["
1865      if test "$auxdir" != "build-aux"; then
1866        sed_replace_build_aux='
1867          :a
1868          /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1869            s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
1870            ba
1871          }'
1872        sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1873      else
1874        sed_replace_build_aux=
1875      fi
1876      # We don't have explicit ordering constraints between the various
1877      # autoconf snippets. It's cleanest to put those of the library before
1878      # those of the tests.
1879      for module in $modules; do
1880        func_verify_nontests_module
1881        if test -n "$module"; then
1882          func_get_autoconf_snippet "$module" \
1883            | sed -e "$sed_replace_build_aux"
1884        fi
1885      done
1886      for module in $modules; do
1887        func_verify_tests_module
1888        if test -n "$module"; then
1889          func_get_autoconf_snippet "$module" \
1890            | sed -e "$sed_replace_build_aux"
1891        fi
1892      done
1893      echo "])"
1894      echo "gl_INIT"
1895      echo
1896      # Usually $testsbase/config.h will be a superset of config.h. Verify this
1897      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
1898      echo "AH_TOP([#include \"../config.h\"])"
1899      echo
1900      echo "AC_OUTPUT([Makefile])"
1901     ) > "$testdir/$testsbase/configure.ac"
1902     subdirs="$subdirs $testsbase"
1903     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
1904   fi
1905
1906   # Create Makefile.am.
1907   (echo "## Process this file with automake to produce Makefile.in."
1908    echo
1909    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1910    echo
1911    echo "SUBDIRS = $subdirs"
1912    echo
1913    echo "ACLOCAL_AMFLAGS = -I $m4base"
1914   ) > "$testdir/Makefile.am"
1915
1916   # Create configure.ac.
1917   (echo "# Process this file with autoconf to produce a configure script."
1918    echo "AC_INIT([dummy], [0])"
1919    if test "$auxdir" != "."; then
1920      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1921    fi
1922    echo "AM_INIT_AUTOMAKE"
1923    echo
1924    echo "AM_CONFIG_HEADER([config.h])"
1925    echo
1926    echo "AC_PROG_CC"
1927    echo "AC_PROG_INSTALL"
1928    echo "AC_PROG_MAKE_SET"
1929    echo
1930    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
1931    echo "m4_pattern_allow([^gl_ES$])dnl a valid locale name"
1932    echo
1933    echo "AC_PROG_RANLIB"
1934    echo
1935    if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1936      echo "AC_GNU_SOURCE"
1937      echo
1938    fi
1939    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1940      echo "gl_USE_SYSTEM_EXTENSIONS"
1941      echo
1942    fi
1943    if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1944      echo "gl_LOCK_EARLY"
1945      echo
1946    fi
1947    if test "$libtool" = true; then
1948      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1949    else
1950      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1951    fi
1952    # Wrap the set of autoconf snippets into an autoconf macro that is then
1953    # invoked. This is needed because autoconf does not support AC_REQUIRE
1954    # at the top level:
1955    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
1956    # but we want the AC_REQUIRE to have its normal meaning (provide one
1957    # expansion of the required macro before the current point, and only one
1958    # expansion total).
1959    echo "AC_DEFUN([gl_INIT], ["
1960    if test "$auxdir" != "build-aux"; then
1961      sed_replace_build_aux='
1962        :a
1963        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1964          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1965          ba
1966        }'
1967      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1968    else
1969      sed_replace_build_aux=
1970    fi
1971    for module in $modules; do
1972      func_verify_nontests_module
1973      if test -n "$module"; then
1974        func_get_autoconf_snippet "$module" \
1975          | sed -e "$sed_replace_build_aux"
1976      fi
1977    done
1978    echo "])"
1979    echo "gl_INIT"
1980    echo
1981    if test -n "$subdirs_with_configure_ac"; then
1982      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
1983    fi
1984    makefiles="Makefile"
1985    for d in $subdirs; do
1986      # For subdirs that have a configure.ac by their own, it's the subdir's
1987      # configure.ac which creates the subdir's Makefile.am, not this one.
1988      case " $subdirs_with_configure_ac " in
1989        *" $d "*) ;;
1990        *) makefiles="$makefiles $d/Makefile" ;;
1991      esac
1992    done
1993    echo "AC_OUTPUT([$makefiles])"
1994   ) > "$testdir/configure.ac"
1995
1996   # Create autogenerated files.
1997   (cd "$testdir"
1998    echo "executing ${AUTORECONF} --force --install"
1999    ${AUTORECONF} --force --install
2000   )
2001   if grep '^BUILT_SOURCES *+=' "$testdir/$sourcebase/Makefile.am" > /dev/null; then
2002     (cd "$testdir"
2003      ./configure
2004        cd "$sourcebase"
2005        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
2006        make built_sources
2007        cd ..
2008      make distclean
2009     )
2010   fi
2011 }
2012
2013 # func_create_megatestdir megatestdir allmodules
2014 # Input:
2015 # - auxdir          directory relative to destdir where to place build aux files
2016 func_create_megatestdir ()
2017 {
2018   megatestdir="$1"
2019   allmodules="$2"
2020   if test -z "$allmodules"; then
2021     allmodules=`func_all_modules`
2022   fi
2023
2024   megasubdirs=
2025   # First, all modules one by one.
2026   for onemodule in $allmodules; do
2027     func_create_testdir "$megatestdir/$onemodule" $onemodule
2028     megasubdirs="${megasubdirs}$onemodule "
2029   done
2030   # Then, all modules all together.
2031   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2032   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
2033   func_create_testdir "$megatestdir/ALL" "$allmodules"
2034   megasubdirs="${megasubdirs}ALL"
2035
2036   # Create Makefile.am.
2037   (echo "## Process this file with automake to produce Makefile.in."
2038    echo
2039    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2040    echo
2041    echo "SUBDIRS = $megasubdirs"
2042   ) > "$megatestdir/Makefile.am"
2043
2044   # Create configure.ac.
2045   (echo "# Process this file with autoconf to produce a configure script."
2046    echo "AC_INIT([dummy], [0])"
2047    if test "$auxdir" != "."; then
2048      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2049    fi
2050    echo "AM_INIT_AUTOMAKE"
2051    echo
2052    echo "AC_PROG_MAKE_SET"
2053    echo
2054    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
2055    echo "AC_OUTPUT([Makefile])"
2056   ) > "$megatestdir/configure.ac"
2057
2058   # Create autogenerated files.
2059   (cd "$megatestdir"
2060    # Do not use "${AUTORECONF} --install", because autoreconf operates
2061    # recursively, but the subdirectories are already finished, therefore
2062    # calling autoreconf here would only waste lots of CPU time.
2063    echo "executing ${ACLOCAL}"
2064    ${ACLOCAL}
2065    echo "executing mkdir build-aux"
2066    mkdir build-aux
2067    echo "executing ${AUTOCONF}"
2068    ${AUTOCONF}
2069    echo "executing ${AUTOMAKE} --add-missing --copy"
2070    ${AUTOMAKE} --add-missing --copy
2071   )
2072 }
2073
2074 case $mode in
2075   "" )
2076     func_fatal_error "no mode specified" ;;
2077
2078   list )
2079     func_all_modules
2080     ;;
2081
2082   import | update )
2083
2084     # Where to import.
2085     if test -z "$destdir"; then
2086       destdir=.
2087     fi
2088     test -d "$destdir" \
2089       || func_fatal_error "destination directory does not exist: $destdir"
2090
2091     # Prefer configure.ac to configure.in.
2092     if test -f "$destdir"/configure.ac; then
2093       configure_ac="$destdir/configure.ac"
2094     else
2095       if test -f "$destdir"/configure.in; then
2096         configure_ac="$destdir/configure.in"
2097       else
2098         func_fatal_error "cannot find $destdir/configure.ac"
2099       fi
2100     fi
2101
2102     # Analyze configure.ac.
2103     guessed_auxdir="."
2104     guessed_libtool=false
2105     my_sed_traces='
2106       s,#.*$,,
2107       s,^dnl .*$,,
2108       s, dnl .*$,,
2109       /AC_CONFIG_AUX_DIR/ {
2110         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
2111       }
2112       /A[CM]_PROG_LIBTOOL/ {
2113         s,^.*$,guessed_libtool=true,p
2114       }'
2115     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
2116
2117     if test -z "$auxdir"; then
2118       auxdir="$guessed_auxdir"
2119     fi
2120
2121     # Determine where to apply func_import.
2122     if test -n "$m4base"; then
2123       # Apply func_import to a particular gnulib directory.
2124       # Any number of additional modules can be given.
2125       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
2126         # First use of gnulib in the given m4base.
2127         test -n "$supplied_libname" || supplied_libname=true
2128         test -n "$sourcebase" || sourcebase="lib"
2129         test -n "$docbase" || docbase="doc"
2130         test -n "$testsbase" || testsbase="tests"
2131         test -n "$macro_prefix" || macro_prefix="gl"
2132       fi
2133       func_import "$*"
2134     else
2135       # Apply func_import to all gnulib directories.
2136       # To get this list of directories, look at Makefile.am. (Not at
2137       # configure, because it may be omitted from CVS. Also, don't run
2138       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
2139       m4dirs=
2140       m4dirs_count=0
2141       if test -f "$destdir"/Makefile.am; then
2142         aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[         ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
2143         m4dir_is_next=
2144         for arg in $aclocal_amflags; do
2145           if test -n "$m4dir_is_next"; then
2146             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
2147             case "$arg" in
2148               /*) ;;
2149               *)
2150                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
2151                   m4dirs="$m4dirs $arg"
2152                   m4dirs_count=`expr $m4dirs_count + 1`
2153                 fi
2154                 ;;
2155             esac
2156           else
2157             if test "X$arg" = "X-I"; then
2158               m4dir_is_next=yes
2159             else
2160               m4dir_is_next=
2161             fi
2162           fi
2163         done
2164       else
2165         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
2166         if test -f "$destdir"/aclocal.m4; then
2167           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
2168           sedexpr2='s,^[^/]*$,.,'
2169           sedexpr3='s,/[^/]*$,,'
2170           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort | LC_ALL=C uniq`
2171           m4dirs_count=`echo "$m4dirs" | wc -l`
2172         fi
2173       fi
2174       if test $m4dirs_count = 0; then
2175         # First use of gnulib in a package.
2176         # Any number of additional modules can be given.
2177         test -n "$supplied_libname" || supplied_libname=true
2178         test -n "$sourcebase" || sourcebase="lib"
2179         m4base="m4"
2180         test -n "$docbase" || docbase="doc"
2181         test -n "$testsbase" || testsbase="tests"
2182         test -n "$macro_prefix" || macro_prefix="gl"
2183         func_import "$*"
2184       else
2185         if test $m4dirs_count = 1; then
2186           # There's only one use of gnulib here. Assume the user means it.
2187           # Any number of additional modules can be given.
2188           for m4base in $m4dirs; do
2189             func_import "$*"
2190           done
2191         else
2192           # Ambiguous - guess what the user meant.
2193           if test $# = 0; then
2194             # No further arguments. Guess the user wants to update all of them.
2195             for m4base in $m4dirs; do
2196               func_import
2197             done
2198           else
2199             # Really ambiguous.
2200             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
2201           fi
2202         fi
2203       fi
2204     fi
2205     ;;
2206
2207   create-testdir )
2208     if test -z "$destdir"; then
2209       func_fatal_error "please specify --dir option"
2210     fi
2211     mkdir "$destdir"
2212     test -d "$destdir" \
2213       || func_fatal_error "could not create destination directory"
2214     test -n "$auxdir" || auxdir="build-aux"
2215     func_create_testdir "$destdir" "$*"
2216     ;;
2217
2218   create-megatestdir )
2219     if test -z "$destdir"; then
2220       func_fatal_error "please specify --dir option"
2221     fi
2222     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2223     test -n "$auxdir" || auxdir="build-aux"
2224     func_create_megatestdir "$destdir" "$*"
2225     ;;
2226
2227   test )
2228     test -n "$destdir" || destdir=testdir$$
2229     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2230     test -n "$auxdir" || auxdir="build-aux"
2231     func_create_testdir "$destdir" "$*"
2232     cd "$destdir"
2233       mkdir build
2234       cd build
2235         ../configure
2236         make
2237         make check
2238         make distclean
2239         remaining=`find . -type f -print`
2240         if test -n "$remaining"; then
2241           echo "Remaining files:" $remaining 1>&2
2242           echo "gnulib-tool: *** Stop." 1>&2
2243           exit 1
2244         fi
2245       cd ..
2246     cd ..
2247     rm -rf "$destdir"
2248     ;;
2249
2250   megatest )
2251     test -n "$destdir" || destdir=testdir$$
2252     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2253     test -n "$auxdir" || auxdir="build-aux"
2254     func_create_megatestdir "$destdir" "$*"
2255     cd "$destdir"
2256       mkdir build
2257       cd build
2258         ../configure
2259         make
2260         make check
2261         make distclean
2262         remaining=`find . -type f -print`
2263         if test -n "$remaining"; then
2264           echo "Remaining files:" $remaining 1>&2
2265           echo "gnulib-tool: *** Stop." 1>&2
2266           exit 1
2267         fi
2268       cd ..
2269     cd ..
2270     rm -rf "$destdir"
2271     ;;
2272
2273   extract-description )
2274     for module
2275     do
2276       func_verify_module
2277       if test -n "$module"; then
2278         func_get_description "$module"
2279       fi
2280     done
2281     ;;
2282
2283   extract-filelist )
2284     for module
2285     do
2286       func_verify_module
2287       if test -n "$module"; then
2288         func_get_filelist "$module"
2289       fi
2290     done
2291     ;;
2292
2293   extract-dependencies )
2294     for module
2295     do
2296       func_verify_module
2297       if test -n "$module"; then
2298         func_get_dependencies "$module"
2299       fi
2300     done
2301     ;;
2302
2303   extract-autoconf-snippet )
2304     for module
2305     do
2306       func_verify_module
2307       if test -n "$module"; then
2308         func_get_autoconf_snippet "$module"
2309       fi
2310     done
2311     ;;
2312
2313   extract-automake-snippet )
2314     for module
2315     do
2316       func_verify_module
2317       if test -n "$module"; then
2318         func_get_automake_snippet "$module"
2319       fi
2320     done
2321     ;;
2322
2323   extract-include-directive )
2324     for module
2325     do
2326       func_verify_module
2327       if test -n "$module"; then
2328         func_get_include_directive "$module"
2329       fi
2330     done
2331     ;;
2332
2333   extract-license )
2334     for module
2335     do
2336       func_verify_module
2337       if test -n "$module"; then
2338         func_get_license "$module"
2339       fi
2340     done
2341     ;;
2342
2343   extract-maintainer )
2344     for module
2345     do
2346       func_verify_module
2347       if test -n "$module"; then
2348         func_get_maintainer "$module"
2349       fi
2350     done
2351     ;;
2352
2353   extract-tests-module )
2354     for module
2355     do
2356       func_verify_module
2357       if test -n "$module"; then
2358         func_get_tests_module "$module"
2359       fi
2360     done
2361     ;;
2362
2363   * )
2364     func_fatal_error "unknown operation mode --$mode" ;;
2365 esac
2366
2367 rm -rf "$tmp"
2368 # Undo the effect of the previous 'trap' command. Some shellology:
2369 # We cannot use "trap - 0 1 2 3 15", because Solaris sh would attempt to
2370 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
2371 # exit); for the others we need to call 'exit' explicitly. The value of $? is
2372 # 128 + signal number and is set before the trap-registered command is run.
2373 trap '' 0
2374 trap 'exit $?' 1 2 3 15
2375
2376 exit 0