4 unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
12 my ($config, $str) = @_;
15 $cf{'save_expanded'} = 1;
16 Build::Rpm::parse(\%cf, [ "$str" ], \@xspec);
17 return @xspec && ref($xspec[0]) ? $xspec[0]->[1] : '';
20 my ($dist, $buildroot, $rpmdeps, $archs, $configdir, $release, $changelog);
23 if ($ARGV[0] eq '--root') {
25 $buildroot = shift @ARGV;
28 if ($ARGV[0] eq '--dist') {
33 if ($ARGV[0] eq '--archpath') {
38 if ($ARGV[0] eq '--configdir') {
40 $configdir = shift @ARGV;
43 if ($ARGV[0] eq '--release') {
45 $release = shift @ARGV;
48 if ($ARGV[0] eq '--changelog') {
50 $changelog = shift @ARGV;
55 die("Usage: substitutedeps --dist <dist> --archpath <archpath> [--configdir <configdir>] <specin> <specout>\n") unless @ARGV == 2;
58 $specdir =~ s/[^\/]*$//;
59 $specdir = "./" if $specdir eq '';
61 my $newspec = $ARGV[1];
63 my $cf = Build::read_config_dist($dist, $archs, $configdir);
64 $cf->{'warnings'} = 1;
66 #######################################################################
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;
78 open(F, '>', $newspec) || die("$newspec: $!\n");
88 if (!defined($l->[1])) {
97 $inchangelog = 0 if $l =~ /^\s*%[^%]/;
100 if ($changelog && ($l =~ /\s*\%changelog\b/)) {
105 if ($l =~ /^Name\s*:\s*(\S+)/i) {
106 $pkg = $mainpkg = $1 unless $mainpkg;
108 if ($l =~ /^\s*%package\s+(-n\s+)?(\S+)/) {
112 $pkg = "$mainpkg-$2";
116 if ($l =~ /^Release:/i) {
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/;
126 } elsif ($release =~ /(\d+)$/) {
128 $l =~ s/<B_CNT>/$b/ unless $l =~ s/<CI_CNT>/$b/;
131 $l =~ s/^(Release:\s*).*/$1$release/i;
135 # this is to be compatible to legacy autobuild.
136 # you can specify a releaseprg in the project configuration,
137 # if your package contains this file it is executed and its
138 # output is used as a release.
139 # use only if you really must.
140 if ($cf->{'releaseprg'} && -f "$specdir$cf->{'releaseprg'}") {
142 $newl =~ s/^Release:\s*//;
143 $oldl =~ s/^Release:\s*//;
144 my $project = expand($cf, "%?_project") || 'BUILD_BASENAME';
145 my $arch = expand($cf, "%?_target_cpu") || 'noarch';
146 $::ENV{'BUILD_OLDRELEASE'} = $oldl;
148 my $interpreter = "/bin/bash";
149 if (open(RP, '<', "$specdir$cf->{'releaseprg'}")) {
152 if (@nl && $nl[0] =~ /^#!\s*(\S*)/) {
158 $sd =~ s/^\Q$buildroot\E//;
159 open(RP, "-|", 'chroot', $buildroot, $interpreter, "$sd$cf->{'releaseprg'}", $project, $newl, $pkg, $arch) || die("$cf->{'releaseprg'}: $!\n");
161 open(RP, "-|", $interpreter, "$specdir$cf->{'releaseprg'}", $project, $newl, $pkg, $arch) || die("$cf->{'releaseprg'}: $!\n");
163 @nl = grep {$_ ne ''} <RP>;
165 warn("$cf->{'releaseprg'} failed: $?\n");
167 # and another compatibility hack: if the prg returns pkg:<package>,
168 # the release of the package will be used. yuck...
169 if (@nl && $nl[0] =~ s/^pkg://) {
173 open(RP, "-|", 'chroot', $buildroot, 'rpm', '-q', '--qf', '%{RELEASE}', $relpkg) || die("rpm: $!\n");
175 open(RP, "-|", 'rpm', '-q', '--qf', '%{RELEASE}', $relpkg) || die("rpm: $!\n");
177 @nl = grep {$_ ne ''} <RP>;
179 warn("rpm package query of '$relpkg' failed: $?\n");
184 $l =~ s/^(Release:\s*).*/$1$nl[0]/i;
185 if (defined $release) {
186 if (!($l =~ s/<RELEASE\d*>/$release/g)) {
187 if ($l =~ /<(?:CI_CNT|B_CNT)>/) {
188 # XXX: should pass ci_cnt/b_cnt instead
189 if ($release =~ /(\d+)\.(\d+)$/) {
190 my ($ci, $b) = ($1, $2);
191 $l =~ s/<CI_CNT>/$ci/;
193 } elsif ($release =~ /(\d+)$/) {
195 $l =~ s/<B_CNT>/$b/ unless $l =~ s/<CI_CNT>/$b/;
202 # all compat stuff done. we return to your scheduled program
205 if (!$used || ($l !~ /^(?:Build)?Requires:/i)) {
210 my $isbuildrequires = 0;
211 $isbuildrequires = 1 if $l =~ /^BuildRequires:/i;
213 $r =~ s/^[^:]*:\s*//;
214 my @deps = $r =~ /([^\s\[,]+)(\s+[<=>]+\s+[^\s\[,]+)?[\s,]*/g;
217 my @f2 = Build::do_subst_vers($cf, @deps);
219 if ($isbuildrequires) {
220 delete $f2{$_} for @neg;
221 delete $f2{$_} for grep {/^-/} keys %f2;
224 my ($pack, $vers) = splice(@deps, 0, 2);
225 $vers = '' unless defined $vers;
226 if (($isbuildrequires && $sdeps{$pack}) || exists($f2{$pack})) {
227 push @ndeps, "$pack$vers";
235 my ($pack, $vers) = splice(@f2, 0, 2);
236 next unless exists $f2{$pack};
237 $vers = '' unless defined $vers;
238 push @ndeps, "$pack$vers";
244 print F $1.join(' ', @ndeps)."\n" if @ndeps;
251 print F "%changelog\n";
252 if (open(CF, '<', $changelog)) {
260 close(F) || die("close: $!\n");