autoconf/bin/autoheader.in
Akim Demaille d7d052d158 * bin/autoheader.in: Rewrite in Perl.
* tests/autoheader: Adjust.
2001-08-31 13:32:13 +00:00

328 lines
8.6 KiB
Perl

#! @PERL@
# -*- Perl -*-
# @configure_input@
eval 'exec @PERL@ -S $0 ${1+"$@"}'
if 0;
# autoheader -- create `config.h.in' from `configure.ac'
# Copyright 1992, 1993, 1994, 1996, 1998, 1999, 2000, 2001
# Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# Written by Roland McGrath.
# Rewritten in Perl by Akim Demaille.
BEGIN
{
my $datadir = ($ENV{'autom4te_perllibdir'}
|| $ENV{'AC_MACRODIR'}
|| '@datadir@');
unshift @INC, "$datadir";
}
use Getopt::Long;
use File::Basename;
use File::Compare;
use File::Copy;
use File::Spec;
use IO::File;
use Autom4te::General;
use strict;
# Using `do FILE', we need `local' vars.
use vars qw ($config_h %verbatim %symbol);
# Lib files.
my $autoconf_dir = $ENV{"AC_MACRODIR"} || "@datadir@";
my $autoconf = $ENV{'AUTOCONF'} || '@autoconf-name@';
local $config_h;
my $config_h_in;
my $localdir = '.';
my $force = 0;
my @include;
my @warning;
# m4.
my $m4 = $ENV{"M4"} || "@M4@";
my $SIMPLE_BACKUP_SUFFIX = $ENV{'SIMPLE_BACKUP_SUFFIX'} || '~';
## ---------- ##
## Routines. ##
## ---------- ##
# $FILENAME
# find_file ($FILENAME)
# ---------------------
# We match exactly the behavior of GNU m4: first look in the current
# directory (which includes the case of absolute file names), and, if
# the file is not absolute, just fail. Otherwise, look in the path.
#
# If the file is flagged as optional (ends with `?'), then return undef
# if absent.
sub find_file ($)
{
my ($filename) = @_;
my $optional = 0;
$optional = 1
if $filename =~ s/\?$//;
return File::Spec->canonpath ($filename)
if -e $filename;
if (File::Spec->file_name_is_absolute ($filename))
{
die "$me: no such file or directory: $filename\n"
unless $optional;
return undef;
}
foreach my $path (@include)
{
return File::Spec->canonpath (File::Spec->catfile ($path, $filename))
if -e File::Spec->catfile ($path, $filename)
}
die "$me: no such file or directory: $filename\n"
unless $optional;
return undef;
}
# print_usage ()
# --------------
# Display usage (--help).
sub print_usage ()
{
print <<"END";
Usage: $0 [OPTION] ... [TEMPLATE-FILE]
Create a template file of C \`#define\' statements for \`configure\' to
use. To this end, scan TEMPLATE-FILE, or \`configure.ac\' if present,
or else \`configure.in\'.
-h, --help print this help, then exit
-V, --version print version number, then exit
-v, --verbose verbosely report processing
-d, --debug don\'t remove temporary files
-f, --force consider all the files are obsolete
-W, --warnings=CATEGORY report the warnings falling in CATEGORY
Warning categories include:
\`obsolete\' obsolete constructs
\`all\' all the warnings
\`no-CATEGORY\' turn off the warnings on CATEGORY
\`none\' turn off all the warnings
\`error\' warnings are error
Library directories:
-A, --autoconf-dir=ACDIR Autoconf\'s macro files location (rarely needed)
-l, --localdir=DIR location of \`aclocal.m4\' and \`acconfig.h\'
Report bugs to <bug-autoconf\@gnu.org>.
END
exit 0;
}
# print_version ()
# ----------------
# Display version (--version).
sub print_version
{
print <<END;
autoheader (@PACKAGE_NAME@) @VERSION@
Written by Roland McGrath.
Copyright 1992, 1993, 1994, 1996, 1998, 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.
END
exit 0;
}
# parse_args ()
# -------------
# Process any command line arguments.
sub parse_args ()
{
my $srcdir;
# F*k. Getopt seems bogus and dies when given `-' with `bundling'.
# If fixed some day, use this: '' => sub { push @ARGV, "-" }
my $stdin = grep /^-$/, @ARGV;
@ARGV = grep !/^-$/, @ARGV;
Getopt::Long::config ("bundling");
Getopt::Long::GetOptions ('A|autoconf-dir|m|macrodir=s' => \$autoconf_dir,
'l|localdir=s' => \$localdir,
'd|debug' => \$debug,
'h|help' => \&print_usage,
'V|version' => \&print_version,
'v|verbose' => \$verbose,
'f|force' => \$force,
'W|warning' => \@warning)
or exit 1;
push @ARGV, '-'
if $stdin;
if (! @ARGV)
{
my $configure_ac = find_configure_ac;
die "$me: no input file\n"
unless $configure_ac;
push @ARGV, $configure_ac;
}
}
## -------------- ##
## Main program. ##
## -------------- ##
mktmpdir ('ah');
parse_args;
# Preach.
my $config_h_top = find_file ("config.h.top?");
my $config_h_bot = find_file ("config.h.bot?");
my $acconfig_h = find_file ("acconfig.h?");
if ($config_h_top || $config_h_bot || $acconfig_h)
{
my $msg = << "END";
Using auxiliary files such as \`acconfig.h\', \`config.h.bot\'
and \`config.h.top\', to define templates for \`config.h.in\'
is deprecated and discouraged.
Using the third argument of \`AC_DEFINE\' and
\`AC_DEFINE_UNQUOTED\' allows to define a template without
\`acconfig.h\':
AC_DEFINE([NEED_MAIN], 1,
[Define if a function \`main\' is needed.])
More sophisticated templates can also be produced, see the
documentation.
END
$msg =~ s/^ /WARNING: /gm;
print STDERR $msg;
}
# Set up autoconf.
$autoconf .= " --include=$autoconf_dir --include=$localdir";
$autoconf .= ' --verbose' if $verbose;
$autoconf .= ' --debug' if $debug;
# ----------------------- #
# Real work starts here. #
# ----------------------- #
# Source what the traces are trying to tell us.
verbose "$me: running $autoconf to trace from $ARGV[0]";
xsystem ("$autoconf "
. " --trace AC_CONFIG_HEADERS:'\$\$config_h ||= \"\$1\";'"
. " --trace AH_OUTPUT:'\$\$verbatim{\$1} = \"\\\n\$2\";'"
. " --trace AC_DEFINE_TRACE_LITERAL:'\$\$symbol{\"\$1\"} = 1;'"
. " $ARGV[0] >$tmp/traces.pl");
local (%verbatim, %symbol);
do "$tmp/traces.pl";
warn "couldn't parse $tmp/traces.pl: $@" if $@;
die "$me: error: AC_CONFIG_HEADERS not found in $ARGV[0]\n"
unless $config_h;
# We template only the first CONFIG_HEADER.
$config_h =~ s/ .*//;
# Support "outfile[:infile]", defaulting infile="outfile.in".
($config_h, $config_h_in) = split (':', $config_h, 2);
$config_h_in ||= "$config_h.in";
my $out = new IO::File (">$tmp/config.hin");
# Don't write "do not edit" -- it will get copied into the
# config.h, which it's ok to edit.
print $out "/* $config_h_in. Generated from $ARGV[0] by autoheader. */\n";
# Dump the top.
# test -r $config_h.top && cat $config_h.top >>$tmp/config.hin
#
# # Dump `acconfig.h' but its bottom.
# test -r $localdir/acconfig.h &&
# sed '/@BOTTOM@/,$d;s/@TOP@//' $localdir/acconfig.h >>$tmp/config.hin
# Dump the templates from `configure.ac'.
foreach (sort keys %verbatim)
{
print $out "$verbatim{$_}\n";
}
$out->close;
# # Handle the case where @BOTTOM@ is the first line of acconfig.h.
# test -r $localdir/acconfig.h &&
# grep @BOTTOM@ $localdir/acconfig.h >/dev/null &&
# sed -n '/@BOTTOM@/,${/@BOTTOM@/!p;}' $localdir/acconfig.h >>$tmp/config.hin
# test -f $config_h.bot && cat $config_h.bot >>$tmp/config.hin
# # Check that all the symbols have a template.
# $verbose $me: checking completeness of the template >&2
# # Regexp for a white space.
# w='[ ]'
# if test -n "$syms"; then
# for sym in $syms; do
# if egrep "^#$w*[a-z]*$w$w*$sym($w*|$w.*)$" $tmp/config.hin >/dev/null; then
# : # All is well.
# else
# echo "$me: No template for symbol \`$sym'" >&2
# status=1
# fi
# done
# fi
if (compare ("$tmp/config.hin", "$config_h_in") == 0)
{
# File didn't change, so don't update its mod time.
print STDERR "$me: `$config_h_in' is unchanged\n"
}
else
{
# Back up and install the new one.
if (-f $config_h_in)
{
move ("$config_h_in", "$config_h_in$SIMPLE_BACKUP_SUFFIX")
or die "$me: cannot not backup $config_h_in: $!\n";
}
move ("$tmp/config.hin", "$config_h_in")
or die "$me: cannot not update $config_h_in: $!\n";
# print STDERR "$me: `$config_h_in' is updated\n";
}
__END__
(exit $status); exit $status