4 unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
13 my ($config, $str) = @_;
16 $cf{'save_expanded'} = 1;
17 Build::Rpm::parse(\%cf, [ "$str" ], \@xspec);
18 return @xspec && ref($xspec[0]) ? $xspec[0]->[1] : '';
21 my ($dist, $buildroot, $rpmdeps, $archs, $configdir, $release, $changelog);
24 if ($ARGV[0] eq '--root') {
26 $buildroot = shift @ARGV;
29 if ($ARGV[0] eq '--dist') {
34 if ($ARGV[0] eq '--archpath') {
39 if ($ARGV[0] eq '--configdir') {
41 $configdir = shift @ARGV;
44 if ($ARGV[0] eq '--release') {
46 $release = shift @ARGV;
49 if ($ARGV[0] eq '--changelog') {
51 $changelog = shift @ARGV;
56 die("Usage: substitutedeps --dist <dist> --archpath <archpath> [--configdir <configdir>] <specin> <specout>\n") unless @ARGV == 2;
59 $specdir =~ s/[^\/]*$//;
60 $specdir = "./" if $specdir eq '';
62 my $newspec = $ARGV[1];
64 my $cf = Build::read_config_dist($dist, $archs, $configdir);
65 $cf->{'warnings'} = 1;
67 #######################################################################
70 my $d = Build::parse($cf, $spec, $xspec) || {};
71 my @sdeps = @{$d->{'deps'} || []};
72 my @neg = map {substr($_, 1)} grep {/^-/} @{$d->{'deps'} || []};
73 my %neg = map {$_ => 1} @neg;
74 @sdeps = grep {!$neg{$_}} @sdeps;
75 @sdeps = Build::do_subst($cf, @sdeps);
76 @sdeps = grep {!$neg{$_}} @sdeps;
77 my %sdeps = map {$_ => 1} @sdeps;
79 open(F, '>', $newspec) || die("$newspec: $!\n");
89 if (!defined($l->[1])) {
98 $inchangelog = 0 if $l =~ /^\s*%[^%]/;
101 if ($changelog && ($l =~ /\s*\%changelog\b/)) {
106 if ($l =~ /^Name\s*:\s*(\S+)/i) {
107 $pkg = $mainpkg = $1 unless $mainpkg;
109 if ($l =~ /^\s*%package\s+(-n\s+)?(\S+)/) {
113 $pkg = "$mainpkg-$2";
117 if ($l =~ /^Release:/i) {
120 if (!($l =~ s/<RELEASE\d*>/$release/g)) {
121 if ($l =~ /<(?:CI_CNT|B_CNT)>/) {
122 # XXX: should pass ci_cnt/b_cnt instead
123 if ($release =~ /(\d+)\.(\d+)$/) {
124 my ($ci, $b) = ($1, $2);
125 $l =~ s/<CI_CNT>/$ci/;
127 } elsif ($release =~ /(\d+)$/) {
129 $l =~ s/<B_CNT>/$b/ unless $l =~ s/<CI_CNT>/$b/;
132 $l =~ s/^(Release:\s*).*/$1$release/i;
136 # this is to be compatible to legacy autobuild.
137 # you can specify a releaseprg in the project configuration,
138 # if your package contains this file it is executed and its
139 # output is used as a release.
140 # use only if you really must.
141 if ($cf->{'releaseprg'} && -f "$specdir$cf->{'releaseprg'}") {
143 $newl =~ s/^Release:\s*//;
144 $oldl =~ s/^Release:\s*//;
145 my $project = expand($cf, "%?_project") || 'BUILD_BASENAME';
146 my $arch = expand($cf, "%?_target_cpu") || 'noarch';
147 $::ENV{'BUILD_OLDRELEASE'} = $oldl;
149 my $interpreter = "/bin/bash";
150 if (open(RP, '<', "$specdir$cf->{'releaseprg'}")) {
153 if (@nl && $nl[0] =~ /^#!\s*(\S*)/) {
159 $sd =~ s/^\Q$buildroot\E//;
160 open(RP, "-|", 'chroot', $buildroot, $interpreter, "$sd$cf->{'releaseprg'}", $project, $newl, $pkg, $arch) || die("$cf->{'releaseprg'}: $!\n");
162 open(RP, "-|", $interpreter, "$specdir$cf->{'releaseprg'}", $project, $newl, $pkg, $arch) || die("$cf->{'releaseprg'}: $!\n");
164 @nl = grep {$_ ne ''} <RP>;
166 warn("$cf->{'releaseprg'} failed: $?\n");
168 # and another compatibility hack: if the prg returns pkg:<package>,
169 # the release of the package will be used. yuck...
170 if (@nl && $nl[0] =~ s/^pkg://) {
174 open(RP, "-|", 'chroot', $buildroot, 'rpm', '-q', '--qf', '%{RELEASE}', $relpkg) || die("rpm: $!\n");
176 open(RP, "-|", 'rpm', '-q', '--qf', '%{RELEASE}', $relpkg) || die("rpm: $!\n");
178 @nl = grep {$_ ne ''} <RP>;
180 warn("rpm package query of '$relpkg' failed: $?\n");
185 $l =~ s/^(Release:\s*).*/$1$nl[0]/i;
187 if (!($l =~ s/<RELEASE\d*>/$release/g)) {
188 if ($l =~ /<(?:CI_CNT|B_CNT)>/) {
189 # XXX: should pass ci_cnt/b_cnt instead
190 if ($release =~ /(\d+)\.(\d+)$/) {
191 my ($ci, $b) = ($1, $2);
192 $l =~ s/<CI_CNT>/$ci/;
194 } elsif ($release =~ /(\d+)$/) {
196 $l =~ s/<B_CNT>/$b/ unless $l =~ s/<CI_CNT>/$b/;
203 # all compat stuff done. we return to your scheduled program
206 if (!$used || ($l !~ /^(?:Build)?Requires:/i)) {
211 my $isbuildrequires = 0;
212 $isbuildrequires = 1 if $l =~ /^BuildRequires:/i;
214 $r =~ s/^[^:]*:\s*//;
215 my @deps = $r =~ /([^\s\[,]+)(\s+[<=>]+\s+[^\s\[,]+)?[\s,]*/g;
218 my @f2 = Build::do_subst_vers($cf, @deps);
220 if ($isbuildrequires) {
221 delete $f2{$_} for @neg;
222 delete $f2{$_} for grep {/^-/} keys %f2;
225 my ($pack, $vers) = splice(@deps, 0, 2);
226 $vers = '' unless defined $vers;
227 if (($isbuildrequires && $sdeps{$pack}) || exists($f2{$pack})) {
228 push @ndeps, "$pack$vers";
236 my ($pack, $vers) = splice(@f2, 0, 2);
237 next unless exists $f2{$pack};
238 $vers = '' unless defined $vers;
239 push @ndeps, "$pack$vers";
245 print F $1.join(' ', @ndeps)."\n" if @ndeps;
252 print F "%changelog\n";
253 if (open(CF, '<', $changelog)) {
261 close(F) || die("close: $!\n");