* modules/stdarg (configure.ac-early): Require AC_PROG_CC_STDC.
[gnulib.git] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2007 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: 2007-02-04 19:09:25 $'
26 last_checkin_date=`echo "$cvsdatestamp" | sed -e 's,^\$[D]ate: ,,'`
27 version=`echo "$last_checkin_date" | sed -e 's/ .*$//' -e 's,/,-,g'`
28 nl='
29 '
30
31 # You can set AUTOCONFPATH to empty if autoconf 2.57 is already in your PATH.
32 AUTOCONFPATH=
33 #case $USER in
34 #  bruno )
35 #    AUTOCONFBINDIR=/packages/gnu-inst-autoconf/2.57/bin
36 #    AUTOCONFPATH="eval env PATH=${AUTOCONFBINDIR}:\$PATH "
37 #    ;;
38 #esac
39
40 # You can set AUTOMAKEPATH to empty if automake 1.9.x is already in your PATH.
41 AUTOMAKEPATH=
42
43 # You can set GETTEXTPATH to empty if autopoint 0.15 is already in your PATH.
44 GETTEXTPATH=
45
46 # If you didn't set AUTOCONFPATH and AUTOMAKEPATH, you can also set the
47 # variables AUTOCONF, AUTOHEADER, ACLOCAL, AUTOMAKE, AUTORECONF individually.
48 if test -z "${AUTOCONF}" || test -n "${AUTOCONFPATH}"; then
49   AUTOCONF="${AUTOCONFPATH}autoconf"
50 fi
51 if test -z "${AUTOHEADER}" || test -n "${AUTOCONFPATH}"; then
52   AUTOHEADER="${AUTOCONFPATH}autoheader"
53 fi
54 if test -z "${ACLOCAL}" || test -n "${AUTOMAKEPATH}"; then
55   ACLOCAL="${AUTOMAKEPATH}aclocal"
56 fi
57 if test -z "${AUTOMAKE}" || test -n "${AUTOMAKEPATH}"; then
58   AUTOMAKE="${AUTOMAKEPATH}automake"
59 fi
60 if test -z "${AUTORECONF}" || test -n "${AUTOCONFPATH}"; then
61   AUTORECONF="${AUTOCONFPATH}autoreconf"
62 fi
63
64 # If you didn't set GETTEXTPATH, you can also set the variable AUTOPOINT.
65 if test -z "${AUTOPOINT}" || test -n "${GETTEXTPATH}"; then
66   AUTOPOINT="${GETTEXTPATH}autopoint"
67 fi
68
69 # When using GNU sed, turn off as many GNU extensions as possible,
70 # to minimize the risk of accidentally using non-portable features.
71 # However, do this only for gnulib-tool itself, not for the code that
72 # gnulib-tool generates, since we don't want "sed --posix" to leak
73 # into makefiles.
74 if (alias) > /dev/null 2>&1 && echo | sed --posix -e d >/dev/null 2>&1; then
75   alias sed='sed --posix'
76 fi
77
78 # func_usage
79 # outputs to stdout the --help usage message.
80 func_usage ()
81 {
82   echo "\
83 Usage: gnulib-tool --list
84        gnulib-tool --import [module1 ... moduleN]
85        gnulib-tool --update
86        gnulib-tool --create-testdir --dir=directory [module1 ... moduleN]
87        gnulib-tool --create-megatestdir --dir=directory [module1 ... moduleN]
88        gnulib-tool --test --dir=directory module1 ... moduleN
89        gnulib-tool --megatest --dir=directory [module1 ... moduleN]
90        gnulib-tool --extract-description module
91        gnulib-tool --extract-filelist module
92        gnulib-tool --extract-dependencies module
93        gnulib-tool --extract-autoconf-snippet module
94        gnulib-tool --extract-automake-snippet module
95        gnulib-tool --extract-include-directive module
96        gnulib-tool --extract-link-directive module
97        gnulib-tool --extract-license module
98        gnulib-tool --extract-maintainer module
99        gnulib-tool --extract-tests-module module
100
101 Operation modes:
102       --list                print the available module names
103       --import              import the given modules into the current package;
104                             if no modules are specified, update the current
105                             package from the current gnulib
106       --update              update the current package, restore files omitted
107                             from CVS
108       --create-testdir      create a scratch package with the given modules
109       --create-megatestdir  create a mega scratch package with the given modules
110                             one by one and all together
111       --test                test the combination of the given modules
112                             (recommended to use CC=\"gcc -Wall\" here)
113       --megatest            test the given modules one by one and all together
114                             (recommended to use CC=\"gcc -Wall\" here)
115       --extract-description        extract the description
116       --extract-filelist           extract the list of files
117       --extract-dependencies       extract the dependencies
118       --extract-autoconf-snippet   extract the snippet for configure.ac
119       --extract-automake-snippet   extract the snippet for library makefile
120       --extract-include-directive  extract the #include directive
121       --extract-link-directive     extract the linker directive
122       --extract-license            report the license terms of the source files
123                                    under lib/
124       --extract-maintainer         report the maintainer(s) inside gnulib
125       --extract-tests-module       report the unit test module, if it exists
126
127 General options:
128       --dir=DIRECTORY       Specify the target directory.
129                             For --import, this specifies where your
130                             configure.ac can be found.  Defaults to current
131                             directory.
132       --local-dir=DIRECTORY  Specify a local override directory where to look
133                             up files before looking in gnulib's directory.
134
135 Options for --import:
136       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
137       --source-base=DIRECTORY
138                             Directory relative to --dir where source code is
139                             placed (default \"lib\").
140       --m4-base=DIRECTORY   Directory relative to --dir where *.m4 macros are
141                             placed (default \"m4\").
142       --doc-base=DIRECTORY  Directory relative to --dir where doc files are
143                             placed (default \"doc\").
144       --tests-base=DIRECTORY
145                             Directory relative to --dir where unit tests are
146                             placed (default \"tests\").
147       --aux-dir=DIRECTORY   Directory relative to --dir where auxiliary build
148                             tools are placed (default \"build-aux\").
149       --with-tests          Include unit tests for the included modules.
150       --avoid=MODULE        Avoid including the given MODULE. Useful if you
151                             have code that provides equivalent functionality.
152                             This option can be repeated.
153       --lgpl                Abort if modules aren't available under the LGPL.
154                             Also modify license template from GPL to LGPL.
155       --makefile-name=NAME  Name of makefile in automake syntax in the
156                             source-base and tests-base directories
157                             (default \"Makefile.am\").
158       --libtool             Use libtool rules.
159       --no-libtool          Don't use libtool rules.
160       --macro-prefix=PREFIX  Specify the prefix of the macros 'gl_EARLY' and
161                             'gl_INIT'. Default is 'gl'.
162       --no-changelog        don't update or create ChangeLog files
163
164 Options for --import and --update:
165       --dry-run             For --import, only print what would have been done.
166   -s, --symbolic, --symlink Make symbolic links instead of copying files.
167       --local-symlink       Make symbolic links instead of copying files, only
168                             for files from the local override directory.
169
170 Report bugs to <bug-gnulib@gnu.org>."
171 }
172
173 # func_version
174 # outputs to stdout the --version message.
175 func_version ()
176 {
177   year=`echo "$last_checkin_date" | sed -e 's,/.*$,,'`
178   echo "\
179 $progname (GNU $package) $version
180 Copyright (C) $year Free Software Foundation, Inc.
181 This is free software; see the source for copying conditions.  There is NO
182 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
183 Written by" "Bruno Haible" "and" "Simon Josefsson"
184 }
185
186 # func_emit_copyright_notice
187 # outputs to stdout a header for a generated file.
188 func_emit_copyright_notice ()
189 {
190   echo "# Copyright (C) 2004-2007 Free Software Foundation, Inc."
191   echo "#"
192   echo "# This file is free software, distributed under the terms of the GNU"
193   echo "# General Public License.  As a special exception to the GNU General"
194   echo "# Public License, this file may be distributed as part of a program"
195   echo "# that contains a configuration script generated by Autoconf, under"
196   echo "# the same distribution terms as the rest of that program."
197   echo "#"
198   echo "# Generated by gnulib-tool."
199 }
200
201 # func_exit STATUS
202 # exit with status
203 func_exit ()
204 {
205   (exit $1); exit $1
206 }
207
208 # func_tmpdir
209 # creates a temporary directory.
210 # Sets variable
211 # - tmp             pathname of freshly created temporary directory
212 func_tmpdir ()
213 {
214   # Use the environment variable TMPDIR, falling back to /tmp. This allows
215   # users to specify a different temporary directory, for example, if their
216   # /tmp is filled up or too small.
217   : ${TMPDIR=/tmp}
218   {
219     # Use the mktemp program if available. If not available, hide the error
220     # message.
221     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
222     test -n "$tmp" && test -d "$tmp"
223   } ||
224   {
225     # Use a simple mkdir command. It is guaranteed to fail if the directory
226     # already exists.  $RANDOM is bash specific and expands to empty in shells
227     # other than bash, ksh and zsh.  Its use does not increase security;
228     # rather, it minimizes the probability of failure in a very cluttered /tmp
229     # directory.
230     tmp=$TMPDIR/gl$$-$RANDOM
231     (umask 077 && mkdir "$tmp")
232   } ||
233   {
234     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
235     func_exit 1
236   }
237 }
238
239 # func_append var value
240 # appends the given value to the shell variable var.
241 if ( foo=bar; foo+=baz && test "$foo" = barbaz ) >/dev/null 2>&1; then
242   # Use bash's += operator. It reduces complexity of appending repeatedly to
243   # a single variable from O(n^2) to O(n).
244   func_append ()
245   {
246     eval "$1+=\"\$2\""
247   }
248 else
249   func_append ()
250   {
251     eval "$1=\"\$$1\$2\""
252   }
253 fi
254
255 # func_fatal_error message
256 # outputs to stderr a fatal error message, and terminates the program.
257 func_fatal_error ()
258 {
259   echo "gnulib-tool: *** $1" 1>&2
260   echo "gnulib-tool: *** Stop." 1>&2
261   func_exit 1
262 }
263
264 # func_readlink SYMLINK
265 # outputs the target of the given symlink.
266 if (type -p readlink) > /dev/null 2>&1; then
267   func_readlink ()
268   {
269     # Use the readlink program from GNU coreutils.
270     readlink "$1"
271   }
272 else
273   func_readlink ()
274   {
275     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
276     # would do the wrong thing if the link target contains " -> ".
277     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
278   }
279 fi
280
281 # func_relativize DIR1 DIR2
282 # computes a relative pathname RELDIR such that DIR1/RELDIR = DIR2.
283 # Input:
284 # - DIR1            relative pathname, relative to the current directory
285 # - DIR2            relative pathname, relative to the current directory
286 # Output:
287 # - reldir          relative pathname of DIR2, relative to DIR1
288 func_relativize ()
289 {
290   dir0=`pwd`
291   dir1="$1"
292   dir2="$2"
293   sed_first='s,^\([^/]*\)/.*$,\1,'
294   sed_rest='s,^[^/]*/*,,'
295   sed_last='s,^.*/\([^/]*\)$,\1,'
296   sed_butlast='s,/*[^/]*$,,'
297   while test -n "$dir1"; do
298     first=`echo "$dir1" | sed -e "$sed_first"`
299     if test "$first" != "."; then
300       if test "$first" = ".."; then
301         dir2=`echo "$dir0" | sed -e "$sed_last"`/"$dir2"
302         dir0=`echo "$dir0" | sed -e "$sed_butlast"`
303       else
304         first2=`echo "$dir2" | sed -e "$sed_first"`
305         if test "$first2" = "$first"; then
306           dir2=`echo "$dir2" | sed -e "$sed_rest"`
307         else
308           dir2="../$dir2"
309         fi
310         dir0="$dir0"/"$first"
311       fi
312     fi
313     dir1=`echo "$dir1" | sed -e "$sed_rest"`
314   done
315   reldir="$dir2"
316 }
317
318 # func_relconcat DIR1 DIR2
319 # computes a relative pathname DIR1/DIR2, with obvious simplifications.
320 # Input:
321 # - DIR1            relative pathname, relative to the current directory
322 # - DIR2            relative pathname, relative to DIR1
323 # Output:
324 # - relconcat       DIR1/DIR2, relative to the current directory
325 func_relconcat ()
326 {
327   dir1="$1"
328   dir2="$2"
329   sed_first='s,^\([^/]*\)/.*$,\1,'
330   sed_rest='s,^[^/]*/*,,'
331   sed_last='s,^.*/\([^/]*\)$,\1,'
332   sed_butlast='s,/*[^/]*$,,'
333   while true; do
334     first=`echo "$dir2" | sed -e "$sed_first"`
335     if test "$first" = "."; then
336       dir2=`echo "$dir2" | sed -e "$sed_rest"`
337       if test -z "$dir2"; then
338         relconcat="$dir1"
339         break
340       fi
341     else
342       last=`echo "$dir1" | sed -e "$sed_last"`
343       while test "$last" = "."; do
344         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
345         last=`echo "$dir1" | sed -e "$sed_last"`
346       done
347       if test -z "$dir1"; then
348         relconcat="$dir2"
349         break
350       fi
351       if test "$first" = ".."; then
352         if test "$last" = ".."; then
353           relconcat="$dir1/$dir2"
354           break
355         fi
356         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
357         dir2=`echo "$dir2" | sed -e "$sed_rest"`
358         if test -z "$dir1"; then
359           relconcat="$dir2"
360           break
361         fi
362         if test -z "$dir2"; then
363           relconcat="$dir1"
364           break
365         fi
366       else
367         relconcat="$dir1/$dir2"
368         break
369       fi
370     fi
371   done
372 }
373
374 # func_ln SRC DEST
375 # Like ln -s, except that SRC is given relative to the current directory (or
376 # absolute), not given relative to the directory of DEST.
377 func_ln ()
378 {
379   case "$1" in
380     /*)
381       ln -s "$1" "$2" ;;
382     *) # SRC is relative.
383       case "$2" in
384         /*)
385           ln -s "`pwd`/$1" "$2" ;;
386         *) # DEST is relative too.
387           ln_destdir=`echo "$2" | sed -e 's,[^/]*$,,'`
388           test -n "$ln_destdir" || ln_destdir="."
389           func_relativize "$ln_destdir" "$1"
390           ln -s "$reldir" "$2"
391           ;;
392       esac
393       ;;
394   esac
395 }
396
397 # func_ln_if_changed SRC DEST
398 # Like func_ln, but avoids munging timestamps if the link is correct.
399 func_ln_if_changed ()
400 {
401   if test $# -ne 2; then
402     echo "usage: func_ln_if_changed SRC DEST" >&2
403   fi
404   ln_target=`func_readlink "$2"`
405   if test -L "$2" && test "$1" = "$ln_target"; then
406     :
407   else
408     rm -f "$2"
409     func_ln "$1" "$2"
410   fi
411 }
412
413 # Command-line option processing.
414 # Removes the OPTIONS from the arguments. Sets the variables:
415 # - mode            list or import or create-testdir or create-megatestdir
416 # - destdir         from --dir
417 # - local_gnulib_dir  from --local-dir
418 # - libname, supplied_libname  from --lib
419 # - sourcebase      from --source-base
420 # - m4base          from --m4-base
421 # - docbase         from --doc-base
422 # - testsbase       from --tests-base
423 # - auxdir          from --aux-dir
424 # - inctests        true if --with-tests was given, blank otherwise
425 # - avoidlist       list of modules to avoid, from --avoid
426 # - lgpl            true if --lgpl was given, blank otherwise
427 # - makefile_name   from --makefile-name
428 # - libtool         true if --libtool was given, false if --no-libtool was
429 #                   given, blank otherwise
430 # - macro_prefix    from --macro-prefix
431 # - autoconf_minversion  minimum supported autoconf version
432 # - do_changelog    false if --no-changelog was given, : otherwise
433 # - doit            : if actions shall be executed, false if only to be printed
434 # - symbolic        true if --symlink was given, blank otherwise
435 # - lsymbolic       true if --local-symlink was given, blank otherwise
436 {
437   mode=
438   destdir=
439   local_gnulib_dir=
440   libname=libgnu
441   supplied_libname=
442   sourcebase=
443   m4base=
444   docbase=
445   testsbase=
446   auxdir=
447   inctests=
448   avoidlist=
449   lgpl=
450   makefile_name=
451   libtool=
452   macro_prefix=
453   do_changelog=:
454   doit=:
455   symbolic=
456   lsymbolic=
457
458   supplied_opts="$@"
459
460   while test $# -gt 0; do
461     case "$1" in
462       --list | --lis )
463         mode=list
464         shift ;;
465       --import | --impor | --impo | --imp | --im | --i )
466         mode=import
467         shift ;;
468       --update | --updat | --upda | --upd | --up | --u )
469         mode=update
470         shift ;;
471       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
472         mode=create-testdir
473         shift ;;
474       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
475         mode=create-megatestdir
476         shift ;;
477       --test | --tes | --te | --t )
478         mode=test
479         shift ;;
480       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
481         mode=megatest
482         shift ;;
483       --extract-* )
484         mode=`echo "X$1" | sed -e 's/^X--//'`
485         shift ;;
486       --dir )
487         shift
488         if test $# = 0; then
489           func_fatal_error "missing argument for --dir"
490         fi
491         destdir=$1
492         shift ;;
493       --dir=* )
494         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
495         shift ;;
496       --local-dir )
497         shift
498         if test $# = 0; then
499           func_fatal_error "missing argument for --local-dir"
500         fi
501         local_gnulib_dir=$1
502         shift ;;
503       --local-dir=* )
504         local_gnulib_dir=`echo "X$1" | sed -e 's/^X--local-dir=//'`
505         shift ;;
506       --lib )
507         shift
508         if test $# = 0; then
509           func_fatal_error "missing argument for --lib"
510         fi
511         libname=$1
512         supplied_libname=true
513         shift ;;
514       --lib=* )
515         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
516         supplied_libname=true
517         shift ;;
518       --source-base )
519         shift
520         if test $# = 0; then
521           func_fatal_error "missing argument for --source-base"
522         fi
523         sourcebase=$1
524         shift ;;
525       --source-base=* )
526         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
527         shift ;;
528       --m4-base )
529         shift
530         if test $# = 0; then
531           func_fatal_error "missing argument for --m4-base"
532         fi
533         m4base=$1
534         shift ;;
535       --m4-base=* )
536         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
537         shift ;;
538       --doc-base )
539         shift
540         if test $# = 0; then
541           func_fatal_error "missing argument for --doc-base"
542         fi
543         docbase=$1
544         shift ;;
545       --doc-base=* )
546         docbase=`echo "X$1" | sed -e 's/^X--doc-base=//'`
547         shift ;;
548       --tests-base )
549         shift
550         if test $# = 0; then
551           func_fatal_error "missing argument for --tests-base"
552         fi
553         testsbase=$1
554         shift ;;
555       --tests-base=* )
556         testsbase=`echo "X$1" | sed -e 's/^X--tests-base=//'`
557         shift ;;
558       --aux-dir )
559         shift
560         if test $# = 0; then
561           func_fatal_error "missing argument for --aux-dir"
562         fi
563         auxdir=$1
564         shift ;;
565       --aux-dir=* )
566         auxdir=`echo "X$1" | sed -e 's/^X--aux-dir=//'`
567         shift ;;
568       --with-tests )
569         inctests=true
570         shift ;;
571       --avoid )
572         shift
573         if test $# = 0; then
574           func_fatal_error "missing argument for --avoid"
575         fi
576         func_append avoidlist " $1"
577         shift ;;
578       --avoid=* )
579         arg=`echo "X$1" | sed -e 's/^X--avoid=//'`
580         func_append avoidlist " $arg"
581         shift ;;
582       --lgpl )
583         lgpl=true
584         shift ;;
585       --makefile-name )
586         shift
587         if test $# = 0; then
588           func_fatal_error "missing argument for --makefile-name"
589         fi
590         makefile_name="$1"
591         shift ;;
592       --makefile-name=* )
593         makefile_name=`echo "X$1" | sed -e 's/^X--makefile-name=//'`
594         shift ;;
595       --libtool )
596         libtool=true
597         shift ;;
598       --no-libtool )
599         libtool=false
600         shift ;;
601       --macro-prefix )
602         shift
603         if test $# = 0; then
604           func_fatal_error "missing argument for --macro-prefix"
605         fi
606         macro_prefix="$1"
607         shift ;;
608       --macro-prefix=* )
609         macro_prefix=`echo "X$1" | sed -e 's/^X--macro-prefix=//'`
610         shift ;;
611       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
612         do_changelog=false
613         shift ;;
614       --dry-run )
615         doit=false
616         shift ;;
617       -s | --symbolic | --symboli | --symbol | --symbo | --symb | --symlink | --symlin | --symli | --syml | --sym | --sy )
618         symbolic=true
619         shift ;;
620       --local-symlink | --local-symlin | --local-symli | --local-syml | --local-sym | --local-sy | --local-s )
621         lsymbolic=true
622         shift ;;
623       --help | --hel | --he | --h )
624         func_usage
625         func_exit $? ;;
626       --version | --versio | --versi | --vers | --ver | --ve | --v )
627         func_version
628         func_exit $? ;;
629       -- )
630         # Stop option processing
631         shift
632         break ;;
633       -* )
634         echo "gnulib-tool: unknown option $1" 1>&2
635         echo "Try 'gnulib-tool --help' for more information." 1>&2
636         func_exit 1 ;;
637       * )
638         break ;;
639     esac
640   done
641
642   if test "$mode" = update; then
643     if test $# != 0; then
644       echo "gnulib-tool: too many arguments in 'update' mode" 1>&2
645       echo "Try 'gnulib-tool --help' for more information." 1>&2
646       echo "If you really want to modify the gnulib configuration of your project," 1>&2
647       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
648       func_exit 1
649     fi
650     if test -n "$local_gnulib_dir" || test -n "$supplied_libname" \
651        || test -n "$sourcebase" || test -n "$m4base" \
652        || test -n "$docbase" || test -n "$testsbase" || test -n "$auxdir" \
653        || test -n "$inctests" || test -n "$avoidlist" || test -n "$lgpl" \
654        || test -n "$makefile_name" || test -n "$macro_prefix"; then
655       echo "gnulib-tool: invalid options for 'update' mode" 1>&2
656       echo "Try 'gnulib-tool --help' for more information." 1>&2
657       echo "If you really want to modify the gnulib configuration of your project," 1>&2
658       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
659       func_exit 1
660     fi
661     do_changelog=false
662   fi
663
664   # Determine the minimum supported autoconf version from the project's
665   # configure.ac.
666   DEFAULT_AUTOCONF_MINVERSION="2.59"
667   autoconf_minversion=
668   configure_ac=
669   if { test "$mode" = import || test "$mode" = update; } && test -n "$destdir"; then
670     if test -f "$destdir"/configure.ac; then
671       configure_ac="$destdir/configure.ac"
672     else
673       if test -f "$destdir"/configure.in; then
674         configure_ac="$destdir/configure.in"
675       fi
676     fi
677   else
678     if test -f configure.ac; then
679       configure_ac="configure.ac"
680     else
681       if test -f configure.in; then
682         configure_ac="configure.in"
683       fi
684     fi
685   fi
686   if test -n "$configure_ac"; then
687     # Use sed, not autoconf --trace, to look for the AC_PREREQ invocation,
688     # because when some m4 files are omitted from a CVS repository,
689     # "autoconf --trace=AC_PREREQ" fails with an error message like this:
690     #   m4: aclocal.m4:851: Cannot open m4/absolute-header.m4: No such file or directory
691     #   autom4te: m4 failed with exit status: 1
692     prereqs=
693     my_sed_traces='
694       s,#.*$,,
695       s,^dnl .*$,,
696       s, dnl .*$,,
697       /AC_PREREQ/ {
698         s,^.*AC_PREREQ([[ ]*\([^])]*\).*$,\1,p
699       }'
700     prereqs=`sed -n -e "$my_sed_traces" < "$configure_ac"`
701     if test -n "$prereqs"; then
702       autoconf_minversion=`
703         for version in $prereqs; do echo $version; done |
704         LC_ALL=C sort -nru | sed 1q
705       `
706     fi
707   fi
708   if test -z "$autoconf_minversion"; then
709     autoconf_minversion=$DEFAULT_AUTOCONF_MINVERSION
710   fi
711   case "$autoconf_minversion" in
712     1.* | 2.[0-4]* | 2.5[0-8]*)
713       func_fatal_error "minimum supported autoconf version is 2.59. Try adding AC_PREREQ([$DEFAULT_AUTOCONF_MINVERSION]) to your configure.ac." ;;
714   esac
715
716   # Remove trailing slashes from the directory names. This is necessary for
717   # m4base (to avoid an error in func_import) and optional for the others.
718   sed_trimtrailingslashes='s,\([^/]\)//*$,\1,'
719   case "$local_gnulib_dir" in
720     */ ) local_gnulib_dir=`echo "$local_gnulib_dir" | sed -e "$sed_trimtrailingslashes"` ;;
721   esac
722   case "$sourcebase" in
723     */ ) sourcebase=`echo "$sourcebase" | sed -e "$sed_trimtrailingslashes"` ;;
724   esac
725   case "$m4base" in
726     */ ) m4base=`echo "$m4base" | sed -e "$sed_trimtrailingslashes"` ;;
727   esac
728   case "$docbase" in
729     */ ) docbase=`echo "$docbase" | sed -e "$sed_trimtrailingslashes"` ;;
730   esac
731   case "$testsbase" in
732     */ ) testsbase=`echo "$testsbase" | sed -e "$sed_trimtrailingslashes"` ;;
733   esac
734   case "$auxdir" in
735     */ ) auxdir=`echo "$auxdir" | sed -e "$sed_trimtrailingslashes"` ;;
736   esac
737 }
738
739 case "$0" in
740   /*) self_abspathname="$0" ;;
741   */*) self_abspathname=`pwd`/"$0" ;;
742   *) for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`; do
743        if test -x "$d/$0" && test ! -d "$d/$0"; then
744          self_abspathname="$d/$0"
745          break
746        fi
747      done
748      if test -z "$self_abspathname"; then
749        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
750      fi
751      ;;
752 esac
753 while test -h "$self_abspathname"; do
754   # Resolve symbolic link.
755   linkval=`func_readlink "$self_abspathname"`
756   test -n "$linkval" || break
757   case "$linkval" in
758     /* ) self_abspathname="$linkval" ;;
759     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
760   esac
761 done
762 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
763
764 func_tmpdir
765 trap 'exit_status=$?
766       if test "$signal" != 0; then
767         echo "caught signal $signal" >&2
768       fi
769       rm -rf "$tmp"
770       exit $exit_status' 0
771 for signal in 1 2 3 13 15; do
772   trap '{ signal='$signal'; func_exit 1; }' $signal
773 done
774 signal=0
775
776 # func_lookup_file file
777 # looks up a file in $local_gnulib_dir or $gnulib_dir, or combines it through
778 # 'patch'.
779 # Input:
780 # - local_gnulib_dir  from --local-dir
781 # Output:
782 # - lookedup_file   name of the merged (combined) file
783 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
784 func_lookup_file ()
785 {
786   lkfile="$1"
787   if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile"; then
788     lookedup_file="$local_gnulib_dir/$lkfile"
789     lookedup_tmp=
790   else
791     if test -f "$gnulib_dir/$lkfile"; then
792       if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile.diff"; then
793         lkbase=`echo "$lkfile" | sed -e 's,^.*/,,'`
794         rm -f "$tmp/$lkbase"
795         cp "$gnulib_dir/$lkfile" "$tmp/$lkbase"
796         patch -s "$tmp/$lkbase" < "$local_gnulib_dir/$lkfile.diff" \
797           || func_fatal_error "patch file $local_gnulib_dir/$lkfile.diff didn't apply cleanly"
798         lookedup_file="$tmp/$lkbase"
799         lookedup_tmp=true
800       else
801         lookedup_file="$gnulib_dir/$lkfile"
802         lookedup_tmp=
803       fi
804     else
805       func_fatal_error "file $gnulib_dir/$lkfile not found"
806     fi
807   fi
808 }
809
810 # func_all_modules
811 # Input:
812 # - local_gnulib_dir  from --local-dir
813 func_all_modules ()
814 {
815   # Filter out metainformation files like README, which are not modules.
816   # Filter out unit test modules; they can be retrieved through
817   # --extract-tests-module if desired.
818   {
819     (cd "$gnulib_dir" && find modules -type f -print | sed -e 's,^modules/,,')
820     if test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules"; then
821       (cd "$local_gnulib_dir" && find modules -type f -print | sed -e 's,^modules/,,' -e 's,\.diff$,,')
822     fi
823   } \
824       | sed -e '/^CVS\//d' -e '/\/CVS\//d' \
825             -e '/^ChangeLog$/d' -e '/\/ChangeLog$/d' \
826             -e '/^COPYING$/d' -e '/\/COPYING$/d' \
827             -e '/^README$/d' -e '/\/README$/d' \
828             -e '/^TEMPLATE$/d' \
829             -e '/^TEMPLATE-TESTS$/d' \
830             -e '/^\..*/d' \
831             -e '/~$/d' \
832       | sed -e '/-tests$/d' \
833       | LC_ALL=C sort -u
834 }
835
836 # func_verify_module
837 # verifies a module name
838 # Input:
839 # - local_gnulib_dir  from --local-dir
840 # - module          module name argument
841 func_verify_module ()
842 {
843   if { test -f "$gnulib_dir/modules/$module" \
844        || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
845             && test -f "$local_gnulib_dir/modules/$module"; }; } \
846      && test "CVS" != "$module" \
847      && test "ChangeLog" != "$module" \
848      && test "COPYING" != "$module" \
849      && test "README" != "$module" \
850      && test "TEMPLATE" != "$module" \
851      && test "TEMPLATE-TESTS" != "$module"; then
852     # OK, $module is a correct module name.
853     # Verify that building the module description with 'patch' succeeds.
854     func_lookup_file "modules/$module"
855   else
856     echo "gnulib-tool: module $module doesn't exist" 1>&2
857     module=
858   fi
859 }
860
861 # func_verify_nontests_module
862 # verifies a module name, excluding tests modules
863 # Input:
864 # - local_gnulib_dir  from --local-dir
865 # - module          module name argument
866 func_verify_nontests_module ()
867 {
868   case "$module" in
869     *-tests ) module= ;;
870     * ) func_verify_module ;;
871   esac
872 }
873
874 # func_verify_tests_module
875 # verifies a module name, considering only tests modules
876 # Input:
877 # - local_gnulib_dir  from --local-dir
878 # - module          module name argument
879 func_verify_tests_module ()
880 {
881   case "$module" in
882     *-tests ) func_verify_module ;;
883     * ) module= ;;
884   esac
885 }
886
887 sed_extract_prog=':[     ]*$/ {
888   :a
889     n
890     s/^Description:[     ]*$//
891     s/^Files:[   ]*$//
892     s/^Depends-on:[      ]*$//
893     s/^configure\.ac-early:[     ]*$//
894     s/^configure\.ac:[   ]*$//
895     s/^Makefile\.am:[    ]*$//
896     s/^Include:[         ]*$//
897     s/^Link:[    ]*$//
898     s/^License:[         ]*$//
899     s/^Maintainer:[      ]*$//
900     tb
901     p
902     ba
903   :b
904 }'
905
906 # func_get_description module
907 # Input:
908 # - local_gnulib_dir  from --local-dir
909 func_get_description ()
910 {
911   func_lookup_file "modules/$1"
912   sed -n -e "/^Description$sed_extract_prog" < "$lookedup_file"
913 }
914
915 # func_get_filelist module
916 # Input:
917 # - local_gnulib_dir  from --local-dir
918 func_get_filelist ()
919 {
920   func_lookup_file "modules/$1"
921   sed -n -e "/^Files$sed_extract_prog" < "$lookedup_file"
922   echo m4/gnulib-common.m4
923   case "$autoconf_minversion" in
924     2.59)
925       #echo m4/onceonly.m4
926       echo m4/onceonly_2_57.m4
927       ;;
928   esac
929 }
930
931 # func_get_dependencies module
932 # Input:
933 # - local_gnulib_dir  from --local-dir
934 func_get_dependencies ()
935 {
936   # ${module}-tests always implicitly depends on ${module}.
937   echo "$1" | sed -n -e 's/-tests//p'
938   # Then the explicit dependencies listed in the module description.
939   func_lookup_file "modules/$1"
940   sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
941 }
942
943 # func_get_autoconf_early_snippet module
944 # Input:
945 # - local_gnulib_dir  from --local-dir
946 func_get_autoconf_early_snippet ()
947 {
948   func_lookup_file "modules/$1"
949   sed -n -e "/^configure\.ac-early$sed_extract_prog" < "$lookedup_file"
950 }
951
952 # func_get_autoconf_snippet module
953 # Input:
954 # - local_gnulib_dir  from --local-dir
955 func_get_autoconf_snippet ()
956 {
957   func_lookup_file "modules/$1"
958   sed -n -e "/^configure\.ac$sed_extract_prog" < "$lookedup_file"
959 }
960
961 # func_get_automake_snippet module
962 # Input:
963 # - local_gnulib_dir  from --local-dir
964 func_get_automake_snippet ()
965 {
966   func_lookup_file "modules/$1"
967   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file"
968   case "$1" in
969     *-tests)
970       # *-tests module live in tests/, not lib/.
971       ;;
972     *)
973       # Synthesize an EXTRA_DIST augmentation.
974       sed_combine_lines='/\\$/{
975         :a
976         N
977         s/\\\n/ /
978         s/\\$/\\/
979         ta
980       }'
981       sed_extract_mentioned_files='s/^lib_SOURCES[       ]*+=[   ]*//p'
982       already_mentioned_files=` \
983         sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file" \
984         | sed -e "$sed_combine_lines" \
985         | sed -n -e "$sed_extract_mentioned_files" | sed -e 's/#.*//'`
986       all_files=`func_get_filelist $1`
987       lib_files=`for f in $all_files; do \
988                    case $f in \
989                      lib/*) echo $f ;; \
990                    esac; \
991                  done | sed -e 's,^lib/,,'`
992       # Remove $already_mentioned_files from $lib_files.
993       echo "$lib_files" | LC_ALL=C sort -u > "$tmp"/lib-files
994       extra_files=`for f in $already_mentioned_files; do echo $f; done \
995                    | LC_ALL=C sort -u | LC_ALL=C join -v 2 - "$tmp"/lib-files`
996       if test -n "$extra_files"; then
997         echo "EXTRA_DIST +=" $extra_files
998         echo
999       fi
1000       # Synthesize also an EXTRA_lib_SOURCES augmentation.
1001       # This is necessary so that automake can generate the right list of
1002       # dependency rules.
1003       # A possible approach would be to use autom4te --trace of the redefined
1004       # AC_LIBOBJ and AC_REPLACE_FUNCS macros when creating the Makefile.am
1005       # (use autom4te --trace, not just grep, so that AC_LIBOBJ invocations
1006       # inside autoconf's built-in macros are not missed).
1007       # But it's simpler and more robust to do it here, based on the file list.
1008       # If some .c file exists and is not used with AC_LIBOBJ - for example,
1009       # a .c file is preprocessed into another .c file for BUILT_SOURCES -,
1010       # automake will generate a useless dependency; this is harmless.
1011       sed_extract_c_files='/\.c$/p'
1012       extra_files=`echo "$extra_files" | sed -n -e "$sed_extract_c_files"`
1013       if test -n "$extra_files"; then
1014         echo "EXTRA_lib_SOURCES +=" $extra_files
1015         echo
1016       fi
1017       ;;
1018   esac
1019 }
1020
1021 # func_get_include_directive module
1022 # Input:
1023 # - local_gnulib_dir  from --local-dir
1024 func_get_include_directive ()
1025 {
1026   func_lookup_file "modules/$1"
1027   sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
1028   sed -e 's/^\(["<]\)/#include \1/'
1029 }
1030
1031 # func_get_link_directive module
1032 # Input:
1033 # - local_gnulib_dir  from --local-dir
1034 func_get_link_directive ()
1035 {
1036   func_lookup_file "modules/$1"
1037   sed -n -e "/^Link$sed_extract_prog" < "$lookedup_file"
1038 }
1039
1040 # func_get_license module
1041 # Input:
1042 # - local_gnulib_dir  from --local-dir
1043 func_get_license ()
1044 {
1045   func_lookup_file "modules/$1"
1046   { sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
1047     # The default is GPL.
1048     echo "GPL"
1049   } | sed -e 's,^ *$,,' | sed -e 1q
1050 }
1051
1052 # func_get_maintainer module
1053 # Input:
1054 # - local_gnulib_dir  from --local-dir
1055 func_get_maintainer ()
1056 {
1057   func_lookup_file "modules/$1"
1058   sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
1059 }
1060
1061 # func_get_tests_module module
1062 # Input:
1063 # - local_gnulib_dir  from --local-dir
1064 func_get_tests_module ()
1065 {
1066   # The naming convention for tests modules is hardwired: ${module}-tests.
1067   if test -f "$gnulib_dir/modules/$1"-tests \
1068      || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
1069           && test -f "$local_gnulib_dir/modules/$1"-tests; }; then
1070     echo "$1"-tests
1071   fi
1072 }
1073
1074 # func_acceptable module
1075 # tests whether a module is acceptable.
1076 # Input:
1077 # - avoidlist       list of modules to avoid
1078 func_acceptable ()
1079 {
1080   for avoid in $avoidlist; do
1081     if test "$avoid" = "$1"; then
1082       return 1
1083     fi
1084   done
1085   return 0
1086 }
1087
1088 # func_modules_transitive_closure
1089 # Input:
1090 # - local_gnulib_dir  from --local-dir
1091 # - modules         list of specified modules
1092 # - inctests        true if tests should be included, blank otherwise
1093 # - avoidlist       list of modules to avoid
1094 # - tmp             pathname of a temporary directory
1095 # Output:
1096 # - modules         list of modules, including dependencies
1097 func_modules_transitive_closure ()
1098 {
1099   # In order to process every module only once (for speed), process an "input
1100   # list" of modules, producing an "output list" of modules. During each round,
1101   # more modules can be queued in the input list. Once a module on the input
1102   # list has been processed, it is added to the "handled list", so we can avoid
1103   # to process it again.
1104   handledmodules=
1105   inmodules="$modules"
1106   outmodules=
1107   while test -n "$inmodules"; do
1108     inmodules_this_round="$inmodules"
1109     inmodules=                    # Accumulator, queue for next round
1110     for module in $inmodules_this_round; do
1111       func_verify_module
1112       if test -n "$module"; then
1113         if func_acceptable $module; then
1114           func_append outmodules " $module"
1115           deps=`func_get_dependencies $module`
1116           # Duplicate dependencies are harmless, but Jim wants a warning.
1117           duplicated_deps=`echo "$deps" | LC_ALL=C sort | LC_ALL=C uniq -d`
1118           if test -n "$duplicated_deps"; then
1119             echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
1120           fi
1121           func_append inmodules " $deps"
1122           if test -n "$inctests"; then
1123             testsmodule=`func_get_tests_module $module`
1124             if test -n "$testsmodule"; then
1125               func_append inmodules " $testsmodule"
1126             fi
1127           fi
1128         fi
1129       fi
1130     done
1131     handledmodules=`for m in $handledmodules $inmodules_this_round; do echo $m; done | LC_ALL=C sort -u`
1132     # Remove $handledmodules from $inmodules.
1133     for m in $inmodules; do echo $m; done | LC_ALL=C sort -u > "$tmp"/queued-modules
1134     inmodules=`echo "$handledmodules" | LC_ALL=C join -v 2 - "$tmp"/queued-modules`
1135   done
1136   modules=`for m in $outmodules; do echo $m; done | LC_ALL=C sort -u`
1137   rm -f "$tmp"/queued-modules
1138 }
1139
1140 # func_modules_add_dummy
1141 # Input:
1142 # - local_gnulib_dir  from --local-dir
1143 # - modules         list of modules, including dependencies
1144 # Output:
1145 # - modules         list of modules, including 'dummy' if needed
1146 func_modules_add_dummy ()
1147 {
1148   have_lib_SOURCES=
1149   sed_remove_backslash_newline=':a
1150 /\\$/{
1151 s/\\$//
1152 N
1153 s/\n//
1154 ba
1155 }'
1156   for module in $modules; do
1157     func_verify_nontests_module
1158     if test -n "$module"; then
1159       # Extract the value of "lib_SOURCES += ...".
1160       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
1161         # Ignore .h files since they are not compiled.
1162         case "$file" in
1163           *.h) ;;
1164           *) have_lib_SOURCES=yes ;;
1165         esac
1166       done
1167     fi
1168   done
1169   # Add the dummy module, to make sure the library will be non-empty.
1170   if test -z "$have_lib_SOURCES"; then
1171     func_append modules " dummy"
1172   fi
1173 }
1174
1175 # func_modules_to_filelist
1176 # Input:
1177 # - local_gnulib_dir  from --local-dir
1178 # - modules         list of modules, including dependencies
1179 # Output:
1180 # - files           list of files
1181 func_modules_to_filelist ()
1182 {
1183   files=
1184   for module in $modules; do
1185     func_verify_module
1186     if test -n "$module"; then
1187       fs=`func_get_filelist $module`
1188       func_append files " $fs"
1189     fi
1190   done
1191   files=`for f in $files; do echo $f; done | LC_ALL=C sort -u`
1192 }
1193
1194 # func_emit_lib_Makefile_am
1195 # emits the contents of library makefile to standard output.
1196 # Input:
1197 # - local_gnulib_dir  from --local-dir
1198 # - modules         list of modules, including dependencies
1199 # - libname         library name
1200 # - auxdir          directory relative to destdir where to place build aux files
1201 # - makefile_name   from --makefile-name
1202 # - libtool         true if libtool will be used, false or blank otherwise
1203 # - macro_prefix    prefix of gl_LIBOBJS macros to use
1204 # - actioncmd       (optional) command that will reproduce this invocation
1205 # - for_test        true if creating a package for testing, false otherwise
1206 # Output:
1207 # - uses_subdirs    nonempty if object files in subdirs exist
1208 func_emit_lib_Makefile_am ()
1209 {
1210   # When creating an includable Makefile.am snippet, augment variables with
1211   # += instead of assigning them.
1212   if test -n "$makefile_name"; then
1213     assign='+='
1214   else
1215     assign='='
1216   fi
1217   if test "$libtool" = true; then
1218     libext=la
1219     perhapsLT=LT
1220     sed_eliminate_LDFLAGS=
1221   else
1222     libext=a
1223     perhapsLT=
1224     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
1225   fi
1226   if $for_test; then
1227     # When creating a package for testing: Attempt to provoke failures,
1228     # especially link errors, already during "make" rather than during
1229     # "make check", because "make check" is not possible in a cross-compiling
1230     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
1231     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
1232   else
1233     sed_transform_check_PROGRAMS=
1234   fi
1235   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
1236   echo "## Process this file with automake to produce Makefile.in."
1237   func_emit_copyright_notice
1238   if test -n "$actioncmd"; then
1239     echo "# Reproduce by: $actioncmd"
1240   fi
1241   echo
1242   uses_subdirs=
1243   {
1244     for module in $modules; do
1245       func_verify_nontests_module
1246       if test -n "$module"; then
1247         {
1248           func_get_automake_snippet "$module" |
1249             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1250                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1251             sed -e "$sed_eliminate_LDFLAGS" |
1252             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1253             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1254                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' |
1255             sed -e "$sed_transform_check_PROGRAMS"
1256           if test "$module" = 'alloca'; then
1257             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
1258             echo "${libname}_${libext}_DEPENDENCIES += @${perhapsLT}ALLOCA@"
1259           fi
1260         } > amsnippet.tmp
1261         # Skip the contents if its entirely empty.
1262         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1263           echo "## begin gnulib module $module"
1264           echo
1265           cat amsnippet.tmp
1266           echo "## end   gnulib module $module"
1267           echo
1268         fi
1269         rm -f amsnippet.tmp
1270         # Test whether there are some source files in subdirectories.
1271         for f in `func_get_filelist "$module"`; do
1272           case $f in
1273             lib/*/*.c) uses_subdirs=yes ;;
1274           esac
1275         done
1276       fi
1277     done
1278   } > allsnippets.tmp
1279   if test -z "$makefile_name"; then
1280     # If there are source files in subdirectories, prevent collision of the
1281     # object files (example: hash.c and libxml/hash.c).
1282     subdir_options=
1283     if test -n "$uses_subdirs"; then
1284       subdir_options=' subdir-objects'
1285     fi
1286     echo "AUTOMAKE_OPTIONS = 1.5 gnits${subdir_options}"
1287   fi
1288   echo
1289   if test -z "$makefile_name"; then
1290     echo "noinst_HEADERS ="
1291     echo "noinst_LIBRARIES ="
1292     echo "noinst_LTLIBRARIES ="
1293     # Automake versions < 1.9b create an empty pkgdatadir at installation time
1294     # if you specify pkgdata_DATA to empty. This is a workaround.
1295     if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1296       echo "pkgdata_DATA ="
1297     fi
1298     echo "EXTRA_DIST ="
1299     echo "BUILT_SOURCES ="
1300     echo "SUFFIXES ="
1301   fi
1302   echo "MOSTLYCLEANFILES $assign core *.stackdump"
1303   if test -z "$makefile_name"; then
1304     echo "MOSTLYCLEANDIRS ="
1305     echo "CLEANFILES ="
1306     echo "DISTCLEANFILES ="
1307     echo "MAINTAINERCLEANFILES ="
1308     echo
1309     echo "AM_CPPFLAGS ="
1310   fi
1311   echo
1312   if LC_ALL=C grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *= *$libname\\.$libext\$" allsnippets.tmp > /dev/null; then
1313     # One of the snippets already specifies an installation location for the
1314     # library. Don't confuse automake by saying it should not be installed.
1315     :
1316   else
1317     # By default, the generated library should not be installed.
1318     echo "noinst_${perhapsLT}LIBRARIES += $libname.$libext"
1319   fi
1320   echo
1321   echo "${libname}_${libext}_SOURCES ="
1322   # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
1323   # automake during its analyses looks for $(LIBOBJS), not for @LIBOBJS@.
1324   echo "${libname}_${libext}_LIBADD = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
1325   echo "${libname}_${libext}_DEPENDENCIES = \$(${macro_prefix}_${perhapsLT}LIBOBJS)"
1326   echo "EXTRA_${libname}_${libext}_SOURCES ="
1327   if test "$libtool" = true; then
1328     echo "${libname}_${libext}_LDFLAGS = \$(AM_LDFLAGS)"
1329   fi
1330   echo
1331   cat allsnippets.tmp \
1332     | sed -e 's|\$(top_srcdir)/build-aux/|$(top_srcdir)/'"$auxdir"'/|g'
1333   echo
1334   echo "mostlyclean-local: mostlyclean-generic"
1335   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
1336   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
1337   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1338   echo "          fi; \\"
1339   echo "        done"
1340   rm -f allsnippets.tmp
1341 }
1342
1343 # func_emit_tests_Makefile_am
1344 # emits the contents of tests makefile to standard output.
1345 # Input:
1346 # - local_gnulib_dir  from --local-dir
1347 # - modules         list of modules, including dependencies
1348 # - libname         library name
1349 # - makefile_name   from --makefile-name
1350 # - libtool         true if libtool will be used, false or blank otherwise
1351 # - sourcebase      relative directory containing lib source code
1352 # - m4base          relative directory containing autoconf macros
1353 # - testsbase       relative directory containing unit test code
1354 # - for_test        true if creating a package for testing, false otherwise
1355 func_emit_tests_Makefile_am ()
1356 {
1357   if test "$libtool" = true; then
1358     libext=la
1359   else
1360     libext=a
1361   fi
1362   if test "$libtool" = true; then
1363     libext=la
1364     sed_eliminate_LDFLAGS=
1365   else
1366     libext=a
1367     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[        ]*+=/d'
1368   fi
1369   if $for_test; then
1370     # When creating a package for testing: Attempt to provoke failures,
1371     # especially link errors, already during "make" rather than during
1372     # "make check", because "make check" is not possible in a cross-compiling
1373     # situation. Turn check_PROGRAMS into noinst_PROGRAMS.
1374     sed_transform_check_PROGRAMS='s,check_PROGRAMS,noinst_PROGRAMS,g'
1375   else
1376     sed_transform_check_PROGRAMS=
1377   fi
1378   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
1379   echo "## DO NOT EDIT! GENERATED AUTOMATICALLY!"
1380   echo "## Process this file with automake to produce Makefile.in."
1381   func_emit_copyright_notice
1382   echo
1383   # Generate dependencies here, since it eases the debugging of test failures.
1384   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1385   echo
1386   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
1387   echo
1388   (
1389     for module in $modules; do
1390       func_verify_tests_module
1391       if test -n "$module"; then
1392         {
1393           func_get_automake_snippet "$module" |
1394             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1395                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1396             sed -e "$sed_eliminate_LDFLAGS" |
1397             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1398             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1399                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g' |
1400             sed -e "$sed_transform_check_PROGRAMS"
1401         } > amsnippet.tmp
1402         # Skip the contents if its entirely empty.
1403         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1404           echo "## begin gnulib module $module"
1405           echo
1406           cat amsnippet.tmp
1407           echo "## end   gnulib module $module"
1408           echo
1409         fi
1410         rm -f amsnippet.tmp
1411       fi
1412     done
1413   ) > allsnippets.tmp
1414   # Nothing is being added to SUBDIRS; nevertheless the existence of this
1415   # variable is needed to avoid an error from automake:
1416   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
1417   echo "SUBDIRS ="
1418   echo "TESTS ="
1419   echo "TESTS_ENVIRONMENT ="
1420   echo "noinst_PROGRAMS ="
1421   if ! $for_test; then
1422     echo "check_PROGRAMS ="
1423   fi
1424   echo "noinst_HEADERS ="
1425   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1426     echo "pkgdata_DATA ="
1427   fi
1428   echo "EXTRA_DIST ="
1429   echo "BUILT_SOURCES ="
1430   echo "SUFFIXES ="
1431   echo "MOSTLYCLEANFILES = core *.stackdump"
1432   echo "MOSTLYCLEANDIRS ="
1433   echo "CLEANFILES ="
1434   echo "DISTCLEANFILES ="
1435   echo "MAINTAINERCLEANFILES ="
1436   echo
1437   echo "AM_CPPFLAGS = \\"
1438   echo "  -I. -I\$(srcdir) \\"
1439   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
1440   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
1441   echo
1442   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
1443   echo
1444   cat allsnippets.tmp
1445   echo "# Clean up after Solaris cc."
1446   echo "clean-local:"
1447   echo "        rm -rf SunWS_cache"
1448   echo
1449   echo "mostlyclean-local: mostlyclean-generic"
1450   echo "        @for dir in '' \$(MOSTLYCLEANDIRS); do \\"
1451   echo "          if test -n \"\$\$dir\" && test -d \$\$dir; then \\"
1452   echo "            echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1453   echo "          fi; \\"
1454   echo "        done"
1455   rm -f allsnippets.tmp
1456 }
1457
1458 # func_emit_initmacro_start
1459 # emits the first few statements of the gl_INIT macro to standard output.
1460 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1461 func_emit_initmacro_start ()
1462 {
1463   # Overriding AC_LIBOBJ and AC_REPLACE_FUNCS has the effect of storing
1464   # platform-dependent object files in ${macro_prefix}_LIBOBJS instead of
1465   # LIBOBJS.  The purpose is to allow several gnulib instantiations under
1466   # a single configure.ac file.  (AC_CONFIG_LIBOBJ_DIR does not allow this
1467   # flexibility.)
1468   # Furthermore it avoids an automake error like this when a Makefile.am
1469   # that uses pieces of gnulib also uses $(LIBOBJ):
1470   #   automatically discovered file `error.c' should not be explicitly mentioned
1471   echo "  m4_pushdef([AC_LIBOBJ], m4_defn([${macro_prefix}_LIBOBJ]))"
1472   echo "  m4_pushdef([AC_REPLACE_FUNCS], m4_defn([${macro_prefix}_REPLACE_FUNCS]))"
1473   # Overriding AC_LIBSOURCES has the same purpose of avoiding the automake
1474   # error when a Makefile.am that uses pieces of gnulib also uses $(LIBOBJ):
1475   #   automatically discovered file `error.c' should not be explicitly mentioned
1476   # We let automake know about the files to be distributed through the
1477   # EXTRA_lib_SOURCES variable.
1478   echo "  m4_pushdef([AC_LIBSOURCES], m4_defn([${macro_prefix}_LIBSOURCES]))"
1479 }
1480
1481 # func_emit_initmacro_end
1482 # emits the last few statements of the gl_INIT macro to standard output.
1483 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1484 func_emit_initmacro_end ()
1485 {
1486   echo "  m4_popdef([AC_LIBSOURCES])"
1487   echo "  m4_popdef([AC_REPLACE_FUNCS])"
1488   echo "  m4_popdef([AC_LIBOBJ])"
1489   echo "  AC_CONFIG_COMMANDS_PRE(["
1490   echo "    ${macro_prefix}_libobjs="
1491   echo "    ${macro_prefix}_ltlibobjs="
1492   echo "    if test -n \"\$${macro_prefix}_LIBOBJS\"; then"
1493   echo "      # Remove the extension."
1494   echo "      sed_drop_objext='s/\\.o\$//;s/\\.obj\$//'"
1495   echo "      for i in \`for i in \$${macro_prefix}_LIBOBJS; do echo \"\$i\"; done | sed \"\$sed_drop_objext\" | sort | uniq\`; do"
1496   echo "        ${macro_prefix}_libobjs=\"\$${macro_prefix}_libobjs \$i.\$ac_objext\""
1497   echo "        ${macro_prefix}_ltlibobjs=\"\$${macro_prefix}_ltlibobjs \$i.lo\""
1498   echo "      done"
1499   echo "    fi"
1500   echo "    AC_SUBST([${macro_prefix}_LIBOBJS], [\$${macro_prefix}_libobjs])"
1501   echo "    AC_SUBST([${macro_prefix}_LTLIBOBJS], [\$${macro_prefix}_ltlibobjs])"
1502   echo "  ])"
1503 }
1504
1505 # func_emit_initmacro_done
1506 # emits a few statements after the gl_INIT macro to standard output.
1507 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1508 func_emit_initmacro_done ()
1509 {
1510   echo
1511   echo "# Like AC_LIBOBJ, except that the module name goes"
1512   echo "# into ${macro_prefix}_LIBOBJS instead of into LIBOBJS."
1513   echo "AC_DEFUN([${macro_prefix}_LIBOBJ],"
1514   echo "  [${macro_prefix}_LIBOBJS=\"\$${macro_prefix}_LIBOBJS \$1.\$ac_objext\"])"
1515   echo
1516   echo "# Like AC_REPLACE_FUNCS, except that the module name goes"
1517   echo "# into ${macro_prefix}_LIBOBJS instead of into LIBOBJS."
1518   echo "AC_DEFUN([${macro_prefix}_REPLACE_FUNCS],"
1519   echo "  [AC_CHECK_FUNCS([\$1], , [${macro_prefix}_LIBOBJ(\$ac_func)])])"
1520   echo
1521   echo "# Like AC_LIBSOURCES, except that it does nothing."
1522   echo "# We rely on EXTRA_lib..._SOURCES instead."
1523   echo "AC_DEFUN([${macro_prefix}_LIBSOURCES],"
1524   echo "  [])"
1525 }
1526
1527 # func_import modules
1528 # Uses also the variables
1529 # - destdir         target directory
1530 # - local_gnulib_dir  from --local-dir
1531 # - libname         library name
1532 # - sourcebase      directory relative to destdir where to place source code
1533 # - m4base          directory relative to destdir where to place *.m4 macros
1534 # - docbase         directory relative to destdir where to place doc files
1535 # - testsbase       directory relative to destdir where to place unit test code
1536 # - auxdir          directory relative to destdir where to place build aux files
1537 # - inctests        true if --with-tests was given, blank otherwise
1538 # - avoidlist       list of modules to avoid, from --avoid
1539 # - lgpl            true if library's license shall be LGPL, blank otherwise
1540 # - makefile_name   from --makefile-name
1541 # - libtool         true if --libtool was given, false if --no-libtool was
1542 #                   given, blank otherwise
1543 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
1544 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1545 # - autoconf_minversion  minimum supported autoconf version
1546 # - doit            : if actions shall be executed, false if only to be printed
1547 # - symbolic        true if files should be symlinked, copied otherwise
1548 # - lsymbolic       true if files from local_gnulib_dir should be symlinked,
1549 #                   copied otherwise
1550 func_import ()
1551 {
1552   # Get the cached settings.
1553   cached_local_gnulib_dir=
1554   cached_specified_modules=
1555   cached_avoidlist=
1556   cached_sourcebase=
1557   cached_m4base=
1558   cached_docbase=
1559   cached_testsbase=
1560   cached_libname=
1561   cached_lgpl=
1562   cached_makefile_name=
1563   cached_libtool=
1564   cached_macro_prefix=
1565   cached_files=
1566   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1567     cached_libtool=false
1568     my_sed_traces='
1569       s,#.*$,,
1570       s,^dnl .*$,,
1571       s, dnl .*$,,
1572       /gl_LOCAL_DIR(/ {
1573         s,^.*gl_LOCAL_DIR([[ ]*\([^])]*\).*$,cached_local_gnulib_dir="\1",p
1574       }
1575       /gl_MODULES(/ {
1576         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
1577       }
1578       /gl_AVOID(/ {
1579         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
1580       }
1581       /gl_SOURCE_BASE(/ {
1582         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
1583       }
1584       /gl_M4_BASE(/ {
1585         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
1586       }
1587       /gl_DOC_BASE(/ {
1588         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
1589       }
1590       /gl_TESTS_BASE(/ {
1591         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
1592       }
1593       /gl_LIB(/ {
1594         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
1595       }
1596       /gl_LGPL/ {
1597         s,^.*$,cached_lgpl=true,p
1598       }
1599       /gl_MAKEFILE_NAME(/ {
1600         s,^.*gl_MAKEFILE_NAME([[ ]*\([^])]*\).*$,cached_makefile_name="\1",p
1601       }
1602       /gl_LIBTOOL/ {
1603         s,^.*$,cached_libtool=true,p
1604       }
1605       /gl_MACRO_PREFIX(/ {
1606         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
1607       }'
1608     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
1609     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1610       my_sed_traces='
1611         s,#.*$,,
1612         s,^dnl .*$,,
1613         s, dnl .*$,,
1614         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
1615           s,^.*$,cached_files=",p
1616           n
1617           ta
1618           :a
1619           s,^\]).*$,",
1620           tb
1621           p
1622           n
1623           ba
1624           :b
1625           p
1626         }'
1627       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1628     fi
1629   fi
1630
1631   # Merge the cached settings with the specified ones.
1632   # The m4base must be the same as expected from the pathname.
1633   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1634     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1635   fi
1636   # The local_gnulib_dir defaults to the cached one. Recall that the cached one
1637   # is relative to $destdir, whereas the one we use is relative to . or absolute.
1638   if test -z "$local_gnulib_dir"; then
1639     if test -n "$cached_local_gnulib_dir"; then
1640       case "$destdir" in
1641         /*)
1642           local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1643         *)
1644           case "$cached_local_gnulib_dir" in
1645             /*)
1646               local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1647             *)
1648               func_relconcat "$destdir" "$cached_local_gnulib_dir"
1649               local_gnulib_dir="$relconcat" ;;
1650           esac ;;
1651       esac
1652     fi
1653   fi
1654   # Append the cached and the specified module names. So that
1655   # "gnulib-tool --import foo" means to add the module foo.
1656   specified_modules="$cached_specified_modules $1"
1657   # Append the cached and the specified avoidlist. This is probably better
1658   # than dropping the cached one when --avoid is specified at least once.
1659   avoidlist=`echo $cached_avoidlist $avoidlist`
1660   # The sourcebase defaults to the cached one.
1661   if test -z "$sourcebase"; then
1662     sourcebase="$cached_sourcebase"
1663     if test -z "$sourcebase"; then
1664       func_fatal_error "missing --source-base option"
1665     fi
1666   fi
1667   # The docbase defaults to the cached one.
1668   if test -z "$docbase"; then
1669     docbase="$cached_docbase"
1670     if test -z "$docbase"; then
1671       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."
1672     fi
1673   fi
1674   # The testsbase defaults to the cached one.
1675   if test -z "$testsbase"; then
1676     testsbase="$cached_testsbase"
1677     if test -z "$testsbase"; then
1678       func_fatal_error "missing --tests-base option"
1679     fi
1680   fi
1681   # The libname defaults to the cached one.
1682   if test -z "$supplied_libname"; then
1683     libname="$cached_libname"
1684     if test -z "$libname"; then
1685       func_fatal_error "missing --lib option"
1686     fi
1687   fi
1688   # Require LGPL if specified either way.
1689   if test -z "$lgpl"; then
1690     lgpl="$cached_lgpl"
1691   fi
1692   # The makefile_name defaults to the cached one.
1693   if test -z "$makefile_name"; then
1694     makefile_name="$cached_makefile_name"
1695   fi
1696   # Use libtool if specified either way, or if guessed.
1697   if test -z "$libtool"; then
1698     if test -n "$cached_m4base"; then
1699       libtool="$cached_libtool"
1700     else
1701       libtool="$guessed_libtool"
1702     fi
1703   fi
1704   # The macro_prefix defaults to the cached one.
1705   if test -z "$macro_prefix"; then
1706     macro_prefix="$cached_macro_prefix"
1707     if test -z "$macro_prefix"; then
1708       func_fatal_error "missing --macro-prefix option"
1709     fi
1710   fi
1711
1712   # Canonicalize the list of specified modules.
1713   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort -u`
1714
1715   # Determine final module list.
1716   modules="$specified_modules"
1717   func_modules_transitive_closure
1718   echo "Module list with included dependencies:"
1719   echo "$modules" | sed -e 's/^/  /'
1720
1721   # Add the dummy module if needed.
1722   func_modules_add_dummy
1723
1724   # If --lgpl, verify that the licenses of modules are compatible.
1725   if test -n "$lgpl"; then
1726     for module in $modules; do
1727       license=`func_get_license $module`
1728       case $license in
1729         LGPL | 'GPLed build tool') ;;
1730         'public domain' | 'unlimited' | 'unmodifiable license text') ;;
1731         *) func_fatal_error "incompatible license on module $module: $license" ;;
1732       esac
1733     done
1734   fi
1735
1736   # Determine script to apply to imported library files.
1737   sed_transform_lib_file=
1738   for module in $modules; do
1739     if test $module = config-h; then
1740       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
1741       sed_transform_lib_file=$sed_transform_lib_file'
1742         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
1743       '
1744       break
1745     fi
1746   done
1747   if test -n "$lgpl"; then
1748     # Update license.
1749     sed_transform_lib_file=$sed_transform_lib_file'
1750       s/GNU General/GNU Lesser General/g
1751       s/version 2\([ ,]\)/version 2.1\1/g
1752     '
1753   fi
1754
1755   # Determine final file list.
1756   func_modules_to_filelist
1757   echo "File list:"
1758   echo "$files" | sed -e 's/^/  /'
1759
1760   test -n "$files" \
1761     || func_fatal_error "refusing to do nothing"
1762
1763   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1764   new_files="$files m4/gnulib-tool.m4"
1765   old_files="$cached_files"
1766   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1767     func_append old_files " m4/gnulib-tool.m4"
1768   fi
1769
1770   sed_rewrite_old_files="\
1771     s,^build-aux/,$auxdir/,
1772     s,^doc/,$cached_docbase/,
1773     s,^lib/,$cached_sourcebase/,
1774     s,^m4/,$cached_m4base/,
1775     s,^tests/,$cached_testsbase/,"
1776   sed_rewrite_new_files="\
1777     s,^build-aux/,$auxdir/,
1778     s,^doc/,$docbase/,
1779     s,^lib/,$sourcebase/,
1780     s,^m4/,$m4base/,
1781     s,^tests/,$testsbase/,"
1782
1783   # Create directories.
1784   { echo "$sourcebase"
1785     echo "$m4base"
1786     docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1787     if test -n "$docfiles"; then
1788       echo "$docbase"
1789     fi
1790     if test -n "$inctests"; then
1791       echo "$testsbase"
1792     fi
1793     echo "$auxdir"
1794     for f in $files; do echo $f; done \
1795       | sed -e "$sed_rewrite_new_files" \
1796       | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
1797       | LC_ALL=C sort -u
1798   } > "$tmp"/dirs
1799   { # Rearrange file descriptors. Needed because "while ... done < ..."
1800     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1801     exec 5<&0 < "$tmp"/dirs
1802     while read d; do
1803       if test ! -d "$destdir/$d"; then
1804         if $doit; then
1805           echo "Creating directory $destdir/$d"
1806           mkdir -p "$destdir/$d" || func_fatal_error "failed"
1807         else
1808           echo "Create directory $destdir/$d"
1809         fi
1810       fi
1811     done
1812     exec 0<&5 5<&-
1813   }
1814
1815   # func_dest_tmpfilename file
1816   # determines the name of a temporary file (file is relative to destdir).
1817   # Sets variable:
1818   #   - tmpfile       absolute filename of the temporary file
1819   func_dest_tmpfilename ()
1820   {
1821     if $doit; then
1822       # Put the new contents of $file in a file in the same directory (needed
1823       # to guarantee that an 'mv' to "$destdir/$file" works).
1824       tmpfile="$destdir/$1.tmp"
1825     else
1826       # Put the new contents of $file in a file in a temporary directory
1827       # (because the directory of "$file" might not exist).
1828       tmpfile="$tmp"/`basename "$1"`.tmp
1829     fi
1830   }
1831
1832   # Copy files or make symbolic links. Remove obsolete files.
1833   delimiter='   '
1834   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1835   # representing the files according to the last gnulib-tool invocation.
1836   for f in $old_files; do echo $f; done \
1837     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_old_files" \
1838     | LC_ALL=C sort \
1839     > "$tmp"/old-files
1840   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1841   # representing the files after this gnulib-tool invocation.
1842   for f in $new_files; do echo $f; done \
1843     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" \
1844     | LC_ALL=C sort \
1845     > "$tmp"/new-files
1846   # First the files that are in old-files, but not in new-files:
1847   sed_take_first_column='s,'"$delimiter"'.*,,'
1848   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1849     # Remove the file. Do nothing if the user already removed it.
1850     if test -f "$destdir/$g"; then
1851       if $doit; then
1852         echo "Removing file $g (backup in ${g}~)"
1853         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1854       else
1855         echo "Remove file $g (backup in ${g}~)"
1856       fi
1857     fi
1858   done
1859   # func_add_or_update handles a file that ought to be present afterwards.
1860   # Uses parameters
1861   # - f             the original file name
1862   # - g             the rewritten file name
1863   # - already_present  nonempty if the file already exists, empty otherwise
1864   func_add_or_update ()
1865   {
1866     func_dest_tmpfilename "$g"
1867     func_lookup_file "$f"
1868     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
1869     if test -n "$sed_transform_lib_file"; then
1870       case "$f" in
1871         lib/*)
1872           sed -e "$sed_transform_lib_file" \
1873             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
1874           ;;
1875       esac
1876     fi
1877     if test -f "$destdir/$g"; then
1878       # The file already exists.
1879       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1880         : # The file has not changed.
1881       else
1882         # Replace the file.
1883         if $doit; then
1884           if test -n "$already_present"; then
1885             echo "Updating file $g (backup in ${g}~)"
1886           else
1887             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1888           fi
1889           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1890           if { test -n "$symbolic" \
1891                || { test -n "$lsymbolic" \
1892                     && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
1893              && test -z "$lookedup_tmp" \
1894              && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1895             func_ln_if_changed "$lookedup_file" "$destdir/$g"
1896           else
1897             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1898           fi
1899         else
1900           if test -n "$already_present"; then
1901             echo "Update file $g (backup in ${g}~)"
1902           else
1903             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1904           fi
1905         fi
1906       fi
1907     else
1908       # Install the file.
1909       # Don't protest if the file should be there but isn't: it happens
1910       # frequently that developers don't put autogenerated files into CVS.
1911       if $doit; then
1912         echo "Copying file $g"
1913         if { test -n "$symbolic" \
1914              || { test -n "$lsymbolic" \
1915                   && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; } \
1916            && test -z "$lookedup_tmp" \
1917            && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1918           func_ln_if_changed "$lookedup_file" "$destdir/$g"
1919         else
1920           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1921         fi
1922       else
1923         echo "Copy file $g"
1924       fi
1925     fi
1926     rm -f "$tmpfile"
1927   }
1928   # Then the files that are in new-files, but not in old-files:
1929   sed_take_last_column='s,^.*'"$delimiter"',,'
1930   already_present=
1931   LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files \
1932     | sed -e "$sed_take_last_column" \
1933     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/added-files
1934   { # Rearrange file descriptors. Needed because "while ... done < ..."
1935     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1936     exec 5<&0 < "$tmp"/added-files
1937     while read g f; do
1938       func_add_or_update
1939     done
1940     exec 0<&5 5<&-
1941   }
1942   # Then the files that are in new-files and in old-files:
1943   already_present=true
1944   LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files \
1945     | sed -e "$sed_take_last_column" \
1946     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/kept-files
1947   { # Rearrange file descriptors. Needed because "while ... done < ..."
1948     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
1949     exec 5<&0 < "$tmp"/kept-files
1950     while read g f; do
1951       func_add_or_update
1952     done
1953     exec 0<&5 5<&-
1954   }
1955
1956   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1957   actioncmd="gnulib-tool --import"
1958   func_append actioncmd " --dir=$destdir"
1959   if test -n "$local_gnulib_dir"; then
1960     func_append actioncmd " --local-dir=$local_gnulib_dir"
1961   fi
1962   func_append actioncmd " --lib=$libname"
1963   func_append actioncmd " --source-base=$sourcebase"
1964   func_append actioncmd " --m4-base=$m4base"
1965   func_append actioncmd " --doc-base=$docbase"
1966   func_append actioncmd " --aux-dir=$auxdir"
1967   for module in $avoidlist; do
1968     func_append actioncmd " --avoid=$module"
1969   done
1970   if test -n "$lgpl"; then
1971     func_append actioncmd " --lgpl"
1972   fi
1973   if test -n "$makefile_name"; then
1974     func_append actioncmd " --makefile-name=$makefile_name"
1975   fi
1976   if test "$libtool" = true; then
1977     func_append actioncmd " --libtool"
1978   else
1979     func_append actioncmd " --no-libtool"
1980   fi
1981   func_append actioncmd " --macro-prefix=$macro_prefix"
1982   func_append actioncmd " `echo $specified_modules`"
1983
1984   # Default the makefile name to Makefile.am.
1985   if test -n "$makefile_name"; then
1986     makefile_am=$makefile_name
1987   else
1988     makefile_am=Makefile.am
1989   fi
1990
1991   # Create normal Makefile.ams.
1992   for_test=false
1993
1994   # Create library makefile.
1995   func_dest_tmpfilename $sourcebase/$makefile_am
1996   func_emit_lib_Makefile_am > "$tmpfile"
1997   if test -f "$destdir"/$sourcebase/$makefile_am; then
1998     if cmp "$destdir"/$sourcebase/$makefile_am "$tmpfile" > /dev/null; then
1999       rm -f "$tmpfile"
2000     else
2001       if $doit; then
2002         echo "Updating $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
2003         mv -f "$destdir"/$sourcebase/$makefile_am "$destdir"/$sourcebase/$makefile_am~
2004         mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
2005       else
2006         echo "Update $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
2007         rm -f "$tmpfile"
2008       fi
2009     fi
2010   else
2011     if $doit; then
2012       echo "Creating $sourcebase/$makefile_am"
2013       mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
2014     else
2015       echo "Create $sourcebase/$makefile_am"
2016       rm -f "$tmpfile"
2017     fi
2018   fi
2019
2020   # Create m4/gnulib-cache.m4.
2021   func_dest_tmpfilename $m4base/gnulib-cache.m4
2022   (
2023     func_emit_copyright_notice
2024     echo "#"
2025     echo "# This file represents the specification of how gnulib-tool is used."
2026     echo "# It acts as a cache: It is written and read by gnulib-tool."
2027     echo "# In projects using CVS, this file is meant to be stored in CVS,"
2028     echo "# like the configure.ac and various Makefile.am files."
2029     echo
2030     echo
2031     echo "# Specification in the form of a command-line invocation:"
2032     echo "#   $actioncmd"
2033     echo
2034     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
2035     # Store the local_gnulib_dir relative to destdir.
2036     case "$local_gnulib_dir" in
2037       "" | /*)
2038         relative_local_gnulib_dir="$local_gnulib_dir" ;;
2039       * )
2040         case "$destdir" in
2041           /*) relative_local_gnulib_dir="$local_gnulib_dir" ;;
2042           *)
2043             # destdir, local_gnulib_dir are both relative.
2044             func_relativize "$destdir" "$local_gnulib_dir"
2045             relative_local_gnulib_dir="$reldir" ;;
2046         esac ;;
2047     esac
2048     echo "gl_LOCAL_DIR([$relative_local_gnulib_dir])"
2049     echo "gl_MODULES(["`echo $specified_modules`"])"
2050     echo "gl_AVOID([$avoidlist])"
2051     echo "gl_SOURCE_BASE([$sourcebase])"
2052     echo "gl_M4_BASE([$m4base])"
2053     echo "gl_DOC_BASE([$docbase])"
2054     echo "gl_TESTS_BASE([$testsbase])"
2055     echo "gl_LIB([$libname])"
2056     test -z "$lgpl" || echo "gl_LGPL"
2057     echo "gl_MAKEFILE_NAME([$makefile_name])"
2058     if test "$libtool" = true; then
2059       echo "gl_LIBTOOL"
2060     fi
2061     echo "gl_MACRO_PREFIX([$macro_prefix])"
2062   ) > "$tmpfile"
2063   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
2064     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
2065       rm -f "$tmpfile"
2066     else
2067       if $doit; then
2068         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
2069         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
2070         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
2071       else
2072         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
2073         if false; then
2074           cat "$tmpfile"
2075           echo
2076           echo "# gnulib-cache.m4 ends here"
2077         fi
2078         rm -f "$tmpfile"
2079       fi
2080     fi
2081   else
2082     if $doit; then
2083       echo "Creating $m4base/gnulib-cache.m4"
2084       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
2085     else
2086       echo "Create $m4base/gnulib-cache.m4"
2087       cat "$tmpfile"
2088       rm -f "$tmpfile"
2089     fi
2090   fi
2091
2092   # Create m4/gnulib-comp.m4.
2093   func_dest_tmpfilename $m4base/gnulib-comp.m4
2094   (
2095     echo "# DO NOT EDIT! GENERATED AUTOMATICALLY!"
2096     func_emit_copyright_notice
2097     echo "#"
2098     echo "# This file represents the compiled summary of the specification in"
2099     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
2100     echo "# to be invoked from configure.ac."
2101     echo "# In projects using CVS, this file can be treated like other built files."
2102     echo
2103     echo
2104     echo "# This macro should be invoked from $configure_ac, in the section"
2105     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
2106     echo "# any checks for libraries, header files, types and library functions."
2107     echo "AC_DEFUN([${macro_prefix}_EARLY],"
2108     echo "["
2109     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
2110     echo "  m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
2111     echo "  m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
2112     echo "  m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
2113     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
2114     if test -n "$uses_subdirs"; then
2115       echo "  AC_REQUIRE([AM_PROG_CC_C_O])"
2116     fi
2117     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
2118       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
2119     fi
2120     for module in $modules; do
2121       func_verify_module
2122       if test -n "$module"; then
2123         func_get_autoconf_early_snippet "$module"
2124       fi
2125     done \
2126       | sed -e '/^$/d;' -e 's/^/  /'
2127     echo "])"
2128     echo
2129     echo "# This macro should be invoked from $configure_ac, in the section"
2130     echo "# \"Check for header files, types and library functions\"."
2131     echo "AC_DEFUN([${macro_prefix}_INIT],"
2132     echo "["
2133     func_emit_initmacro_start
2134     if test "$libtool" = true; then
2135       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2136       echo "  gl_cond_libtool=true"
2137     else
2138       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2139       echo "  gl_cond_libtool=false"
2140       echo "  gl_libdeps="
2141       echo "  gl_ltlibdeps="
2142     fi
2143     echo "  gl_source_base='$sourcebase'"
2144     if test "$auxdir" != "build-aux"; then
2145       sed_replace_build_aux='
2146         :a
2147         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2148           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2149           ba
2150         }'
2151       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2152     else
2153       sed_replace_build_aux=
2154     fi
2155     for module in $modules; do
2156       func_verify_module
2157       if test -n "$module"; then
2158         func_get_autoconf_snippet "$module" \
2159           | sed -e '/^$/d;' -e 's/^/  /' \
2160                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
2161                 -e "$sed_replace_build_aux"
2162         if test "$module" = 'alloca' && test "$libtool" = true; then
2163           echo 'changequote(,)dnl'
2164           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
2165           echo 'changequote([, ])dnl'
2166           echo 'AC_SUBST([LTALLOCA])'
2167         fi
2168       fi
2169     done
2170     # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2171     # created using libtool, because libtool already handles the dependencies.
2172     if test "$libtool" != true; then
2173       libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z' 'A-Z'`
2174       echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2175       echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2176       echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2177       echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2178     fi
2179     func_emit_initmacro_end
2180     echo "])"
2181     func_emit_initmacro_done
2182     echo
2183     echo "# This macro records the list of files which have been installed by"
2184     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
2185     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
2186     echo "$files" | sed -e 's,^,  ,'
2187     echo "])"
2188   ) > "$tmpfile"
2189   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
2190     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
2191       rm -f "$tmpfile"
2192     else
2193       if $doit; then
2194         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
2195         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
2196         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
2197       else
2198         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
2199         if false; then
2200           cat "$tmpfile"
2201           echo
2202           echo "# gnulib-comp.m4 ends here"
2203         fi
2204         rm -f "$tmpfile"
2205       fi
2206     fi
2207   else
2208     if $doit; then
2209       echo "Creating $m4base/gnulib-comp.m4"
2210       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
2211     else
2212       echo "Create $m4base/gnulib-comp.m4"
2213       cat "$tmpfile"
2214       rm -f "$tmpfile"
2215     fi
2216   fi
2217
2218   if test -n "$inctests"; then
2219     # Create tests makefile.
2220     func_dest_tmpfilename $testsbase/$makefile_am
2221     func_emit_tests_Makefile_am > "$tmpfile"
2222     if test -f "$destdir"/$testsbase/$makefile_am; then
2223       if cmp "$destdir"/$testsbase/$makefile_am "$tmpfile" > /dev/null; then
2224         rm -f "$tmpfile"
2225       else
2226         if $doit; then
2227           echo "Updating $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
2228           mv -f "$destdir"/$testsbase/$makefile_am "$destdir"/$testsbase/$makefile_am~
2229           mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
2230         else
2231           echo "Update $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
2232           rm -f "$tmpfile"
2233         fi
2234       fi
2235     else
2236       if $doit; then
2237         echo "Creating $testsbase/$makefile_am"
2238         mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
2239       else
2240         echo "Create $testsbase/$makefile_am"
2241         rm -f "$tmpfile"
2242       fi
2243     fi
2244   fi
2245
2246   echo "Finished."
2247   echo
2248   echo "You may need to add #include directives for the following .h files."
2249   # Intersect $specified_modules and $modules
2250   # (since $specified_modules is not necessarily of subset of $modules - some
2251   # may have been skipped through --avoid, and since the elements of $modules
2252   # but not in $specified_modules can go away without explicit notice - through
2253   # changes in the module dependencies).
2254   echo "$specified_modules" > "$tmp"/modules1 # a sorted list, one module per line
2255   echo "$modules" > "$tmp"/modules2 # also a sorted list, one module per line
2256   # First the #include <...> directives without #ifs, sorted for convenience,
2257   # then the #include "..." directives without #ifs, sorted for convenience,
2258   # then the #include directives that are surrounded by #ifs. Not sorted.
2259   for module in `LC_ALL=C join "$tmp"/modules1 "$tmp"/modules2`; do
2260     include_directive=`func_get_include_directive "$module"`
2261     case "$nl$include_directive" in
2262       *"$nl#if"*)
2263         echo "$include_directive" 1>&5
2264         ;;
2265       *)
2266         echo "$include_directive" | grep -v 'include "' 1>&6
2267         echo "$include_directive" | grep 'include "' 1>&7
2268         ;;
2269     esac
2270   done 5> "$tmp"/include-if 6> "$tmp"/include-angles 7> "$tmp"/include-quotes
2271   (
2272    LC_ALL=C sort -u "$tmp"/include-angles
2273    LC_ALL=C sort -u "$tmp"/include-quotes
2274    cat "$tmp"/include-if
2275   ) | sed -e '/^$/d' -e 's/^/  /'
2276   rm -f "$tmp"/include-angles "$tmp"/include-quotes "$tmp"/include-if
2277
2278   for module in $modules; do
2279     func_get_link_directive "$module"
2280   done \
2281     | LC_ALL=C sort -u | sed -e '/^$/d' -e 's/^/  /' > "$tmp"/link
2282   if test `wc -l < "$tmp"/link` != 0; then
2283     echo
2284     echo "You may need to use the following Makefile variables when linking."
2285     echo "Use them in <program>_LDADD when linking a program, or"
2286     echo "in <library>_a_LDFLAGS or <library>_la_LDFLAGS when linking a library."
2287     cat "$tmp"/link
2288   fi
2289   rm -f "$tmp"/link
2290
2291   echo
2292   echo "Don't forget to"
2293   if test "$makefile_am" = Makefile.am; then
2294     echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
2295   else
2296     echo "  - \"include $makefile_name\" from within \"$sourcebase/Makefile.am\","
2297   fi
2298   if test -n "$inctests"; then
2299     if test "$makefile_am" = Makefile.am; then
2300       echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
2301     else
2302       echo "  - \"include $makefile_name\" from within \"$testsbase/Makefile.am\","
2303     fi
2304   fi
2305   if test "$makefile_am" = Makefile.am; then
2306     sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
2307     sourcebase_base=`basename "$sourcebase"`
2308     echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
2309   fi
2310   if test -n "$inctests"; then
2311     if test "$makefile_am" = Makefile.am; then
2312       testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
2313       testsbase_base=`basename "$testsbase"`
2314       echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
2315     fi
2316   fi
2317   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
2318   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
2319   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
2320 }
2321
2322 # func_create_testdir testdir modules
2323 # Input:
2324 # - local_gnulib_dir  from --local-dir
2325 # - auxdir          directory relative to destdir where to place build aux files
2326 func_create_testdir ()
2327 {
2328   testdir="$1"
2329   modules="$2"
2330   if test -z "$modules"; then
2331     # All modules together.
2332     # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
2333     # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2334     # Except ftruncate, mountlist, which abort the configuration on mingw. FIXME.
2335     modules=`func_all_modules`
2336     modules=`for m in $modules; do case $m in config-h | fnmatch-posix | ftruncate | mountlist) ;; *) echo $m;; esac; done`
2337   fi
2338   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort -u`
2339
2340   # Check that the license of every module is consistent with the license of
2341   # its dependencies.
2342   saved_modules="$modules"
2343   saved_inctests="$inctests"
2344   # When computing transitive closures, don't consider $module to depend on
2345   # $module-tests. Need this becauses tests are implicitly GPL and may depend
2346   # on GPL modules - therefore we don't want a warning in this case.
2347   inctests=""
2348   for requested_module in $saved_modules; do
2349     requested_license=`func_get_license "$requested_module"`
2350     if test "$requested_license" != GPL; then
2351       # Here we use func_modules_transitive_closure, not just
2352       # func_get_dependencies, so that we also detect weird situations like
2353       # an LGPL module which depends on a GPLed build tool module which depends
2354       # on a GPL module.
2355       modules="$requested_module"
2356       func_modules_transitive_closure
2357       for module in $modules; do
2358         license=`func_get_license "$module"`
2359         case $license in
2360           LGPL | 'GPLed build tool') ;;
2361           'public domain' | 'unlimited' | 'unmodifiable license text') ;;
2362           *) echo "warning: module $requested_module depends on a module with an incompatible license: $module" 1>&2 ;;
2363         esac
2364       done
2365     fi
2366   done
2367   modules="$saved_modules"
2368   inctests="$saved_inctests"
2369
2370   # Subdirectory names.
2371   sourcebase=gllib
2372   m4base=glm4
2373   docbase=gldoc
2374   testsbase=gltests
2375   macro_prefix=gl
2376
2377   # Determine final module list.
2378   func_modules_transitive_closure
2379   echo "Module list with included dependencies:"
2380   echo "$modules" | sed -e 's/^/  /'
2381
2382   # Add the dummy module if needed.
2383   func_modules_add_dummy
2384
2385   # Determine final file list.
2386   func_modules_to_filelist
2387   echo "File list:"
2388   echo "$files" | sed -e 's/^/  /'
2389
2390   sed_rewrite_files="\
2391     s,^build-aux/,$auxdir/,
2392     s,^doc/,$docbase/,
2393     s,^lib/,$sourcebase/,
2394     s,^m4/,$m4base/,
2395     s,^tests/,$testsbase/,"
2396
2397   # Create directories.
2398   for f in $files; do echo $f; done \
2399     | sed -e "$sed_rewrite_files" \
2400     | sed -n -e 's,^\(.*\)/[^/]*,\1,p' \
2401     | LC_ALL=C sort -u \
2402     > "$tmp"/dirs
2403   { # Rearrange file descriptors. Needed because "while ... done < ..."
2404     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2405     exec 5<&0 < "$tmp"/dirs
2406     while read d; do
2407       mkdir -p "$testdir/$d"
2408     done
2409     exec 0<&5 5<&-
2410   }
2411
2412   # Copy files or make symbolic links.
2413   delimiter='   '
2414   for f in $files; do echo $f; done \
2415     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_files" \
2416     | LC_ALL=C sort \
2417     > "$tmp"/files
2418   { # Rearrange file descriptors. Needed because "while ... done < ..."
2419     # constructs are executed in a subshell e.g. by Solaris 10 /bin/sh.
2420     exec 5<&0 < "$tmp"/files
2421     while read g f; do
2422       func_lookup_file "$f"
2423       if test -n "$lookedup_tmp"; then
2424         cp -p "$lookedup_file" "$testdir/$g"
2425       else
2426         ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
2427         if { test -n "$symbolic" \
2428              || { test -n "$lsymbolic" \
2429                   && test "$lookedup_file" = "$local_gnulib_dir/$f"; }; }; then
2430           func_ln "$lookedup_file" "$testdir/$g"
2431         else
2432           cp -p "$lookedup_file" "$testdir/$g"
2433         fi
2434       fi
2435     done
2436     exec 0<&5 5<&-
2437   }
2438
2439   # Create Makefile.ams that are for testing.
2440   for_test=true
2441
2442   # Create $sourcebase/Makefile.am.
2443   mkdir -p "$testdir/$sourcebase"
2444   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
2445
2446   # Create $m4base/Makefile.am.
2447   mkdir -p "$testdir/$m4base"
2448   (echo "## Process this file with automake to produce Makefile.in."
2449    echo
2450    echo "EXTRA_DIST ="
2451    for f in $files; do
2452      case "$f" in
2453        m4/* )
2454          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
2455      esac
2456    done
2457   ) > "$testdir/$m4base/Makefile.am"
2458
2459   subdirs="$sourcebase $m4base"
2460   subdirs_with_configure_ac=""
2461
2462   if false && test -f "$testdir"/$m4base/gettext.m4; then
2463     # Avoid stupid error message from automake:
2464     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
2465     mkdir -p "$testdir/po"
2466     (echo "## Process this file with automake to produce Makefile.in."
2467     ) > "$testdir/po/Makefile.am"
2468     func_append subdirs " po"
2469   fi
2470
2471   if test -n "$inctests"; then
2472     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
2473     # Create $testsbase/Makefile.am.
2474     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
2475     # Create $testsbase/configure.ac.
2476     (echo "# Process this file with autoconf to produce a configure script."
2477      echo "AC_INIT([dummy], [0])"
2478      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
2479      echo "AM_INIT_AUTOMAKE"
2480      echo
2481      echo "AM_CONFIG_HEADER([config.h])"
2482      echo
2483      echo "AC_PROG_CC"
2484      echo "AC_PROG_INSTALL"
2485      echo "AC_PROG_MAKE_SET"
2486      echo "AC_PROG_RANLIB"
2487      echo
2488      if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2489        echo "AC_GNU_SOURCE"
2490        echo
2491      fi
2492      for module in $modules; do
2493        func_verify_module
2494        if test -n "$module"; then
2495          func_get_autoconf_early_snippet "$module"
2496        fi
2497      done \
2498        | sed -e '/^$/d;' -e 's/AC_REQUIRE(\[\([^()]*\)\])/\1/'
2499      if test "$libtool" = true; then
2500        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2501        echo "gl_cond_libtool=true"
2502      else
2503        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2504        echo "gl_cond_libtool=false"
2505        echo "gl_libdeps="
2506        echo "gl_ltlibdeps="
2507      fi
2508      # Wrap the set of autoconf snippets into an autoconf macro that is then
2509      # invoked. This is needed because autoconf does not support AC_REQUIRE
2510      # at the top level:
2511      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2512      # but we want the AC_REQUIRE to have its normal meaning (provide one
2513      # expansion of the required macro before the current point, and only one
2514      # expansion total).
2515      echo "AC_DEFUN([gl_INIT], ["
2516      func_emit_initmacro_start
2517      sed_replace_build_aux='
2518        :a
2519        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2520          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
2521          ba
2522        }'
2523      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2524      # We don't have explicit ordering constraints between the various
2525      # autoconf snippets. It's cleanest to put those of the library before
2526      # those of the tests.
2527      echo "gl_source_base='../$sourcebase'"
2528      for module in $modules; do
2529        func_verify_nontests_module
2530        if test -n "$module"; then
2531          func_get_autoconf_snippet "$module" \
2532            | sed -e "$sed_replace_build_aux"
2533        fi
2534      done
2535      echo "gl_source_base='.'"
2536      for module in $modules; do
2537        func_verify_tests_module
2538        if test -n "$module"; then
2539          func_get_autoconf_snippet "$module" \
2540            | sed -e "$sed_replace_build_aux"
2541        fi
2542      done
2543      # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2544      # created using libtool, because libtool already handles the dependencies.
2545      if test "$libtool" != true; then
2546        libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z' 'A-Z'`
2547        echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2548        echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2549        echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2550        echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2551      fi
2552      func_emit_initmacro_end
2553      echo "])"
2554      func_emit_initmacro_done
2555      echo
2556      echo "gl_INIT"
2557      echo
2558      # Usually $testsbase/config.h will be a superset of config.h. Verify this
2559      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
2560      echo "AH_TOP([#include \"../config.h\"])"
2561      echo
2562      echo "AC_OUTPUT([Makefile])"
2563     ) > "$testdir/$testsbase/configure.ac"
2564     func_append subdirs " $testsbase"
2565     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
2566   fi
2567
2568   # Create Makefile.am.
2569   (echo "## Process this file with automake to produce Makefile.in."
2570    echo
2571    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2572    echo
2573    echo "SUBDIRS = $subdirs"
2574    echo
2575    echo "ACLOCAL_AMFLAGS = -I $m4base"
2576   ) > "$testdir/Makefile.am"
2577
2578   # Create configure.ac.
2579   (echo "# Process this file with autoconf to produce a configure script."
2580    echo "AC_INIT([dummy], [0])"
2581    if test "$auxdir" != "."; then
2582      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2583    fi
2584    echo "AM_INIT_AUTOMAKE"
2585    echo
2586    echo "AM_CONFIG_HEADER([config.h])"
2587    echo
2588    echo "AC_PROG_CC"
2589    echo "AC_PROG_INSTALL"
2590    echo "AC_PROG_MAKE_SET"
2591    echo
2592    echo "# For autobuild."
2593    echo "AC_CANONICAL_BUILD"
2594    echo "AC_CANONICAL_HOST"
2595    echo
2596    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
2597    echo "m4_pattern_allow([^gl_ES\$])dnl a valid locale name"
2598    echo "m4_pattern_allow([^gl_LIBOBJS\$])dnl a variable"
2599    echo "m4_pattern_allow([^gl_LTLIBOBJS\$])dnl a variable"
2600    echo
2601    echo "AC_PROG_RANLIB"
2602    echo
2603    if test -n "$uses_subdirs"; then
2604      echo "AM_PROG_CC_C_O"
2605      echo
2606    fi
2607    if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2608      echo "AC_GNU_SOURCE"
2609      echo
2610    fi
2611    for module in $modules; do
2612      func_verify_nontests_module
2613      if test -n "$module"; then
2614        func_get_autoconf_early_snippet "$module"
2615      fi
2616    done \
2617      | sed -e '/^$/d;' -e 's/AC_REQUIRE(\[\([^()]*\)\])/\1/'
2618    if test "$libtool" = true; then
2619      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2620      echo "gl_cond_libtool=true"
2621    else
2622      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2623      echo "gl_cond_libtool=false"
2624      echo "gl_libdeps="
2625      echo "gl_ltlibdeps="
2626    fi
2627    echo "gl_source_base='$sourcebase'"
2628    # Wrap the set of autoconf snippets into an autoconf macro that is then
2629    # invoked. This is needed because autoconf does not support AC_REQUIRE
2630    # at the top level:
2631    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2632    # but we want the AC_REQUIRE to have its normal meaning (provide one
2633    # expansion of the required macro before the current point, and only one
2634    # expansion total).
2635    echo "AC_DEFUN([gl_INIT], ["
2636    func_emit_initmacro_start
2637    if test "$auxdir" != "build-aux"; then
2638      sed_replace_build_aux='
2639        :a
2640        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2641          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2642          ba
2643        }'
2644      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2645    else
2646      sed_replace_build_aux=
2647    fi
2648    for module in $modules; do
2649      func_verify_nontests_module
2650      if test -n "$module"; then
2651        func_get_autoconf_snippet "$module" \
2652          | sed -e "$sed_replace_build_aux"
2653      fi
2654    done
2655    # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2656    # created using libtool, because libtool already handles the dependencies.
2657    if test "$libtool" != true; then
2658      libname_upper=`echo "$libname" | LC_ALL=C tr 'a-z' 'A-Z'`
2659      echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2660      echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2661      echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2662      echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2663    fi
2664    func_emit_initmacro_end
2665    echo "])"
2666    func_emit_initmacro_done
2667    echo
2668    echo "gl_INIT"
2669    echo
2670    if test -n "$subdirs_with_configure_ac"; then
2671      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
2672    fi
2673    makefiles="Makefile"
2674    for d in $subdirs; do
2675      # For subdirs that have a configure.ac by their own, it's the subdir's
2676      # configure.ac which creates the subdir's Makefile.am, not this one.
2677      case " $subdirs_with_configure_ac " in
2678        *" $d "*) ;;
2679        *) func_append makefiles " $d/Makefile" ;;
2680      esac
2681    done
2682    echo "AC_OUTPUT([$makefiles])"
2683   ) > "$testdir/configure.ac"
2684
2685   # Create autogenerated files.
2686   (cd "$testdir"
2687    # Do not use "${AUTORECONF} --force --install", because it may invoke
2688    # autopoint, which brings in older versions of some of our .m4 files.
2689    if test -f $m4base/gettext.m4; then
2690      echo "executing ${AUTOPOINT} --force"
2691      ${AUTOPOINT} --force || func_exit 1
2692      for f in $m4base/*.m4~; do
2693        mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
2694      done
2695    fi
2696    echo "executing ${ACLOCAL} -I $m4base"
2697    ${ACLOCAL} -I $m4base || func_exit 1
2698    if ! test -d build-aux; then
2699      echo "executing mkdir build-aux"
2700      mkdir build-aux || func_exit 1
2701    fi
2702    echo "executing ${AUTOCONF}"
2703    ${AUTOCONF} || func_exit 1
2704    echo "executing ${AUTOHEADER}"
2705    ${AUTOHEADER} || func_exit 1
2706    echo "executing ${AUTOMAKE} --add-missing --copy"
2707    ${AUTOMAKE} --add-missing --copy || func_exit 1
2708   ) || func_exit 1
2709   if test -n "$inctests"; then
2710     # Create autogenerated files.
2711     (cd "$testdir/$testsbase" || func_exit 1
2712      # Do not use "${AUTORECONF} --force --install", because it may invoke
2713      # autopoint, which brings in older versions of some of our .m4 files.
2714      if test -f ../$m4base/gettext.m4; then
2715        echo "executing ${AUTOPOINT} --force"
2716        ${AUTOPOINT} --force || func_exit 1
2717        for f in ../$m4base/*.m4~; do
2718          mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
2719        done
2720      fi
2721      echo "executing ${ACLOCAL} -I ../$m4base"
2722      ${ACLOCAL} -I ../$m4base || func_exit 1
2723      if ! test -d ../build-aux; then
2724        echo "executing mkdir ../build-aux"
2725        mkdir ../build-aux
2726      fi
2727      echo "executing ${AUTOCONF}"
2728      ${AUTOCONF} || func_exit 1
2729      echo "executing ${AUTOHEADER}"
2730      ${AUTOHEADER} || func_exit 1
2731      echo "executing ${AUTOMAKE} --add-missing --copy"
2732      ${AUTOMAKE} --add-missing --copy || func_exit 1
2733     ) || func_exit 1
2734   fi
2735   # Need to run configure and make once, to create built files that are to be
2736   # distributed (such as getdate.c).
2737   # Extract the value of "CLEANFILES += ...".
2738   cleaned_files=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
2739                  | sed -n -e 's,^CLEANFILES[    ]*+=\([^#]*\).*$,\1,p'`
2740   cleaned_files=" "`for file in $cleaned_files; do echo "$file"; done`" "
2741   # Extract the value of "BUILT_SOURCES += ...". Remove variable references
2742   # such $(FOO_H) because they don't refer to distributed files.
2743   sed_remove_make_variables='s,[$]([A-Za-z0-9_]*),,g'
2744   built_sources=`sed -e "$sed_remove_backslash_newline" < "$testdir/$sourcebase/Makefile.am" \
2745                  | sed -n -e 's,^BUILT_SOURCES[         ]*+=\([^#]*\).*$,\1,p' \
2746                  | sed -e "$sed_remove_make_variables"`
2747   distributed_built_sources=`for file in $built_sources; do
2748                                case "$cleaned_files" in
2749                                  *" "$file" "*) ;;
2750                                  *) echo $file ;;
2751                                esac;
2752                              done`
2753   if test -n "$distributed_built_sources"; then
2754     (cd "$testdir"
2755      ./configure || func_exit 1
2756        cd "$sourcebase"
2757        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
2758        make built_sources || func_exit 1
2759        cd ..
2760      make distclean || func_exit 1
2761     ) || func_exit 1
2762   fi
2763 }
2764
2765 # func_create_megatestdir megatestdir allmodules
2766 # Input:
2767 # - local_gnulib_dir  from --local-dir
2768 # - auxdir          directory relative to destdir where to place build aux files
2769 func_create_megatestdir ()
2770 {
2771   megatestdir="$1"
2772   allmodules="$2"
2773   if test -z "$allmodules"; then
2774     allmodules=`func_all_modules`
2775   fi
2776
2777   megasubdirs=
2778   # First, all modules one by one.
2779   for onemodule in $allmodules; do
2780     func_create_testdir "$megatestdir/$onemodule" $onemodule
2781     func_append megasubdirs "$onemodule "
2782   done
2783   # Then, all modules all together.
2784   # Except config-h, which breaks all modules which use HAVE_CONFIG_H.
2785   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2786   allmodules=`for m in $allmodules; do if test $m != config-h && test $m != fnmatch-posix; then echo $m; fi; done`
2787   func_create_testdir "$megatestdir/ALL" "$allmodules"
2788   func_append megasubdirs "ALL"
2789
2790   # Create Makefile.am.
2791   (echo "## Process this file with automake to produce Makefile.in."
2792    echo
2793    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2794    echo
2795    echo "SUBDIRS = $megasubdirs"
2796   ) > "$megatestdir/Makefile.am"
2797
2798   # Create configure.ac.
2799   (echo "# Process this file with autoconf to produce a configure script."
2800    echo "AC_INIT([dummy], [0])"
2801    if test "$auxdir" != "."; then
2802      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2803    fi
2804    echo "AM_INIT_AUTOMAKE"
2805    echo
2806    echo "AC_PROG_MAKE_SET"
2807    echo
2808    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
2809    echo "AC_OUTPUT([Makefile])"
2810   ) > "$megatestdir/configure.ac"
2811
2812   # Create autogenerated files.
2813   (cd "$megatestdir"
2814    # Do not use "${AUTORECONF} --install", because autoreconf operates
2815    # recursively, but the subdirectories are already finished, therefore
2816    # calling autoreconf here would only waste lots of CPU time.
2817    echo "executing ${ACLOCAL}"
2818    ${ACLOCAL} || func_exit 1
2819    echo "executing mkdir build-aux"
2820    mkdir build-aux
2821    echo "executing ${AUTOCONF}"
2822    ${AUTOCONF} || func_exit 1
2823    echo "executing ${AUTOMAKE} --add-missing --copy"
2824    ${AUTOMAKE} --add-missing --copy || func_exit 1
2825   ) || func_exit 1
2826 }
2827
2828 case $mode in
2829   "" )
2830     func_fatal_error "no mode specified" ;;
2831
2832   list )
2833     func_all_modules
2834     ;;
2835
2836   import | update )
2837
2838     # Where to import.
2839     if test -z "$destdir"; then
2840       destdir=.
2841     fi
2842     test -d "$destdir" \
2843       || func_fatal_error "destination directory does not exist: $destdir"
2844
2845     # Prefer configure.ac to configure.in.
2846     if test -f "$destdir"/configure.ac; then
2847       configure_ac="$destdir/configure.ac"
2848     else
2849       if test -f "$destdir"/configure.in; then
2850         configure_ac="$destdir/configure.in"
2851       else
2852         func_fatal_error "cannot find $destdir/configure.ac"
2853       fi
2854     fi
2855
2856     # Analyze configure.ac.
2857     guessed_auxdir="."
2858     guessed_libtool=false
2859     my_sed_traces='
2860       s,#.*$,,
2861       s,^dnl .*$,,
2862       s, dnl .*$,,
2863       /AC_CONFIG_AUX_DIR/ {
2864         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
2865       }
2866       /A[CM]_PROG_LIBTOOL/ {
2867         s,^.*$,guessed_libtool=true,p
2868       }'
2869     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
2870
2871     if test -z "$auxdir"; then
2872       auxdir="$guessed_auxdir"
2873     fi
2874
2875     # Determine where to apply func_import.
2876     if test -n "$m4base"; then
2877       # Apply func_import to a particular gnulib directory.
2878       # Any number of additional modules can be given.
2879       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
2880         # First use of gnulib in the given m4base.
2881         test -n "$supplied_libname" || supplied_libname=true
2882         test -n "$sourcebase" || sourcebase="lib"
2883         test -n "$docbase" || docbase="doc"
2884         test -n "$testsbase" || testsbase="tests"
2885         test -n "$macro_prefix" || macro_prefix="gl"
2886       fi
2887       func_import "$*"
2888     else
2889       # Apply func_import to all gnulib directories.
2890       # To get this list of directories, look at Makefile.am. (Not at
2891       # configure, because it may be omitted from CVS. Also, don't run
2892       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
2893       m4dirs=
2894       m4dirs_count=0
2895       if test -f "$destdir"/Makefile.am; then
2896         aclocal_amflags=`sed -n 's/^ACLOCAL_AMFLAGS[     ]*=\(.*\)$/\1/p' "$destdir"/Makefile.am`
2897         m4dir_is_next=
2898         for arg in $aclocal_amflags; do
2899           if test -n "$m4dir_is_next"; then
2900             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
2901             case "$arg" in
2902               /*) ;;
2903               *)
2904                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
2905                   func_append m4dirs " $arg"
2906                   m4dirs_count=`expr $m4dirs_count + 1`
2907                 fi
2908                 ;;
2909             esac
2910             m4dir_is_next=
2911           else
2912             if test "X$arg" = "X-I"; then
2913               m4dir_is_next=yes
2914             else
2915               m4dir_is_next=
2916             fi
2917           fi
2918         done
2919       else
2920         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
2921         if test -f "$destdir"/aclocal.m4; then
2922           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
2923           sedexpr2='s,^[^/]*$,.,'
2924           sedexpr3='s,/[^/]*$,,'
2925           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort -u`
2926           m4dirs_count=`echo "$m4dirs" | wc -l`
2927         fi
2928       fi
2929       if test $m4dirs_count = 0; then
2930         # First use of gnulib in a package.
2931         # Any number of additional modules can be given.
2932         test -n "$supplied_libname" || supplied_libname=true
2933         test -n "$sourcebase" || sourcebase="lib"
2934         m4base="m4"
2935         test -n "$docbase" || docbase="doc"
2936         test -n "$testsbase" || testsbase="tests"
2937         test -n "$macro_prefix" || macro_prefix="gl"
2938         func_import "$*"
2939       else
2940         if test $m4dirs_count = 1; then
2941           # There's only one use of gnulib here. Assume the user means it.
2942           # Any number of additional modules can be given.
2943           for m4base in $m4dirs; do
2944             func_import "$*"
2945           done
2946         else
2947           # Ambiguous - guess what the user meant.
2948           if test $# = 0; then
2949             # No further arguments. Guess the user wants to update all of them.
2950             for m4base in $m4dirs; do
2951               # Perform func_import in a subshell, so that variable values
2952               # such as
2953               #   local_gnulib_dir, avoidlist, sourcebase, m4base, docbase,
2954               #   testsbase, libname, lgpl, makefile_name, libtool, macro_prefix
2955               # don't propagate from one directory to another.
2956               (func_import) || func_exit 1
2957             done
2958           else
2959             # Really ambiguous.
2960             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
2961           fi
2962         fi
2963       fi
2964     fi
2965     ;;
2966
2967   create-testdir )
2968     if test -z "$destdir"; then
2969       func_fatal_error "please specify --dir option"
2970     fi
2971     mkdir "$destdir"
2972     test -d "$destdir" \
2973       || func_fatal_error "could not create destination directory"
2974     test -n "$auxdir" || auxdir="build-aux"
2975     func_create_testdir "$destdir" "$*"
2976     ;;
2977
2978   create-megatestdir )
2979     if test -z "$destdir"; then
2980       func_fatal_error "please specify --dir option"
2981     fi
2982     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2983     test -n "$auxdir" || auxdir="build-aux"
2984     func_create_megatestdir "$destdir" "$*"
2985     ;;
2986
2987   test )
2988     test -n "$destdir" || destdir=testdir$$
2989     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2990     test -n "$auxdir" || auxdir="build-aux"
2991     func_create_testdir "$destdir" "$*"
2992     cd "$destdir"
2993       mkdir build
2994       cd build
2995         ../configure || func_exit 1
2996         make || func_exit 1
2997         make check || func_exit 1
2998         make distclean || func_exit 1
2999         remaining=`find . -type f -print`
3000         if test -n "$remaining"; then
3001           echo "Remaining files:" $remaining 1>&2
3002           echo "gnulib-tool: *** Stop." 1>&2
3003           func_exit 1
3004         fi
3005       cd ..
3006     cd ..
3007     rm -rf "$destdir"
3008     ;;
3009
3010   megatest )
3011     test -n "$destdir" || destdir=testdir$$
3012     mkdir "$destdir" || func_fatal_error "could not create destination directory"
3013     test -n "$auxdir" || auxdir="build-aux"
3014     func_create_megatestdir "$destdir" "$*"
3015     cd "$destdir"
3016       mkdir build
3017       cd build
3018         ../configure
3019         make
3020         make check
3021         make distclean
3022         remaining=`find . -type f -print`
3023         if test -n "$remaining"; then
3024           echo "Remaining files:" $remaining 1>&2
3025           echo "gnulib-tool: *** Stop." 1>&2
3026           func_exit 1
3027         fi
3028       cd ..
3029     cd ..
3030     rm -rf "$destdir"
3031     ;;
3032
3033   extract-description )
3034     for module
3035     do
3036       func_verify_module
3037       if test -n "$module"; then
3038         func_get_description "$module"
3039       fi
3040     done
3041     ;;
3042
3043   extract-filelist )
3044     for module
3045     do
3046       func_verify_module
3047       if test -n "$module"; then
3048         func_get_filelist "$module"
3049       fi
3050     done
3051     ;;
3052
3053   extract-dependencies )
3054     for module
3055     do
3056       func_verify_module
3057       if test -n "$module"; then
3058         func_get_dependencies "$module"
3059       fi
3060     done
3061     ;;
3062
3063   extract-autoconf-snippet )
3064     for module
3065     do
3066       func_verify_module
3067       if test -n "$module"; then
3068         func_get_autoconf_snippet "$module"
3069       fi
3070     done
3071     ;;
3072
3073   extract-automake-snippet )
3074     for module
3075     do
3076       func_verify_module
3077       if test -n "$module"; then
3078         func_get_automake_snippet "$module"
3079       fi
3080     done
3081     ;;
3082
3083   extract-include-directive )
3084     for module
3085     do
3086       func_verify_module
3087       if test -n "$module"; then
3088         func_get_include_directive "$module"
3089       fi
3090     done
3091     ;;
3092
3093   extract-link-directive )
3094     for module
3095     do
3096       func_verify_module
3097       if test -n "$module"; then
3098         func_get_link_directive "$module"
3099       fi
3100     done
3101     ;;
3102
3103   extract-license )
3104     for module
3105     do
3106       func_verify_module
3107       if test -n "$module"; then
3108         func_get_license "$module"
3109       fi
3110     done
3111     ;;
3112
3113   extract-maintainer )
3114     for module
3115     do
3116       func_verify_module
3117       if test -n "$module"; then
3118         func_get_maintainer "$module"
3119       fi
3120     done
3121     ;;
3122
3123   extract-tests-module )
3124     for module
3125     do
3126       func_verify_module
3127       if test -n "$module"; then
3128         func_get_tests_module "$module"
3129       fi
3130     done
3131     ;;
3132
3133   * )
3134     func_fatal_error "unknown operation mode --$mode" ;;
3135 esac
3136
3137 rm -rf "$tmp"
3138 # Undo the effect of the previous 'trap' command. Some shellology:
3139 # We cannot use "trap - 0 1 2 3 13 15", because Solaris sh would attempt to
3140 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
3141 # exit); for the others we need to call 'exit' explicitly. The value of $? is
3142 # 128 + signal number and is set before the trap-registered command is run.
3143 trap '' 0
3144 trap 'func_exit $?' 1 2 3 13 15
3145
3146 exit 0
3147
3148 # Local Variables:
3149 # indent-tabs-mode: nil
3150 # whitespace-check-buffer-indent: nil
3151 # End: