fix submenu sorting
[openwrt.git] / scripts / gen_deps.pl
1 #!/usr/bin/perl
2
3 # Copyright (C) 2006 OpenWrt.org
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8
9 use strict;
10
11 my $name;
12 my $src;
13 my $makefile;
14 my %conf;
15 my %pkg;
16 my %prereq;
17 my %dep;
18 my %options;
19 my $opt;
20
21 while ($opt = shift @ARGV) {
22         $opt =~ /^-s/ and $options{SDK} = 1;
23 }
24
25 my $line;
26 while ($line = <>) {
27         chomp $line;
28         $line =~ /^Source-Makefile: \s*(.+\/([^\/]+)\/Makefile)\s*$/ and do {
29                 $makefile = $1;
30                 $src = $2;
31         };
32         $line =~ /^Package: \s*(.+)\s*$/ and do {
33                 $name = $1;
34                 defined $pkg{$name} or $pkg{$name} = {};
35                 $pkg{$name}->{src} = $src;
36         };
37         $line =~ /^Provides: \s*(.+)\s*$/ and do {
38                 foreach my $vpkg (split /\s+/, $1) {
39                         defined $pkg{$vpkg} or $pkg{$vpkg} = {};
40                         $pkg{$vpkg}->{virtual} = 1;
41                 }
42         };
43         $line =~ /^Prereq-Check:/ and !defined $prereq{$src} and do {
44                 $pkg{$name}->{prereq} = 1;
45         };
46         $line =~ /^(Build-)?Depends: \s*(.+)\s*$/ and do {
47                 $pkg{$name}->{depends} ||= [];
48                 foreach my $v (split /\s+/, $2) {
49                         next if $v =~ /^[\+]?@/;
50                         $v =~ s/^\+//;
51                         push @{$pkg{$name}->{depends}}, $v;
52                 }
53         };
54 }
55
56 $line="";
57
58 foreach $name (sort {uc($a) cmp uc($b)} keys %pkg) {
59         my $config;
60         
61         next if defined $pkg{$name}->{virtual};
62         if ($options{SDK}) {
63                 $conf{$pkg{$name}->{src}} or do {
64                         $config = 'm';
65                         $conf{$pkg{$name}->{src}} = 1;
66                 };
67         } else {
68                 $config = "\$(CONFIG_PACKAGE_$name)"
69         }
70         if ($config) {
71                 print "package-$config += $pkg{$name}->{src}\n";
72                 $pkg{$name}->{prereq} and print "prereq-$config += $pkg{$name}->{src}\n";
73         }
74
75         my $hasdeps = 0;
76         my $depline = "";
77         foreach my $dep (@{$pkg{$name}->{depends}}) {
78                 my $idx;
79                 next if defined $pkg{$dep}->{virtual};
80                 if (defined $pkg{$dep}->{src}) {
81                         ($pkg{$name}->{src} ne $pkg{$dep}->{src}) and $idx = $pkg{$dep}->{src};
82                 } elsif (defined($pkg{$dep}) && !$options{SDK}) {
83                         $idx = $dep;
84                 }
85                 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
86                 if ($idx) {
87                         next if $dep{$pkg{$name}->{src}."->".$idx};
88                         $depline .= " $idx\-compile";
89                         $dep{$pkg{$name}->{src}."->".$idx} = 1;
90                 }
91         }
92         if ($depline ne "") {
93                 $line .= "$pkg{$name}->{src}-compile: $depline\n";
94         }
95 }
96
97 if ($line ne "") {
98         print "\n$line";
99 }