scripts/mk_autoconf.pl: overhaul

* use named variables instead of $_
* also create Makefile-snippet scripts/autoconf.mk
* nicer formatted variables assignments: values align
* fail on lines that do not match any exsting pattern
* stricter parsing of strings in assignments: quotes need to match
* treat FALSE boolean variable assignments the same as unset variables
* more flexible parsing of boolean value assigments:
  mixed cased: y/n, yes/no, true/false, on/off
* more flexible parsing of numerical value assigments:
  allow negative values by accepting a leading minus sign

The goal of creating the Makefile-snippet 'scripts/autoconf.mk' is to use
it instead of '.config' if the top-level Makefile in order to avoid the
issues with string variables from the shell script snippet '.config' when
used in Makefiles: in Makefiles quotes around variable values do not get
stripped but are taken literally.
This commit is contained in:
Peter Marschall 2012-04-19 22:46:29 +02:00
parent ebd403b0ff
commit 18f0186ccd
1 changed files with 60 additions and 32 deletions

View File

@ -1,50 +1,78 @@
#!/usr/bin/perl
# GPL Paul Slootman <paul@wurtel.demon.nl> 1998
# Peter Marschall <peter@adpm.de> 2012
#
# script to convert .config as generated by 'make menuconfig' into
# scripts/autoconf.h , so that one can edit .config by hand and then
# non-interactively create a configuration ready for building. Useful
# for package building (I'm thinking Debian or Red Hat here).
# scripts/autoconf.h ans scripts/autoconf.mk, so that one can edit
# .config by hand and then non-interactively create a configuration
# ready for building. Useful for package building (I'm thinking Debian
# or Red Hat here).
open(IN, "<.config") || die "Can't open .config: $!\n";
open(OUT, ">scripts/autoconf.h") || die "Can't open scripts/autoconf.h: $!\n";
open(IN, "<.config") or die "Can't open .config: $!\n";
open(OUT_H, ">scripts/autoconf.h") or die "Can't open scripts/autoconf.h: $!\n";
open(OUT_MK, ">scripts/autoconf.mk") or die "Can't open scripts/autoconf.mk: $!\n";
while (<IN>) {
chop;
next if /^#\s*$/; # empty comment line; ignore
if (/^\s*$/) { # empty line; echo
print OUT "\n";
next;
while (my $line = <IN>) {
chomp($line);
if ($line =~ /^#\s*$/o) { # empty comment line; ignore for autoconf.h
printf OUT_MK "%s\n", $line;
}
if (/^# ([^\s]+) is not set/) { # unset variables
print OUT "#undef $1\n";
next;
elsif ($line =~ /^\s*$/o) { # empty line; echo
print OUT_H "\n";
printf OUT_MK "%s\n", $line;
}
if (/^#\s+([^\s].*)\s*/) { # comment
print OUT "/*\n * $1\n */\n";
print OUT "#define AUTOCONF_INCLUDED\n" if /Automatically generated by/;
next;
elsif ($line =~ /^# ([^\s]+) is not set/o) { # unset variables
my $var = $1;
printf OUT_H "#undef %s\n", $var;
printf OUT_MK "%s\n", $line;
}
if (/(\w+)=(.*)/) { # assignment
$var = $1;
$val = $2;
if ($val eq 'y') { # boolean true value
print OUT "#define $var 1\n";
next;
elsif ($line =~ /^#\s+([^\s].*)\s*/o) { # comment
my $comment = $1;
$line =~ s/(make menuconfig)/'$1' and '$0'/
if ($line =~/Automatically generated by/);
printf OUT_H "/*\n * %s\n */\n", $comment;
print OUT_H "#define AUTOCONF_INCLUDED\n"
if ($line =~/Automatically generated by/);
printf OUT_MK "# %s\n", $comment;
}
elsif ($line =~ /(\w+)=(.*)/o) { # assignment
my ($var,$val) = ($1,$2);
if ($val =~ /^(?:y|yes|true|on)$/io) { # boolean TRUE value
printf OUT_H "#define %-32s 1\n", $var;
printf OUT_MK "%-32s = y\n", $var;
}
if ($val =~ /^['"](.*)["']$/) { # string value
print OUT "#define $var \"$1\"\n";
next;
elsif ($val =~ /^(?:n|no|false|off)$/io) { # boolean FALSE value; like unset variable
printf OUT_H "#undef %s\n", $var;
printf OUT_MK "# %s is not set\n", $var;
}
if ($val =~ /^(\d+)$/) { # numeric value
print OUT "#define $var ($1)\n";
next;
elsif ($val =~ /^(['"])(.*)\1$/o) { # string value
my $str = $2;
printf OUT_H "#define %-32s \"%s\"\n", $var, $str;
printf OUT_MK "%-32s = %s\n", $var, $str;
}
die "Unexpected input at line $.: $_\n";
elsif ($val =~ /^(-?\d+)$/) { # numeric value
my $num = $1;
printf OUT_H "#define %-32s (%s)\n", $var, $num;
printf OUT_MK "%-32s = %s\n", $var, $num;
}
else {
die "Unexpected input in .config at line $.: $line\n";
}
}
else {
die "Unexpected input in .config at line $.: $line\n";
}
}
close(IN);
close(OUT);
close(OUT_H);
close(OUT_MK);
exit(0);