[ff5bd63] | 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 | |
---|
| 130 | foreach (split /\n/, $buffer1) |
---|
| 131 | { |
---|
| 132 | # print STDERR ">$_<\n"; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | my @buffer2 ; |
---|
| 136 | foreach (split /\n/, $buffer1) |
---|
| 137 | { |
---|
| 138 | if ( /^%if(os|)\s+(.*)$/ ) |
---|
| 139 | { |
---|
| 140 | push @condstack,"<$2>"; |
---|
| 141 | if ( $condstack[$#condstack] =~ m/$npat/ ) { |
---|
| 142 | # transform unary conditionals into binary conditionals |
---|
| 143 | if ( $condstack[$#condstack] =~/.*<(%[a-zA-Z_0-9]+)>.*/ ) { |
---|
| 144 | $condstack[$#condstack] = "<$1 != 0>"; |
---|
| 145 | } |
---|
| 146 | } else { |
---|
| 147 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
| 148 | } |
---|
| 149 | } elsif ( /^%else.*$/ ) |
---|
| 150 | { |
---|
| 151 | my %ops = ( |
---|
| 152 | "!=" => "==", |
---|
| 153 | "==" => "!=" |
---|
| 154 | ); |
---|
| 155 | |
---|
| 156 | if ( $condstack[$#condstack] =~/.*<(.*) (!=|==) (.*)>.*/ ) { |
---|
| 157 | $condstack[$#condstack] = "<$1 " . $ops{$2} . " $3>"; |
---|
| 158 | if ( $condstack[$#condstack] =~ m/$npat/ ) { |
---|
| 159 | } else { |
---|
| 160 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
| 161 | } |
---|
| 162 | } else { |
---|
| 163 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
| 164 | } |
---|
| 165 | } elsif ( /^%endif.*$/ ) |
---|
| 166 | { |
---|
| 167 | if ( $condstack[$#condstack] =~ m/$npat/ ) { |
---|
| 168 | } else { |
---|
| 169 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
| 170 | } |
---|
| 171 | pop @condstack; |
---|
| 172 | } else { |
---|
| 173 | push @buffer2, { state => join('',@condstack), line => "$_" }; |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | my @buffer3; |
---|
| 178 | foreach my $i ( @buffer2 ) |
---|
| 179 | { |
---|
| 180 | # print STDERR $i->{state}, $i->{line}, "\n"; |
---|
| 181 | if ( $i->{state} =~ m/($ppat)/ ) { |
---|
| 182 | } else { |
---|
| 183 | push @buffer3, $i->{line}, "\n" |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | print STDOUT @buffer3; |
---|