- split dep and rpm parts into submodules
[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 my ($dist, $rpmdeps, $archs, $configdir, $release, $changelog);
12
13 while (@ARGV)  {
14   if ($ARGV[0] eq '--dist') {
15     shift @ARGV;
16     $dist = shift @ARGV;
17     next;
18   }
19   if ($ARGV[0] eq '--archpath') {
20     shift @ARGV;
21     $archs = shift @ARGV;
22     next;
23   }
24   if ($ARGV[0] eq '--configdir') {
25     shift @ARGV;
26     $configdir = shift @ARGV;
27     next;
28   }
29   if ($ARGV[0] eq '--release') {
30     shift @ARGV;
31     $release = shift @ARGV;
32     next;
33   }
34   if ($ARGV[0] eq '--changelog') {
35     shift @ARGV;
36     $changelog = shift @ARGV;
37     next;
38   }
39   last;
40 }
41 die("Usage: substitutedeps --dist <dist> --archpath <archpath> [--configdir <configdir>] <specin> <specout>\n") unless @ARGV == 2;
42 my $spec = $ARGV[0];
43 my $newspec = $ARGV[1];
44
45 my $cf = Build::read_config_dist($dist, $archs, $configdir);
46
47 #######################################################################
48
49 my $xspec = [];
50 my $d = Build::parse($cf, $spec, $xspec) || {};
51 my @sdeps = @{$d->{'deps'} || []};
52 my @neg = map {substr($_, 1)} grep {/^-/} @{$d->{'deps'} || []};
53 my %neg = map {$_ => 1} @neg;
54 @sdeps = grep {!$neg{$_}} @sdeps;
55 @sdeps = Build::do_subst($cf, @sdeps);
56 @sdeps = grep {!$neg{$_}} @sdeps;
57 my %sdeps = map {$_ => 1} @sdeps;
58
59 open(F, '>', $newspec) || die("$newspec: $!\n");
60
61 my $used;
62 my $inchangelog = 0;
63 for my $l (@$xspec) {
64   $used = 1;
65   if (ref($l)) {
66     if (!defined($l->[1])) {
67       $used = 0;
68       $l = $l->[0];
69     } else {
70       $l = $l->[1];
71     }
72   }
73
74   if ($inchangelog) {
75     $inchangelog = 0 if $l =~ /^\s*%[^%]/;
76     next if $inchangelog;
77   }
78   if ($changelog && ($l =~ /\s*\%changelog\b/)) {
79     $inchangelog = 1;
80     next;
81   }
82
83   $l =~ s/^(Release:\s*).*/$1$release/i if $release;
84
85   if (!$used || ($l !~ /^BuildRequires:/i)) {
86     print F "$l\n";
87     next;
88   }
89
90   my $r = $l;
91   $r =~ s/^[^:]*:\s*//;
92   my @deps = $r =~ /([^\s\[\(,]+)(\s+[<=>]+\s+[^\s\[,]+)?[\s,]*/g;
93   my @ndeps = ();
94   my $replace = 0;
95   my %f2 = @deps;
96   my @f2 = Build::do_subst($cf, grep {!/^-/} keys %f2);
97   %f2 = map {$_ => 1} @f2;
98   delete $f2{$_} for @neg;
99   while (@deps) {
100     my ($pack, $vers) = splice(@deps, 0, 2);
101     $vers = '' unless defined $vers;
102     if ($sdeps{$pack}) {
103       push @ndeps, "$pack$vers";
104       delete $f2{$pack};
105     } else {
106       $replace = 1;
107     }
108   }
109   if (%f2) {
110     push @ndeps, sort keys %f2;
111     $replace = 1
112   }
113   if ($replace) {
114     print F "BuildRequires:  ".join(' ', @ndeps)."\n" if @ndeps;
115   } else {
116     print F "$l\n";
117   }
118 }
119
120 if ($changelog) {
121   print F "%changelog\n";
122   if (open(CF, '<', $changelog)) {
123     while(<CF>) {
124       print F $_;
125     }
126     close CF;
127   }
128 }
129
130 close(F) || die("close: $!\n");
131
132 exit(0);