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 | |
---|
27 | use Getopt::Long; |
---|
28 | |
---|
29 | use strict; |
---|
30 | |
---|
31 | my $newlib = 0; |
---|
32 | my $infos = 0; |
---|
33 | my $prefix = '/usr'; |
---|
34 | |
---|
35 | my $verbose = 0; |
---|
36 | my @languages = (); |
---|
37 | my %options = (); |
---|
38 | |
---|
39 | GetOptions( |
---|
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 | |
---|
49 | if ( !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 | |
---|
62 | if ( defined($options{newlib}) ) |
---|
63 | { |
---|
64 | $newlib = $options{newlib}; |
---|
65 | } else { |
---|
66 | $newlib = 0; |
---|
67 | } |
---|
68 | |
---|
69 | if ( $verbose ) { |
---|
70 | print STDERR "INFOS : $infos\n"; |
---|
71 | print STDERR "PREFIX : $prefix\n"; |
---|
72 | } |
---|
73 | |
---|
74 | my %langs; |
---|
75 | |
---|
76 | foreach ( split(/,/,join(',',@languages)) ){ |
---|
77 | $langs{$_} = 1; |
---|
78 | } |
---|
79 | |
---|
80 | my @npatterns = ( |
---|
81 | "(\"%\{_prefix\}\" (!=|==) \"/usr\")", |
---|
82 | |
---|
83 | "(%build_cxx)", |
---|
84 | "(%build_gnat)", |
---|
85 | "(%build_objc)", |
---|
86 | "(%build_gcj)", |
---|
87 | "(%build_libgcj)", |
---|
88 | "(%build_fortran)", |
---|
89 | "(%build_newlib)", |
---|
90 | "(%build_infos)" |
---|
91 | ); |
---|
92 | |
---|
93 | my @ppatterns = ( |
---|
94 | ); |
---|
95 | |
---|
96 | push @ppatterns, "(\"%\{_prefix\}\" " . (("$prefix" eq '/usr') ? '!=' : '==' ) . " \"/usr\")"; |
---|
97 | |
---|
98 | push @ppatterns, "(%build_gnat " . ( ($langs{gnat}) ? "==" : "!=" ) . " 0)"; |
---|
99 | push @ppatterns, "(%build_cxx " . ( ($langs{cxx}) ? "==" : "!=" ) . " 0)"; |
---|
100 | push @ppatterns, "(%build_objc " . ( ($langs{objc}) ? "==" : "!=" ) . " 0)"; |
---|
101 | push @ppatterns, "(%build_gcj " . ( ($langs{gcj}) ? "==" : "!=" ) . " 0)"; |
---|
102 | push @ppatterns, "(%build_libgcj " . ( ($langs{libgcj}) ? "==" : "!=" ) . " 0)"; |
---|
103 | push @ppatterns, "(%build_fortran " . ( ($langs{fortran}) ? "==" : "!=" ) . " 0)"; |
---|
104 | |
---|
105 | push @ppatterns, "(%build_newlib " . ( ($newlib) ? "==" : "!=" ) . " 0)"; |
---|
106 | push @ppatterns, "(%build_infos " . ( ($infos) ? "==" : "!=" ) . " 0)"; |
---|
107 | |
---|
108 | my $npat = join('|',@npatterns); |
---|
109 | my $ppat = join('|',@ppatterns); |
---|
110 | |
---|
111 | if ( $verbose > 1 ) { |
---|
112 | print STDERR "PPAT: ", $ppat, "\n"; |
---|
113 | print STDERR "NPAT: ", $npat, "\n"; |
---|
114 | } |
---|
115 | |
---|
116 | my @buffer0 = <> ; |
---|
117 | |
---|
118 | my @buffer2 ; |
---|
119 | |
---|
120 | my @condstack ; |
---|
121 | |
---|
122 | @condstack = (); |
---|
123 | push @condstack,'<>'; |
---|
124 | foreach (@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 | |
---|
166 | my @buffer3; |
---|
167 | foreach my $i ( @buffer2 ) |
---|
168 | { |
---|
169 | print STDERR "STATE:", $i->{state}, " LINE:", $i->{line}, "\n" if $verbose > 1; |
---|
170 | if ( $i->{state} =~ m/($ppat)/ ) { |
---|
171 | } else { |
---|
172 | push @buffer3, $i->{line}; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | #foreach my $line ( @buffer3 ) |
---|
177 | #{ |
---|
178 | # print STDERR "L:<$line>\n"; |
---|
179 | #} |
---|
180 | |
---|
181 | my @buffer4; |
---|
182 | @condstack = (); |
---|
183 | push @condstack, "<>"; |
---|
184 | foreach my $line ( @buffer3 ) |
---|
185 | { |
---|
186 | # print STDERR "READ:{", $line, "}\n"; |
---|
187 | if ( $line =~/^%if\s+"([a-zA-Z_0-9\.\-]+)"\s+==\s+"([a-zA-Z_0-9\.\-]+)"\s*$/ ) |
---|
188 | { |
---|
189 | if ( "$1" eq "$2" ) { |
---|
190 | push @condstack,"<TRUE:$1 == $2>"; |
---|
191 | } else { |
---|
192 | push @condstack,"<FALSE:$1 == $2>"; |
---|
193 | } |
---|
194 | } elsif ( $line =~/^%if\s+"([a-zA-Z_0-9\.\-]+)"\s+!=\s+"([a-zA-Z_0-9\.\-]+)"\s*$/ ) |
---|
195 | { |
---|
196 | if ( "$1" ne "$2" ) { |
---|
197 | push @condstack,"<TRUE:$1 != $2>"; |
---|
198 | } else { |
---|
199 | push @condstack,"<FALSE:$1 != $2>"; |
---|
200 | } |
---|
201 | } elsif ( $line =~/^%if\s+(.*)\s*$/ ) |
---|
202 | { |
---|
203 | my $exp = $1; |
---|
204 | if ( $condstack[$#condstack] =~ m/<FALSE:.*\s*>$/ ) { |
---|
205 | push @condstack,"<FALSE:$exp>"; |
---|
206 | } else { |
---|
207 | push @condstack,"<IFOT:$exp>"; |
---|
208 | push @buffer4, "$line\n"; |
---|
209 | } |
---|
210 | } elsif ( $line =~/^%if((os|narch)\s+.*)\s*$/ ) |
---|
211 | { |
---|
212 | my $exp = $1; |
---|
213 | if ( $condstack[$#condstack] =~ m/<FALSE:(.*)\s*>$/ ) { |
---|
214 | push @condstack,"<FALSE:$exp>"; |
---|
215 | } else { |
---|
216 | push @condstack,"<IFOT:$exp>"; |
---|
217 | push @buffer4, "$line\n"; |
---|
218 | } |
---|
219 | } elsif ( $line =~ /^%else\s*$/ ) { |
---|
220 | if ( $condstack[$#condstack] =~ m/<TRUE:(.*)\s*>$/ ) { |
---|
221 | $condstack[$#condstack] = "<FALSE:$1>"; |
---|
222 | } elsif ( $condstack[$#condstack] =~ m/<FALSE:(.*)\s*>$/ ) { |
---|
223 | $condstack[$#condstack] = "<TRUE:$1>"; |
---|
224 | } else { |
---|
225 | push @buffer4, "$line\n"; |
---|
226 | } |
---|
227 | } elsif ( $line =~ /^%endif\s*$/ ) { |
---|
228 | |
---|
229 | if ( $condstack[$#condstack] =~ m/<TRUE:.*>$/ ) { |
---|
230 | # print STDERR "ENDIF: TRUE\n"; |
---|
231 | } elsif ( $condstack[$#condstack] =~ m/<FALSE:.*>$/ ) { |
---|
232 | # print STDERR "ENDIF: FALSE\n"; |
---|
233 | } else { |
---|
234 | push @buffer4, "%endif\n"; |
---|
235 | } |
---|
236 | # print STDERR "POP: $line\n"; |
---|
237 | pop @condstack; |
---|
238 | } else { |
---|
239 | # print STDERR "CATCH $condstack[$#condstack]:$line\n"; |
---|
240 | if ( $condstack[$#condstack] =~ m/<TRUE:.*>$/ ) { |
---|
241 | push @buffer4, "$line\n"; |
---|
242 | } elsif ( $condstack[$#condstack] =~ m/<FALSE:.*>$/ ) { |
---|
243 | } else { |
---|
244 | push @buffer4, "$line\n"; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | # print STDERR @condstack, "LINE: $line\n"; |
---|
249 | } |
---|
250 | |
---|
251 | print STDOUT @buffer4; |
---|