source: rtems/contrib/crossrpms/specstrip @ 69ad4a4b

4.104.115
Last change on this file since 69ad4a4b was 86018813, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/20/10 at 06:57:46

Filter constant conditionals.

  • Property mode set to 100755
File size: 4.9 KB
Line 
1#!/usr/bin/perl -w
2
3
4# Helper script to strip unused parts out of crossrpms's rpm.specs
5#
6# Usage: specstrip < infile > outfile
7
8
9# Copyright (C) 2005,2006,2010  Ralf Corsépius, Ulm, Germany,
10#
11# This program is free software; you can redistribute it and/or
12# modify it under the terms of the GNU General Public License
13# as published by the Free Software Foundation; either version 2
14# of the License, or (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License for more details.
20#
21# For a copy of the GNU General Public License, visit
22# http://www.gnu.org or write to the Free Software Foundation, Inc.,
23# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24
25# $Id$
26
27use Getopt::Long;
28
29use strict;
30
31my $newlib = 0;
32my $infos = 0;
33my $prefix = '/usr';
34
35my $verbose = 0;
36my @languages = ();
37my %options = ();
38
39GetOptions(
40  \%options,
41  'prefix=s' => \$prefix,
42  'enable-infos' => sub { $options{infos} = 1 },
43  'disable-infos' => sub { $options{infos} = 0 },
44  'newlib!',
45  'languages=s' => \@languages,
46  'verbose+' => \$verbose
47) or die( "failed to GetOptions" );
48
49if ( !defined($options{infos}) )
50{ # User did not override, use defaults
51  if ( $prefix =~ m/^\/usr$/ ) {
52    $infos = 0;
53  } elsif ( $prefix =~ m/^\/usr\/local$/ ) {
54    $infos = 0;
55  } else {
56    $infos = 1;
57  }
58} else {
59  $infos = int($options{infos});
60}
61
62if ( defined($options{newlib}) )
63{
64  $newlib = $options{newlib};
65} else {
66  $newlib = 0;
67}
68
69if ( $verbose ) {
70  print STDERR "INFOS  : $infos\n";
71  print STDERR "PREFIX : $prefix\n";
72}
73
74my %langs;
75
76foreach ( split(/,/,join(',',@languages)) ){
77  $langs{$_} = 1;
78}
79
80my @condstack ;
81my @actionstack ;
82
83push @condstack,'<>';
84
85my @npatterns = (
86  "(\"%\{_prefix\}\" (!=|==) \"/usr\")",
87
88  "(%build_cxx)",
89  "(%build_gnat)",
90  "(%build_objc)",
91  "(%build_gcj)",
92  "(%build_libgcj)",
93  "(%build_fortran)",
94  "(%build_newlib)",
95  "(%build_infos)"
96);
97
98my @ppatterns = (
99);
100
101push @ppatterns,  "(\"%\{_prefix\}\" " . (("$prefix" eq '/usr') ? '!=' : '==' ) . " \"/usr\")";
102
103push @ppatterns, "(%build_gnat "   . ( ($langs{gnat}) ? "==" : "!=" ) . " 0)";
104push @ppatterns, "(%build_cxx "    . ( ($langs{cxx}) ? "==" : "!=" ) . " 0)";
105push @ppatterns, "(%build_objc "   . ( ($langs{objc}) ? "==" : "!=" ) . " 0)";
106push @ppatterns, "(%build_gcj "    . ( ($langs{gcj}) ? "==" : "!=" ) . " 0)";
107push @ppatterns, "(%build_libgcj " . ( ($langs{libgcj}) ? "==" : "!=" ) . " 0)";
108push @ppatterns, "(%build_fortran "    . ( ($langs{fortran}) ? "==" : "!=" ) . " 0)";
109
110push @ppatterns, "(%build_newlib " . ( ($newlib) ? "==" : "!=" ) . " 0)";
111push @ppatterns, "(%build_infos " . ( ($infos) ? "==" : "!=" ) . " 0)";
112
113my $npat = join('|',@npatterns);
114my $ppat = join('|',@ppatterns);
115
116if ( $verbose > 1 ) {
117  print STDERR "PPAT: ", $ppat, "\n";
118  print STDERR "NPAT: ", $npat, "\n";
119}
120
121my @buffer0 = <> ;
122
123my @buffer2 ;
124foreach (@buffer0)
125{
126   chomp $_;
127   if ( /^%if(os|)\s+(.*)$/ )
128   {
129     push @condstack,"<$2>";
130     if ( $condstack[$#condstack] =~ m/$npat/ ) {
131       # transform unary conditionals into binary conditionals
132       if ( $condstack[$#condstack] =~/.*<(%[a-zA-Z_0-9]+)>.*/ ) {
133         $condstack[$#condstack] = "<$1 != 0>";
134       }
135     } else {
136       push @buffer2, { state => join('',@condstack), line => "$_" };
137     }
138   } elsif ( /^%else.*$/ )
139   {
140     my %ops = (
141         "!=" => "==",
142         "==" => "!="
143       );
144
145     if ( $condstack[$#condstack] =~/.*<(.*) (!=|==) (.*)>.*/ ) {
146       $condstack[$#condstack] = "<$1 " .  $ops{$2} . " $3>";
147       if ( $condstack[$#condstack] =~ m/$npat/ ) {
148       } else {
149         push @buffer2, { state => join('',@condstack), line => "$_" };
150       }
151     } else {
152         push @buffer2, { state => join('',@condstack), line => "$_" };
153     }
154   } elsif ( /^%endif.*$/ )
155   {
156     if ( $condstack[$#condstack] =~ m/$npat/ ) {
157     } else {
158       push @buffer2, { state => join('',@condstack), line => "$_" };
159     }
160     pop @condstack;
161   } else {
162     push @buffer2, { state => join('',@condstack), line => "$_" };
163   }
164}
165
166my @buffer3;
167foreach my $i ( @buffer2 )
168{
169  print STDERR $i->{state}, $i->{line}, "\n" if $verbose > 1;
170  if ( $i->{state} =~ m/($ppat)/ ) {
171  } elsif ( $i->{state} =~ m/.*<"([a-zA-Z_0-9\.\-]+)" (!=|==) "([a-zA-Z_0-9\.\-]+)">.*/ ) {
172  # Filter out constant conditionals
173    if ( "$2" eq "==" ) {
174      if ( "$1" eq "$3" ) {
175        if ( $i->{line} =~ m/^%(if|else|endif).*$/ ) {
176        } else {
177          push @buffer3, $i->{line}, "\n";
178        }
179      }
180    } elsif ( "$2" eq "!=" ){
181      if ( "$1" ne "$3" ) {
182        if ( $i->{line} =~ m/^%(if|else|endif).*$/ ) {
183        } else {
184          push @buffer3, $i->{line}, "\n";
185        }
186      }
187    } else {
188      die "invalid condition: $i->{state}";
189    }
190  } else {
191    push @buffer3, $i->{line}, "\n"
192  }
193}
194
195print STDOUT @buffer3;
Note: See TracBrowser for help on using the repository browser.