source: rtems/contrib/crossrpms/specstrip @ c062c295

4.115
Last change on this file since c062c295 was 60d34868, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/02/11 at 06:10:53

Add %build_go.
Cosmetics.

  • Property mode set to 100755
File size: 6.5 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 @npatterns = (
81  "(\"%\{_prefix\}\" (!=|==) \"/usr\")",
82
83  "(%build_cxx)",
84  "(%build_fortran)",
85  "(%build_gcj)",
86  "(%build_gnat)",
87  "(%build_go)",
88  "(%build_libgcj)",
89  "(%build_newlib)",
90  "(%build_objc)",
91  "(%build_infos)"
92);
93
94my @ppatterns = (
95);
96
97push @ppatterns,  "(\"%\{_prefix\}\" " . (("$prefix" eq '/usr') ? '!=' : '==' ) . " \"/usr\")";
98
99push @ppatterns, "(%build_cxx "         . ( ($langs{cxx}) ? "==" : "!=" ) . " 0)";
100push @ppatterns, "(%build_fortran "     . ( ($langs{fortran}) ? "==" : "!=" ) . " 0)";
101push @ppatterns, "(%build_objc "        . ( ($langs{objc}) ? "==" : "!=" ) . " 0)";
102push @ppatterns, "(%build_gcj "         . ( ($langs{gcj}) ? "==" : "!=" ) . " 0)";
103push @ppatterns, "(%build_gnat "        . ( ($langs{gnat}) ? "==" : "!=" ) . " 0)";
104push @ppatterns, "(%build_go "          . ( ($langs{go}) ? "==" : "!=" ) . " 0)";
105push @ppatterns, "(%build_libgcj "      . ( ($langs{libgcj}) ? "==" : "!=" ) . " 0)";
106
107push @ppatterns, "(%build_newlib "      . ( ($newlib) ? "==" : "!=" ) . " 0)";
108push @ppatterns, "(%build_infos "       . ( ($infos) ? "==" : "!=" ) . " 0)";
109
110my $npat = join('|',@npatterns);
111my $ppat = join('|',@ppatterns);
112
113if ( $verbose > 1 ) {
114  print STDERR "PPAT: ", $ppat, "\n";
115  print STDERR "NPAT: ", $npat, "\n";
116}
117
118my @buffer0 = <> ;
119
120my @buffer2 ;
121
122my @condstack ;
123
124@condstack = ();
125push @condstack,'<>';
126foreach (@buffer0)
127{
128   chomp $_;
129   if ( /^%if(os|)\s+(.*)$/ )
130   {
131     push @condstack,"<$2>";
132     if ( $condstack[$#condstack] =~ m/$npat/ ) {
133       # transform unary conditionals into binary conditionals
134       if ( $condstack[$#condstack] =~/.*<(%[a-zA-Z_0-9]+)>.*/ ) {
135         $condstack[$#condstack] = "<$1 != 0>";
136       }
137     } else {
138       push @buffer2, { state => join('',@condstack), line => "$_" };
139     }
140   } elsif ( /^%else.*$/ )
141   {
142     my %ops = (
143         "!=" => "==",
144         "==" => "!="
145       );
146
147     if ( $condstack[$#condstack] =~/.*<(.*) (!=|==) (.*)>.*/ ) {
148       $condstack[$#condstack] = "<$1 " .  $ops{$2} . " $3>";
149       if ( $condstack[$#condstack] =~ m/$npat/ ) {
150       } else {
151         push @buffer2, { state => join('',@condstack), line => "$_" };
152       }
153     } else {
154         push @buffer2, { state => join('',@condstack), line => "$_" };
155     }
156   } elsif ( /^%endif.*$/ )
157   {
158     if ( $condstack[$#condstack] =~ m/$npat/ ) {
159     } else {
160       push @buffer2, { state => join('',@condstack), line => "$_" };
161     }
162     pop @condstack;
163   } else {
164     push @buffer2, { state => join('',@condstack), line => "$_" };
165   }
166}
167
168my @buffer3;
169foreach my $i ( @buffer2 )
170{
171  print STDERR "STATE:", $i->{state}, " LINE:", $i->{line}, "\n" if $verbose > 1;
172  if ( $i->{state} =~ m/($ppat)/ ) {
173  } else {
174          push @buffer3, $i->{line};
175  }
176}
177
178#foreach my $line ( @buffer3 )
179#{
180#  print STDERR "L:<$line>\n";
181#}
182
183my @buffer4;
184@condstack = ();
185push @condstack, "<>";
186foreach my $line ( @buffer3 )
187{
188#  print STDERR "READ:{", $line, "}\n";
189  if ( $line =~/^%if\s+"([a-zA-Z_0-9\.\-]+)"\s+==\s+"([a-zA-Z_0-9\.\-]+)"\s*$/ )
190  {
191    if ( "$1" eq "$2" ) {
192      push @condstack,"<TRUE:$1 == $2>";
193    } else {
194      push @condstack,"<FALSE:$1 == $2>";
195    }
196  } elsif ( $line =~/^%if\s+"([a-zA-Z_0-9\.\-]+)"\s+!=\s+"([a-zA-Z_0-9\.\-]+)"\s*$/ )
197  {
198    if ( "$1" ne "$2" ) {
199      push @condstack,"<TRUE:$1 != $2>";
200    } else {
201      push @condstack,"<FALSE:$1 != $2>";
202    }
203  } elsif ( $line =~/^%if\s+(.*)\s*$/ )
204  {
205    my $exp = $1;
206    push @condstack,"<IFOT:$exp>";
207    push @buffer4, "@condstack:$line\n";
208  } elsif ( $line =~/^%if((os|narch)\s+.*)\s*$/ )
209  {
210    my $exp = $1;
211    push @condstack,"<IFOT:$exp>";
212    push @buffer4, "@condstack:$line\n";
213  } elsif ( $line =~ /^%else\s*$/ ) {
214    if ( $condstack[$#condstack] =~ m/<TRUE:(.*)\s*>$/ ) {
215      $condstack[$#condstack] = "<FALSE:$1>";
216    } elsif ( $condstack[$#condstack] =~ m/<FALSE:(.*)\s*>$/ ) {
217      $condstack[$#condstack] = "<TRUE:$1>";
218    } else {
219      push @buffer4, "@condstack:$line\n";
220    }
221  } elsif ( $line =~ /^%endif\s*$/ ) {
222
223    if ( $condstack[$#condstack] =~ m/<TRUE:.*>$/ ) {
224#    print STDERR "ENDIF: TRUE\n";
225    } elsif ( $condstack[$#condstack] =~ m/<FALSE:.*>$/ ) {
226#    print STDERR "ENDIF: FALSE\n";
227    } else {
228      push @buffer4, "@condstack:$line\n";
229    }
230#    print STDERR "POP: $line\n";
231    pop @condstack;
232  } else {
233#  print STDERR "CATCH $condstack[$#condstack]:$line\n";
234    if ( $condstack[$#condstack] =~ m/<TRUE:.*>$/ ) {
235      push @buffer4, "@condstack:$line\n";
236    } elsif ( $condstack[$#condstack] =~ m/<FALSE:.*>$/ ) {
237    } else {
238      push @buffer4, "@condstack:$line\n";
239    }
240  }
241
242#  print STDERR @condstack, "LINE: $line\n";
243}
244
245print STDERR @buffer4 if $verbose > 2;
246
247foreach my $line (@buffer4) {
248  if ( $line =~ /^(<.*>):(.*)$/ ) {
249    if ( $1 =~ m/.*<FALSE:.*$/ ) {
250    } else {
251      print STDOUT "$2\n";
252    }
253  } else {
254    die "Unexpected value: $line\n";
255  }
256}
257
Note: See TracBrowser for help on using the repository browser.