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