use 5.005;
use File::Basename;
use File::Find;
use Getopt::Long;
use IO::File;
use strict;
use vars qw(@cfiles @makefiles @shfiles %c_keywords %printed);
my $me = basename ($0);
my $verbose = 0;
my %used = ();
my %macro = ();
my %needed_macros = ();
my @kinds = qw (functions headers identifiers programs makevars libraries);
my %generic_macro =
(
'functions' => 'AC_CHECK_FUNCS',
'headers' => 'AC_CHECK_HEADERS',
'identifiers' => 'AC_CHECK_TYPES',
'programs' => 'AC_CHECK_PROGS',
'libraries' => 'AC_CHECK_LIB'
);
my %kind_comment =
(
'functions' => 'Checks for library functions.',
'headers' => 'Checks for header files.',
'identifiers' => 'Checks for typedefs, structures, and compiler characteristics.',
'programs' => 'Checks for programs.',
);
my $configure_scan = 'configure.scan';
my $log = new IO::File ">$me.log"
or die "$me: cannot open $me.log: $!\n";
my $autoconf;
my $datadir = $ENV{"AC_MACRODIR"} || "@datadir@";
sub END
{
use POSIX qw (_exit);
close STDOUT
or (warn "$me: closing standard output: $!\n"), _exit (1);
}
sub print_usage ()
{
print "Usage: $0 [OPTION] ... [SRCDIR]
Examine source files in the directory tree rooted at SRCDIR, or the
current directory if none is given. Search the source files for
common portability problems, check for incompleteness of
`configure.ac', and create a file `$configure_scan' which is a
preliminary `configure.ac' for that package.
-h, --help print this help, then exit
-V, --version print version number, then exit
-v, --verbose verbosely report processing
Library directories:
-A, --autoconf-dir=ACDIR Autoconf's files location (rarely needed)
-l, --localdir=DIR location of `aclocal.m4' and `acconfig.h'
Report bugs to <bug-autoconf\@gnu.org>.\n";
exit 0;
}
sub print_version
{
print "autoscan (@PACKAGE_NAME@) @VERSION@
Written by David J. MacKenzie.
Copyright 1994, 1999, 2000, 2001 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.\n";
exit 0;
}
sub parse_args ()
{
my $srcdir;
Getopt::Long::config ("bundling");
Getopt::Long::GetOptions ("A|autoconf-dir|m|macrodir=s" => \$datadir,
"h|help" => \&print_usage,
"V|version" => \&print_version,
"v|verbose" => \$verbose)
or exit 1;
die "$me: too many arguments
Try `$me --help' for more information.\n"
if (@ARGV > 1);
($srcdir) = @ARGV;
$srcdir = "."
if !defined $srcdir;
print "srcdir=$srcdir\n" if $verbose;
chdir $srcdir || die "$me: cannot cd to $srcdir: $!\n";
}
sub find_autoconf
{
my $dir = dirname ($0);
foreach my $file ($ENV{"AUTOCONF"} || '',
"$dir/@autoconf-name@",
"$dir/autoconf",
"@bindir@/@autoconf-name@")
{
if (-x $file)
{
$autoconf = $file;
last;
}
}
}
sub find_configure_ac ()
{
if (-f 'configure.ac')
{
if (-f 'configure.in')
{
warn "warning: `configure.ac' and `configure.in' both present.\n";
warn "warning: proceeding with `configure.ac'.\n";
}
return 'configure.ac';
}
elsif (-f 'configure.in')
{
return 'configure.in';
}
return;
}
sub init_tables ()
{
foreach (qw (int char float double struct union long short unsigned
auto extern register typedef static goto return sizeof break
continue if else for do while switch case default))
{
$c_keywords{$_} = 0;
}
my $tables_are_consistent = 1;
foreach my $kind (@kinds)
{
my $file = "$datadir/ac$kind";
my $table = new IO::File $file
or die "$me: cannot open $file: $!\n";
while ($_ = $table->getline)
{
next
if /^\s*$/ || /^\s*\ unless (/^(\S+)\s+(\S.*)$/ || /^(\S+)\s*$/)
{
die "$me: cannot parse definition in $file:\n$_\n";
}
my $word = $1;
my $macro = $2 || $generic_macro{$kind};
if (!defined $2 && exists $macro{$kind}{$word})
{
warn ("$datadir/ac$kind:$.: "
. "ignoring implicit call to the generic macro for $word\n");
$tables_are_consistent = 0;
}
else
{
push @{$macro{$kind}{$word}}, $macro;
}
}
$table->close
or die "$me: cannot close $file: $!\n";
}
die "$me: some tables are inconsistent\n"
if !$tables_are_consistent;
}
sub scan_c_file ($)
{
my ($filename) = @_;
push (@cfiles, $File::Find::name);
my $in_comment = 0;
my $file = new IO::File "<$filename"
or die "$me: cannot open $filename: $!\n";
while ($_ = $file->getline)
{
if ($in_comment && m,\*/,)
{
s,.*\*/,,;
$in_comment = 0;
}
s,/\*.*\*/,,g;
if (m,/\*,)
{
$in_comment = 1;
}
next if $in_comment;
if (/^\s*\ {
push (@{$used{'headers'}{$1}}, "$File::Find::name:$.");
}
next if /^\s*\
s,\"[^\"]*\",,g;
s,\'[^\']*\',,g;
while (s/\b([a-zA-Z_]\w*)\s*\(/ /)
{
push (@{$used{'functions'}{$1}}, "$File::Find::name:$.")
if !defined $c_keywords{$1};
}
while (s/\b([a-zA-Z_]\w*)\b/ /)
{
push (@{$used{'identifiers'}{$1}}, "$File::Find::name:$.")
if !defined $c_keywords{$1};
}
}
$file->close
or die "$me: cannot close $filename: $!\n";
}
sub scan_makefile ($)
{
my ($filename) = @_;
push (@makefiles, $File::Find::name);
my $file = new IO::File "<$filename"
or die "$me: cannot open $filename: $!\n";
while ($_ = $file->getline)
{
s/ s/\$\([^\)]*\)//g;
s/\${[^\}]*}//g;
s/@[^@]*@//g;
while (s/\b([a-zA-Z_]\w*)\s*=/ /)
{
push (@{$used{'makevars'}{$1}}, "$File::Find::name:$.");
}
while (s/\B-l([a-zA-Z_]\w*)\b/ /)
{
push (@{$used{'libraries'}{$1}}, "$File::Find::name:$.");
}
while (s/(?<![-\w.])([a-zA-Z_][\w+.-]+)/ /)
{
push (@{$used{'programs'}{$1}}, "$File::Find::name:$.");
}
}
$file->close
or die "$me: cannot close $filename: $!\n";
}
sub scan_sh_file ($)
{
my ($filename) = @_;
push (@shfiles, $File::Find::name);
my $file = new IO::File "<$filename"
or die "$me: cannot open $filename: $!\n";
while ($_ = $file->getline)
{
s/ s/ s/\${[^\}]*}//g;
s/@[^@]*@//g;
while (s/\b([a-zA-Z_]\w*)\b/ /)
{
push (@{$used{'programs'}{$1}}, "$File::Find::name:$.");
}
}
$file->close
or die "$me: cannot close $filename: $!\n";
}
sub scan_file ()
{
return
if -f "$_.in";
my $underscore = $_;
$File::Find::name =~ s,^\./,,;
if (/\.[chlym](\.in)?$/)
{
push (@{$used{'programs'}{"cc"}}, $File::Find::name);
scan_c_file ($_);
}
elsif (/\.(cc|cpp|cxx|CC|C|hh|hpp|hxx|HH|H|yy|ypp|ll|lpp)(\.in)?$/)
{
push (@{$used{'programs'}{"c++"}}, $File::Find::name);
scan_c_file ($_);
}
elsif (/^[Mm]akefile(\.in)?$/ || /^GNUmakefile(\.in)?$/)
{
scan_makefile ($_);
}
elsif (/\.sh(\.in)?$/)
{
scan_sh_file ($_);
}
$_ = $underscore;
}
sub scan_files ()
{
find (\&scan_file, '.');
if ($verbose)
{
print "cfiles:", join(" ", @cfiles), "\n";
print "makefiles:", join(" ", @makefiles), "\n";
print "shfiles:", join(" ", @shfiles), "\n";
foreach my $kind (@kinds)
{
print "\n$kind:\n";
foreach my $word (sort keys %{$used{$kind}})
{
print "$word: @{$used{$kind}{$word}}\n";
}
}
}
}
sub output_kind ($$)
{
my ($file, $kind) = @_;
my @have;
print $file "\n# $kind_comment{$kind}\n"
if exists $kind_comment{$kind};
foreach my $word (sort keys %{$used{$kind}})
{
next
if ! exists $macro{$kind}{$word};
foreach my $macro (@{$macro{$kind}{$word}})
{
if (exists $generic_macro{$kind}
&& $macro eq $generic_macro{$kind})
{
push (@have, $word);
push (@{$needed_macros{"$generic_macro{$kind}([$word])"}},
@{$used{$kind}{$word}});
}
else
{
if (! $printed{$macro})
{
print $file "$macro\n";
$printed{$macro} = 1;
}
push (@{$needed_macros{$macro}},
@{$used{$kind}{$word}});
}
}
}
print $file "$generic_macro{$kind}([" . join(' ', sort(@have)) . "])\n"
if @have;
}
sub output_libraries ($)
{
my ($file) = @_;
print $file "\n# Checks for libraries.\n";
foreach my $word (sort keys %{$used{'libraries'}})
{
print $file "# FIXME: Replace `main' with a function in `-l$word':\n";
print $file "AC_CHECK_LIB([$word], [main])\n";
}
}
sub output ($)
{
my $configure_scan = shift;
my %unique_makefiles;
my $file = new IO::File ">$configure_scan"
or die "$me: cannot create $configure_scan: $!\n";
print $file "# Process this file with autoconf to produce a configure script.\n";
print $file "AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)\n";
if (defined $cfiles[0])
{
print $file "AC_CONFIG_SRCDIR([$cfiles[0]])\n";
print $file "AC_CONFIG_HEADER([config.h])\n";
}
output_kind ($file, 'programs');
output_kind ($file, 'makevars');
output_libraries ($file);
output_kind ($file, 'headers');
output_kind ($file, 'identifiers');
output_kind ($file, 'functions');
foreach my $m (@makefiles)
{
$m =~ s/\.in$//;
$unique_makefiles{$m}++;
}
print $file "\nAC_CONFIG_FILES([",
join ("\n ", sort keys %unique_makefiles), "])\n";
print $file "AC_OUTPUT\n";
$file->close
or die "$me: cannot close $configure_scan: $!\n";
}
sub check_configure_ac ($)
{
my ($configure_ac) = @_;
my ($trace_option) = '';
foreach my $macro (sort keys %needed_macros)
{
$macro =~ s/\(.*//;
$trace_option .= " -t $macro";
}
my $traces =
new IO::File "$autoconf -A $datadir $trace_option $configure_ac|"
or die "$me: cannot create read traces: $!\n";
while ($_ = $traces->getline)
{
chomp;
my ($file, $line, $macro, @args) = split (/:/, $_);
if ($macro =~ /^AC_CHECK_(HEADER|FUNC|TYPE|MEMBER)S$/)
{
foreach my $word (split (/\s|,/, $args[0]))
{
if ($macro eq "AC_CHECK_MEMBERS"
&& $word =~ /^stat.st_/)
{
$word = "struct " . $word;
}
delete ($needed_macros{"$macro([$word])"});
}
}
else
{
delete ($needed_macros{$macro});
}
}
$traces->close
or die "$me: cannot close: $!\n";
foreach my $macro (sort keys %needed_macros)
{
warn ("$configure_ac: warning: missing $macro wanted by: "
. (${$needed_macros{$macro}}[0])
. "\n");
print $log "$me: warning: missing $macro wanted by: \n";
foreach my $need (@{$needed_macros{$macro}})
{
print $log "\t$need\n";
}
}
}
parse_args;
find_autoconf;
my $configure_ac = find_configure_ac;
init_tables;
scan_files;
output ('configure.scan');
if ($configure_ac)
{
check_configure_ac ($configure_ac);
}
$log->close
or die "$me: cannot close $me.log: $!\n";
exit 0;