Better error handling.
[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-28 15:12:52 $'
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     # Verify that building the module description with 'patch' succeeds.
646     func_lookup_file "modules/$module"
647   else
648     echo "gnulib-tool: module $module doesn't exist" 1>&2
649     module=
650   fi
651 }
652
653 # func_verify_nontests_module
654 # verifies a module name, excluding tests modules
655 func_verify_nontests_module ()
656 {
657   case "$module" in
658     *-tests ) module= ;;
659     * ) func_verify_module ;;
660   esac
661 }
662
663 # func_verify_tests_module
664 # verifies a module name, considering only tests modules
665 func_verify_tests_module ()
666 {
667   case "$module" in
668     *-tests ) func_verify_module ;;
669     * ) module= ;;
670   esac
671 }
672
673 sed_extract_prog=':[     ]*$/ {
674   :a
675     n
676     s/^Description:[     ]*$//
677     s/^Files:[   ]*$//
678     s/^Depends-on:[      ]*$//
679     s/^configure\.ac:[   ]*$//
680     s/^Makefile\.am:[    ]*$//
681     s/^Include:[         ]*$//
682     s/^License:[         ]*$//
683     s/^Maintainer:[      ]*$//
684     tb
685     p
686     ba
687   :b
688 }'
689
690 # func_get_description module
691 func_get_description ()
692 {
693   func_lookup_file "modules/$1"
694   sed -n -e "/^Description$sed_extract_prog" < "$lookedup_file"
695 }
696
697 # func_get_filelist module
698 func_get_filelist ()
699 {
700   func_lookup_file "modules/$1"
701   sed -n -e "/^Files$sed_extract_prog" < "$lookedup_file"
702   case "$autoconf_minversion" in
703     2.59)
704       #echo m4/onceonly.m4
705       echo m4/onceonly_2_57.m4
706       ;;
707   esac
708 }
709
710 # func_get_dependencies module
711 func_get_dependencies ()
712 {
713   # ${module}-tests always implicitly depends on ${module}.
714   echo "$1" | sed -n -e 's/-tests//p'
715   # Then the explicit dependencies listed in the module description.
716   func_lookup_file "modules/$1"
717   sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
718 }
719
720 # func_get_autoconf_snippet module
721 func_get_autoconf_snippet ()
722 {
723   func_lookup_file "modules/$1"
724   sed -n -e "/^configure\.ac$sed_extract_prog" < "$lookedup_file"
725 }
726
727 # func_get_automake_snippet module
728 func_get_automake_snippet ()
729 {
730   func_lookup_file "modules/$1"
731   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file"
732 }
733
734 # func_get_include_directive module
735 func_get_include_directive ()
736 {
737   func_lookup_file "modules/$1"
738   sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
739   sed -e 's/^\(["<]\)/#include \1/'
740 }
741
742 # func_get_license module
743 func_get_license ()
744 {
745   func_lookup_file "modules/$1"
746   sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
747 }
748
749 # func_get_maintainer module
750 func_get_maintainer ()
751 {
752   func_lookup_file "modules/$1"
753   sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
754 }
755
756 # func_get_tests_module module
757 func_get_tests_module ()
758 {
759   # The naming convention for tests modules is hardwired: ${module}-tests.
760   if test -f modules/"$1"-tests; then
761     echo "$1"-tests
762   fi
763 }
764
765 # func_acceptable module
766 # tests whether a module is acceptable.
767 # Input:
768 # - avoidlist       list of modules to avoid
769 func_acceptable ()
770 {
771   for avoid in $avoidlist; do
772     if test "$avoid" = "$1"; then
773       return 1
774     fi
775   done
776   return 0
777 }
778
779 # func_modules_transitive_closure
780 # Input:
781 # - modules         list of specified modules
782 # - inctests        true if tests should be included, blank otherwise
783 # - avoidlist       list of modules to avoid
784 # Output:
785 # - modules         list of modules, including dependencies
786 func_modules_transitive_closure ()
787 {
788   while true; do
789     xmodules=
790     for module in $modules; do
791       func_verify_module
792       if test -n "$module"; then
793         # Duplicate dependencies are harmless, but Jim wants a warning.
794         duplicated_deps=`func_get_dependencies $module | LC_ALL=C sort | LC_ALL=C uniq -d`
795         if test -n "$duplicated_deps"; then
796           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
797         fi
798         if func_acceptable $module; then
799           xmodules="$xmodules $module"
800           for depmodule in `func_get_dependencies $module`; do
801             if func_acceptable $depmodule; then
802               xmodules="$xmodules $depmodule"
803             fi
804           done
805           if test -n "$inctests"; then
806             testsmodule=`func_get_tests_module $module`
807             if test -n "$testsmodule"; then
808               if func_acceptable $testsmodule; then
809                 xmodules="$xmodules $testsmodule"
810                 for depmodule in `func_get_dependencies $testsmodule`; do
811                   if func_acceptable $depmodule; then
812                     xmodules="$xmodules $depmodule"
813                   fi
814                 done
815               fi
816             fi
817           fi
818         fi
819       fi
820     done
821     xmodules=`for m in $xmodules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
822     if test "$xmodules" = "$modules"; then
823       break
824     fi
825     modules="$xmodules"
826   done
827 }
828
829 # func_modules_add_dummy
830 # Input:
831 # - modules         list of modules, including dependencies
832 # Output:
833 # - modules         list of modules, including 'dummy' if needed
834 func_modules_add_dummy ()
835 {
836   have_lib_SOURCES=
837   sed_remove_backslash_newline=':a
838 /\\$/{
839 s/\\$//
840 N
841 s/\n//
842 ba
843 }'
844   for module in $modules; do
845     func_verify_nontests_module
846     if test -n "$module"; then
847       # Extract the value of "lib_SOURCES += ...".
848       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
849         # Ignore .h files since they are not compiled.
850         case "$file" in
851           *.h) ;;
852           *) have_lib_SOURCES=yes ;;
853         esac
854       done
855     fi
856   done
857   # Add the dummy module, to make sure the library will be non-empty.
858   if test -z "$have_lib_SOURCES"; then
859     modules="$modules dummy"
860   fi
861 }
862
863 # func_modules_to_filelist
864 # Input:
865 # - modules         list of modules, including dependencies
866 # Output:
867 # - files           list of files
868 func_modules_to_filelist ()
869 {
870   files=
871   for module in $modules; do
872     func_verify_module
873     if test -n "$module"; then
874       files="$files "`func_get_filelist $module`
875     fi
876   done
877   files=`for f in $files; do echo $f; done | LC_ALL=C sort | LC_ALL=C uniq`
878 }
879
880 # func_emit_lib_Makefile_am
881 # emits the contents of lib/Makefile.am to standard output.
882 # Input:
883 # - modules         list of modules, including dependencies
884 # - libname         library name
885 # - libtool         true if libtool will be used, false or blank otherwise
886 # - actioncmd       (optional) command that will reproduce this invocation
887 func_emit_lib_Makefile_am ()
888 {
889   if test "$libtool" = true; then
890     libext=la
891     perhapsLT=LT
892   else
893     libext=a
894     perhapsLT=
895   fi
896   echo "## Process this file with automake to produce Makefile.in."
897   func_emit_copyright_notice
898   if test -n "$actioncmd"; then
899     echo "# Reproduce by: $actioncmd"
900   fi
901   echo
902   # No need to generate dependencies since the sources are in gnulib, not here.
903   echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
904   echo
905   (
906     for module in $modules; do
907       func_verify_nontests_module
908       if test -n "$module"; then
909         {
910           func_get_automake_snippet "$module" |
911             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
912                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
913             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
914             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
915                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g'
916           if test "$module" = 'alloca'; then
917             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
918           fi
919         } > amsnippet.tmp
920         # Skip the contents if its entirely empty.
921         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
922           echo "## begin gnulib module $module"
923           echo
924           cat amsnippet.tmp
925           echo "## end   gnulib module $module"
926           echo
927         fi
928         rm -f amsnippet.tmp
929       fi
930     done
931   ) > allsnippets.tmp
932   if grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *= *$libname\\.$libext\$" allsnippets.tmp > /dev/null; then
933     # One of the snippets already specifies an installation location for the
934     # library. Don't confuse automake by saying it should not be installed.
935     :
936   else
937     # By default, the generated library should not be installed.
938     echo "noinst_${perhapsLT}LIBRARIES = $libname.$libext"
939   fi
940   echo
941   echo "${libname}_${libext}_SOURCES ="
942   echo "${libname}_${libext}_LIBADD = @${perhapsLT}LIBOBJS@"
943   echo "noinst_HEADERS ="
944   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
945     echo "pkgdata_DATA ="
946   fi
947   echo "EXTRA_DIST ="
948   echo "BUILT_SOURCES ="
949   echo "SUFFIXES ="
950   echo "MOSTLYCLEANFILES = core *.stackdump"
951   echo "MOSTLYCLEANDIRS ="
952   echo "CLEANFILES ="
953   echo "DISTCLEANFILES ="
954   echo "MAINTAINERCLEANFILES ="
955   echo
956   echo "AM_CPPFLAGS ="
957   echo
958   cat allsnippets.tmp
959   echo
960   echo "mostlyclean-local: mostlyclean-generic"
961   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
962   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
963   echo "            if test -d \$\$dir; then \\"
964   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
965   echo "            fi; \\"
966   echo "          done"
967   echo
968   echo "# Makefile.am ends here"
969   rm -f allsnippets.tmp
970 }
971
972 # func_emit_tests_Makefile_am
973 # emits the contents of tests/Makefile.am to standard output.
974 # Input:
975 # - modules         list of modules, including dependencies
976 # - libname         library name
977 # - libtool         true if libtool will be used, false or blank otherwise
978 # - sourcebase      relative directory containing lib source code
979 # - m4base          relative directory containing autoconf macros
980 # - testsbase       relative directory containing unit test code
981 func_emit_tests_Makefile_am ()
982 {
983   if test "$libtool" = true; then
984     libext=la
985   else
986     libext=a
987   fi
988   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
989   echo "## Process this file with automake to produce Makefile.in."
990   func_emit_copyright_notice
991   echo
992   # Generate dependencies here, since it eases the debugging of test failures.
993   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
994   echo
995   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
996   echo
997   (
998     for module in $modules; do
999       func_verify_tests_module
1000       if test -n "$module"; then
1001         func_get_automake_snippet "$module" > amsnippet.tmp
1002         # Skip the contents if its entirely empty.
1003         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1004           echo "## begin gnulib module $module"
1005           echo
1006           cat amsnippet.tmp
1007           echo "## end   gnulib module $module"
1008           echo
1009         fi
1010         rm -f amsnippet.tmp
1011       fi
1012     done
1013   ) > allsnippets.tmp
1014   # Nothing is being added to SUBDIRS; nevertheless the existence of this
1015   # variable is needed to avoid an error from automake:
1016   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
1017   echo "SUBDIRS ="
1018   echo "TESTS ="
1019   echo "TESTS_ENVIRONMENT ="
1020   echo "noinst_PROGRAMS ="
1021   echo "check_PROGRAMS ="
1022   echo "noinst_HEADERS ="
1023   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1024     echo "pkgdata_DATA ="
1025   fi
1026   echo "EXTRA_DIST ="
1027   echo "BUILT_SOURCES ="
1028   echo "SUFFIXES ="
1029   echo "MOSTLYCLEANFILES = core *.stackdump"
1030   echo "MOSTLYCLEANDIRS ="
1031   echo "CLEANFILES ="
1032   echo "DISTCLEANFILES ="
1033   echo "MAINTAINERCLEANFILES ="
1034   echo
1035   echo "AM_CPPFLAGS = \\"
1036   echo "  -I. -I\$(srcdir) \\"
1037   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
1038   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
1039   echo
1040   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
1041   echo
1042   cat allsnippets.tmp
1043   echo "# Clean up after Solaris cc."
1044   echo "clean-local:"
1045   echo "        rm -rf SunWS_cache"
1046   echo
1047   echo "mostlyclean-local: mostlyclean-generic"
1048   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
1049   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
1050   echo "            if test -d \$\$dir; then \\"
1051   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1052   echo "            fi; \\"
1053   echo "          done"
1054   echo
1055   echo "# Makefile.am ends here"
1056   rm -f allsnippets.tmp
1057 }
1058
1059 # func_import modules
1060 # Uses also the variables
1061 # - destdir         target directory
1062 # - libname         library name
1063 # - sourcebase      directory relative to destdir where to place source code
1064 # - m4base          directory relative to destdir where to place *.m4 macros
1065 # - docbase         directory relative to destdir where to place doc files
1066 # - testsbase       directory relative to destdir where to place unit test code
1067 # - auxdir          directory relative to destdir where to place build aux files
1068 # - inctests        true if --with-tests was given, blank otherwise
1069 # - avoidlist       list of modules to avoid, from --avoid
1070 # - lgpl            true if library's license shall be LGPL, blank otherwise
1071 # - libtool         true if --libtool was given, false if --no-libtool was
1072 #                   given, blank otherwise
1073 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
1074 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1075 # - autoconf_minversion  minimum supported autoconf version
1076 # - doit            : if actions shall be executed, false if only to be printed
1077 # - symbolic        true if files should be symlinked, copied otherwise
1078 func_import ()
1079 {
1080   # Get the cached settings.
1081   cached_specified_modules=
1082   cached_avoidlist=
1083   cached_sourcebase=
1084   cached_m4base=
1085   cached_docbase=
1086   cached_testsbase=
1087   cached_libname=
1088   cached_lgpl=
1089   cached_libtool=
1090   cached_macro_prefix=
1091   cached_files=
1092   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1093     cached_libtool=false
1094     my_sed_traces='
1095       s,#.*$,,
1096       s,^dnl .*$,,
1097       s, dnl .*$,,
1098       /gl_MODULES(/ {
1099         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
1100       }
1101       /gl_AVOID(/ {
1102         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
1103       }
1104       /gl_SOURCE_BASE(/ {
1105         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
1106       }
1107       /gl_M4_BASE(/ {
1108         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
1109       }
1110       /gl_DOC_BASE(/ {
1111         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
1112       }
1113       /gl_TESTS_BASE(/ {
1114         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
1115       }
1116       /gl_LIB(/ {
1117         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
1118       }
1119       /gl_LGPL/ {
1120         s,^.*$,cached_lgpl=true,p
1121       }
1122       /gl_LIBTOOL/ {
1123         s,^.*$,cached_libtool=true,p
1124       }
1125       /gl_MACRO_PREFIX(/ {
1126         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
1127       }'
1128     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
1129     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1130       my_sed_traces='
1131         s,#.*$,,
1132         s,^dnl .*$,,
1133         s, dnl .*$,,
1134         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
1135           s,^.*$,cached_files=",p
1136           n
1137           ta
1138           :a
1139           s,^\]).*$,",
1140           tb
1141           p
1142           n
1143           ba
1144           :b
1145           p
1146         }'
1147       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1148     fi
1149   fi
1150
1151   # Merge the cached settings with the specified ones.
1152   # The m4base must be the same as expected from the pathname.
1153   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1154     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1155   fi
1156   # Append the cached and the specified module names. So that
1157   # "gnulib-tool --import foo" means to add the module foo.
1158   specified_modules="$cached_specified_modules $1"
1159   # Append the cached and the specified avoidlist. This is probably better
1160   # than dropping the cached one when --avoid is specified at least once.
1161   avoidlist=`echo $cached_avoidlist $avoidlist`
1162   # The sourcebase defaults to the cached one.
1163   if test -z "$sourcebase"; then
1164     sourcebase="$cached_sourcebase"
1165     if test -z "$sourcebase"; then
1166       func_fatal_error "missing --source-base option"
1167     fi
1168   fi
1169   # The docbase defaults to the cached one.
1170   if test -z "$docbase"; then
1171     docbase="$cached_docbase"
1172     if test -z "$docbase"; then
1173       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."
1174     fi
1175   fi
1176   # The testsbase defaults to the cached one.
1177   if test -z "$testsbase"; then
1178     testsbase="$cached_testsbase"
1179     if test -z "$testsbase"; then
1180       func_fatal_error "missing --tests-base option"
1181     fi
1182   fi
1183   # The libname defaults to the cached one.
1184   if test -z "$supplied_libname"; then
1185     libname="$cached_libname"
1186     if test -z "$libname"; then
1187       func_fatal_error "missing --lib option"
1188     fi
1189   fi
1190   # Require LGPL if specified either way.
1191   if test -z "$lgpl"; then
1192     lgpl="$cached_lgpl"
1193   fi
1194   # Use libtool if specified either way, or if guessed.
1195   if test -z "$libtool"; then
1196     if test -n "$cached_m4base"; then
1197       libtool="$cached_libtool"
1198     else
1199       libtool="$guessed_libtool"
1200     fi
1201   fi
1202   # The macro_prefix defaults to the cached one.
1203   if test -z "$macro_prefix"; then
1204     macro_prefix="$cached_macro_prefix"
1205     if test -z "$macro_prefix"; then
1206       func_fatal_error "missing --macro-prefix option"
1207     fi
1208   fi
1209
1210   # Canonicalize the list of specified modules.
1211   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
1212
1213   # Determine final module list.
1214   modules="$specified_modules"
1215   func_modules_transitive_closure
1216   echo "Module list with included dependencies:"
1217   echo "$modules" | sed -e 's/^/  /'
1218
1219   # Add the dummy module if needed.
1220   func_modules_add_dummy
1221
1222   # If --lgpl, check the license of modules are compatible.
1223   if test -n "$lgpl"; then
1224     for module in $modules; do
1225       license=`func_get_license $module`
1226       case $license in
1227         LGPL | 'GPLed build tool' | 'public domain' | 'unlimited') ;;
1228         *) func_fatal_error "incompatible license on module $module: $license" ;;
1229       esac
1230     done
1231   fi
1232
1233   # Determine script to apply to imported library files.
1234   sed_transform_lib_file=
1235   case $nl$modules$nl in
1236     *"${nl}config-h$nl"*)
1237       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
1238       sed_transform_lib_file=$sed_transform_lib_file'
1239         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
1240       ' ;;
1241   esac
1242   if test -n "$lgpl"; then
1243     # Update license.
1244     sed_transform_lib_file=$sed_transform_lib_file'
1245       s/GNU General/GNU Lesser General/g
1246       s/version 2\([ ,]\)/version 2.1\1/g
1247     '
1248   fi
1249
1250   # Determine final file list.
1251   func_modules_to_filelist
1252   echo "File list:"
1253   echo "$files" | sed -e 's/^/  /'
1254
1255   test -n "$files" \
1256     || func_fatal_error "refusing to do nothing"
1257
1258   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1259   new_files="$files m4/gnulib-tool.m4"
1260   old_files="$cached_files"
1261   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1262     old_files="$old_files m4/gnulib-tool.m4"
1263   fi
1264
1265   # Create directories.
1266   if test ! -d "$destdir/$sourcebase"; then
1267     if $doit; then
1268       echo "Creating directory $destdir/$sourcebase"
1269       mkdir "$destdir/$sourcebase" || func_fatal_error "failed"
1270     else
1271       echo "Create directory $destdir/$sourcebase"
1272     fi
1273   fi
1274   if test ! -d "$destdir/$m4base"; then
1275     if $doit; then
1276       echo "Creating directory $destdir/$m4base"
1277       mkdir "$destdir/$m4base" || func_fatal_error "failed"
1278     else
1279       echo "Create directory $destdir/$m4base"
1280     fi
1281   fi
1282   docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1283   if test -n "$docfiles"; then
1284     if test ! -d "$destdir/$docbase"; then
1285       if $doit; then
1286         echo "Creating directory $destdir/$docbase"
1287         mkdir "$destdir/$docbase" || func_fatal_error "failed"
1288       else
1289         echo "Create directory $destdir/$docbase"
1290       fi
1291     fi
1292   fi
1293   if test -n "$inctests"; then
1294     if test ! -d "$destdir/$testsbase"; then
1295       if $doit; then
1296         echo "Creating directory $destdir/$testsbase"
1297         mkdir "$destdir/$testsbase" || func_fatal_error "failed"
1298       else
1299         echo "Create directory $destdir/$testsbase"
1300       fi
1301     fi
1302   fi
1303   if test ! -d "$destdir/$auxdir"; then
1304     if $doit; then
1305       echo "Creating directory $destdir/$auxdir"
1306       mkdir "$destdir/$auxdir" || func_fatal_error "failed"
1307     else
1308       echo "Create directory $destdir/$auxdir"
1309     fi
1310   fi
1311
1312   # func_dest_tmpfilename file
1313   # determines the name of a temporary file (file is relative to destdir).
1314   # Sets variable:
1315   #   - tmpfile       absolute filename of the temporary file
1316   func_dest_tmpfilename ()
1317   {
1318     if $doit; then
1319       # Put the new contents of $file in a file in the same directory (needed
1320       # to guarantee that an 'mv' to "$destdir/$file" works).
1321       tmpfile="$destdir/$1.tmp"
1322     else
1323       # Put the new contents of $file in a file in a temporary directory
1324       # (because the directory of "$file" might not exist).
1325       tmpfile="$tmp"/`basename "$1"`.tmp
1326     fi
1327   }
1328
1329   # Copy files or make symbolic links. Remove obsolete files.
1330   delimiter='   '
1331   for f in $old_files; do
1332     case "$f" in
1333       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1334       doc/*) g=`echo "$f" | sed -e "s,^doc/,$cached_docbase/,"` ;;
1335       lib/*) g=`echo "$f" | sed -e "s,^lib/,$cached_sourcebase/,"` ;;
1336       m4/*) g=`echo "$f" | sed -e "s,^m4/,$cached_m4base/,"` ;;
1337       tests/*) g=`echo "$f" | sed -e "s,^tests/,$cached_testsbase/,"` ;;
1338       *) g="$f" ;;
1339     esac
1340     echo "$g""$delimiter""$f"
1341   done | LC_ALL=C sort > "$tmp"/old-files
1342   for f in $new_files; do
1343     case "$f" in
1344       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1345       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1346       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1347       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1348       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1349       *) g="$f" ;;
1350     esac
1351     echo "$g""$delimiter""$f"
1352   done | LC_ALL=C sort > "$tmp"/new-files
1353   # First the files that are in old-files, but not in new-files:
1354   sed_take_first_column='s,'"$delimiter"'.*,,'
1355   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1356     # Remove the file. Do nothing if the user already removed it.
1357     if test -f "$destdir/$g"; then
1358       if $doit; then
1359         echo "Removing file $g (backup in ${g}~)"
1360         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1361       else
1362         echo "Remove file $g (backup in ${g}~)"
1363       fi
1364     fi
1365   done
1366   # func_add_or_update handles a file that ought to be present afterwards.
1367   # Uses parameters f, g, already_present.
1368   func_add_or_update ()
1369   {
1370     func_dest_tmpfilename "$g"
1371     func_lookup_file "$f"
1372     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
1373     if test -n "sed_transform_lib_file"; then
1374       case "$f" in
1375         lib/*)
1376           sed -e "$sed_transform_lib_file" \
1377             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
1378           ;;
1379       esac
1380     fi
1381     if test -f "$destdir/$g"; then
1382       # The file already exists.
1383       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1384         : # The file has not changed.
1385       else
1386         # Replace the file.
1387         if $doit; then
1388           if test -n "$already_present"; then
1389             echo "Updating file $g (backup in ${g}~)"
1390           else
1391             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1392           fi
1393           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1394           if test -n "$symbolic" && test -z "$lookedup_tmp" \
1395              && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1396             func_ln_if_changed "$lookedup_file" "$destdir/$g"
1397           else
1398             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1399           fi
1400         else
1401           if test -n "$already_present"; then
1402             echo "Update file $g (backup in ${g}~)"
1403           else
1404             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1405           fi
1406         fi
1407       fi
1408     else
1409       # Install the file.
1410       # Don't protest if the file should be there but isn't: it happens
1411       # frequently that developers don't put autogenerated files into CVS.
1412       if $doit; then
1413         echo "Copying file $g"
1414         if test -n "$symbolic" && test -z "$lookedup_tmp" \
1415            && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1416           func_ln_if_changed "$lookedup_file" "$destdir/$g"
1417         else
1418           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1419         fi
1420       else
1421         echo "Copy file $g"
1422       fi
1423     fi
1424     rm -f "$tmpfile"
1425   }
1426   # Then the files that are in new-files, but not in old-files:
1427   sed_take_last_column='s,^.*'"$delimiter"',,'
1428   already_present=
1429   for f in `LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1430     case "$f" in
1431       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1432       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1433       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1434       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1435       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1436       *) g="$f" ;;
1437     esac
1438     func_add_or_update
1439   done
1440   # Then the files that are in new-files and in old-files:
1441   already_present=true
1442   for f in `LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1443     case "$f" in
1444       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1445       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1446       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1447       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1448       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1449       *) g="$f" ;;
1450     esac
1451     func_add_or_update
1452   done
1453
1454   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1455   actioncmd="gnulib-tool --import"
1456   actioncmd="$actioncmd --dir=$destdir"
1457   actioncmd="$actioncmd --lib=$libname"
1458   actioncmd="$actioncmd --source-base=$sourcebase"
1459   actioncmd="$actioncmd --m4-base=$m4base"
1460   actioncmd="$actioncmd --doc-base=$docbase"
1461   actioncmd="$actioncmd --aux-dir=$auxdir"
1462   for module in $avoidlist; do
1463     actioncmd="$actioncmd --avoid=$module"
1464   done
1465   if test -n "$lgpl"; then
1466     actioncmd="$actioncmd --lgpl"
1467   fi
1468   if test "$libtool" = true; then
1469     actioncmd="$actioncmd --libtool"
1470   else
1471     actioncmd="$actioncmd --no-libtool"
1472   fi
1473   actioncmd="$actioncmd --macro-prefix=$macro_prefix"
1474   actioncmd="$actioncmd `echo $specified_modules`"
1475
1476   # Create lib/Makefile.am.
1477   func_dest_tmpfilename $sourcebase/Makefile.am
1478   func_emit_lib_Makefile_am > "$tmpfile"
1479   if test -f "$destdir"/$sourcebase/Makefile.am; then
1480     if cmp "$destdir"/$sourcebase/Makefile.am "$tmpfile" > /dev/null; then
1481       rm -f "$tmpfile"
1482     else
1483       if $doit; then
1484         echo "Updating $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1485         mv -f "$destdir"/$sourcebase/Makefile.am "$destdir"/$sourcebase/Makefile.am~
1486         mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1487       else
1488         echo "Update $sourcebase/Makefile.am (backup in $sourcebase/Makefile.am~)"
1489         rm -f "$tmpfile"
1490       fi
1491     fi
1492   else
1493     if $doit; then
1494       echo "Creating $sourcebase/Makefile.am"
1495       mv -f "$tmpfile" "$destdir"/$sourcebase/Makefile.am
1496     else
1497       echo "Create $sourcebase/Makefile.am"
1498       rm -f "$tmpfile"
1499     fi
1500   fi
1501
1502   # Create m4/gnulib-cache.m4.
1503   func_dest_tmpfilename $m4base/gnulib-cache.m4
1504   (
1505     func_emit_copyright_notice
1506     echo "#"
1507     echo "# This file represents the specification of how gnulib-tool is used."
1508     echo "# It acts as a cache: It is written and read by gnulib-tool."
1509     echo "# In projects using CVS, this file is meant to be stored in CVS,"
1510     echo "# like the configure.ac and various Makefile.am files."
1511     echo
1512     echo
1513     echo "# Specification in the form of a command-line invocation:"
1514     echo "#   $actioncmd"
1515     echo
1516     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
1517     echo "gl_MODULES(["`echo $specified_modules`"])"
1518     echo "gl_AVOID([$avoidlist])"
1519     echo "gl_SOURCE_BASE([$sourcebase])"
1520     echo "gl_M4_BASE([$m4base])"
1521     echo "gl_DOC_BASE([$docbase])"
1522     echo "gl_TESTS_BASE([$testsbase])"
1523     echo "gl_LIB([$libname])"
1524     test -z "$lgpl" || echo "gl_LGPL"
1525     if test "$libtool" = true; then
1526       echo "gl_LIBTOOL"
1527     fi
1528     echo "gl_MACRO_PREFIX([$macro_prefix])"
1529   ) > "$tmpfile"
1530   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1531     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
1532       rm -f "$tmpfile"
1533     else
1534       if $doit; then
1535         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1536         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
1537         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1538       else
1539         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1540         if false; then
1541           cat "$tmpfile"
1542           echo
1543           echo "# gnulib-cache.m4 ends here"
1544         fi
1545         rm -f "$tmpfile"
1546       fi
1547     fi
1548   else
1549     if $doit; then
1550       echo "Creating $m4base/gnulib-cache.m4"
1551       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1552     else
1553       echo "Create $m4base/gnulib-cache.m4"
1554       cat "$tmpfile"
1555       rm -f "$tmpfile"
1556     fi
1557   fi
1558
1559   # Create m4/gnulib-comp.m4.
1560   func_dest_tmpfilename $m4base/gnulib-comp.m4
1561   (
1562     func_emit_copyright_notice
1563     echo "#"
1564     echo "# This file represents the compiled summary of the specification in"
1565     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
1566     echo "# to be invoked from configure.ac."
1567     echo "# In projects using CVS, this file can be treated like other built files."
1568     echo
1569     echo
1570     echo "# This macro should be invoked from $configure_ac, in the section"
1571     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
1572     echo "# any checks for libraries, header files, types and library functions."
1573     echo "AC_DEFUN([${macro_prefix}_EARLY],"
1574     echo "["
1575     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
1576     echo "  m4_pattern_allow([^gl_ES$])dnl a valid locale name"
1577     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
1578     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1579       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
1580     fi
1581     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1582       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
1583     fi
1584     if grep gl_LOCK "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1585       echo "  AC_REQUIRE([gl_LOCK_EARLY])"
1586     fi
1587     echo "])"
1588     echo
1589     echo "# This macro should be invoked from $configure_ac, in the section"
1590     echo "# \"Check for header files, types and library functions\"."
1591     echo "AC_DEFUN([${macro_prefix}_INIT],"
1592     echo "["
1593     if test "$libtool" = true; then
1594       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1595     else
1596       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1597     fi
1598     if test "$auxdir" != "build-aux"; then
1599       sed_replace_build_aux='
1600         :a
1601         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1602           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1603           ba
1604         }'
1605       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1606     else
1607       sed_replace_build_aux=
1608     fi
1609     for module in $modules; do
1610       func_verify_module
1611       if test -n "$module"; then
1612         func_get_autoconf_snippet "$module" \
1613           | sed -e '/^$/d;' -e 's/^/  /' \
1614                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
1615                 -e "$sed_replace_build_aux"
1616         if test "$module" = 'alloca' && test "$libtool" = true; then
1617           echo 'changequote(,)dnl'
1618           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
1619           echo 'changequote([, ])dnl'
1620           echo 'AC_SUBST([LTALLOCA])'
1621         fi
1622       fi
1623     done
1624     echo "])"
1625     echo
1626     echo "# This macro records the list of files which have been installed by"
1627     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
1628     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
1629     echo "$files" | sed -e 's,^,  ,'
1630     echo "])"
1631   ) > "$tmpfile"
1632   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1633     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
1634       rm -f "$tmpfile"
1635     else
1636       if $doit; then
1637         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1638         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
1639         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1640       else
1641         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1642         if false; then
1643           cat "$tmpfile"
1644           echo
1645           echo "# gnulib-comp.m4 ends here"
1646         fi
1647         rm -f "$tmpfile"
1648       fi
1649     fi
1650   else
1651     if $doit; then
1652       echo "Creating $m4base/gnulib-comp.m4"
1653       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1654     else
1655       echo "Create $m4base/gnulib-comp.m4"
1656       cat "$tmpfile"
1657       rm -f "$tmpfile"
1658     fi
1659   fi
1660
1661   if test -n "$inctests"; then
1662     # Create tests/Makefile.am.
1663     func_dest_tmpfilename $testsbase/Makefile.am
1664     func_emit_tests_Makefile_am > "$tmpfile"
1665     if test -f "$destdir"/$testsbase/Makefile.am; then
1666       if cmp "$destdir"/$testsbase/Makefile.am "$tmpfile" > /dev/null; then
1667         rm -f "$tmpfile"
1668       else
1669         if $doit; then
1670           echo "Updating $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1671           mv -f "$destdir"/$testsbase/Makefile.am "$destdir"/$testsbase/Makefile.am~
1672           mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1673         else
1674           echo "Update $testsbase/Makefile.am (backup in $testsbase/Makefile.am~)"
1675           rm -f "$tmpfile"
1676         fi
1677       fi
1678     else
1679       if $doit; then
1680         echo "Creating $testsbase/Makefile.am"
1681         mv -f "$tmpfile" "$destdir"/$testsbase/Makefile.am
1682       else
1683         echo "Create $testsbase/Makefile.am"
1684         rm -f "$tmpfile"
1685       fi
1686     fi
1687   fi
1688
1689   echo "Finished."
1690   echo
1691   echo "You may need to add #include directives for the following .h files."
1692   (
1693    # First the #include <...> directives without #ifs, sorted for convenience.
1694    for module in $modules; do
1695      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1696        :
1697      else
1698        func_get_include_directive "$module" | grep -v 'include "'
1699      fi
1700    done | LC_ALL=C sort -u
1701    # Then the #include "..." directives without #ifs, sorted for convenience.
1702    for module in $modules; do
1703      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1704        :
1705      else
1706        func_get_include_directive "$module" | grep 'include "'
1707      fi
1708    done | LC_ALL=C sort -u
1709    # Then the #include directives that are surrounded by #ifs. Not sorted.
1710    for module in $modules; do
1711      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1712        func_get_include_directive "$module"
1713      fi
1714    done
1715   ) | sed -e '/^$/d;' -e 's/^/  /'
1716   echo
1717   echo "Don't forget to"
1718   echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1719   if test -n "$inctests"; then
1720     echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1721   fi
1722   sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
1723   sourcebase_base=`basename "$sourcebase"`
1724   echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
1725   if test -n "$inctests"; then
1726     testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
1727     testsbase_base=`basename "$testsbase"`
1728     echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
1729   fi
1730   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
1731   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
1732   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
1733 }
1734
1735 # func_create_testdir testdir modules
1736 # Input:
1737 # - auxdir          directory relative to destdir where to place build aux files
1738 func_create_testdir ()
1739 {
1740   testdir="$1"
1741   modules="$2"
1742   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
1743
1744   # Subdirectory names.
1745   sourcebase=gllib
1746   m4base=glm4
1747   testsbase=gltests
1748
1749   # Determine final module list.
1750   func_modules_transitive_closure
1751   echo "Module list with included dependencies:"
1752   echo "$modules" | sed -e 's/^/  /'
1753
1754   # Add the dummy module if needed.
1755   func_modules_add_dummy
1756
1757   # Determine final file list.
1758   func_modules_to_filelist
1759   echo "File list:"
1760   echo "$files" | sed -e 's/^/  /'
1761
1762   # Create directories.
1763   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
1764     case "$d" in
1765       build-aux) mkdir -p "$testdir/$auxdir" ;;
1766       lib)       mkdir -p "$testdir/$sourcebase" ;;
1767       m4)        mkdir -p "$testdir/$m4base" ;;
1768       tests)     mkdir -p "$testdir/$testsbase" ;;
1769       *)         mkdir -p "$testdir/$d" ;;
1770     esac
1771   done
1772
1773   # Copy files or make symbolic links.
1774   for f in $files; do
1775     func_lookup_file "$f"
1776     case "$f" in
1777       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1778       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1779       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1780       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1781       *) g="$f" ;;
1782     esac
1783     if test -n "$lookedup_tmp"; then
1784       cp -p "$lookedup_file" "$testdir/$g"
1785     else
1786       ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
1787       if test -z "$symbolic"; then
1788         cp -p "$lookedup_file" "$testdir/$g"
1789       else
1790         ln -s "$lookedup_file" "$testdir/$g"
1791       fi
1792     fi
1793   done
1794
1795   # Create $sourcebase/Makefile.am.
1796   mkdir -p "$testdir/$sourcebase"
1797   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
1798
1799   # Create $m4base/Makefile.am.
1800   mkdir -p "$testdir/$m4base"
1801   (echo "## Process this file with automake to produce Makefile.in."
1802    echo
1803    echo "EXTRA_DIST ="
1804    for f in $files; do
1805      case "$f" in
1806        m4/* )
1807          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
1808      esac
1809    done
1810   ) > "$testdir/$m4base/Makefile.am"
1811
1812   subdirs="$sourcebase $m4base"
1813   subdirs_with_configure_ac=""
1814
1815   if test -f "$testdir"/$m4base/gettext.m4; then
1816     # Avoid stupid error message from automake:
1817     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
1818     mkdir -p "$testdir/po"
1819     (echo "## Process this file with automake to produce Makefile.in."
1820     ) > "$testdir/po/Makefile.am"
1821     subdirs="$subdirs po"
1822   fi
1823
1824   if test -n "$inctests"; then
1825     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
1826     # Create $testsbase/Makefile.am.
1827     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
1828     # Create $testsbase/configure.ac.
1829     (echo "# Process this file with autoconf to produce a configure script."
1830      echo "AC_INIT([dummy], [0])"
1831      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
1832      echo "AM_INIT_AUTOMAKE"
1833      echo
1834      echo "AM_CONFIG_HEADER([config.h])"
1835      echo
1836      echo "AC_PROG_CC"
1837      echo "AC_PROG_INSTALL"
1838      echo "AC_PROG_MAKE_SET"
1839      echo "AC_PROG_RANLIB"
1840      echo
1841      if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1842        echo "AC_GNU_SOURCE"
1843        echo
1844      fi
1845      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1846        echo "gl_USE_SYSTEM_EXTENSIONS"
1847        echo
1848      fi
1849      if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1850        echo "gl_LOCK_EARLY"
1851        echo
1852      fi
1853      if test "$libtool" = true; then
1854        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1855      else
1856        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1857      fi
1858      # Wrap the set of autoconf snippets into an autoconf macro that is then
1859      # invoked. This is needed because autoconf does not support AC_REQUIRE
1860      # at the top level:
1861      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
1862      # but we want the AC_REQUIRE to have its normal meaning (provide one
1863      # expansion of the required macro before the current point, and only one
1864      # expansion total).
1865      echo "AC_DEFUN([gl_INIT], ["
1866      if test "$auxdir" != "build-aux"; then
1867        sed_replace_build_aux='
1868          :a
1869          /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1870            s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
1871            ba
1872          }'
1873        sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1874      else
1875        sed_replace_build_aux=
1876      fi
1877      # We don't have explicit ordering constraints between the various
1878      # autoconf snippets. It's cleanest to put those of the library before
1879      # those of the tests.
1880      for module in $modules; do
1881        func_verify_nontests_module
1882        if test -n "$module"; then
1883          func_get_autoconf_snippet "$module" \
1884            | sed -e "$sed_replace_build_aux"
1885        fi
1886      done
1887      for module in $modules; do
1888        func_verify_tests_module
1889        if test -n "$module"; then
1890          func_get_autoconf_snippet "$module" \
1891            | sed -e "$sed_replace_build_aux"
1892        fi
1893      done
1894      echo "])"
1895      echo "gl_INIT"
1896      echo
1897      # Usually $testsbase/config.h will be a superset of config.h. Verify this
1898      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
1899      echo "AH_TOP([#include \"../config.h\"])"
1900      echo
1901      echo "AC_OUTPUT([Makefile])"
1902     ) > "$testdir/$testsbase/configure.ac"
1903     subdirs="$subdirs $testsbase"
1904     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
1905   fi
1906
1907   # Create Makefile.am.
1908   (echo "## Process this file with automake to produce Makefile.in."
1909    echo
1910    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1911    echo
1912    echo "SUBDIRS = $subdirs"
1913    echo
1914    echo "ACLOCAL_AMFLAGS = -I $m4base"
1915   ) > "$testdir/Makefile.am"
1916
1917   # Create configure.ac.
1918   (echo "# Process this file with autoconf to produce a configure script."
1919    echo "AC_INIT([dummy], [0])"
1920    if test "$auxdir" != "."; then
1921      echo "AC_CONFIG_AUX_DIR([$auxdir])"
1922    fi
1923    echo "AM_INIT_AUTOMAKE"
1924    echo
1925    echo "AM_CONFIG_HEADER([config.h])"
1926    echo
1927    echo "AC_PROG_CC"
1928    echo "AC_PROG_INSTALL"
1929    echo "AC_PROG_MAKE_SET"
1930    echo
1931    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
1932    echo "m4_pattern_allow([^gl_ES$])dnl a valid locale name"
1933    echo
1934    echo "AC_PROG_RANLIB"
1935    echo
1936    if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1937      echo "AC_GNU_SOURCE"
1938      echo
1939    fi
1940    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1941      echo "gl_USE_SYSTEM_EXTENSIONS"
1942      echo
1943    fi
1944    if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
1945      echo "gl_LOCK_EARLY"
1946      echo
1947    fi
1948    if test "$libtool" = true; then
1949      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1950    else
1951      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1952    fi
1953    # Wrap the set of autoconf snippets into an autoconf macro that is then
1954    # invoked. This is needed because autoconf does not support AC_REQUIRE
1955    # at the top level:
1956    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
1957    # but we want the AC_REQUIRE to have its normal meaning (provide one
1958    # expansion of the required macro before the current point, and only one
1959    # expansion total).
1960    echo "AC_DEFUN([gl_INIT], ["
1961    if test "$auxdir" != "build-aux"; then
1962      sed_replace_build_aux='
1963        :a
1964        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1965          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1966          ba
1967        }'
1968      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1969    else
1970      sed_replace_build_aux=
1971    fi
1972    for module in $modules; do
1973      func_verify_nontests_module
1974      if test -n "$module"; then
1975        func_get_autoconf_snippet "$module" \
1976          | sed -e "$sed_replace_build_aux"
1977      fi
1978    done
1979    echo "])"
1980    echo "gl_INIT"
1981    echo
1982    if test -n "$subdirs_with_configure_ac"; then
1983      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
1984    fi
1985    makefiles="Makefile"
1986    for d in $subdirs; do
1987      # For subdirs that have a configure.ac by their own, it's the subdir's
1988      # configure.ac which creates the subdir's Makefile.am, not this one.
1989      case " $subdirs_with_configure_ac " in
1990        *" $d "*) ;;
1991        *) makefiles="$makefiles $d/Makefile" ;;
1992      esac
1993    done
1994    echo "AC_OUTPUT([$makefiles])"
1995   ) > "$testdir/configure.ac"
1996
1997   # Create autogenerated files.
1998   (cd "$testdir"
1999    echo "executing ${AUTORECONF} --force --install"
2000    ${AUTORECONF} --force --install
2001   )
2002   if grep '^BUILT_SOURCES *+=' "$testdir/$sourcebase/Makefile.am" > /dev/null; then
2003     (cd "$testdir"
2004      ./configure
2005        cd "$sourcebase"
2006        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
2007        make built_sources
2008        cd ..
2009      make distclean
2010     )
2011   fi
2012 }
2013
2014 # func_create_megatestdir megatestdir allmodules
2015 # Input:
2016 # - auxdir          directory relative to destdir where to place build aux files
2017 func_create_megatestdir ()
2018 {
2019   megatestdir="$1"
2020   allmodules="$2"
2021   if test -z "$allmodules"; then
2022     allmodules=`func_all_modules`
2023   fi
2024
2025   megasubdirs=
2026   # First, all modules one by one.
2027   for onemodule in $allmodules; do
2028     func_create_testdir "$megatestdir/$onemodule" $onemodule
2029     megasubdirs="${megasubdirs}$onemodule "
2030   done
2031   # Then, all modules all together.
2032   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2033   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
2034   func_create_testdir "$megatestdir/ALL" "$allmodules"
2035   megasubdirs="${megasubdirs}ALL"
2036
2037   # Create Makefile.am.
2038   (echo "## Process this file with automake to produce Makefile.in."
2039    echo
2040    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2041    echo
2042    echo "SUBDIRS = $megasubdirs"
2043   ) > "$megatestdir/Makefile.am"
2044
2045   # Create configure.ac.
2046   (echo "# Process this file with autoconf to produce a configure script."
2047    echo "AC_INIT([dummy], [0])"
2048    if test "$auxdir" != "."; then
2049      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2050    fi
2051    echo "AM_INIT_AUTOMAKE"
2052    echo
2053    echo "AC_PROG_MAKE_SET"
2054    echo
2055    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
2056    echo "AC_OUTPUT([Makefile])"
2057   ) > "$megatestdir/configure.ac"
2058
2059   # Create autogenerated files.
2060   (cd "$megatestdir"
2061    # Do not use "${AUTORECONF} --install", because autoreconf operates
2062    # recursively, but the subdirectories are already finished, therefore
2063    # calling autoreconf here would only waste lots of CPU time.
2064    echo "executing ${ACLOCAL}"
2065    ${ACLOCAL}
2066    echo "executing mkdir build-aux"
2067    mkdir build-aux
2068    echo "executing ${AUTOCONF}"
2069    ${AUTOCONF}
2070    echo "executing ${AUTOMAKE} --add-missing --copy"
2071    ${AUTOMAKE} --add-missing --copy
2072   )
2073 }
2074
2075 case $mode in
2076   "" )
2077     func_fatal_error "no mode specified" ;;
2078
2079   list )
2080     func_all_modules
2081     ;;
2082
2083   import | update )
2084
2085     # Where to import.
2086     if test -z "$destdir"; then
2087       destdir=.
2088     fi
2089     test -d "$destdir" \
2090       || func_fatal_error "destination directory does not exist: $destdir"
2091
2092     # Prefer configure.ac to configure.in.
2093     if test -f "$destdir"/configure.ac; then
2094       configure_ac="$destdir/configure.ac"
2095     else
2096       if test -f "$destdir"/configure.in; then
2097         configure_ac="$destdir/configure.in"
2098       else
2099         func_fatal_error "cannot find $destdir/configure.ac"
2100       fi
2101     fi
2102
2103     # Analyze configure.ac.
2104     guessed_auxdir="."
2105     guessed_libtool=false
2106     my_sed_traces='
2107       s,#.*$,,
2108       s,^dnl .*$,,
2109       s, dnl .*$,,
2110       /AC_CONFIG_AUX_DIR/ {
2111         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
2112       }
2113       /A[CM]_PROG_LIBTOOL/ {
2114         s,^.*$,guessed_libtool=true,p
2115       }'
2116     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
2117
2118     if test -z "$auxdir"; then
2119       auxdir="$guessed_auxdir"
2120     fi
2121
2122     # Determine where to apply func_import.
2123     if test -n "$m4base"; then
2124       # Apply func_import to a particular gnulib directory.
2125       # Any number of additional modules can be given.
2126       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
2127         # First use of gnulib in the given m4base.
2128         test -n "$supplied_libname" || supplied_libname=true
2129         test -n "$sourcebase" || sourcebase="lib"
2130         test -n "$docbase" || docbase="doc"
2131         test -n "$testsbase" || testsbase="tests"
2132         test -n "$macro_prefix" || macro_prefix="gl"
2133       fi
2134       func_import "$*"
2135     else
2136       # Apply func_import to all gnulib directories.
2137       # To get this list of directories, look at Makefile.am. (Not at
2138       # configure, because it may be omitted from CVS. Also, don't run
2139       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
2140       m4dirs=
2141       m4dirs_count=0
2142       if test -f "$destdir"/Makefile.am; then
2143         aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[         ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
2144         m4dir_is_next=
2145         for arg in $aclocal_amflags; do
2146           if test -n "$m4dir_is_next"; then
2147             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
2148             case "$arg" in
2149               /*) ;;
2150               *)
2151                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
2152                   m4dirs="$m4dirs $arg"
2153                   m4dirs_count=`expr $m4dirs_count + 1`
2154                 fi
2155                 ;;
2156             esac
2157           else
2158             if test "X$arg" = "X-I"; then
2159               m4dir_is_next=yes
2160             else
2161               m4dir_is_next=
2162             fi
2163           fi
2164         done
2165       else
2166         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
2167         if test -f "$destdir"/aclocal.m4; then
2168           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
2169           sedexpr2='s,^[^/]*$,.,'
2170           sedexpr3='s,/[^/]*$,,'
2171           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort | LC_ALL=C uniq`
2172           m4dirs_count=`echo "$m4dirs" | wc -l`
2173         fi
2174       fi
2175       if test $m4dirs_count = 0; then
2176         # First use of gnulib in a package.
2177         # Any number of additional modules can be given.
2178         test -n "$supplied_libname" || supplied_libname=true
2179         test -n "$sourcebase" || sourcebase="lib"
2180         m4base="m4"
2181         test -n "$docbase" || docbase="doc"
2182         test -n "$testsbase" || testsbase="tests"
2183         test -n "$macro_prefix" || macro_prefix="gl"
2184         func_import "$*"
2185       else
2186         if test $m4dirs_count = 1; then
2187           # There's only one use of gnulib here. Assume the user means it.
2188           # Any number of additional modules can be given.
2189           for m4base in $m4dirs; do
2190             func_import "$*"
2191           done
2192         else
2193           # Ambiguous - guess what the user meant.
2194           if test $# = 0; then
2195             # No further arguments. Guess the user wants to update all of them.
2196             for m4base in $m4dirs; do
2197               func_import
2198             done
2199           else
2200             # Really ambiguous.
2201             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
2202           fi
2203         fi
2204       fi
2205     fi
2206     ;;
2207
2208   create-testdir )
2209     if test -z "$destdir"; then
2210       func_fatal_error "please specify --dir option"
2211     fi
2212     mkdir "$destdir"
2213     test -d "$destdir" \
2214       || func_fatal_error "could not create destination directory"
2215     test -n "$auxdir" || auxdir="build-aux"
2216     func_create_testdir "$destdir" "$*"
2217     ;;
2218
2219   create-megatestdir )
2220     if test -z "$destdir"; then
2221       func_fatal_error "please specify --dir option"
2222     fi
2223     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2224     test -n "$auxdir" || auxdir="build-aux"
2225     func_create_megatestdir "$destdir" "$*"
2226     ;;
2227
2228   test )
2229     test -n "$destdir" || destdir=testdir$$
2230     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2231     test -n "$auxdir" || auxdir="build-aux"
2232     func_create_testdir "$destdir" "$*"
2233     cd "$destdir"
2234       mkdir build
2235       cd build
2236         ../configure
2237         make
2238         make check
2239         make distclean
2240         remaining=`find . -type f -print`
2241         if test -n "$remaining"; then
2242           echo "Remaining files:" $remaining 1>&2
2243           echo "gnulib-tool: *** Stop." 1>&2
2244           exit 1
2245         fi
2246       cd ..
2247     cd ..
2248     rm -rf "$destdir"
2249     ;;
2250
2251   megatest )
2252     test -n "$destdir" || destdir=testdir$$
2253     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2254     test -n "$auxdir" || auxdir="build-aux"
2255     func_create_megatestdir "$destdir" "$*"
2256     cd "$destdir"
2257       mkdir build
2258       cd build
2259         ../configure
2260         make
2261         make check
2262         make distclean
2263         remaining=`find . -type f -print`
2264         if test -n "$remaining"; then
2265           echo "Remaining files:" $remaining 1>&2
2266           echo "gnulib-tool: *** Stop." 1>&2
2267           exit 1
2268         fi
2269       cd ..
2270     cd ..
2271     rm -rf "$destdir"
2272     ;;
2273
2274   extract-description )
2275     for module
2276     do
2277       func_verify_module
2278       if test -n "$module"; then
2279         func_get_description "$module"
2280       fi
2281     done
2282     ;;
2283
2284   extract-filelist )
2285     for module
2286     do
2287       func_verify_module
2288       if test -n "$module"; then
2289         func_get_filelist "$module"
2290       fi
2291     done
2292     ;;
2293
2294   extract-dependencies )
2295     for module
2296     do
2297       func_verify_module
2298       if test -n "$module"; then
2299         func_get_dependencies "$module"
2300       fi
2301     done
2302     ;;
2303
2304   extract-autoconf-snippet )
2305     for module
2306     do
2307       func_verify_module
2308       if test -n "$module"; then
2309         func_get_autoconf_snippet "$module"
2310       fi
2311     done
2312     ;;
2313
2314   extract-automake-snippet )
2315     for module
2316     do
2317       func_verify_module
2318       if test -n "$module"; then
2319         func_get_automake_snippet "$module"
2320       fi
2321     done
2322     ;;
2323
2324   extract-include-directive )
2325     for module
2326     do
2327       func_verify_module
2328       if test -n "$module"; then
2329         func_get_include_directive "$module"
2330       fi
2331     done
2332     ;;
2333
2334   extract-license )
2335     for module
2336     do
2337       func_verify_module
2338       if test -n "$module"; then
2339         func_get_license "$module"
2340       fi
2341     done
2342     ;;
2343
2344   extract-maintainer )
2345     for module
2346     do
2347       func_verify_module
2348       if test -n "$module"; then
2349         func_get_maintainer "$module"
2350       fi
2351     done
2352     ;;
2353
2354   extract-tests-module )
2355     for module
2356     do
2357       func_verify_module
2358       if test -n "$module"; then
2359         func_get_tests_module "$module"
2360       fi
2361     done
2362     ;;
2363
2364   * )
2365     func_fatal_error "unknown operation mode --$mode" ;;
2366 esac
2367
2368 rm -rf "$tmp"
2369 # Undo the effect of the previous 'trap' command. Some shellology:
2370 # We cannot use "trap - 0 1 2 3 15", because Solaris sh would attempt to
2371 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
2372 # exit); for the others we need to call 'exit' explicitly. The value of $? is
2373 # 128 + signal number and is set before the trap-registered command is run.
2374 trap '' 0
2375 trap 'exit $?' 1 2 3 15
2376
2377 exit 0