Merge branch 'master' into devel
[tools/build.git] / expanddeps
1 #!/usr/bin/perl -w
2
3 ################################################################
4 #
5 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2 or 3 as
9 # published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program (see the file COPYING); if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 #
21 ################################################################
22
23 BEGIN {
24   unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
25 }
26
27 use strict;
28
29 use Build;
30 use File::Basename;
31
32 my ($dist, $rpmdeps, $archs, $configdir, $useusedforbuild, $installonly, $noinstall, $usehigherdeps, $isvm);
33
34 $configdir = ($::ENV{'BUILD_DIR'} || '/usr/lib/build') . '/configs';
35
36 while (@ARGV)  {
37   if ($ARGV[0] eq '--dist') {
38     shift @ARGV;
39     $dist = shift @ARGV;
40     next;
41   }
42   if ($ARGV[0] eq '--depfile') {
43     shift @ARGV;
44     $rpmdeps = shift @ARGV;
45     next;
46   }
47   if ($ARGV[0] eq '--archpath') {
48     shift @ARGV;
49     $archs = shift @ARGV;
50     next;
51   }
52   if ($ARGV[0] eq '--configdir') {
53     shift @ARGV;
54     $configdir = shift @ARGV;
55     next;
56   }
57   if ($ARGV[0] eq '--useusedforbuild') {
58     shift @ARGV;
59     $useusedforbuild = 1;
60     next;
61   }
62   if ($ARGV[0] eq '--define') {
63     shift @ARGV;
64     my $def = shift @ARGV;
65     Build::define($def);
66     next;
67   }
68   if ($ARGV[0] eq '--with') {
69     shift @ARGV;
70     my $def = shift @ARGV;
71     Build::define("_with_$def --with-$def");
72     next;
73   }
74   if ($ARGV[0] eq '--without') {
75     shift @ARGV;
76     my $def = shift @ARGV;
77     Build::define("_without_$def --without-$def");
78     next;
79   }
80   if ($ARGV[0] eq '--usehigherdeps') {
81     shift @ARGV;
82     $usehigherdeps = 1;
83     next;
84   }
85   if ($ARGV[0] eq '--vm') {
86     shift @ARGV;
87     $isvm = 1;
88     next;
89   }
90   last;
91 }
92
93 $archs = '' unless defined $archs;
94 die("you must specfiy a depfile!\n") unless defined $rpmdeps;
95
96 # split args in recipe and pkgnames
97 my $recipe;
98 my @extradeps;
99 for my $arg (@ARGV) {
100   my $buildtype = Build::recipe2buildtype($arg);
101   if ($buildtype) {
102     die("can only work with at most one recipe file\n") if defined $recipe;
103     $recipe = $arg;
104   } else {
105     push @extradeps, $arg;
106   }
107 }
108
109 my $binarytype;
110 my @archs = split(':', $archs);
111 if ($recipe =~ /(^|\/)PKGBUILD$/) {
112   push @archs, 'any' unless grep {$_ eq 'any'} @archs;
113   $binarytype = 'arch';
114 } elsif ($recipe =~ /\.dsc$/) {
115   push @archs, 'all' unless grep {$_ eq 'noarch'} @archs;
116   $binarytype = 'deb';
117 } else {
118   push @archs, 'noarch' unless grep {$_ eq 'noarch'} @archs;
119   $binarytype = 'rpm';
120 }
121
122 # read dist if we can
123 my $cf;
124 if (defined($dist) && $dist ne '') {
125   $cf = Build::read_config_dist($dist, $archs[0], $configdir);
126   $binarytype = $cf->{'binarytype'} if $cf->{'binarytype'} && $cf->{'binarytype'} ne 'UNDEFINED';
127 }
128
129 my (%fn, %prov, %req, %con, %obs, %rec, %sup);
130
131 my %packs;
132 my %repo;
133 my %ids;
134
135 my %packs_arch;
136 my %packs_done;
137 # XXX: move to separate tool
138 if (!defined($dist) || $dist eq '') {
139   my $rpmarch = (grep {$fn{"rpm.$_"}} @archs)[0];
140   if (!$rpmarch) {
141     $dist = 'default';
142   } else {
143     my $rpmfn = $fn{"rpm.$rpmarch"};
144     if ($rpmfn =~ /^[a-z]+:\/\//) {
145       require File::Temp;
146       my $tmpdir = File::Temp::tempdir('CLEANUP' => 1);
147       $rpmfn =~ s/.*\//$tmpdir\// unless system("$INC[0]/download", $tmpdir, $rpmfn);
148     }
149     my $rpmdist = '';
150     if ($rpmfn =~ /^\// && -e $rpmfn) {
151       my %res = Build::Rpm::rpmq($rpmfn, 1010);
152       $rpmdist = $res{1010}->[0] || '';
153     }
154     $dist = Build::dist_canon($rpmdist, $archs[0]);
155     # need some extra work for sles11 :(
156     if ($dist =~ /^sles11-/) {
157       my %res = Build::Rpm::rpmq($rpmfn, 1049);
158       $dist =~ s/^sles11-/sles11sp2-/ if grep {/^liblzma/} @{$res{1049} || []};
159     }
160   }
161   print STDERR "Warning: distribution not specified, assuming '$dist' (see $configdir).\n";
162 }
163
164 $cf ||= Build::read_config_dist($dist, $archs[0], $configdir);
165 $cf->{'warnings'} = 1;
166
167 my $dofileprovides = %{$cf->{'fileprovides'}};
168 $dofileprovides = 1 if ($binarytype || 'rpm') ne 'rpm';
169 my %exportfilters = %{$cf->{'exportfilter'}};
170
171 open(F, '<', $rpmdeps) || die("$rpmdeps: $!\n");
172 # WARNING: the following code assumes that the 'I' tag comes last
173 my ($pkgF, $pkgP, $pkgR, $pkgC, $pkgO, $pkgr, $pkgs);
174
175 my $verscmp = \&Build::Rpm::verscmp;
176
177 if ($binarytype && $binarytype eq 'deb') {
178   $verscmp = \&Build::Deb::verscmp;
179   for my $arch (@archs) {
180     $arch = Build::Deb::basearch($arch) unless $arch =~ /^i[456]86$/;
181   }
182 }
183
184 while(<F>) {
185   chomp;
186   if (/^F:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
187     my $pkgname = basename($2);
188     $pkgF = $2;
189     next if $fn{$1};
190     $fn{$1} = $2;
191     my $pack = $1;
192     $pack =~ /^(.*)\.([^\.]+)$/ or die;
193     push @{$packs_arch{$2}}, $1;
194     my $basename = $1;
195     my $arch = $2;
196     for(keys %exportfilters) {
197       next if ($pkgname !~ /$_/);
198       for (@{$exportfilters{$_}}) {
199         my $a = $_;
200         next if ($a eq ".");
201         next if (! grep ($_ eq $a, @archs));
202         $packs{$basename} = "$basename.$arch"
203       }
204     }
205   } elsif (/^P:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
206     $pkgP = $2;
207     next if $prov{$1};
208     $prov{$1} = $2;
209   } elsif (/^R:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
210     $pkgR = $2;
211     next if $req{$1};
212     $req{$1} = $2;
213   } elsif (/^r:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
214     $pkgr = $2;
215   } elsif (/^C:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
216     $pkgC = $2;
217     next if $con{$1};
218     $con{$1} = $2;
219   } elsif (/^O:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
220     $pkgO = $2;
221     next if $obs{$1};
222     $obs{$1} = $2;
223   } elsif (/^r:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
224     $pkgr = $2;
225   } elsif (/^s:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
226     $pkgs = $2;
227   } elsif (/^I:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
228     my $r = 0;
229     if ($usehigherdeps) {
230       $r = 1;
231     } else {
232       if ($packs_done{$1}) {
233         $r = 0;
234       } else {
235         $r = 1;
236       }
237     }
238
239     next unless ($r);
240     my ($i, $newid) = ($1, $2);
241     undef $i unless !$ids{$i} || $verscmp->($ids{$i}, $newid) < 0;
242     undef $i unless defined($pkgF) && defined($pkgP) && defined($pkgR);
243     if (defined $i) {
244       $i =~ /^(.*)\.([^\.]+)$/ or die;
245       push @{$packs_arch{$2}}, $1;
246       $ids{$i}  = $newid;
247       $fn{$i}   = $pkgF;
248       $prov{$i} = $pkgP;
249       delete $req{$i};
250       delete $rec{$i};
251       delete $con{$i};
252       delete $obs{$i};
253       delete $rec{$i};
254       delete $sup{$i};
255       $req{$i}  = $pkgR;
256       $con{$i}  = $pkgC if defined $pkgC;
257       $obs{$i}  = $pkgO if defined $pkgO;
258       $rec{$i}  = $pkgr if defined $pkgr;
259       $sup{$i}  = $pkgs if defined $pkgs;
260     } else {
261       next if $ids{$1};
262       $ids{$1} = $2;
263     }
264     undef $pkgF;
265     undef $pkgP;
266     undef $pkgR;
267     undef $pkgC;
268     undef $pkgO;
269     undef $pkgr;
270     undef $pkgs;
271   } elsif ($_ eq 'D:') {
272     %packs_done = %ids;
273   }
274 }
275 close F;
276
277 for my $arch (@archs) {
278   $packs{$_} ||= "$_.$arch" for @{$packs_arch{$arch} || []};
279 }
280
281 for my $pack (keys %packs) {
282   my $r = {};
283   my (@s, $s, @pr, @re, @co, @ob, @rc, @su);
284   @s = split(' ', $prov{$packs{$pack}} || '');
285   while (@s) {
286     $s = shift @s;
287     next if !$dofileprovides && $s =~ /^\//;
288     if ($s =~ /^rpmlib\(/) {
289       splice(@s, 0, 2);
290       next;
291     }
292     push @pr, $s;
293     while (@s && $s[0] =~ /^[\(<=>|]/) {
294       $pr[-1] .= " $s[0] $s[1]";
295       $pr[-1] =~ s/ \((.*)\)/ $1/;
296       $pr[-1] =~ s/(<|>){2}/$1/;
297       splice(@s, 0, 2);
298     }
299   }
300   @s = split(' ', $req{$packs{$pack}} || '');
301   while (@s) {
302     $s = shift @s;
303     next if !$dofileprovides && $s =~ /^\//;
304     if ($s =~ /^rpmlib\(/) {
305       splice(@s, 0, 2);
306       next;
307     }
308     push @re, $s;
309     while (@s && $s[0] =~ /^[\(<=>|]/) {
310       $re[-1] .= " $s[0] $s[1]";
311       $re[-1] =~ s/ \((.*)\)/ $1/;
312       $re[-1] =~ s/(<|>){2}/$1/;
313       splice(@s, 0, 2);
314     }
315   }
316   @s = split(' ', $con{$packs{$pack}} || '');
317   while (@s) {
318     $s = shift @s;
319     next if !$dofileprovides && $s =~ /^\//;
320     push @co, $s;
321     while (@s && $s[0] =~ /^[\(<=>|]/) {
322       $co[-1] .= " $s[0] $s[1]";
323       $co[-1] =~ s/ \((.*)\)/ $1/;
324       $co[-1] =~ s/(<|>){2}/$1/;
325       splice(@s, 0, 2);
326     }
327   }
328   @s = split(' ', $obs{$packs{$pack}} || '');
329   while (@s) {
330     $s = shift @s;
331     next if !$dofileprovides && $s =~ /^\//;
332     push @ob, $s;
333     while (@s && $s[0] =~ /^[\(<=>|]/) {
334       $ob[-1] .= " $s[0] $s[1]";
335       $ob[-1] =~ s/ \((.*)\)/ $1/;
336       $ob[-1] =~ s/(<|>){2}/$1/;
337       splice(@s, 0, 2);
338     }
339   }
340   @s = split(' ', $rec{$packs{$pack}} || '');
341   while (@s) {
342     $s = shift @s;
343     next if !$dofileprovides && $s =~ /^\//;
344     if ($s =~ /^rpmlib\(/) {
345       splice(@s, 0, 2);
346       next;
347     }
348     push @rc, $s;
349     while (@s && $s[0] =~ /^[\(<=>|]/) {
350       $rc[-1] .= " $s[0] $s[1]";
351       $rc[-1] =~ s/ \((.*)\)/ $1/;
352       $rc[-1] =~ s/(<|>){2}/$1/;
353       splice(@s, 0, 2);
354     }
355   }
356   @s = split(' ', $sup{$packs{$pack}} || '');
357   while (@s) {
358     $s = shift @s;
359     next if !$dofileprovides && $s =~ /^\//;
360     if ($s =~ /^rpmlib\(/) {
361       splice(@s, 0, 2);
362       next;
363     }
364     push @su, $s;
365     while (@s && $s[0] =~ /^[\(<=>|]/) {
366       $su[-1] .= " $s[0] $s[1]";
367       $su[-1] =~ s/ \((.*)\)/ $1/;
368       $su[-1] =~ s/(<|>){2}/$1/;
369       splice(@s, 0, 2);
370     }
371   }
372   $r->{'provides'} = \@pr;
373   $r->{'requires'} = \@re;
374   $r->{'conflicts'} = \@co;
375   $r->{'obsoletes'} = \@ob;
376   $r->{'recommends'} = \@rc;
377   $r->{'supplements'} = \@su;
378   $repo{$pack} = $r;
379 }
380
381
382 #######################################################################
383
384 sub print_rpmlist {
385   for (@_) {
386     print "$_ $fn{$packs{$_}}\n";
387     print "rpmid: $_:$ids{$packs{$_}}\n" if exists $ids{$packs{$_}};
388   }
389   print "preinstall: @{$cf->{'preinstall'} || []}\n";
390   print "vminstall: @{$cf->{'vminstall'} || []}\n";
391   print "runscripts: @{$cf->{'runscripts'} || []}\n";
392   print "dist: $dist\n" if defined $dist;
393   print "installonly: $installonly\n" if defined $installonly;
394   print "noinstall: $noinstall\n" if defined $noinstall;
395 }
396
397 if ($useusedforbuild) {
398   die("Need a recipe file for --usedforbuild\n") unless defined $recipe;
399   local *F;
400   open(F, '<', $recipe) || die("$recipe: $!\n");
401   my @usedforbuild;
402   my @buildrequires;
403   while(<F>) {
404     chomp;
405     if (/^#\s*usedforbuild\s*(.*)$/) {
406       push @usedforbuild, split(' ', $1);
407     }
408     if (/^buildrequires:\s*(.*)$/i) {
409       push @buildrequires, split(' ', $1);
410     }
411   }
412   close F;
413   @usedforbuild = @buildrequires unless @usedforbuild;
414   @usedforbuild = Build::unify(@usedforbuild) if @usedforbuild;
415   my @errors;
416   for (@usedforbuild) {
417     push @errors, "package $_ not found" unless $packs{$_} && $fn{$packs{$_}};
418   }
419   if (@errors) {
420     print STDERR "expansion error\n";
421     print STDERR "  $_\n" for @errors;
422     exit(1);
423   }
424   print_rpmlist(@usedforbuild);
425   exit(0);
426 }
427
428 #######################################################################
429
430 my ($packname, $packvers, $subpacks, @packdeps);
431 $subpacks = [];
432
433 if ($recipe) {
434   my $d = Build::parse($cf, $recipe) || {};
435   my $buildtype = Build::recipe2buildtype($recipe) || '';
436   $cf->{'type'} = $buildtype if $buildtype;
437   if ($buildtype eq 'kiwi') {
438     # lets see if this is a product or image build
439     my $type = $d->{'imagetype'} && $d->{'imagetype'}->[0] eq 'product' ? 'product' : 'image';
440     my @kdeps;
441     if ($type eq 'image') {
442       @kdeps = @{$cf->{'substitute'}->{'kiwi-setup:image'} || []};
443       @kdeps = ('kiwi', 'createrepo', 'tar') unless @kdeps;
444     } else {
445       @kdeps = @{$cf->{'substitute'}->{'kiwi-setup:product'} || []};
446       @kdeps = ('kiwi') unless @kdeps;
447     }
448     push @kdeps, grep {/^kiwi-.*:/} @{$d->{'deps'} || []};
449     $d = { 'deps' => \@kdeps, 'subpacks' => [] };
450   }
451   $packname = $d->{'name'};
452   $packvers = $d->{'version'};
453   $subpacks = $d->{'subpacks'};
454   @packdeps = @{$d->{'deps'} || []};
455   if ($d->{'prereqs'}) {
456     my %deps = map {$_ => 1} (@packdeps, @{$d->{'subpacks'} || []});
457     push @packdeps, grep {!$deps{$_} && !/^%/} @{$d->{'prereqs'}};
458   }
459 }
460
461 Build::readdeps($cf, undef, \%repo);
462
463 #######################################################################
464
465 if ($isvm) {
466   push @packdeps, @{$cf->{'vminstall'}};
467 }
468 my @bdeps = Build::get_build($cf, $subpacks, @packdeps, @extradeps);
469
470 if (!shift @bdeps) {
471   print STDERR "expansion error\n";
472   print STDERR "  $_\n" for @bdeps;
473   exit(1);
474 }
475
476 my @sysdeps = Build::get_sysbuild($cf);
477 if (@sysdeps) {
478   if (!shift @sysdeps) {
479     print STDERR "expansion error\n";
480     print STDERR "  $_\n" for @sysdeps;
481     exit(1);
482   }
483   my %sysdeps = map {$_ => 1} @sysdeps;
484   my %bdeps = map {$_ => 1} @bdeps;
485   $installonly = join(' ', grep {!$bdeps{$_}} @sysdeps);
486   $noinstall = join(' ', grep {!$sysdeps{$_}} @bdeps);
487   @bdeps = Build::unify(@sysdeps, @bdeps);
488 }
489
490 # make sure all preinstalls are in bdeps;
491 # XXX: also add vmdeps?
492 @bdeps = Build::unify(@bdeps, Build::get_preinstalls($cf));
493 if ($isvm) {
494   @bdeps = Build::unify(@bdeps, Build::get_vminstalls($cf));
495 }
496
497 print_rpmlist(@bdeps);