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 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 $sources = 0; |
---|
32 | my $newlib = 0; |
---|
33 | my $infos = 0; |
---|
34 | my $prefix = '/usr'; |
---|
35 | |
---|
36 | my $verbose = 0; |
---|
37 | my @languages = (); |
---|
38 | my %options = (); |
---|
39 | |
---|
40 | GetOptions( |
---|
41 | \%options, |
---|
42 | 'infos!', |
---|
43 | 'newlib!', |
---|
44 | 'languages=s' => \@languages, |
---|
45 | 'prefix=s' => \$prefix, |
---|
46 | 'sources!', |
---|
47 | 'verbose+' => \$verbose |
---|
48 | ) or die( "failed to GetOptions" ); |
---|
49 | |
---|
50 | if ( defined($options{infos}) ) |
---|
51 | { |
---|
52 | $infos = $options{infos}; |
---|
53 | } else { |
---|
54 | $infos = ( $prefix =~ m/\/opt\/rtems.*/ ) ? 1 : 0; |
---|
55 | } |
---|
56 | |
---|
57 | if ( defined($options{sources}) ) |
---|
58 | { |
---|
59 | $sources = $options{sources}; |
---|
60 | } else { |
---|
61 | $sources = ( $prefix =~ m/\/opt\/rtems.*/ ) ? 0 : 1; |
---|
62 | } |
---|
63 | |
---|
64 | if ( defined($options{newlib}) ) |
---|
65 | { |
---|
66 | $newlib = $options{newlib}; |
---|
67 | } else { |
---|
68 | $newlib = 0; |
---|
69 | } |
---|
70 | |
---|
71 | if ( $verbose ) { |
---|
72 | print STDERR "SOURCES: $sources\n"; |
---|
73 | print STDERR "INFOS : $infos\n"; |
---|
74 | print STDERR "PREFIX : $prefix\n"; |
---|
75 | } |
---|
76 | |
---|
77 | my %langs; |
---|
78 | |
---|
79 | foreach ( split(/,/,join(',',@languages)) ){ |
---|
80 | $langs{$_} = 1; |
---|
81 | } |
---|
82 | |
---|
83 | my @condstack ; |
---|
84 | my @actionstack ; |
---|
85 | |
---|
86 | push @condstack,'<>'; |
---|
87 | |
---|
88 | my @npatterns = ( |
---|
89 | "(\"%\{_prefix\}\" (!=|==) \"/usr\")", |
---|
90 | |
---|
91 | "(%build_cxx)", |
---|
92 | "(%build_gnat)", |
---|
93 | "(%build_objc)", |
---|
94 | "(%build_gcj)", |
---|
95 | "(%build_libgcj)", |
---|
96 | "(%build_f95)", |
---|
97 | "(%build_g77)", |
---|
98 | "(%build_newlib)", |
---|
99 | "(%build_infos)" |
---|
100 | ); |
---|
101 | |
---|
102 | my @ppatterns = ( |
---|
103 | ); |
---|
104 | |
---|
105 | push @ppatterns, "(\"%\{_prefix\}\" " . (("$prefix" eq '/usr') ? '!=' : '==' ) . " \"/usr\")"; |
---|
106 | |
---|
107 | push @ppatterns, "(%build_gnat " . ( ($langs{gnat}) ? "==" : "!=" ) . " 0)"; |
---|
108 | push @ppatterns, "(%build_cxx " . ( ($langs{cxx}) ? "==" : "!=" ) . " 0)"; |
---|
109 | push @ppatterns, "(%build_objc " . ( ($langs{objc}) ? "==" : "!=" ) . " 0)"; |
---|
110 | push @ppatterns, "(%build_gcj " . ( ($langs{gcj}) ? "==" : "!=" ) . " 0)"; |
---|
111 | push @ppatterns, "(%build_libgcj " . ( ($langs{libgcj}) ? "==" : "!=" ) . " 0)"; |
---|
112 | push @ppatterns, "(%build_f95 " . ( ($langs{f95}) ? "==" : "!=" ) . " 0)"; |
---|
113 | push @ppatterns, "(%build_g77 " . ( ($langs{g77}) ? "==" : "!=" ) . " 0)"; |
---|
114 | |
---|
115 | push @ppatterns, "(%build_newlib " . ( ($newlib) ? "==" : "!=" ) . " 0)"; |
---|
116 | push @ppatterns, "(%build_infos " . ( ($infos) ? "==" : "!=" ) . " 0)"; |
---|
117 | |
---|
118 | my $npat = join('|',@npatterns); |
---|
119 | my $ppat = join('|',@ppatterns); |
---|
120 | |
---|
121 | if ( $verbose > 1 ) { |
---|
122 | print STDERR "PPAT: ", $ppat, "\n"; |
---|
123 | print STDERR "NPAT: ", $npat, "\n"; |
---|
124 | } |
---|
125 | |
---|
126 | my @buffer0 = <> ; |
---|
127 | |
---|
128 | my $buffer1 = join( '', @buffer0 ); |
---|
129 | # print STDERR $buffer1; |
---|
130 | |
---|
131 | foreach (split /\n/, $buffer1) |
---|
132 | { |
---|
133 | # print STDERR ">$_<\n"; |
---|
134 | } |
---|
135 | # exit 1; |
---|
136 | |
---|
137 | my @buffer2 ; |
---|
138 | foreach (split /\n/, $buffer1) |
---|
139 | { |
---|
140 | if ( /^%if(os|)\s+(.*)$/ ) |
---|
141 | { |
---|
142 | push @condstack,"<$2>"; |
---|
143 | if ( $condstack[$#condstack] =~ m/$npat/ ) { |
---|
144 | # transform unary conditionals into binary conditionals |
---|
145 | if ( $condstack[$#condstack] =~/.*<(%[a-zA-Z_0-9]+)>.*/ ) { |
---|
146 | $condstack[$#condstack] = "<$1 != 0>"; |
---|
147 | } |
---|
148 | } else { |
---|
149 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
150 | } |
---|
151 | } elsif ( /^%else.*$/ ) |
---|
152 | { |
---|
153 | my %ops = ( |
---|
154 | "!=" => "==", |
---|
155 | "==" => "!=" |
---|
156 | ); |
---|
157 | |
---|
158 | if ( $condstack[$#condstack] =~/.*<(.*) (!=|==) (.*)>.*/ ) { |
---|
159 | $condstack[$#condstack] = "<$1 " . $ops{$2} . " $3>"; |
---|
160 | if ( $condstack[$#condstack] =~ m/$npat/ ) { |
---|
161 | } else { |
---|
162 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
163 | } |
---|
164 | } else { |
---|
165 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
166 | } |
---|
167 | } elsif ( /^%endif.*$/ ) |
---|
168 | { |
---|
169 | if ( $condstack[$#condstack] =~ m/$npat/ ) { |
---|
170 | } else { |
---|
171 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
172 | } |
---|
173 | pop @condstack; |
---|
174 | } else { |
---|
175 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | my @buffer3; |
---|
180 | foreach my $i ( @buffer2 ) |
---|
181 | { |
---|
182 | # print STDERR $i->{state}, $i->{line}, "\n"; |
---|
183 | if ( $i->{state} =~ m/($ppat)/ ) { |
---|
184 | } else { |
---|
185 | push @buffer3, $i->{line}, "\n" |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | print STDOUT @buffer3; |
---|