Remove three forks per _AC_RUN_LOG_STDERR in the common case.
[autoconf.git] / bin / autoreconf.in
blob923f8caf7a456150da60a070fd71cb32d76b287d
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  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 2, or (at your option)
15 # 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, write to the Free Software
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 # 02110-1301, USA.
27 # Written by David J. MacKenzie.
28 # Extended and rewritten in Perl by Akim Demaille.
30 BEGIN
32   my $datadir = $ENV{'autom4te_perllibdir'} || '@datadir@';
33   unshift @INC, $datadir;
35   # Override SHELL.  On DJGPP SHELL may not be set to a shell
36   # that can handle redirection and quote arguments correctly,
37   # e.g.: COMMAND.COM.  For DJGPP always use the shell that configure
38   # has detected.
39   $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
42 use Autom4te::ChannelDefs;
43 use Autom4te::Channels;
44 use Autom4te::Configure_ac;
45 use Autom4te::FileUtils;
46 use Autom4te::General;
47 use Autom4te::XFile;
48 # Do not use Cwd::chdir, since it might hang.
49 use Cwd 'cwd';
50 use strict;
52 ## ----------- ##
53 ## Variables.  ##
54 ## ----------- ##
56 # $HELP
57 # -----
58 $help = "Usage: $0 [OPTION]... [DIRECTORY]...
60 Run `autoconf' (and `autoheader', `aclocal', `automake', `autopoint'
61 (formerly `gettextize'), and `libtoolize' where appropriate)
62 repeatedly to remake the GNU Build System files in specified
63 DIRECTORIES and their subdirectories (defaulting to `.').
65 By default, it only remakes those files that are older than their
66 sources.  If you install new versions of the GNU Build System,
67 you can make `autoreconf' remake all of the files by giving it the
68 `--force' option.
70 Operation modes:
71   -h, --help               print this help, then exit
72   -V, --version            print version number, then exit
73   -v, --verbose            verbosely report processing
74   -d, --debug              don't remove temporary files
75   -f, --force              consider all files obsolete
76   -i, --install            copy missing auxiliary files
77       --no-recursive       don't rebuild sub-packages
78   -s, --symlink            with -i, install symbolic links instead of copies
79   -m, --make               when applicable, re-run ./configure && make
80   -W, --warnings=CATEGORY  report the warnings falling in CATEGORY [syntax]
82 " . Autom4te::ChannelDefs::usage . "
84 The environment variable \`WARNINGS\' is honored.  Some subtools might
85 support other warning types, using \`all' is encouraged.
87 Library directories:
88   -B, --prepend-include=DIR  prepend directory DIR to search path
89   -I, --include=DIR          append directory DIR to search path
91 The environment variables AUTOCONF, AUTOHEADER, AUTOMAKE, ACLOCAL,
92 AUTOPOINT, LIBTOOLIZE, M4, and MAKE are honored.
94 Report bugs to <bug-autoconf\@gnu.org>.
97 # $VERSION
98 # --------
99 $version = "autoreconf (@PACKAGE_NAME@) @VERSION@
100 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
101 License GPLv2+: GNU GPL version 2 or later
102 <http://gnu.org/licenses/old-licenses/gpl-2.0.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 $automake   = $ENV{'AUTOMAKE'}   || 'automake';
113 my $aclocal    = $ENV{'ACLOCAL'}    || 'aclocal';
114 my $libtoolize = $ENV{'LIBTOOLIZE'} || 'libtoolize';
115 my $autopoint  = $ENV{'AUTOPOINT'}  || 'autopoint';
116 my $make       = $ENV{'MAKE'}       || 'make';
118 # --install -- as --add-missing in other tools.
119 my $install = 0;
120 # symlink -- when --install, use symlinks instead.
121 my $symlink = 0;
122 # Does aclocal support --force?
123 my $aclocal_supports_force = 0;
124 # Does automake support --force-missing?
125 my $automake_supports_force_missing = 0;
127 my @prepend_include;
128 my @include;
130 # List of command line warning requests.
131 my @warning;
133 # Rerun `./configure && make'?
134 my $run_make = 0;
136 # Recurse into subpackages
137 my $recursive = 1;
139 ## ---------- ##
140 ## Routines.  ##
141 ## ---------- ##
144 # parse_args ()
145 # -------------
146 # Process any command line arguments.
147 sub parse_args ()
149   my $srcdir;
151   getopt ("W|warnings=s"         => \@warning,
152           'I|include=s'          => \@include,
153           'B|prepend-include=s'  => \@prepend_include,
154           'i|install'            => \$install,
155           's|symlink'            => \$symlink,
156           'm|make'               => \$run_make,
157           'recursive!'           => \$recursive);
159   # Split the warnings as a list of elements instead of a list of
160   # lists.
161   @warning = map { split /,/ } @warning;
162   parse_WARNINGS;
163   parse_warnings '--warnings', @warning;
165   # Even if the user specified a configure.ac, trim to get the
166   # directory, and look for configure.ac again.  Because (i) the code
167   # is simpler, and (ii) we are still able to diagnose simultaneous
168   # presence of configure.ac and configure.in.
169   @ARGV = map { /configure\.(ac|in)$/ ? dirname ($_) : $_ } @ARGV;
170   push @ARGV, '.' unless @ARGV;
172   if ($verbose && $debug)
173     {
174       for my $prog ($autoconf, $autoheader,
175                     $automake, $aclocal,
176                     $autopoint,
177                     $libtoolize)
178         {
179           xsystem ("$prog --version | sed 1q >&2");
180           print STDERR "\n";
181         }
182     }
184   $aclocal_supports_force = `$aclocal --help 2>/dev/null` =~ /--force/;
185   $automake_supports_force_missing = `$automake --help 2>/dev/null` =~ /--force-missing/;
187   # Dispatch autoreconf's option to the tools.
188   # --include;
189   $autoconf   .= join (' --include=', '', map { shell_quote ($_) } @include);
190   $autoconf   .= join (' --prepend-include=', '', map { shell_quote ($_) } @prepend_include);
191   $autoheader .= join (' --include=', '', map { shell_quote ($_) } @include);
192   $autoheader .= join (' --prepend-include=', '', map { shell_quote ($_) } @prepend_include);
194   # --install and --symlink;
195   if ($install)
196     {
197       $automake   .= ' --add-missing';
198       $automake   .= ' --copy' unless $symlink;
199       $libtoolize .= ' --copy' unless $symlink;
200     }
201   # --force;
202   if ($force)
203     {
204       $aclocal    .= ' --force'
205         if $aclocal_supports_force;
206       $autoconf   .= ' --force';
207       $autoheader .= ' --force';
208       $automake   .= ' --force-missing'
209         if $automake_supports_force_missing;
210       $autopoint  .= ' --force';
211       $libtoolize .= ' --force';
212     }
213   else
214     {
215       # The implementation of --no-force is bogus in all implementations
216       # of Automake up to 1.8, so we avoid it in these cases.  (Automake
217       # 1.8 is the first version where aclocal supports force, hence
218       # the condition.)
219       $automake .= ' --no-force'
220         if $aclocal_supports_force;
221     }
222   # --verbose --verbose or --debug;
223   if ($verbose > 1 || $debug)
224     {
225       $autoconf   .= ' --verbose';
226       $autoheader .= ' --verbose';
227       $automake   .= ' --verbose';
228       $aclocal    .= ' --verbose';
229     }
230   if ($debug)
231     {
232       $autoconf   .= ' --debug';
233       $autoheader .= ' --debug';
234       $libtoolize .= ' --debug';
235     }
236   # --warnings;
237   if (@warning)
238     {
239       my $warn = ' --warnings=' . join (',', @warning);
240       $autoconf   .= $warn;
241       $autoheader .= $warn;
242       $automake   .= $warn
243         if `$automake --help` =~ /--warnings/;
244     }
248 # &run_aclocal ($ACLOCAL, $FLAGS)
249 # -------------------------------
250 # Update aclocal.m4 as lazily as possible, as aclocal pre-1.8 always
251 # overwrites aclocal.m4, hence triggers autoconf, autoheader, automake
252 # etc. uselessly.  aclocal 1.8+ does not need this.
253 sub run_aclocal ($$)
255   my ($aclocal, $flags) = @_;
257   # aclocal 1.8+ does all this for free.  It can be recognized by its
258   # --force support.
259   if ($aclocal_supports_force)
260     {
261       xsystem ("$aclocal $flags");
262     }
263   else
264     {
265       xsystem ("$aclocal $flags --output=aclocal.m4t");
266       # aclocal may produce no output.
267       if (-f 'aclocal.m4t')
268         {
269           update_file ('aclocal.m4t', 'aclocal.m4');
270           # Make sure that the local m4 files are older than
271           # aclocal.m4.
272           #
273           # Why is not always the case?  Because we already run
274           # aclocal at first (before tracing), which, for instance,
275           # can find Gettext's macros in .../share/aclocal, so we may
276           # have had the right aclocal.m4 already.  Then autopoint is
277           # run, and installs locally these M4 files.  Then
278           # autoreconf, via update_file, sees it is the _same_
279           # aclocal.m4, and doesn't change its timestamp.  But later,
280           # Automake's Makefile expresses that aclocal.m4 depends on
281           # these local files, which are newer, so it triggers aclocal
282           # again.
283           #
284           # To make sure aclocal.m4 is no older, we change the
285           # modification times of the local M4 files to be not newer
286           # than it.
287           #
288           # First, where are the local files?
289           my $aclocal_local_dir = '.';
290           if ($flags =~ /-I\s+(\S+)/)
291             {
292               $aclocal_local_dir = $1;
293             }
294           # All the local files newer than aclocal.m4 are to be
295           # made not newer than it.
296           my $aclocal_m4_mtime = mtime ('aclocal.m4');
297           for my $file (glob ("$aclocal_local_dir/*.m4"), 'acinclude.m4')
298             {
299               if ($aclocal_m4_mtime < mtime ($file))
300                 {
301                   debug "aging $file to be not newer than aclocal.m4";
302                   utime $aclocal_m4_mtime, $aclocal_m4_mtime, $file;
303                 }
304             }
305         }
306     }
309 # &autoreconf_current_directory
310 # -----------------------------
311 sub autoreconf_current_directory ()
313   my $configure_ac = find_configure_ac;
315   # ---------------------- #
316   # Is it using Autoconf?  #
317   # ---------------------- #
319   my $uses_autoconf;
320   my $uses_gettext;
321   if (-f $configure_ac)
322     {
323       my $configure_ac_file = new Autom4te::XFile "< $configure_ac";
324       while ($_ = $configure_ac_file->getline)
325         {
326           s/#.*//;
327           s/dnl.*//;
328           $uses_autoconf = 1 if /AC_INIT/;
329           # See below for why we look for gettext here.
330           $uses_gettext = 1  if /^AM_GNU_GETTEXT_VERSION/;
331         }
332     }
333   if (!$uses_autoconf)
334     {
335       verb "$configure_ac: not using Autoconf";
336       return;
337     }
340   # ------------------- #
341   # Running autopoint.  #
342   # ------------------- #
344   # Gettext is a bit of a problem: its macros are not necessarily
345   # visible to aclocal, so if we start with a completely striped down
346   # package (think of a fresh CVS checkout), running `aclocal' first
347   # will fail: the Gettext macros are missing.
348   #
349   # Therefore, we can't use the traces to decide if we use Gettext or
350   # not.  I guess that once Gettext move to 2.5x we will be able to,
351   # but in the meanwhile forget it.
352   #
353   # We can only grep for AM_GNU_GETTEXT_VERSION in configure.ac.  You
354   # might think this approach is naive, and indeed it is, as it
355   # prevents one to embed AM_GNU_GETTEXT_VERSION in another *.m4, but
356   # anyway we don't limit the generality, since... that's what
357   # autopoint does.  Actually, it is even more restrictive, as it
358   # greps for `^AM_GNU_GETTEXT_VERSION('.  We did this above, while
359   # scanning configure.ac.
360   if (!$uses_gettext)
361     {
362       verb "$configure_ac: not using Gettext";
363     }
364   elsif (!$install)
365     {
366       verb "$configure_ac: not running autopoint: --install not given";
367     }
368   else
369     {
370       xsystem_hint ("autopoint is needed because this package uses Gettext", "$autopoint");
371     }
374   # ----------------- #
375   # Running aclocal.  #
376   # ----------------- #
378   # Run it first: it might discover new macros to add, e.g.,
379   # AC_PROG_LIBTOOL, which we will trace later to see if Libtool is
380   # used.
381   #
382   # Always run it.  Tracking its sources for up-to-dateness is too
383   # complex and too error prone.  The best we can do is avoiding
384   # nuking the time stamp.
385   my $uses_aclocal = 1;
387   # Nevertheless, if aclocal.m4 exists and is not made by aclocal,
388   # don't run aclocal.
390   if (-f 'aclocal.m4')
391     {
392       my $aclocal_m4 = new Autom4te::XFile 'aclocal.m4';
393       $_ = $aclocal_m4->getline;
394       $uses_aclocal = 0
395         unless defined ($_) && /generated.*by aclocal/;
396     }
398   # If there are flags for aclocal in Makefile.am, use them.
399   my $aclocal_flags = '';
400   if ($uses_aclocal && -f 'Makefile.am')
401     {
402       my $makefile = new Autom4te::XFile 'Makefile.am';
403       while ($_ = $makefile->getline)
404         {
405           if (/^ACLOCAL_[A-Z_]*FLAGS\s*=\s*(.*)/)
406             {
407               $aclocal_flags = $1;
408               last;
409             }
410         }
411     }
413   if (!$uses_aclocal)
414     {
415       verb "$configure_ac: not using aclocal";
416     }
417   else
418     {
419       # Some file systems have sub-second time stamps, and if so we may
420       # run into trouble later, after we rerun autoconf and set the
421       # time stamps of input files to be no greater than aclocal.m4,
422       # because the time-stamp-setting operation (utime) has a
423       # resolution of only 1 second.  Work around the problem by
424       # ensuring that there is at least a one-second window before the
425       # time stamp of aclocal.m4t in which no file time stamps can
426       # fall.
427       sleep 1;
429       run_aclocal ($aclocal, $aclocal_flags);
430     }
432   # We might have to rerun aclocal if Libtool (or others) imports new
433   # macros.
434   my $rerun_aclocal = 0;
438   # ------------------------------- #
439   # See what tools will be needed.  #
440   # ------------------------------- #
442   # Perform a single trace reading to avoid --force forcing a rerun
443   # between two --trace, that's useless.  If there is no AC_INIT, then
444   # we are not interested: it looks like a Cygnus thingy.
445   my $aux_dir;
446   my $uses_gettext_via_traces;
447   my $uses_libtool;
448   my $uses_libltdl;
449   my $uses_autoheader;
450   my $uses_automake;
451   my @subdir;
452   verb "$configure_ac: tracing";
453   my $traces = new Autom4te::XFile
454     ("$autoconf"
455      . join (' --trace=', '',
456              # If you change this list, update the
457              # `Autoreconf-preselections' section of autom4te.in.
458              'AC_CONFIG_AUX_DIR:AC_CONFIG_AUX_DIR:\$1',
459              'AC_CONFIG_HEADERS',
460              'AC_CONFIG_SUBDIRS:AC_CONFIG_SUBDIRS:\$1',
461              'AC_INIT',
462              'AC_PROG_LIBTOOL',
463              'LT_INIT',
464              'LT_CONFIG_LTDL_DIR',
465              'AM_GNU_GETTEXT',
466              'AM_INIT_AUTOMAKE',
467             )
468      . ' |');
469   while ($_ = $traces->getline)
470     {
471       $aux_dir = $1                 if /AC_CONFIG_AUX_DIR:(.*)/;
472       $uses_autoconf = 1            if /AC_INIT/;
473       $uses_gettext_via_traces = 1  if /AM_GNU_GETTEXT/;
474       $uses_libtool = 1             if /(AC_PROG_LIBTOOL|LT_INIT)/;
475       $uses_libltdl = 1             if /LT_CONFIG_LTDL_DIR/;
476       $uses_autoheader = 1          if /AC_CONFIG_HEADERS/;
477       $uses_automake = 1            if /AM_INIT_AUTOMAKE/;
478       push @subdir, split (' ', $1) if /AC_CONFIG_SUBDIRS:(.*)/ && $recursive;
479     }
481   # The subdirs are *optional*, they may not exist.
482   foreach (@subdir)
483     {
484       if (-d)
485         {
486           verb "$configure_ac: adding subdirectory $_ to autoreconf";
487           autoreconf ($_);
488         }
489       else
490         {
491           verb "$configure_ac: subdirectory $_ not present";
492         }
493     }
495   # Gettext consistency checks...
496   error "$configure_ac: AM_GNU_GETTEXT is used, but not AM_GNU_GETTEXT_VERSION"
497     if $uses_gettext_via_traces && ! $uses_gettext;
498   error "$configure_ac: AM_GNU_GETTEXT_VERSION is used, but not AM_GNU_GETTEXT"
499     if $uses_gettext && ! $uses_gettext_via_traces;
502   # ---------------------------- #
503   # Setting up the source tree.  #
504   # ---------------------------- #
506   # libtoolize, automake --add-missing etc. will drop files in the
507   # $AUX_DIR.  But these tools fail to install these files if the
508   # directory itself does not exist, which valid: just imagine a CVS
509   # repository with hand written code only (there is not even a need
510   # for a Makefile.am!).
512   if (defined $aux_dir && ! -d $aux_dir)
513     {
514       verb "$configure_ac: creating directory $aux_dir";
515       mkdir $aux_dir, 0755
516         or error "cannot create $aux_dir: $!";
517     }
520   # -------------------- #
521   # Running libtoolize.  #
522   # -------------------- #
524   if (!$uses_libtool)
525     {
526       verb "$configure_ac: not using Libtool";
527     }
528   elsif ($install)
529     {
530       if ($uses_libltdl)
531         {
532           $libtoolize .= " --ltdl";
533         }
534       xsystem_hint ("libtoolize is needed because this package uses Libtool", $libtoolize);
535       $rerun_aclocal = 1;
536     }
537   else
538     {
539       verb "$configure_ac: not running libtoolize: --install not given";
540     }
544   # ------------------- #
545   # Rerunning aclocal.  #
546   # ------------------- #
548   # If we re-installed Libtool or Gettext, the macros might have changed.
549   # Automake also needs an up-to-date aclocal.m4.
550   if ($rerun_aclocal)
551     {
552       if (!$uses_aclocal)
553         {
554           verb "$configure_ac: not using aclocal";
555         }
556       else
557         {
558           run_aclocal ($aclocal, $aclocal_flags);
559         }
560     }
563   # ------------------ #
564   # Running autoconf.  #
565   # ------------------ #
567   # Don't try to be smarter than `autoconf', which does its own up to
568   # date checks.
569   #
570   # We prefer running autoconf before autoheader, because (i) the
571   # latter runs the former, and (ii) autoconf is stricter than
572   # autoheader.  So all in all, autoconf should give better error
573   # messages.
574   xsystem ($autoconf);
577   # -------------------- #
578   # Running autoheader.  #
579   # -------------------- #
581   # We now consider that if AC_CONFIG_HEADERS is used, then autoheader
582   # is used too.
583   #
584   # Just as for autoconf, up to date ness is performed by the tool
585   # itself.
586   #
587   # Run it before automake, since the latter checks the presence of
588   # config.h.in when it sees an AC_CONFIG_HEADERS.
589   if (!$uses_autoheader)
590     {
591       verb "$configure_ac: not using Autoheader";
592     }
593   else
594     {
595       xsystem ($autoheader);
596     }
599   # ------------------ #
600   # Running automake.  #
601   # ------------------ #
603   if (!$uses_automake)
604     {
605       verb "$configure_ac: not using Automake";
606     }
607   else
608     {
609       # We should always run automake, and let it decide whether it shall
610       # update the file or not.  In fact, the effect of `$force' is already
611       # included in `$automake' via `--no-force'.
612       xsystem ($automake);
613     }
616   # -------------- #
617   # Running make.  #
618   # -------------- #
620   if ($run_make)
621     {
622       if (!-f "config.status")
623         {
624           verb "no config.status: cannot re-make";
625         }
626       else
627         {
628           xsystem ("./config.status --recheck");
629           xsystem ("./config.status");
630           if (!-f "Makefile")
631             {
632               verb "no Makefile: cannot re-make";
633             }
634           else
635             {
636               xsystem ("$make");
637             }
638         }
639     }
643 # &autoreconf ($DIRECTORY)
644 # ------------------------
645 # Reconf the $DIRECTORY.
646 sub autoreconf ($)
648   my ($directory) = @_;
649   my $cwd = cwd;
651   # The format for this message is not free: taken from Emacs, itself
652   # using GNU Make's format.
653   verb "Entering directory `$directory'";
654   chdir $directory
655     or error "cannot chdir to $directory: $!";
657   autoreconf_current_directory;
659   # The format is not free: taken from Emacs, itself using GNU Make's
660   # format.
661   verb "Leaving directory `$directory'";
662   chdir $cwd
663     or error "cannot chdir to $cwd: $!";
667 ## ------ ##
668 ## Main.  ##
669 ## ------ ##
671 # When debugging, it is convenient that all the related temporary
672 # files be at the same place.
673 mktmpdir ('ar');
674 $ENV{'TMPDIR'} = $tmp;
675 parse_args;
677 # Autoreconf all the given configure.ac.  Unless `--no-recursive' is passed,
678 # AC_CONFIG_SUBDIRS will be traversed in &autoreconf_current_directory.
679 for my $directory (@ARGV)
680   {
681     require_configure_ac ($directory);
682     autoreconf ($directory);
683   }
685 ### Setup "GNU" style for perl-mode and cperl-mode.
686 ## Local Variables:
687 ## perl-indent-level: 2
688 ## perl-continued-statement-offset: 2
689 ## perl-continued-brace-offset: 0
690 ## perl-brace-offset: 0
691 ## perl-brace-imaginary-offset: 0
692 ## perl-label-offset: -2
693 ## cperl-indent-level: 2
694 ## cperl-brace-offset: 0
695 ## cperl-continued-brace-offset: 0
696 ## cperl-label-offset: -2
697 ## cperl-extra-newline-before-brace: t
698 ## cperl-merge-trailing-else: nil
699 ## cperl-continued-statement-offset: 2
700 ## End: