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