- substitute <RELEASE> even if no release is specified
[platform/upstream/build.git] / substitutedeps
1 #!/usr/bin/perl -w
2
3 BEGIN {
4   unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
5 }
6
7 use strict;
8
9 use Build;
10
11 sub expand {
12   my ($config, $str) = @_;
13   my @xspec;
14   my %cf = %$config;
15   $cf{'save_expanded'} = 1;
16   Build::Rpm::parse(\%cf, [ "$str" ], \@xspec);
17   return @xspec && ref($xspec[0]) ? $xspec[0]->[1] : '';
18 }
19
20 my ($dist, $buildroot, $rpmdeps, $archs, $configdir, $release, $changelog);
21
22 while (@ARGV)  {
23   if ($ARGV[0] eq '--root') {
24     shift @ARGV;
25     $buildroot = shift @ARGV;
26     next;
27   }
28   if ($ARGV[0] eq '--dist') {
29     shift @ARGV;
30     $dist = shift @ARGV;
31     next;
32   }
33   if ($ARGV[0] eq '--archpath') {
34     shift @ARGV;
35     $archs = shift @ARGV;
36     next;
37   }
38   if ($ARGV[0] eq '--configdir') {
39     shift @ARGV;
40     $configdir = shift @ARGV;
41     next;
42   }
43   if ($ARGV[0] eq '--release') {
44     shift @ARGV;
45     $release = shift @ARGV;
46     next;
47   }
48   if ($ARGV[0] eq '--changelog') {
49     shift @ARGV;
50     $changelog = shift @ARGV;
51     next;
52   }
53   last;
54 }
55 die("Usage: substitutedeps --dist <dist> --archpath <archpath> [--configdir <configdir>] <specin> <specout>\n") unless @ARGV == 2;
56 my $spec = $ARGV[0];
57 my $specdir = $spec;
58 $specdir =~ s/[^\/]*$//;
59 $specdir = "./" if $specdir eq '';
60
61 my $newspec = $ARGV[1];
62
63 my $cf = Build::read_config_dist($dist, $archs, $configdir);
64 $cf->{'warnings'} = 1;
65
66 #######################################################################
67
68 my $xspec = [];
69 my $d = Build::parse($cf, $spec, $xspec) || {};
70 my @sdeps = @{$d->{'deps'} || []};
71 my @neg = map {substr($_, 1)} grep {/^-/} @{$d->{'deps'} || []};
72 my %neg = map {$_ => 1} @neg;
73 @sdeps = grep {!$neg{$_}} @sdeps;
74 @sdeps = Build::do_subst($cf, @sdeps);
75 @sdeps = grep {!$neg{$_}} @sdeps;
76 my %sdeps = map {$_ => 1} @sdeps;
77
78 open(F, '>', $newspec) || die("$newspec: $!\n");
79
80 my $used;
81 my $inchangelog = 0;
82 my $mainpkg = '';
83 my $pkg;
84
85 for my $l (@$xspec) {
86   $used = 1;
87   if (ref($l)) {
88     if (!defined($l->[1])) {
89       $used = 0;
90       $l = $l->[0];
91     } else {
92       $l = $l->[1];
93     }
94   }
95
96   if ($inchangelog) {
97     $inchangelog = 0 if $l =~ /^\s*%[^%]/;
98     next if $inchangelog;
99   }
100   if ($changelog && ($l =~ /\s*\%changelog\b/)) {
101     $inchangelog = 1;
102     next;
103   }
104
105   if ($l =~ /^Name\s*:\s*(\S+)/i) {
106     $pkg = $mainpkg = $1 unless $mainpkg;
107   }
108   if ($l =~ /^\s*%package\s+(-n\s+)?(\S+)/) {
109     if ($1) {
110       $pkg = $2;
111     } else {
112       $pkg = "$mainpkg-$2";
113     }
114   }
115
116   if ($l =~ /^Release:/i) {
117     my $oldl = $l;
118     if (defined $release) {
119       if (!($l =~ s/<RELEASE\d*>/$release/g)) {
120         if ($l =~ /<(?:CI_CNT|B_CNT)>/) {
121           # XXX: should pass ci_cnt/b_cnt instead
122           if ($release =~ /(\d+)\.(\d+)$/) {
123             my ($ci, $b) = ($1, $2);
124             $l =~ s/<CI_CNT>/$ci/;
125             $l =~ s/<B_CNT>/$b/;
126           } elsif ($release =~ /(\d+)$/) {
127             my $b = $1;
128             $b = '0' if $l =~ s/<CI_CNT>/$b/;
129             $l =~ s/<B_CNT>/$b/;
130           }
131         } else {
132           $l =~ s/^(Release:\s*).*/$1$release/i;
133         }
134       }
135     } else {
136       # remove macros, as rpm doesn't like them
137       $l =~ s/<RELEASE\d*>/0/;
138       $l =~ s/<CI_CNT>/0/;
139       $l =~ s/<B_CNT>/0/;
140     }
141     # this is to be compatible to legacy autobuild.
142     # you can specify a releaseprg in the project configuration,
143     # if your package contains this file it is executed and its
144     # output is used as a release.
145     # use only if you really must.
146     if ($cf->{'releaseprg'} && -f "$specdir$cf->{'releaseprg'}") {
147       my $newl = $l;
148       $newl =~ s/^Release:\s*//;
149       $oldl =~ s/^Release:\s*//;
150       my $project = expand($cf, "%?_project") || 'BUILD_BASENAME';
151       my $arch = expand($cf, "%?_target_cpu") || 'noarch';
152       $::ENV{'BUILD_OLDRELEASE'} = $oldl;
153       my @nl;
154       my $interpreter = "/bin/bash";
155       if (open(RP, '<', "$specdir$cf->{'releaseprg'}")) {
156         @nl = <RP>;
157         close RP;
158         if (@nl && $nl[0] =~ /^#!\s*(\S*)/) {
159           $interpreter = $1;
160         }
161       }
162       if ($buildroot) {
163         my $sd = $specdir;
164         $sd =~ s/^\Q$buildroot\E//;
165         open(RP, "-|", 'chroot', $buildroot, $interpreter, "$sd$cf->{'releaseprg'}", $project, $newl, $pkg, $arch) || die("$cf->{'releaseprg'}: $!\n");
166       } else {
167         open(RP, "-|", $interpreter, "$specdir$cf->{'releaseprg'}", $project, $newl, $pkg, $arch) || die("$cf->{'releaseprg'}: $!\n");
168       }
169       @nl = grep {$_ ne ''} <RP>;
170       if (!close(RP)) {
171         warn("$cf->{'releaseprg'} failed: $?\n");
172       }
173       # and another compatibility hack: if the prg returns pkg:<package>,
174       # the release of the package will be used. yuck...
175       if (@nl && $nl[0] =~ s/^pkg://) {
176         my $relpkg = $nl[0];
177         chomp $relpkg;
178         if ($buildroot) {
179           open(RP, "-|", 'chroot', $buildroot, 'rpm', '-q', '--qf', '%{RELEASE}', $relpkg) || die("rpm: $!\n");
180         } else {
181           open(RP, "-|", 'rpm', '-q', '--qf', '%{RELEASE}', $relpkg) || die("rpm: $!\n");
182         }
183         @nl = grep {$_ ne ''} <RP>;
184         if (!close(RP)) {
185           warn("rpm package query of '$relpkg' failed: $?\n");
186         }
187       }
188       if ($nl[0]) {
189         chomp $nl[0];
190         $l =~ s/^(Release:\s*).*/$1$nl[0]/i;
191         if (defined $release) {
192           if (!($l =~ s/<RELEASE\d*>/$release/g)) {
193             if ($l =~ /<(?:CI_CNT|B_CNT)>/) {
194               # XXX: should pass ci_cnt/b_cnt instead
195               if ($release =~ /(\d+)\.(\d+)$/) {
196                 my ($ci, $b) = ($1, $2);
197                 $l =~ s/<CI_CNT>/$ci/;
198                 $l =~ s/<B_CNT>/$b/;
199               } elsif ($release =~ /(\d+)$/) {
200                 my $b = $1;
201                 $l =~ s/<B_CNT>/$b/ unless $l =~ s/<CI_CNT>/$b/;
202               }
203             }
204           }
205         }
206       }
207     }
208     # all compat stuff done. we return to your scheduled program
209   }
210
211   if (!$used || ($l !~ /^(?:Build)?Requires:/i)) {
212     print F "$l\n";
213     next;
214   }
215   if ($l =~ /%\(/) {
216     # too hard for us
217     print F "$l\n";
218     next;
219   }
220
221   my $isbuildrequires = 0;
222   $isbuildrequires = 1 if $l =~ /^BuildRequires:/i;
223   my $r = $l;
224   $r =~ s/^[^:]*:\s*//;
225   my @deps = $r =~ /([^\s\[,]+)(\s+[<=>]+\s+[^\s\[,]+)?[\s,]*/g;
226   my @ndeps = ();
227   my $replace = 0;
228   my @f2 = Build::do_subst_vers($cf, @deps);
229   my %f2 = @f2;
230   if ($isbuildrequires) {
231     delete $f2{$_} for @neg;
232     delete $f2{$_} for grep {/^-/} keys %f2;
233   }
234   while (@deps) {
235     my ($pack, $vers) = splice(@deps, 0, 2);
236     $vers = '' unless defined $vers;
237     if (($isbuildrequires && $sdeps{$pack}) || exists($f2{$pack})) {
238       push @ndeps, "$pack$vers";
239       delete $f2{$pack};
240     } else {
241       $replace = 1;
242     }
243   }
244   if (%f2) {
245     while (@f2) {
246       my ($pack, $vers) = splice(@f2, 0, 2);
247       next unless exists $f2{$pack};
248       $vers = '' unless defined $vers;
249       push @ndeps, "$pack$vers";
250     }
251     $replace = 1
252   }
253   if ($replace) {
254     $l =~ /^(.*?:\s*)/;
255     print F $1.join(' ', @ndeps)."\n" if @ndeps;
256   } else {
257     print F "$l\n";
258   }
259 }
260
261 if ($changelog) {
262   print F "%changelog\n";
263   if (open(CF, '<', $changelog)) {
264     while(<CF>) {
265       print F $_;
266     }
267     close CF;
268   }
269 }
270
271 close(F) || die("close: $!\n");
272
273 exit(0);