eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
if 0;
use File::Basename ();
my($_bindir, $_datadir);
BEGIN
{
$_bindir = File::Basename::dirname($0);
($_datadir = '@datadir@') =~ s,^/usr,$_bindir/..,;
my $perllibdir = $ENV{'perllibdir'} || "$_datadir/@PACKAGE@-@APIVERSION@";
unshift @INC, (split '@PATH_SEPARATOR@', $perllibdir);
}
use strict;
use Automake::Config;
use Automake::General;
use Automake::Configure_ac;
use Automake::Channels;
use Automake::ChannelDefs;
use Automake::XFile;
use Automake::FileUtils;
use File::Basename;
use File::stat;
use Cwd;
my @user_includes = ();
my @automake_includes = ("$_datadir/aclocal-$APIVERSION");
my @system_includes = ("$_datadir/aclocal");
my $install = 0;
my @diff_command;
my $dry_run = 0;
my $configure_ac;
my $output_file = 'aclocal.m4';
my $force_output = 0;
my $greatest_mtime = 0;
my %macro_seen = ();
my @file_order = ();
my %map = ();
my %map_traced_defs = ();
my %invmap = ();
my %file_contents = ();
my %file_type = ();
use constant FT_USER => 1;
use constant FT_AUTOMAKE => 2;
use constant FT_SYSTEM => 3;
my %file_includes = ();
my %file_added = ();
my %scanned_configure_dep = ();
my %serial = ();
my $ac_defun_rx =
"(?:AU_ALIAS|A[CU]_DEFUN|AC_DEFUN_ONCE)\\((?:\\[([^]]+)\\]|([^],)\n]+))";
my $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
my $m4_include_rx = "(m4_|m4_s|s)include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
my $serial_line_rx = '^#\s*serial\s+(\S*)';
my $serial_number_rx = '^\d+(?:\.\d+)*$';
my $ac_version;
my $erase_me;
sub unlink_tmp
{
if (defined $erase_me && -e $erase_me && !unlink ($erase_me))
{
fatal "could not remove `$erase_me': $!";
}
undef $erase_me;
}
$SIG{'INT'} = $SIG{'TERM'} = $SIG{'QUIT'} = $SIG{'HUP'} = 'unlink_tmp';
END { unlink_tmp }
sub check_acinclude ()
{
foreach my $key (keys %map)
{
msg ('syntax', "warning: macro `$key' defined in "
. "acinclude.m4 but never used")
if $map{$key} eq 'acinclude.m4' && ! exists $macro_seen{$key};
}
}
sub reset_maps ()
{
$greatest_mtime = 0;
%macro_seen = ();
@file_order = ();
%map = ();
%map_traced_defs = ();
%file_contents = ();
%file_type = ();
%file_includes = ();
%file_added = ();
%scanned_configure_dep = ();
%invmap = ();
%serial = ();
undef &search;
}
sub install_file ($$)
{
my ($src, $dest) = @_;
my $diff_dest;
if ($force_output
|| !exists $file_contents{$dest}
|| $file_contents{$src} ne $file_contents{$dest})
{
if (-e $dest)
{
msg 'note', "overwriting `$dest' with `$src'";
$diff_dest = $dest;
}
else
{
msg 'note', "installing `$dest' from `$src'";
}
if (@diff_command)
{
if (! defined $diff_dest)
{
$erase_me = $dest;
my $f = new IO::File "> $dest";
if (! defined $f)
{
undef $erase_me;
$diff_dest = '/dev/null';
}
else
{
$diff_dest = $dest;
$f->close;
}
}
my @cmd = (@diff_command, $diff_dest, $src);
$! = 0;
verb "running: @cmd";
my $res = system (@cmd);
Automake::FileUtils::handle_exec_errors "@cmd", 1
if $res;
unlink_tmp;
}
elsif (!$dry_run)
{
xsystem ('cp', $src, $dest);
}
}
}
sub list_compare (\@\@)
{
my @l = @{$_[0]};
my @r = @{$_[1]};
while (1)
{
if (0 == @l)
{
return (0 == @r) ? 0 : -1;
}
elsif (0 == @r)
{
return 1;
}
elsif ($l[0] < $r[0])
{
return -1;
}
elsif ($l[0] > $r[0])
{
return 1;
}
shift @l;
shift @r;
}
}
sub scan_m4_dirs ($@)
{
my ($type, @dirlist) = @_;
foreach my $m4dir (@dirlist)
{
if (! opendir (DIR, $m4dir))
{
fatal "couldn't open directory `$m4dir': $!";
}
foreach my $file (reverse sort grep (! /^\./, readdir (DIR)))
{
next unless $file =~ /\.m4$/;
next if $file eq 'aclocal.m4';
my $fullfile = File::Spec->canonpath ("$m4dir/$file");
&scan_file ($type, $fullfile, 'aclocal');
}
closedir (DIR);
}
}
sub scan_m4_files ()
{
&scan_file (FT_USER, $configure_ac, 'aclocal');
if (-f 'acinclude.m4')
{
&scan_file (FT_USER, 'acinclude.m4', 'aclocal');
}
scan_m4_dirs (FT_USER, @user_includes);
scan_m4_dirs (FT_AUTOMAKE, @automake_includes);
scan_m4_dirs (FT_SYSTEM, @system_includes);
my $search = "sub search {\nmy \$found = 0;\n";
foreach my $key (reverse sort keys %map)
{
$search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
. '"); $found = 1; }' . "\n");
}
$search .= "return \$found;\n};\n";
eval $search;
prog_error "$@\n search is $search" if $@;
}
sub add_macro ($)
{
my ($macro) = @_;
return unless defined $map{$macro};
verb "saw macro $macro";
$macro_seen{$macro} = 1;
&add_file ($map{$macro});
}
sub scan_configure_dep ($)
{
my ($file) = @_;
return ()
if exists $scanned_configure_dep{$file};
$scanned_configure_dep{$file} = 1;
my $mtime = mtime $file;
$greatest_mtime = $mtime if $greatest_mtime < $mtime;
my $contents = exists $file_contents{$file} ?
$file_contents{$file} : contents $file;
my $line = 0;
my @rlist = ();
my @ilist = ();
foreach (split ("\n", $contents))
{
++$line;
s/\bdnl\b.*$//;
s/\ next if /^\s*$/;
while (/$m4_include_rx/go)
{
my $ifile = $2 || $3;
next if $1 ne 'm4_' && ! -f $ifile;
push @ilist, $ifile;
}
while (/$ac_require_rx/go)
{
push (@rlist, $1 || $2);
}
if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
{
msg ('unsupported', "$file:$line",
"warning: macro `$2' not found in library");
}
}
add_macro ($_) foreach (@rlist);
&scan_configure_dep ($_) foreach @ilist;
}
sub add_file ($)
{
my ($file) = @_;
return if ($file_added{$file});
$file_added{$file} = 1;
scan_configure_dep $file;
}
my $underquoted_manual_once = 0;
sub scan_file ($$$)
{
my ($type, $file, $where) = @_;
my $basename = basename $file;
return @{$file_includes{$file}} if exists $file_includes{$file};
return () if exists $file_contents{$file};
unshift @file_order, $file;
$file_type{$file} = $type;
fatal "$where: file `$file' does not exist" if ! -e $file;
my $fh = new Automake::XFile $file;
my $contents = '';
my @inc_files = ();
my %inc_lines = ();
my $defun_seen = 0;
my $serial_seen = 0;
my $serial_older = 0;
while ($_ = $fh->getline)
{
next if /^
$contents .= $_;
my $line = $_;
if ($line =~ /$serial_line_rx/go)
{
my $number = $1;
if ($number !~ /$serial_number_rx/go)
{
msg ('syntax', "$file:$.",
"warning: ill-formed serial number `$number', "
. "expecting a version string with only digits and dots");
}
elsif ($defun_seen)
{
msg ('syntax', "$file:$.",
'the serial number must appear before any macro definition');
}
elsif ($install && $type != FT_AUTOMAKE)
{
$serial_seen = 1;
my @new = split (/\./, $number);
verb "$file:$.: serial $number";
if (!exists $serial{$basename}
|| list_compare (@new, @{$serial{$basename}}) > 0)
{
foreach my $def (@{$invmap{$basename}})
{
verb "$file:$.: ignoring previous definition of $def";
delete $map{$def};
}
$invmap{$basename} = [];
$serial{$basename} = \@new;
}
else
{
$serial_older = 1;
}
}
}
$line =~ s/\bdnl\b.*$//;
$line =~ s/\
while ($line =~ /$ac_defun_rx/go)
{
$defun_seen = 1;
if (! defined $1)
{
msg ('syntax', "$file:$.", "warning: underquoted definition of $2"
. "\n run info '(automake)Extending aclocal'\n"
. " or see http://sources.redhat.com/automake/"
. "automake.html#Extending-aclocal")
unless $underquoted_manual_once;
$underquoted_manual_once = 1;
}
$serial_older ||= ($type != FT_AUTOMAKE
&& !$serial_seen && exists $serial{$basename});
my $macro = $1 || $2;
if (!$serial_older && !defined $map{$macro})
{
verb "found macro $macro in $file: $.";
$map{$macro} = $file;
push @{$invmap{$basename}}, $macro;
}
else
{
verb "ignoring macro $macro in $file: $.";
}
}
while ($line =~ /$m4_include_rx/go)
{
my $ifile = $2 || $3;
next if $1 ne 'm4_' && ! -f $ifile;
push (@inc_files, $ifile);
$inc_lines{$ifile} = $.;
}
}
return ()
if ($serial_older ||
($type != FT_AUTOMAKE && !$serial_seen && exists $serial{$basename}));
$file_contents{$file} = $contents;
my @copy = @inc_files;
my @all_inc_files = (@inc_files,
map { scan_file ($type, $_,
"$file:$inc_lines{$_}") } @copy);
$file_includes{$file} = \@all_inc_files;
return @all_inc_files;
}
sub strip_redundant_includes (%)
{
my %files = @_;
$files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
$files{$configure_ac} = 1;
foreach my $file (reverse @file_order)
{
next unless exists $files{$file};
foreach my $ifile (@{$file_includes{$file}})
{
next unless exists $files{$ifile};
delete $files{$ifile};
verb "$ifile is already included by $file";
}
}
delete $files{$configure_ac};
return %files;
}
sub trace_used_macros ()
{
my %files = map { $map{$_} => 1 } keys %macro_seen;
%files = strip_redundant_includes %files;
my $traces = ($ENV{AUTOM4TE} || "$_bindir/autom4te");
$traces .= " --language Autoconf-without-aclocal-m4 ";
$traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
$traces .= join (' ',
(map { "--trace='$_:\$f::\$n::\$1'" }
('AC_DEFUN',
'AC_DEFUN_ONCE',
'AU_DEFUN',
'_AM_AUTOCONF_VERSION')),
(map { "--trace='$_:\$f::\$n'" } (keys %macro_seen)));
verb "running $traces $configure_ac";
my $tracefh = new Automake::XFile ("$traces $configure_ac |");
my %traced = ();
while ($_ = $tracefh->getline)
{
chomp;
my ($file, $macro, $arg1) = split (/::/);
$traced{$macro} = 1 if exists $macro_seen{$macro};
$map_traced_defs{$arg1} = $file
if ($macro eq 'AC_DEFUN'
|| $macro eq 'AC_DEFUN_ONCE'
|| $macro eq 'AU_DEFUN');
$ac_version = $arg1 if $macro eq '_AM_AUTOCONF_VERSION';
}
$tracefh->close;
return %traced;
}
sub scan_configure ()
{
if (-f 'acinclude.m4')
{
add_file ('acinclude.m4');
}
scan_configure_dep ($configure_ac);
}
sub write_aclocal ($@)
{
my ($output_file, @macros) = @_;
my $output = '';
my %files = ();
for my $m (@macros)
{
$files{$map{$m}} = 1
if (exists $map_traced_defs{$m}
&& $map{$m} eq $map_traced_defs{$m});
}
%files = strip_redundant_includes %files;
my $installed = 0;
for my $file (grep { exists $files{$_} } @file_order)
{
for my $ifile ($file, @{$file_includes{$file}})
{
my $mtime = mtime $ifile;
$greatest_mtime = $mtime if $greatest_mtime < $mtime;
}
if ($file_type{$file} != FT_USER
|| $file =~ m,^(?:\w:)?[\\/],)
{
if (!$install || $file_type{$file} != FT_SYSTEM)
{
$output .= $file_contents{$file} . "\n";
}
else
{
my $dest;
for my $ifile (@{$file_includes{$file}}, $file)
{
$dest = "$user_includes[0]/" . basename $ifile;
verb "installing $ifile to $dest";
install_file ($ifile, $dest);
}
$installed = 1;
}
}
else
{
$output .= "m4_include([$file])\n";
}
}
if ($installed)
{
verb "running aclocal anew, because some files were installed locally";
return 0;
}
return 1 if ! length ($output);
if ($ac_version)
{
$output = "m4_if(m4_PACKAGE_VERSION, [$ac_version],,
[m4_fatal([this file was generated for autoconf $ac_version.
You have another version of autoconf. If you want to use that,
you should regenerate the build system entirely.], [63])])
$output";
}
$output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
# 2005, 2006 Free Software Foundation, Inc.
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
$output";
if (!$force_output
&& $greatest_mtime < mtime ($output_file)
&& $output eq contents ($output_file))
{
verb "$output_file unchanged";
return 1;
}
verb "writing $output_file";
if (!$dry_run)
{
if (-e $output_file && !unlink $output_file)
{
fatal "could not remove `$output_file': $!";
}
my $out = new Automake::XFile "> $output_file";
print $out $output;
}
return 1;
}
sub usage ($)
{
my ($status) = @_;
print "Usage: aclocal [OPTIONS] ...
Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
Options:
--acdir=DIR directory holding config files (for debugging)
--diff[=COMMAND] run COMMAND [diff -u] on M4 files that would be
changed (implies --install and --dry-run)
--dry-run pretend to, but do not actually update any file
--force always update output file
--help print this help, then exit
-I DIR add directory to search list for .m4 files
--install copy third-party files to the first -I directory
--output=FILE put output in FILE (default aclocal.m4)
--print-ac-dir print name of directory holding m4 files, then exit
--verbose don't be silent
--version print version number, then exit
-W, --warnings=CATEGORY report the warnings falling in CATEGORY
Warning categories include:
`syntax' dubious syntactic constructs (default)
`unsupported' unknown macros (default)
`all' all the warnings (default)
`no-CATEGORY' turn off warnings in CATEGORY
`none' turn off all the warnings
`error' treat warnings as errors
Report bugs to <bug-automake\@gnu.org>.\n";
exit $status;
}
sub version()
{
print <<EOF;
aclocal (GNU $PACKAGE) $VERSION
Written by Tom Tromey <tromey\@redhat.com>
and Alexandre Duret-Lutz <adl\@gnu.org>.
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
exit 0;
}
sub parse_arguments ()
{
my $print_and_exit = 0;
my $diff_command;
my %cli_options =
(
'acdir=s' => sub { @automake_includes = ();
@system_includes = ($_[1])
},
'diff:s' => \$diff_command,
'dry-run' => \$dry_run,
'force' => \$force_output,
'I=s' => \@user_includes,
'install' => \$install,
'output=s' => \$output_file,
'print-ac-dir' => \$print_and_exit,
'verbose' => sub { setup_channel 'verb', silent => 0; },
'W|warnings=s' => \&parse_warnings,
);
use Getopt::Long;
Getopt::Long::config ("bundling", "pass_through");
my %cli_options_1st_pass =
(
'version' => \&version,
'help' => sub { usage(0); },
map { $_ => sub {} } (keys %cli_options)
);
my @ARGV_backup = @ARGV;
Getopt::Long::GetOptions %cli_options_1st_pass
or exit 1;
@ARGV = @ARGV_backup;
Getopt::Long::GetOptions %cli_options, 'version' => sub {}, 'help' => sub {}
or exit 1;
if (@ARGV)
{
my %argopts;
for my $k (keys %cli_options)
{
if ($k =~ /(.*)=s$/)
{
map { $argopts{(length ($_) == 1)
? "-$_" : "--$_" } = 1; } (split (/\|/, $1));
}
}
if (exists $argopts{$ARGV[0]})
{
fatal ("option `$ARGV[0]' requires an argument\n"
. "Try `$0 --help' for more information.");
}
else
{
fatal ("unrecognized option `$ARGV[0]'\n"
. "Try `$0 --help' for more information.");
}
}
if ($print_and_exit)
{
print "@system_includes\n";
exit 0;
}
if (defined $diff_command)
{
$diff_command = 'diff -u' if $diff_command eq '';
@diff_command = split (' ', $diff_command);
$install = 1;
$dry_run = 1;
}
if ($install && !@user_includes)
{
fatal ("--install should copy macros in the directory indicated by the"
. "\nfirst -I option, but no -I was supplied.");
}
if (! -d $system_includes[0])
{
@system_includes = () if @automake_includes;
}
else
{
if (open (DIRLIST, "$system_includes[0]/dirlist"))
{
while (<DIRLIST>)
{
next if /^ s/\s*\ chomp;
foreach my $dir (glob)
{
push (@system_includes, $dir) if -d $dir;
}
}
close (DIRLIST);
}
}
}
parse_WARNINGS; parse_arguments;
$configure_ac = require_configure_ac;
my $loop = 0;
while (1)
{
++$loop;
prog_error "Too many loops." if $loop > 2;
reset_maps;
scan_m4_files;
scan_configure;
last if $exit_code;
my %macro_traced = trace_used_macros;
last if write_aclocal ($output_file, keys %macro_traced);
last if $dry_run;
}
check_acinclude;
exit $exit_code;