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 $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 @condstack ; |
---|
81 | my @actionstack ; |
---|
82 | |
---|
83 | push @condstack,'<>'; |
---|
84 | |
---|
85 | my @npatterns = ( |
---|
86 | "(\"%\{_prefix\}\" (!=|==) \"/usr\")", |
---|
87 | |
---|
88 | "(%build_cxx)", |
---|
89 | "(%build_gnat)", |
---|
90 | "(%build_objc)", |
---|
91 | "(%build_gcj)", |
---|
92 | "(%build_libgcj)", |
---|
93 | "(%build_f95)", |
---|
94 | "(%build_g77)", |
---|
95 | "(%build_newlib)", |
---|
96 | "(%build_infos)" |
---|
97 | ); |
---|
98 | |
---|
99 | my @ppatterns = ( |
---|
100 | ); |
---|
101 | |
---|
102 | push @ppatterns, "(\"%\{_prefix\}\" " . (("$prefix" eq '/usr') ? '!=' : '==' ) . " \"/usr\")"; |
---|
103 | |
---|
104 | push @ppatterns, "(%build_gnat " . ( ($langs{gnat}) ? "==" : "!=" ) . " 0)"; |
---|
105 | push @ppatterns, "(%build_cxx " . ( ($langs{cxx}) ? "==" : "!=" ) . " 0)"; |
---|
106 | push @ppatterns, "(%build_objc " . ( ($langs{objc}) ? "==" : "!=" ) . " 0)"; |
---|
107 | push @ppatterns, "(%build_gcj " . ( ($langs{gcj}) ? "==" : "!=" ) . " 0)"; |
---|
108 | push @ppatterns, "(%build_libgcj " . ( ($langs{libgcj}) ? "==" : "!=" ) . " 0)"; |
---|
109 | push @ppatterns, "(%build_f95 " . ( ($langs{f95}) ? "==" : "!=" ) . " 0)"; |
---|
110 | push @ppatterns, "(%build_g77 " . ( ($langs{g77}) ? "==" : "!=" ) . " 0)"; |
---|
111 | |
---|
112 | push @ppatterns, "(%build_newlib " . ( ($newlib) ? "==" : "!=" ) . " 0)"; |
---|
113 | push @ppatterns, "(%build_infos " . ( ($infos) ? "==" : "!=" ) . " 0)"; |
---|
114 | |
---|
115 | my $npat = join('|',@npatterns); |
---|
116 | my $ppat = join('|',@ppatterns); |
---|
117 | |
---|
118 | if ( $verbose > 1 ) { |
---|
119 | print STDERR "PPAT: ", $ppat, "\n"; |
---|
120 | print STDERR "NPAT: ", $npat, "\n"; |
---|
121 | } |
---|
122 | |
---|
123 | my @buffer0 = <> ; |
---|
124 | |
---|
125 | my @buffer2 ; |
---|
126 | foreach (@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 | |
---|
168 | my @buffer3; |
---|
169 | foreach my $i ( @buffer2 ) |
---|
170 | { |
---|
171 | # print STDERR $i->{state}, $i->{line}, "\n"; |
---|
172 | if ( $i->{state} =~ m/($ppat)/ ) { |
---|
173 | } else { |
---|
174 | push @buffer3, $i->{line}, "\n" |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | print STDOUT @buffer3; |
---|