Describe a Solaris /bin/sh bug w.r.t. for loops.
[autoconf.git] / bin / autoreconf.in
blob0fc3bb8fefe748dc4cc65fc6aa85ef4d8b8dcd47
1 #! @PERL@ -w
2 # -*- perl -*-
3 # @configure_input@
5 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
6     if 0;
8 # autoreconf - install the GNU Build System in a directory tree
9 # Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
10 # 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
12 # This program is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 # Written by David J. MacKenzie.
26 # Extended and rewritten in Perl by Akim Demaille.
28 BEGIN
30   my $pkgdatadir = $ENV{'autom4te_perllibdir'} || '@pkgdatadir@';
31   unshift @INC, $pkgdatadir;
33   # Override SHELL.  On DJGPP SHELL may not be set to a shell
34   # that can handle redirection and quote arguments correctly,
35   # e.g.: COMMAND.COM.  For DJGPP always use the shell that configure
36   # has detected.
37   $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
40 use Autom4te::ChannelDefs;
41 use Autom4te::Channels;
42 use Autom4te::Configure_ac;
43 use Autom4te::FileUtils;
44 use Autom4te::General;
45 use Autom4te::XFile;
46 # Do not use Cwd::chdir, since it might hang.
47 use Cwd 'cwd';
48 use strict;
50 ## ----------- ##
51 ## Variables.  ##
52 ## ----------- ##
54 # $HELP
55 # -----
56 $help = "Usage: $0 [OPTION]... [DIRECTORY]...
58 Run `autoconf' (and `autoheader', `aclocal', `automake', `autopoint'
59 (formerly `gettextize'), and `libtoolize' where appropriate)
60 repeatedly to remake the GNU Build System files in specified
61 DIRECTORIES and their subdirectories (defaulting to `.').
63 By default, it only remakes those files that are older than their
64 sources.  If you install new versions of the GNU Build System,
65 you can make `autoreconf' remake all of the files by giving it the
66 `--force' option.
68 Operation modes:
69   -h, --help               print this help, then exit
70   -V, --version            print version number, then exit
71   -v, --verbose            verbosely report processing
72   -d, --debug              don't remove temporary files
73   -f, --force              consider all files obsolete
74   -i, --install            copy missing auxiliary files
75       --no-recursive       don't rebuild sub-packages
76   -s, --symlink            with -i, install symbolic links instead of copies
77   -m, --make               when applicable, re-run ./configure && make
78   -W, --warnings=CATEGORY  report the warnings falling in CATEGORY [syntax]
80 " . Autom4te::ChannelDefs::usage . "
82 The environment variable \`WARNINGS\' is honored.  Some subtools might
83 support other warning types, using \`all' is encouraged.
85 Library directories:
86   -B, --prepend-include=DIR  prepend directory DIR to search path
87   -I, --include=DIR          append directory DIR to search path
89 The environment variables AUTOCONF, AUTOHEADER, AUTOMAKE, ACLOCAL,
90 AUTOPOINT, LIBTOOLIZE, M4, and MAKE are honored.
92 Report bugs to <bug-autoconf\@gnu.org>.
93 GNU Autoconf home page: <http://www.gnu.org/software/autoconf/>.
94 General help using GNU software: <http://www.gnu.org/gethelp/>.
97 # $VERSION
98 # --------
99 $version = "autoreconf (@PACKAGE_NAME@) @VERSION@
100 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
101 License GPLv3+/Autoconf: GNU GPL version 3 or later
102 <http://gnu.org/licenses/gpl.html>, <http://gnu.org/licenses/exceptions.html>
103 This is free software: you are free to change and redistribute it.
104 There is NO WARRANTY, to the extent permitted by law.
106 Written by David J. MacKenzie and Akim Demaille.
109 # Lib files.
110 my $autoconf   = $ENV{'AUTOCONF'}   || '@bindir@/@autoconf-name@';
111 my $autoheader = $ENV{'AUTOHEADER'} || '@bindir@/@autoheader-name@';
112 my $autom4te   = $ENV{'AUTOM4TE'}   || '@bindir@/@autom4te-name@';
113 my $automake   = $ENV{'AUTOMAKE'}   || 'automake';
114 my $aclocal    = $ENV{'ACLOCAL'}    || 'aclocal';
115 my $libtoolize = $ENV{'LIBTOOLIZE'} || 'libtoolize';
116 my $autopoint  = $ENV{'AUTOPOINT'}  || 'autopoint';
117 my $make       = $ENV{'MAKE'}       || 'make';
119 # --install -- as --add-missing in other tools.
120 my $install = 0;
121 # symlink -- when --install, use symlinks instead.
122 my $symlink = 0;
123 # Does aclocal support --force?
124 my $aclocal_supports_force = 0;
125 # Does automake support --force-missing?
126 my $automake_supports_force_missing = 0;
128 my @prepend_include;
129 my @include;
131 # List of command line warning requests.
132 my @warning;
134 # Rerun `./configure && make'?
135 my $run_make = 0;
137 # Recurse into subpackages
138 my $recursive = 1;
140 ## ---------- ##
141 ## Routines.  ##
142 ## ---------- ##
145 # parse_args ()
146 # -------------
147 # Process any command line arguments.
148 sub parse_args ()
150   my $srcdir;
152   getopt ("W|warnings=s"         => \@warning,
153           'I|include=s'          => \@include,
154           'B|prepend-include=s'  => \@prepend_include,
155           'i|install'            => \$install,
156           's|symlink'            => \$symlink,
157           'm|make'               => \$run_make,
158           'recursive!'           => \$recursive);
160   # Split the warnings as a list of elements instead of a list of
161   # lists.
162   @warning = map { split /,/ } @warning;
163   parse_WARNINGS;
164   parse_warnings '--warnings', @warning;
166   # Even if the user specified a configure.ac, trim to get the
167   # directory, and look for configure.ac again.  Because (i) the code
168   # is simpler, and (ii) we are still able to diagnose simultaneous
169   # presence of configure.ac and configure.in.
170   @ARGV = map { /configure\.(ac|in)$/ ? dirname ($_) : $_ } @ARGV;
171   push @ARGV, '.' unless @ARGV;
173   if ($verbose && $debug)
174     {
175       for my $prog ($autoconf, $autoheader,
176                     $automake, $aclocal,
177                     $autopoint,
178                     $libtoolize)
179         {
180           xsystem ("$prog --version | sed 1q >&2");
181           print STDERR "\n";
182         }
183     }
185   $aclocal_supports_force = `$aclocal --help 2>/dev/null` =~ /--force/;
186   $automake_supports_force_missing = `$automake --help 2>/dev/null` =~ /--force-missing/;
188   # Dispatch autoreconf's option to the tools.
189   # --include;
190   $aclocal    .= join (' -I ', '', map { shell_quote ($_) } @include);
191   $autoconf   .= join (' --include=', '', map { shell_quote ($_) } @include);
192   $autoconf   .= join (' --prepend-include=', '', map { shell_quote ($_) } @prepend_include);
193   $autoheader .= join (' --include=', '', map { shell_quote ($_) } @include);
194   $autoheader .= join (' --prepend-include=', '', map { shell_quote ($_) } @prepend_include);
196   # --install and --symlink;
197   if ($install)
198     {
199       $automake   .= ' --add-missing';
200       $automake   .= ' --copy' unless $symlink;
201       $libtoolize .= ' --copy' unless $symlink;
202     }
203   # --force;
204   if ($force)
205     {
206       $aclocal    .= ' --force'
207         if $aclocal_supports_force;
208       $autoconf   .= ' --force';
209       $autoheader .= ' --force';
210       $automake   .= ' --force-missing'
211         if $automake_supports_force_missing;
212       $autopoint  .= ' --force';
213       $libtoolize .= ' --force';
214     }
215   else
216     {
217       # The implementation of --no-force is bogus in all implementations
218       # of Automake up to 1.8, so we avoid it in these cases.  (Automake
219       # 1.8 is the first version where aclocal supports force, hence
220       # the condition.)
221       $automake .= ' --no-force'
222         if $aclocal_supports_force;
223     }
224   # --verbose --verbose or --debug;
225   if ($verbose > 1 || $debug)
226     {
227       $autoconf   .= ' --verbose';
228       $autoheader .= ' --verbose';
229       $automake   .= ' --verbose';
230       $aclocal    .= ' --verbose';
231     }
232   if ($debug)
233     {
234       $autoconf   .= ' --debug';
235       $autoheader .= ' --debug';
236       $libtoolize .= ' --debug';
237     }
238   # --warnings;
239   if (@warning)
240     {
241       my $warn = ' --warnings=' . join (',', @warning);
242       $autoconf   .= $warn;
243       $autoheader .= $warn;
244       $automake   .= $warn
245         if `$automake --help` =~ /--warnings/;
246     }
250 # &run_aclocal ($ACLOCAL, $FLAGS)
251 # -------------------------------
252 # Update aclocal.m4 as lazily as possible, as aclocal pre-1.8 always
253 # overwrites aclocal.m4, hence triggers autoconf, autoheader, automake
254 # etc. uselessly.  aclocal 1.8+ does not need this.
255 sub run_aclocal ($$)
257   my ($aclocal, $flags) = @_;
259   # aclocal 1.8+ does all this for free.  It can be recognized by its
260   # --force support.
261   if ($aclocal_supports_force)
262     {
263       xsystem ("$aclocal $flags");
264     }
265   else
266     {
267       xsystem ("$aclocal $flags --output=aclocal.m4t");
268       # aclocal may produce no output.
269       if (-f 'aclocal.m4t')
270         {
271           update_file ('aclocal.m4t', 'aclocal.m4');
272           # Make sure that the local m4 files are older than
273           # aclocal.m4.
274           #
275           # Why is not always the case?  Because we already run
276           # aclocal at first (before tracing), which, for instance,
277           # can find Gettext's macros in .../share/aclocal, so we may
278           # have had the right aclocal.m4 already.  Then autopoint is
279           # run, and installs locally these M4 files.  Then
280           # autoreconf, via update_file, sees it is the _same_
281           # aclocal.m4, and doesn't change its timestamp.  But later,
282           # Automake's Makefile expresses that aclocal.m4 depends on
283           # these local files, which are newer, so it triggers aclocal
284           # again.
285           #
286           # To make sure aclocal.m4 is no older, we change the
287           # modification times of the local M4 files to be not newer
288           # than it.
289           #
290           # First, where are the local files?
291           my $aclocal_local_dir = '.';
292           if ($flags =~ /-I\s+(\S+)/)
293             {
294               $aclocal_local_dir = $1;
295             }
296           # All the local files newer than aclocal.m4 are to be
297           # made not newer than it.
298           my $aclocal_m4_mtime = mtime ('aclocal.m4');
299           for my $file (glob ("$aclocal_local_dir/*.m4"), 'acinclude.m4')
300             {
301               if ($aclocal_m4_mtime < mtime ($file))
302                 {
303                   debug "aging $file to be not newer than aclocal.m4";
304                   utime $aclocal_m4_mtime, $aclocal_m4_mtime, $file;
305                 }
306             }
307         }
308     }
311 # &autoreconf_current_directory
312 # -----------------------------
313 sub autoreconf_current_directory ()
315   my $configure_ac = find_configure_ac;
317   # ---------------------- #
318   # Is it using Autoconf?  #
319   # ---------------------- #
321   my $uses_autoconf;
322   my $uses_gettext;
323   if (-f $configure_ac)
324     {
325       my $configure_ac_file = new Autom4te::XFile "< $configure_ac";
326       while ($_ = $configure_ac_file->getline)
327         {
328           s/#.*//;
329           s/dnl.*//;
330           $uses_autoconf = 1 if /AC_INIT/;
331           # See below for why we look for gettext here.
332           $uses_gettext = 1  if /^AM_GNU_GETTEXT_VERSION/;
333         }
334     }
335   if (!$uses_autoconf)
336     {
337       verb "$configure_ac: not using Autoconf";
338       return;
339     }
342   # ------------------- #
343   # Running autopoint.  #
344   # ------------------- #
346   # Gettext is a bit of a problem: its macros are not necessarily
347   # visible to aclocal, so if we start with a completely striped down
348   # package (think of a fresh CVS checkout), running `aclocal' first
349   # will fail: the Gettext macros are missing.
350   #
351   # Therefore, we can't use the traces to decide if we use Gettext or
352   # not.  I guess that once Gettext move to 2.5x we will be able to,
353   # but in the meanwhile forget it.
354   #
355   # We can only grep for AM_GNU_GETTEXT_VERSION in configure.ac.  You
356   # might think this approach is naive, and indeed it is, as it
357   # prevents one to embed AM_GNU_GETTEXT_VERSION in another *.m4, but
358   # anyway we don't limit the generality, since... that's what
359   # autopoint does.  Actually, it is even more restrictive, as it
360   # greps for `^AM_GNU_GETTEXT_VERSION('.  We did this above, while
361   # scanning configure.ac.
362   if (!$uses_gettext)
363     {
364       verb "$configure_ac: not using Gettext";
365     }
366   elsif (!$install)
367     {
368       verb "$configure_ac: not running autopoint: --install not given";
369     }
370   else
371     {
372       xsystem_hint ("autopoint is needed because this package uses Gettext", "$autopoint");
373     }
376   # ----------------- #
377   # Running aclocal.  #
378   # ----------------- #
380   # Run it first: it might discover new macros to add, e.g.,
381   # AC_PROG_LIBTOOL, which we will trace later to see if Libtool is
382   # used.
383   #
384   # Always run it.  Tracking its sources for up-to-dateness is too
385   # complex and too error prone.  The best we can do is avoiding
386   # nuking the time stamp.
387   my $uses_aclocal = 1;
389   # Nevertheless, if aclocal.m4 exists and is not made by aclocal,
390   # don't run aclocal.
392   if (-f 'aclocal.m4')
393     {
394       my $aclocal_m4 = new Autom4te::XFile 'aclocal.m4';
395       $_ = $aclocal_m4->getline;
396       $uses_aclocal = 0
397         unless defined ($_) && /generated.*by aclocal/;
398     }
400   # If there are flags for aclocal in Makefile.am, use them.
401   my $aclocal_flags = '';
402   if ($uses_aclocal && -f 'Makefile.am')
403     {
404       my $makefile = new Autom4te::XFile 'Makefile.am';
405       while ($_ = $makefile->getline)
406         {
407           if (/^ACLOCAL_[A-Z_]*FLAGS\s*=\s*(.*)/)
408             {
409               $aclocal_flags = $1;
410               last;
411             }
412         }
413     }
415   if (!$uses_aclocal)
416     {
417       verb "$configure_ac: not using aclocal";
418     }
419   else
420     {
421       # Some file systems have sub-second time stamps, and if so we may
422       # run into trouble later, after we rerun autoconf and set the
423       # time stamps of input files to be no greater than aclocal.m4,
424       # because the time-stamp-setting operation (utime) has a
425       # resolution of only 1 second.  Work around the problem by
426       # ensuring that there is at least a one-second window before the
427       # time stamp of aclocal.m4t in which no file time stamps can
428       # fall.
429       sleep 1;
431       run_aclocal ($aclocal, $aclocal_flags);
432     }
434   # We might have to rerun aclocal if Libtool (or others) imports new
435   # macros.
436   my $rerun_aclocal = 0;
440   # ------------------------------- #
441   # See what tools will be needed.  #
442   # ------------------------------- #
444   # Perform a single trace reading to avoid --force forcing a rerun
445   # between two --trace, that's useless.  If there is no AC_INIT, then
446   # we are not interested: it looks like a Cygnus thingy.
447   my $aux_dir;
448   my $uses_gettext_via_traces;
449   my $uses_libtool;
450   my $uses_libltdl;
451   my $uses_autoheader;
452   my $uses_automake;
453   my @subdir;
454   verb "$configure_ac: tracing";
455   my $traces = new Autom4te::XFile
456     ("$autoconf"
457      . join (' ',
458              map { ' --trace=' . $_ . ':\$n::\${::}%' }
459              # If you change this list, update the
460              # `Autoreconf-preselections' section of autom4te.in.
461              'AC_CONFIG_AUX_DIR',
462              'AC_CONFIG_HEADERS',
463              'AC_CONFIG_SUBDIRS',
464              'AC_INIT',
465              'AC_PROG_LIBTOOL',
466              'LT_INIT',
467              'LT_CONFIG_LTDL_DIR',
468              'AM_GNU_GETTEXT',
469              'AM_INIT_AUTOMAKE',
470             )
471      . ' |');
472   while ($_ = $traces->getline)
473     {
474       chomp;
475       my ($macro, @args) = split (/::/);
476       $aux_dir = $args[0]           if $macro eq "AC_CONFIG_AUX_DIR";
477       $uses_autoconf = 1            if $macro eq "AC_INIT";
478       $uses_gettext_via_traces = 1  if $macro eq "AM_GNU_GETTEXT";
479       $uses_libtool = 1             if $macro eq "AC_PROG_LIBTOOL"
480                                        || $macro eq "LT_INIT";
481       $uses_libltdl = 1             if $macro eq "LT_CONFIG_LTDL_DIR";
482       $uses_autoheader = 1          if $macro eq "AC_CONFIG_HEADERS";
483       $uses_automake = 1            if $macro eq "AM_INIT_AUTOMAKE";
484       push @subdir, split (' ', $args[0])
485                                     if $macro eq "AC_CONFIG_SUBDIRS" && $recursive;
486     }
488   # The subdirs are *optional*, they may not exist.
489   foreach (@subdir)
490     {
491       if (-d)
492         {
493           verb "$configure_ac: adding subdirectory $_ to autoreconf";
494           autoreconf ($_);
495         }
496       else
497         {
498           verb "$configure_ac: subdirectory $_ not present";
499         }
500     }
502   # Gettext consistency checks...
503   error "$configure_ac: AM_GNU_GETTEXT is used, but not AM_GNU_GETTEXT_VERSION"
504     if $uses_gettext_via_traces && ! $uses_gettext;
505   error "$configure_ac: AM_GNU_GETTEXT_VERSION is used, but not AM_GNU_GETTEXT"
506     if $uses_gettext && ! $uses_gettext_via_traces;
509   # ---------------------------- #
510   # Setting up the source tree.  #
511   # ---------------------------- #
513   # libtoolize, automake --add-missing etc. will drop files in the
514   # $AUX_DIR.  But these tools fail to install these files if the
515   # directory itself does not exist, which valid: just imagine a CVS
516   # repository with hand written code only (there is not even a need
517   # for a Makefile.am!).
519   if (defined $aux_dir && ! -d $aux_dir)
520     {
521       verb "$configure_ac: creating directory $aux_dir";
522       mkdir $aux_dir, 0755
523         or error "cannot create $aux_dir: $!";
524     }
527   # -------------------- #
528   # Running libtoolize.  #
529   # -------------------- #
531   if (!$uses_libtool)
532     {
533       verb "$configure_ac: not using Libtool";
534     }
535   elsif ($install)
536     {
537       if ($uses_libltdl)
538         {
539           $libtoolize .= " --ltdl";
540         }
541       xsystem_hint ("libtoolize is needed because this package uses Libtool", $libtoolize);
542       $rerun_aclocal = 1;
543     }
544   else
545     {
546       verb "$configure_ac: not running libtoolize: --install not given";
547     }
551   # ------------------- #
552   # Rerunning aclocal.  #
553   # ------------------- #
555   # If we re-installed Libtool or Gettext, the macros might have changed.
556   # Automake also needs an up-to-date aclocal.m4.
557   if ($rerun_aclocal)
558     {
559       if (!$uses_aclocal)
560         {
561           verb "$configure_ac: not using aclocal";
562         }
563       else
564         {
565           run_aclocal ($aclocal, $aclocal_flags);
566         }
567     }
570   # ------------------ #
571   # Running autoconf.  #
572   # ------------------ #
574   # Don't try to be smarter than `autoconf', which does its own up to
575   # date checks.
576   #
577   # We prefer running autoconf before autoheader, because (i) the
578   # latter runs the former, and (ii) autoconf is stricter than
579   # autoheader.  So all in all, autoconf should give better error
580   # messages.
581   xsystem ($autoconf);
584   # -------------------- #
585   # Running autoheader.  #
586   # -------------------- #
588   # We now consider that if AC_CONFIG_HEADERS is used, then autoheader
589   # is used too.
590   #
591   # Just as for autoconf, up to date ness is performed by the tool
592   # itself.
593   #
594   # Run it before automake, since the latter checks the presence of
595   # config.h.in when it sees an AC_CONFIG_HEADERS.
596   if (!$uses_autoheader)
597     {
598       verb "$configure_ac: not using Autoheader";
599     }
600   else
601     {
602       xsystem ($autoheader);
603     }
606   # ------------------ #
607   # Running automake.  #
608   # ------------------ #
610   if (!$uses_automake)
611     {
612       verb "$configure_ac: not using Automake";
613     }
614   else
615     {
616       # We should always run automake, and let it decide whether it shall
617       # update the file or not.  In fact, the effect of `$force' is already
618       # included in `$automake' via `--no-force'.
619       xsystem ($automake);
620     }
623   # -------------- #
624   # Running make.  #
625   # -------------- #
627   if ($run_make)
628     {
629       if (!-f "config.status")
630         {
631           verb "no config.status: cannot re-make";
632         }
633       else
634         {
635           xsystem ("./config.status --recheck");
636           xsystem ("./config.status");
637           if (!-f "Makefile")
638             {
639               verb "no Makefile: cannot re-make";
640             }
641           else
642             {
643               xsystem ("$make");
644             }
645         }
646     }
650 # &autoreconf ($DIRECTORY)
651 # ------------------------
652 # Reconf the $DIRECTORY.
653 sub autoreconf ($)
655   my ($directory) = @_;
656   my $cwd = cwd;
658   # The format for this message is not free: taken from Emacs, itself
659   # using GNU Make's format.
660   verb "Entering directory `$directory'";
661   chdir $directory
662     or error "cannot chdir to $directory: $!";
664   autoreconf_current_directory;
666   # The format is not free: taken from Emacs, itself using GNU Make's
667   # format.
668   verb "Leaving directory `$directory'";
669   chdir $cwd
670     or error "cannot chdir to $cwd: $!";
674 ## ------ ##
675 ## Main.  ##
676 ## ------ ##
678 # When debugging, it is convenient that all the related temporary
679 # files be at the same place.
680 mktmpdir ('ar');
681 $ENV{'TMPDIR'} = $tmp;
682 parse_args;
684 # Autoreconf all the given configure.ac.  Unless `--no-recursive' is passed,
685 # AC_CONFIG_SUBDIRS will be traversed in &autoreconf_current_directory.
686 $ENV{'AUTOM4TE'} = $autom4te;
687 for my $directory (@ARGV)
688   {
689     require_configure_ac ($directory);
690     autoreconf ($directory);
691   }
693 ### Setup "GNU" style for perl-mode and cperl-mode.
694 ## Local Variables:
695 ## perl-indent-level: 2
696 ## perl-continued-statement-offset: 2
697 ## perl-continued-brace-offset: 0
698 ## perl-brace-offset: 0
699 ## perl-brace-imaginary-offset: 0
700 ## perl-label-offset: -2
701 ## cperl-indent-level: 2
702 ## cperl-brace-offset: 0
703 ## cperl-continued-brace-offset: 0
704 ## cperl-label-offset: -2
705 ## cperl-extra-newline-before-brace: t
706 ## cperl-merge-trailing-else: nil
707 ## cperl-continued-statement-offset: 2
708 ## End: