use shell functions to speed up autotest and produce slimmer testsuites
[autoconf.git] / bin / autoreconf.in
blobc964b031753d306567fd8aa6aa18891d373110ff
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, 2007
10 # 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 $datadir = $ENV{'autom4te_perllibdir'} || '@datadir@';
31   unshift @INC, $datadir;
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 are honored.
92 Report bugs to <bug-autoconf\@gnu.org>.
95 # $VERSION
96 # --------
97 $version = "autoreconf (@PACKAGE_NAME@) @VERSION@
98 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
99 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
100 This is free software: you are free to change and redistribute it.
101 There is NO WARRANTY, to the extent permitted by law.
103 Written by David J. MacKenzie and Akim Demaille.
106 # Lib files.
107 my $autoconf   = $ENV{'AUTOCONF'}   || '@bindir@/@autoconf-name@';
108 my $autoheader = $ENV{'AUTOHEADER'} || '@bindir@/@autoheader-name@';
109 my $automake   = $ENV{'AUTOMAKE'}   || 'automake';
110 my $aclocal    = $ENV{'ACLOCAL'}    || 'aclocal';
111 my $libtoolize = $ENV{'LIBTOOLIZE'} || 'libtoolize';
112 my $autopoint  = $ENV{'AUTOPOINT'}  || 'autopoint';
114 # --install -- as --add-missing in other tools.
115 my $install = 0;
116 # symlink -- when --install, use symlinks instead.
117 my $symlink = 0;
118 # Does aclocal support --force?
119 my $aclocal_supports_force = 0;
120 # Does automake support --force-missing?
121 my $automake_supports_force_missing = 0;
123 my @prepend_include;
124 my @include;
126 # List of command line warning requests.
127 my @warning;
129 # Rerun `./configure && make'?
130 my $make = 0;
132 # Recurse into subpackages
133 my $recursive = 1;
135 ## ---------- ##
136 ## Routines.  ##
137 ## ---------- ##
140 # parse_args ()
141 # -------------
142 # Process any command line arguments.
143 sub parse_args ()
145   my $srcdir;
147   getopt ("W|warnings=s"         => \@warning,
148           'I|include=s'          => \@include,
149           'B|prepend-include=s'  => \@prepend_include,
150           'i|install'            => \$install,
151           's|symlink'            => \$symlink,
152           'm|make'               => \$make,
153           'recursive!'           => \$recursive);
155   # Split the warnings as a list of elements instead of a list of
156   # lists.
157   @warning = map { split /,/ } @warning;
158   parse_WARNINGS;
159   parse_warnings '--warnings', @warning;
161   # Even if the user specified a configure.ac, trim to get the
162   # directory, and look for configure.ac again.  Because (i) the code
163   # is simpler, and (ii) we are still able to diagnose simultaneous
164   # presence of configure.ac and configure.in.
165   @ARGV = map { /configure\.(ac|in)$/ ? dirname ($_) : $_ } @ARGV;
166   push @ARGV, '.' unless @ARGV;
168   if ($verbose && $debug)
169     {
170       for my $prog ($autoconf, $autoheader,
171                     $automake, $aclocal,
172                     $autopoint,
173                     $libtoolize)
174         {
175           xsystem ("$prog --version | sed 1q >&2");
176           print STDERR "\n";
177         }
178     }
180   $aclocal_supports_force = `$aclocal --help` =~ /--force/;
181   $automake_supports_force_missing = `$automake --help` =~ /--force-missing/;
183   # Dispatch autoreconf's option to the tools.
184   # --include;
185   $autoconf   .= join (' --include=', '', @include);
186   $autoconf   .= join (' --prepend-include=', '', @prepend_include);
187   $autoheader .= join (' --include=', '', @include);
188   $autoheader .= join (' --prepend-include=', '', @prepend_include);
190   # --install and --symlink;
191   if ($install)
192     {
193       $automake   .= ' --add-missing';
194       $automake   .= ' --copy' unless $symlink;
195       $libtoolize .= ' --copy' unless $symlink;
196     }
197   # --force;
198   if ($force)
199     {
200       $aclocal    .= ' --force'
201         if $aclocal_supports_force;
202       $autoconf   .= ' --force';
203       $autoheader .= ' --force';
204       $automake   .= ' --force-missing'
205         if $automake_supports_force_missing;
206       $autopoint  .= ' --force';
207       $libtoolize .= ' --force';
208     }
209   else
210     {
211       # The implementation of --no-force is bogus in all implementations
212       # of Automake up to 1.8, so we avoid it in these cases.  (Automake
213       # 1.8 is the first version where aclocal supports force, hence
214       # the condition.)
215       $automake .= ' --no-force'
216         if $aclocal_supports_force;
217     }
218   # --verbose --verbose or --debug;
219   if ($verbose > 1 || $debug)
220     {
221       $autoconf   .= ' --verbose';
222       $autoheader .= ' --verbose';
223       $automake   .= ' --verbose';
224       $aclocal    .= ' --verbose';
225     }
226   if ($debug)
227     {
228       $autoconf   .= ' --debug';
229       $autoheader .= ' --debug';
230       $libtoolize .= ' --debug';
231     }
232   # --warnings;
233   if (@warning)
234     {
235       my $warn = ' --warnings=' . join (',', @warning);
236       $autoconf   .= $warn;
237       $autoheader .= $warn;
238       $automake   .= $warn
239         if `$automake --help` =~ /--warnings/;
240     }
244 # &run_aclocal ($ACLOCAL, $FLAGS)
245 # -------------------------------
246 # Update aclocal.m4 as lazily as possible, as aclocal pre-1.8 always
247 # overwrites aclocal.m4, hence triggers autoconf, autoheader, automake
248 # etc. uselessly.  aclocal 1.8+ does not need this.
249 sub run_aclocal ($$)
251   my ($aclocal, $flags) = @_;
253   # aclocal 1.8+ does all this for free.  It can be recognized by its
254   # --force support.
255   if ($aclocal_supports_force)
256     {
257       xsystem ("$aclocal $flags");
258     }
259   else
260     {
261       xsystem ("$aclocal $flags --output=aclocal.m4t");
262       # aclocal may produce no output.
263       if (-f 'aclocal.m4t')
264         {
265           update_file ('aclocal.m4t', 'aclocal.m4');
266           # Make sure that the local m4 files are older than
267           # aclocal.m4.
268           #
269           # Why is not always the case?  Because we already run
270           # aclocal at first (before tracing), which, for instance,
271           # can find Gettext's macros in .../share/aclocal, so we may
272           # have had the right aclocal.m4 already.  Then autopoint is
273           # run, and installs locally these M4 files.  Then
274           # autoreconf, via update_file, sees it is the _same_
275           # aclocal.m4, and doesn't change its timestamp.  But later,
276           # Automake's Makefile expresses that aclocal.m4 depends on
277           # these local files, which are newer, so it triggers aclocal
278           # again.
279           #
280           # To make sure aclocal.m4 is no older, we change the
281           # modification times of the local M4 files to be not newer
282           # than it.
283           #
284           # First, where are the local files?
285           my $aclocal_local_dir = '.';
286           if ($flags =~ /-I\s+(\S+)/)
287             {
288               $aclocal_local_dir = $1;
289             }
290           # All the local files newer than aclocal.m4 are to be
291           # made not newer than it.
292           my $aclocal_m4_mtime = mtime ('aclocal.m4');
293           for my $file (glob ("$aclocal_local_dir/*.m4"), 'acinclude.m4')
294             {
295               if ($aclocal_m4_mtime < mtime ($file))
296                 {
297                   debug "aging $file to be not newer than aclocal.m4";
298                   utime $aclocal_m4_mtime, $aclocal_m4_mtime, $file;
299                 }
300             }
301         }
302     }
305 # &autoreconf_current_directory
306 # -----------------------------
307 sub autoreconf_current_directory ()
309   my $configure_ac = find_configure_ac;
311   # ---------------------- #
312   # Is it using Autoconf?  #
313   # ---------------------- #
315   my $uses_autoconf;
316   my $uses_gettext;
317   if (-f $configure_ac)
318     {
319       my $configure_ac_file = new Autom4te::XFile $configure_ac;
320       while ($_ = $configure_ac_file->getline)
321         {
322           s/#.*//;
323           s/dnl.*//;
324           $uses_autoconf = 1 if /AC_INIT/;
325           # See below for why we look for gettext here.
326           $uses_gettext = 1  if /^AM_GNU_GETTEXT_VERSION/;
327         }
328     }
329   if (!$uses_autoconf)
330     {
331       verb "$configure_ac: not using Autoconf";
332       return;
333     }
336   # ------------------- #
337   # Running autopoint.  #
338   # ------------------- #
340   # Gettext is a bit of a problem: its macros are not necessarily
341   # visible to aclocal, so if we start with a completely striped down
342   # package (think of a fresh CVS checkout), running `aclocal' first
343   # will fail: the Gettext macros are missing.
344   #
345   # Therefore, we can't use the traces to decide if we use Gettext or
346   # not.  I guess that once Gettext move to 2.5x we will be able to,
347   # but in the meanwhile forget it.
348   #
349   # We can only grep for AM_GNU_GETTEXT_VERSION in configure.ac.  You
350   # might think this approach is naive, and indeed it is, as it
351   # prevents one to embed AM_GNU_GETTEXT_VERSION in another *.m4, but
352   # anyway we don't limit the generality, since... that's what
353   # autopoint does.  Actually, it is even more restrictive, as it
354   # greps for `^AM_GNU_GETTEXT_VERSION('.  We did this above, while
355   # scanning configure.ac.
356   if (!$uses_gettext)
357     {
358       verb "$configure_ac: not using Gettext";
359     }
360   elsif (!$install)
361     {
362       verb "$configure_ac: not running autopoint: --install not given";
363     }
364   else
365     {
366       xsystem "$autopoint";
367     }
370   # ----------------- #
371   # Running aclocal.  #
372   # ----------------- #
374   # Run it first: it might discover new macros to add, e.g.,
375   # AC_PROG_LIBTOOL, which we will trace later to see if Libtool is
376   # used.
377   #
378   # Always run it.  Tracking its sources for up-to-dateness is too
379   # complex and too error prone.  The best we can do is avoiding
380   # nuking the time stamp.
381   my $uses_aclocal = 1;
383   # Nevertheless, if aclocal.m4 exists and is not made by aclocal,
384   # don't run aclocal.
386   if (-f 'aclocal.m4')
387     {
388       my $aclocal_m4 = new Autom4te::XFile 'aclocal.m4';
389       $_ = $aclocal_m4->getline;
390       $uses_aclocal = 0
391         unless defined ($_) && /generated.*by aclocal/;
392     }
394   # If there are flags for aclocal in Makefile.am, use them.
395   my $aclocal_flags = '';
396   if ($uses_aclocal && -f 'Makefile.am')
397     {
398       my $makefile = new Autom4te::XFile 'Makefile.am';
399       while ($_ = $makefile->getline)
400         {
401           if (/^ACLOCAL_[A-Z_]*FLAGS\s*=\s*(.*)/)
402             {
403               $aclocal_flags = $1;
404               last;
405             }
406         }
407     }
409   if (!$uses_aclocal)
410     {
411       verb "$configure_ac: not using aclocal";
412     }
413   else
414     {
415       # Some file systems have sub-second time stamps, and if so we may
416       # run into trouble later, after we rerun autoconf and set the
417       # time stamps of input files to be no greater than aclocal.m4,
418       # because the time-stamp-setting operation (utime) has a
419       # resolution of only 1 second.  Work around the problem by
420       # ensuring that there is at least a one-second window before the
421       # time stamp of aclocal.m4t in which no file time stamps can
422       # fall.
423       sleep 1;
425       run_aclocal ($aclocal, $aclocal_flags);
426     }
428   # We might have to rerun aclocal if Libtool (or others) imports new
429   # macros.
430   my $rerun_aclocal = 0;
434   # ------------------------------- #
435   # See what tools will be needed.  #
436   # ------------------------------- #
438   # Perform a single trace reading to avoid --force forcing a rerun
439   # between two --trace, that's useless.  If there is no AC_INIT, then
440   # we are not interested: it looks like a Cygnus thingy.
441   my $aux_dir;
442   my $uses_gettext_via_traces;
443   my $uses_libtool;
444   my $uses_libltdl;
445   my $uses_autoheader;
446   my $uses_automake;
447   my @subdir;
448   verb "$configure_ac: tracing";
449   my $traces = new Autom4te::XFile
450     ("$autoconf"
451      . join (' --trace=', '',
452              # If you change this list, update the
453              # `Autoreconf-preselections' section of autom4te.in.
454              'AC_CONFIG_AUX_DIR:AC_CONFIG_AUX_DIR:\$1',
455              'AC_CONFIG_HEADERS',
456              'AC_CONFIG_SUBDIRS:AC_CONFIG_SUBDIRS:\$1',
457              'AC_INIT',
458              'AC_PROG_LIBTOOL',
459              'LT_INIT',
460              'LT_CONFIG_LTDL_DIR',
461              'AM_GNU_GETTEXT',
462              'AM_INIT_AUTOMAKE',
463             )
464      . ' |');
465   while ($_ = $traces->getline)
466     {
467       $aux_dir = $1                 if /AC_CONFIG_AUX_DIR:(.*)/;
468       $uses_autoconf = 1            if /AC_INIT/;
469       $uses_gettext_via_traces = 1  if /AM_GNU_GETTEXT/;
470       $uses_libtool = 1             if /(AC_PROG_LIBTOOL|LT_INIT)/;
471       $uses_libltdl = 1             if /LT_CONFIG_LTDL_DIR/;
472       $uses_autoheader = 1          if /AC_CONFIG_HEADERS/;
473       $uses_automake = 1            if /AM_INIT_AUTOMAKE/;
474       push @subdir, split (' ', $1) if /AC_CONFIG_SUBDIRS:(.*)/ && $recursive;
475     }
477   # The subdirs are *optional*, they may not exist.
478   foreach (@subdir)
479     {
480       if (-d)
481         {
482           verb "$configure_ac: adding subdirectory $_ to autoreconf";
483           autoreconf ($_);
484         }
485       else
486         {
487           verb "$configure_ac: subdirectory $_ not present";
488         }
489     }
491   # Gettext consistency checks...
492   error "$configure_ac: AM_GNU_GETTEXT is used, but not AM_GNU_GETTEXT_VERSION"
493     if $uses_gettext_via_traces && ! $uses_gettext;
494   error "$configure_ac: AM_GNU_GETTEXT_VERSION is used, but not AM_GNU_GETTEXT"
495     if $uses_gettext && ! $uses_gettext_via_traces;
498   # ---------------------------- #
499   # Setting up the source tree.  #
500   # ---------------------------- #
502   # libtoolize, automake --add-missing etc. will drop files in the
503   # $AUX_DIR.  But these tools fail to install these files if the
504   # directory itself does not exist, which valid: just imagine a CVS
505   # repository with hand written code only (there is not even a need
506   # for a Makefile.am!).
508   if (defined $aux_dir && ! -d $aux_dir)
509     {
510       verb "$configure_ac: creating directory $aux_dir";
511       mkdir $aux_dir, 0755
512         or error "cannot create $aux_dir: $!";
513     }
516   # -------------------- #
517   # Running libtoolize.  #
518   # -------------------- #
520   if (!$uses_libtool)
521     {
522       verb "$configure_ac: not using Libtool";
523     }
524   elsif ($install)
525     {
526       if ($uses_libltdl)
527         {
528           $libtoolize .= " --ltdl";
529         }
530       xsystem ($libtoolize);
531       $rerun_aclocal = 1;
532     }
533   else
534     {
535       verb "$configure_ac: not running libtoolize: --install not given";
536     }
540   # ------------------- #
541   # Rerunning aclocal.  #
542   # ------------------- #
544   # If we re-installed Libtool or Gettext, the macros might have changed.
545   # Automake also needs an up-to-date aclocal.m4.
546   if ($rerun_aclocal)
547     {
548       if (!$uses_aclocal)
549         {
550           verb "$configure_ac: not using aclocal";
551         }
552       else
553         {
554           run_aclocal ($aclocal, $aclocal_flags);
555         }
556     }
559   # ------------------ #
560   # Running autoconf.  #
561   # ------------------ #
563   # Don't try to be smarter than `autoconf', which does its own up to
564   # date checks.
565   #
566   # We prefer running autoconf before autoheader, because (i) the
567   # latter runs the former, and (ii) autoconf is stricter than
568   # autoheader.  So all in all, autoconf should give better error
569   # messages.
570   xsystem ($autoconf);
573   # -------------------- #
574   # Running autoheader.  #
575   # -------------------- #
577   # We now consider that if AC_CONFIG_HEADERS is used, then autoheader
578   # is used too.
579   #
580   # Just as for autoconf, up to date ness is performed by the tool
581   # itself.
582   #
583   # Run it before automake, since the latter checks the presence of
584   # config.h.in when it sees an AC_CONFIG_HEADERS.
585   if (!$uses_autoheader)
586     {
587       verb "$configure_ac: not using Autoheader";
588     }
589   else
590     {
591       xsystem ($autoheader);
592     }
595   # ------------------ #
596   # Running automake.  #
597   # ------------------ #
599   if (!$uses_automake)
600     {
601       verb "$configure_ac: not using Automake";
602     }
603   else
604     {
605       # We should always run automake, and let it decide whether it shall
606       # update the file or not.  In fact, the effect of `$force' is already
607       # included in `$automake' via `--no-force'.
608       xsystem ($automake);
609     }
612   # -------------- #
613   # Running make.  #
614   # -------------- #
616   if ($make)
617     {
618       if (!-f "config.status")
619         {
620           verb "no config.status: cannot re-make";
621         }
622       else
623         {
624           xsystem ("./config.status --recheck");
625           xsystem ("./config.status");
626           if (!-f "Makefile")
627             {
628               verb "no Makefile: cannot re-make";
629             }
630           else
631             {
632               xsystem ("make");
633             }
634         }
635     }
639 # &autoreconf ($DIRECTORY)
640 # ------------------------
641 # Reconf the $DIRECTORY.
642 sub autoreconf ($)
644   my ($directory) = @_;
645   my $cwd = cwd;
647   # The format for this message is not free: taken from Emacs, itself
648   # using GNU Make's format.
649   verb "Entering directory `$directory'";
650   chdir $directory
651     or error "cannot chdir to $directory: $!";
653   autoreconf_current_directory;
655   # The format is not free: taken from Emacs, itself using GNU Make's
656   # format.
657   verb "Leaving directory `$directory'";
658   chdir $cwd
659     or error "cannot chdir to $cwd: $!";
663 ## ------ ##
664 ## Main.  ##
665 ## ------ ##
667 # When debugging, it is convenient that all the related temporary
668 # files be at the same place.
669 mktmpdir ('ar');
670 $ENV{'TMPDIR'} = $tmp;
671 parse_args;
673 # Autoreconf all the given configure.ac.  Unless `--no-recursive' is passed,
674 # AC_CONFIG_SUBDIRS will be traversed in &autoreconf_current_directory.
675 for my $directory (@ARGV)
676   {
677     require_configure_ac ($directory);
678     autoreconf ($directory);
679   }
681 ### Setup "GNU" style for perl-mode and cperl-mode.
682 ## Local Variables:
683 ## perl-indent-level: 2
684 ## perl-continued-statement-offset: 2
685 ## perl-continued-brace-offset: 0
686 ## perl-brace-offset: 0
687 ## perl-brace-imaginary-offset: 0
688 ## perl-label-offset: -2
689 ## cperl-indent-level: 2
690 ## cperl-brace-offset: 0
691 ## cperl-continued-brace-offset: 0
692 ## cperl-label-offset: -2
693 ## cperl-extra-newline-before-brace: t
694 ## cperl-merge-trailing-else: nil
695 ## cperl-continued-statement-offset: 2
696 ## End: