diff options
author | Trent Piepho <xyzzy@speakeasy.org> | 2007-02-23 02:09:33 -0800 |
---|---|---|
committer | Trent Piepho <xyzzy@speakeasy.org> | 2007-02-23 02:09:33 -0800 |
commit | c34c53638fecfe18f59fb63819ac79bb8090bc34 (patch) | |
tree | c7a9ad6567d59149ff7af0d04cadd127bbe83c00 /v4l | |
parent | fc36114f0e0a850ab5d6caa079c98a3e4d95565b (diff) | |
download | mediapointer-dvb-s2-c34c53638fecfe18f59fb63819ac79bb8090bc34.tar.gz mediapointer-dvb-s2-c34c53638fecfe18f59fb63819ac79bb8090bc34.tar.bz2 |
Largely re-write gentree.pl
From: Trent Piepho <xyzzy@speakeasy.org>
Mostly re-write gentree.pl to make a few improvements:
About three times faster
Correctly processes BTTV_VERSION_CODE checks
Properly handles #elif
Doesn't need absolute path names specified in command line arguments
Improved debug output, which now shows input line number
Can handle almost any general expression for a conditional compilation
directive. Can understand all these:
#if 0
#ifdef BROKEN_XAWTV
#ifndef MM_KERNEL
#if defined(STV0297_CS2)
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,20)
#elif defined(MM_KERNEL) || !BROKEN_XAWTV
#if BTTV_VERSION_CODE > KERNEL_VERSION(0,8,0) || defined(BROKEN_XAWTV)
#if (1 > 2) || (I2C_PEC && defined(I2C_CLASS_TV_ANALOG))
#ifndef CONFIG_XC3028 /* Keep */ <- knows to keep this one in the code
Signed-off-by: Trent Piepho <xyzzy@speakeasy.org>
Diffstat (limited to 'v4l')
-rwxr-xr-x | v4l/scripts/gentree.pl | 482 |
1 files changed, 165 insertions, 317 deletions
diff --git a/v4l/scripts/gentree.pl b/v4l/scripts/gentree.pl index 1b4e44f26..2f918468e 100755 --- a/v4l/scripts/gentree.pl +++ b/v4l/scripts/gentree.pl @@ -1,45 +1,93 @@ #!/usr/bin/perl +# +# Original version by Mauro Carvalho Chehab +# Largely re-written (C) 2007 Trent Piepho <xyzzy@speakeasy.org> +# +# Theory of Operation +# +# This acts as a sort of mini version of cpp, which will process +# #if/#elif/#ifdef/etc directives to strip out code used to support +# multiple kernel versions or otherwise not wanted to be sent upstream to +# git. +# +# Conditional compilation directives fall into two catagories, +# "processed" and "other". The "other" directives are ignored and simply +# output as they come in without changes (see 'keep' exception). The +# "processed" variaty are evaluated and only the lines in the 'true' part +# are kept, like cpp would do. +# +# If gentree knows the result of an expression, that directive will be +# "processed", otherwise it will be an "other". gentree knows the value +# of LINUX_VERSION_CODE, BTTV_VERSION_CODE, the KERNEL_VERSION(x,y,z) +# macro, numeric constants like 0 and 1, and a few defines like MM_KERNEL +# and STV0297_CS2. +# +# An exception is if the comment "/*KEEP*/" appears after the expression, +# in which case that directive will be considered an "other" and not +# processed, other than to remove the keep comment. +# +# Known bugs: +# don't specify the root directory e.g. '/' or even '////' +# directives continued with a back-slash will always be ignored +# you can't modify a source tree in-place, i.e. source dir == dest dir + use strict; use File::Find; use Fcntl ':mode'; -my $VER = shift; +my $VERSION = shift; my $SRC = shift; my $DESTDIR = shift; -my $extra; - - -#$VER = "2.6.14" if !defined($VER); -#$SRC = "../linux" if !defined($SRC); -#$DESTDIR = "/tmp/linux" if !defined($DESTDIR); if (!defined($DESTDIR)) { print "Usage:\ngentree.pl\t<version> <source dir> <dest dir>\n\n"; exit; } -my ($VERSION,$CODE) = &kernel_version; +my $BTTVCODE = KERNEL_VERSION(0,9,17); +my ($LINUXCODE, $extra) = kernel_version($VERSION); my $DEBUG = 0; +my %defs = ( + 'LINUX_VERSION_CODE' => $LINUXCODE, + 'BTTV_VERSION_CODE' => $BTTVCODE, + '_COMPAT_H' => 0, + 'MM_KERNEL' => ($extra =~ /-mm/)?1:0, + 'BROKEN_XAWTV' => 0, + 'STV0297_CS2' => 0, + 'HAVE_VIDEO_BUF_DVB' => 1, + 'I2C_PEC' => 1, + 'I2C_DF_DUMMY' => 0, + 'CONFIG_XC3028' => 0, + 'I2C_CLASS_TV_ANALOG' => 1, + 'I2C_CLASS_TV_DIGITAL' => 1); ################################################################# # helpers -sub kernel_version() { - my ($ver,$code); - - $code = 0; - $ver = $VER; - - $ver =~ m/(\d)\.(\d)\.([0-9]*(.*))/; - my $v1 = $1; - my $v2 = $2; - my $v3 = $3; - $extra = $4; - - $code = $v1*65536 + $v2*256 + $v3; +sub kernel_version($) { + $_[0] =~ m/(\d+)\.(\d+)\.(\d+)(.*)/; + return ($1*65536 + $2*256 + $3, $4); +} - return ($ver,$code); +# used in eval() +sub KERNEL_VERSION($$$) { return $_[0]*65536 + $_[1]*256 + $_[2]; } + +sub evalexp($) { + local $_ = shift; + s|/\*.*?\*/||go; # delete /* */ comments + s|//.*$||o; # delete // comments + s/\bdefined\s*\(/(/go; # defined(foo) to (foo) + while (/\b([_A-Za-z]\w*)\b/go) { + if (exists $defs{$1}) { + my $id = $1; my $pos = $-[0]; + s/$id/$defs{$id}/; + pos = $-[0]; + } elsif ($1 ne 'KERNEL_VERSION') { + return(undef); + } + } + return(eval($_) ? 1 : 0); } ################################################################# @@ -47,298 +95,116 @@ sub kernel_version() { sub filter_source ($$) { my ($in,$out) = @_; - my ($line,$mmkernel); + my $line; my $level=0; my %if = (); - my %state =(); - my $print=0; + my %state = (); - if ($extra =~ m/\-mm/) { - $mmkernel = 1; - } else { - $mmkernel = 0; + my @dbgargs = \($level, %state, %if, $line); + sub dbgline($\@) { + my $level = ${$_[1][0]}; + printf STDERR ("/* BP %4d $_[0] state=$_[1][1]->{$level} if=$_[1][2]->{$level} level=$level (${$_[1][3]}) */\n", $.) if $DEBUG; } - open IN, "<$in"; - open OUT, ">$out"; + open IN, '<', $in or die "Error opening $in: $!\n"; + open OUT, '>', $out or die "Error opening $out: $!\n"; - print STDERR "File: $in, for kernel $VERSION($CODE)/\n" if $DEBUG; + print STDERR "File: $in, for kernel $VERSION($LINUXCODE)/\n" if $DEBUG; while ($line = <IN>) { - if ($line =~ m/^#include \"compat.h\"/) { - next; - } -# if ($line =~ m/[\$]Id:/) { -# next; -# } - if ($line =~ /^\s*#\s*ifdef\s+MM_KERNEL/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = $mmkernel; - print STDERR "/* BP #if MM_KERNEL state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*ifdef\s+_COMPAT_H/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 0; - print STDERR "/* BP #if MM_KERNEL state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ m|^\s*\#\s*if\s+0\s*\/[\*]\s*[Kk][Ee].[Pp]\s*[\*]\/(.*)|) { - if ($1 eq ";") { - print OUT "#if 0\n"; - } else { - print OUT "#if 0$1\n"; - } - chomp($line); + chomp $line; + next if ($line =~ m/^#include \"compat.h\"/o); +# next if ($line =~ m/[\$]Id:/); + + # For "#if 0 /*KEEP*/;" the ; should be dropped too + if ($line =~ m@^\s*#\s*if(n?def)?\s.*?(\s*/\*\s*(?i)keep\s*\*/;?)@) { $state{$level} = "ifother"; - $if{$level} = 0; - print STDERR "/* BP #if 0 (keep) state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*ifdef\s+STV0297_CS2/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 0; - print STDERR "/* BP #if 0 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*ifndef\s+BROKEN_XAWTV/) { - chomp($line); - $state{$level} = "if"; $if{$level} = 1; - print STDERR "/* BP #if 0 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; + dbgline "#if$1 (keep)", @dbgargs; + $line =~ s/\Q$2\E//; $level++; - next; } - if ($line =~ /^\s*#\s*ifndef\s+STV0297_CS2/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 1; - print STDERR "/* BP #if 1 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*if\s+0/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 0; - print STDERR "/* BP #if 0 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ m|^\s*\#\s*if\s+1\s*\/[\*]\s*[Kk][Ee].[Pp]\s*[\*]\/(.*)|) { - print OUT "#if 1$1\n"; + # handle all ifdef/ifndef lines + elsif ($line =~ /^\s*#\s*if(n?)def\s*(\w+)/o) { + if (exists $defs{$2}) { + $state{$level} = 'if'; + $if{$level} = ($1 eq 'n') ? !$defs{$2} : $defs{$2}; + dbgline "#if$1def $2", @dbgargs; + $level++; + next; + } $state{$level} = "ifother"; $if{$level} = 1; - print STDERR "/* BP #if 1 (keep) state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; + dbgline "#if$1def (other)", @dbgargs; $level++; - next; } - if ($line =~ /^\s*#\s*if\s+1/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 1; - print STDERR "/* BP #if 1 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*if.*HAVE_VIDEO_BUF_DVB/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 1; - print STDERR "/* BP #if 1 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*if.*CONFIG_XC3028/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 0; - print STDERR "/* BP #if 1 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*if.*I2C_CLASS_TV_ANALOG/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 1; - print STDERR "/* BP #if 1 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*ifndef.\s*I2C_DF_DUMMY/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 1; - print STDERR "/* BP #if 1 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*ifndef\s+I2C_PEC/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 0; - print STDERR "/* BP #if 1 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*ifdef\s+I2C_PEC/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 1; - print STDERR "/* BP #if 1 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*if.*I2C_CLASS_TV_DIGITAL/) { - chomp($line); - $state{$level} = "if"; - $if{$level} = 1; - print STDERR "/* BP #if 1 state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*if.*BTTV_VERSION_CODE/) { - chomp($line); - $line =~ s@^#if\s*@@; - $line =~ s@BTTV_VERSION_CODE@\$CODE@; - $line =~ s@KERNEL_VERSION\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)@ - sprintf("%d",$1*65536 + $2*256 + $3) @eg; - $if{$level} = eval $line; - $state{$level} = "if"; - print STDERR "/* BP #if BTTV_VERSION_CODE state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*if.*LINUX_VERSION_CODE/) { - chomp($line); - $line =~ s@^#if\s*@@; - $line =~ s@LINUX_VERSION_CODE@\$CODE@g; - print STDERR "/* BP orig: ($line) */\n" if $DEBUG; - - $line =~ s@KERNEL_VERSION\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)@ - sprintf("%d",$1*65536 + $2*256 + $3) @eg; - $if{$level} = eval $line; - $state{$level} = "if"; - print STDERR "/* BP #if LINUX_VERSION_CODE state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*elif.*LINUX_VERSION_CODE/) { - chomp($line); - if ( !$level ) { - die "elif without if"; + # handle all ifs + elsif ($line =~ /^\s*#\s*if\s+(.*)$/o) { + my $res = evalexp($1); + if (defined $res) { + $state{$level} = 'if'; + $if{$level} = $res; + dbgline '#if '.($res?'(yes)':'(no)'), @dbgargs; + $level++; + next; + } else { + $state{$level} = 'ifother'; + $if{$level} = 1; + dbgline '#if (other)', @dbgargs; + $level++; } + } + # handle all elifs + elsif ($line =~ /^\s*#\s*elif\s+(.*)$/o) { + my $exp = $1; $level--; - if ( $if{$level} ) { - # Else is 0 + $level < 0 and die "more elifs than ifs"; + $state{$level} =~ /if/ or die "unmatched elif"; + + if ($state{$level} eq 'if' && !$if{$level}) { + my $res = evalexp($exp); + defined $res or die 'moving from if to ifother'; + $state{$level} = 'if'; + $if{$level} = $res; + dbgline '#elif1 '.($res?'(yes)':'(no)'), @dbgargs; + $level++; + next; + } elsif ($state{$level} ne 'ifother') { $if{$level} = 0; - $state{$level} = "elif"; - print STDERR "/* BP #if LINUX_VERSION_CODE state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - + $state{$level} = 'elif'; + dbgline '#elif0', @dbgargs; $level++; next; } - - $line =~ s@^#if\s*@@; - $line =~ s@LINUX_VERSION_CODE@\$CODE@g; - print STDERR "/* BP orig: ($line) */\n" if $DEBUG; - - $line =~ s@KERNEL_VERSION\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)@ - sprintf("%d",$1*65536 + $2*256 + $3) @eg; - $if{$level} = eval $line; - $state{$level} = "if"; - print STDERR "/* BP #if LINUX_VERSION_CODE state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - next; - } - if ($line =~ /^\s*#\s*if\s/) { - $state{$level} = "ifother"; - $if{$level} = 1; - print STDERR "/* BP $line state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - $level++; - } - - if ($line =~ /^\s*#\s*ifdef/) { - $state{$level} = "ifother"; - $if{$level} = 1; - print STDERR "/* BP $line state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; $level++; } - - if ($line =~ /^\s*#\s*ifndef/) { - $state{$level} = "ifother"; - $if{$level} = 1; - print STDERR "/* BP $line state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; + elsif ($line =~ /^\s*#\s*else/o) { + $level--; + $level < 0 and die "more elses than ifs"; + $state{$level} =~ /if/ or die "unmatched else"; + $if{$level} = !$if{$level} if ($state{$level} eq 'if'); + $state{$level} =~ s/^if/else/o; # if -> else, ifother -> elseother, elif -> elif + dbgline '#else', @dbgargs; $level++; + next if $state{$level-1} !~ /other$/o; } - - if ($level) { - if ($state{$level-1} eq "if" && $line =~ /^\s*#\s*else/) { - $state{$level-1} = "else"; - print STDERR "/* BP #else state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - next; - } - } - if ($line =~ /^\s*#\s*endif/) { - if ($level) { - $level--; - - print STDERR "/* BP #endif state=$state{$level} if=$if{$level} level=$level ($line) */\n" if $DEBUG; - } else { - die "more endifs than ifs"; - } - if ($state{$level} ne "ifother") { - next; - } - } - if ($level == 0) { - print OUT $line; - } else { - my $i=0,$print=1; - for ($i=0;$i<$level;$i++) { - if (!($state{$i} eq "if" && $if{$i} || - $state{$i} eq "else" && !$if{$i}) && - $state{$i} ne "ifother" ) { - $print=0; - chomp($line); - print STDERR "/* BP DEL $line state=$state{$i} if=$if{$i} level=$i */\n" if $DEBUG; - } - } - - if ($print) { - print OUT $line; + elsif ($line =~ /^\s*#\s*endif/o) { + $level--; + $level < 0 and die "more endifs than ifs"; + dbgline '#endif', @dbgargs; + next if $state{$level} !~ /other$/o; + } + + my $print = 1; + for (my $i=0;$i<$level;$i++) { + next if $state{$i} =~ /other$/o; # keep code in ifother/elseother blocks + if (!$if{$i}) { + $print = 0; + dbgline 'DEL', @{[\$i, \%state, \%if, \$line]}; + last; } } - } - close IN; - close OUT; -} - -sub filter_dest ($$) { - my ($in,$out) = @_; - my ($line,$if,$state,$mmkernel); - - if ($extra =~ "/\-mm/") { - $mmkernel = 1; - } else { - $mmkernel = 0; - } - - open IN, "<$in"; - open OUT, ">$out"; - - while ($line = <IN>) { - if ($line =~ m/[\$]Id:.*/) { - next; - } - print OUT $line; + print OUT "$line\n" if $print; } close IN; close OUT; @@ -348,54 +214,36 @@ sub filter_dest ($$) { sub parse_dir { my $file = $File::Find::name; - my $srcdir=$SRC; - if ($file =~ /CVS/) { - return; - } - - if ($file =~ /\~$/) { - return; - } + return if ($file =~ /CVS/); + return if ($file =~ /~$/); - my $mode = (lstat("$file"))[2]; + my $f2 = $file; + $f2 =~ s/^\Q$SRC\E/$DESTDIR/; + my $mode = (stat($file))[2]; if ($mode & S_IFDIR) { + print("mkdir -p '$f2'\n"); + system("mkdir -p '$f2'"); # should check for error return; } - - $srcdir =~ s/(.)/\[$1\]/g; - my $f2 = $file; - - $f2 =~ s,^$srcdir,$DESTDIR/,; print "from $file to $f2\n"; - my $tmp = "/tmp/src.$$"; if ($file =~ m/.*\.[ch]$/) { - filter_source("$file","$tmp"); + filter_source($file, $f2); } else { - system("cp $file $tmp"); + system("cp $file $f2"); } - - my $dir = $f2; - $dir =~ s,(.*)[/][^/]*$,$1,; - - print("mkdir -p $dir\n"); - system("mkdir -p $dir\n"); - system("cp $tmp $f2"); - unlink $tmp; } # main -my $patchtmploc = "/tmp/temp.patch"; +printf "kernel is %s (0x%x)\n",$VERSION,$LINUXCODE; -printf <<EOF,$VER,$CODE; -kernel is %s (0x%x) -EOF +# remove any trailing slashes from dir names. don't pass in just '/' +$SRC =~ s|/*$||; $DESTDIR =~ s|/*$||; print "finding files at $SRC\n"; - -find(\&parse_dir, $SRC); +find({wanted => \&parse_dir, no_chdir => 1}, $SRC); |