fix submenu sorting
[openwrt/openwrt.git] / scripts / gen_menuconfig.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 $src;
12 my $makefile;
13 my $pkg;
14 my %package;
15 my %category;
16
17
18 sub find_dep($$) {
19 my $pkg = shift;
20 my $name = shift;
21 my $deps = ($pkg->{vdepends} or $pkg->{depends});
22
23 return 0 unless defined $deps;
24 foreach my $dep (@{$deps}) {
25 return 1 if $dep eq $name;
26 return 1 if ($package{$dep} and (find_dep($package{$dep},$name) == 1));
27 }
28 return 0;
29 }
30
31 sub depends($$) {
32 my $a = shift;
33 my $b = shift;
34 my $ret;
35
36 return 0 if ($a->{submenu} ne $b->{submenu});
37 if (find_dep($a, $b->{name}) == 1) {
38 $ret = 1;
39 } elsif (find_dep($b, $a->{name}) == 1) {
40 $ret = -1;
41 } else {
42 return 0;
43 }
44 # print STDERR "depends($a->{name}, $b->{name}) == $ret\n";
45 return $ret;
46 }
47
48
49 sub print_category($) {
50 my $cat = shift;
51 my %menus;
52 my %menu_dep;
53
54 return unless $category{$cat};
55
56 print "menu \"$cat\"\n\n";
57 my %spkg = %{$category{$cat}};
58
59 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
60 foreach my $pkg (@{$spkg{$spkg}}) {
61 my $menu = $pkg->{submenu};
62 if ($menu) {
63 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
64 } else {
65 $menu = 'undef';
66 }
67 $menus{$menu} or $menus{$menu} = [];
68 push @{$menus{$menu}}, $pkg;
69 }
70 }
71 my @menus = sort {
72 ($a eq 'undef' ? 1 : 0) or
73 ($b eq 'undef' ? -1 : 0) or
74 ($a cmp $b)
75 } keys %menus;
76
77 foreach my $menu (@menus) {
78 my @pkgs = sort {
79 depends($a, $b) or
80 ($a->{name} cmp $b->{name})
81 } @{$menus{$menu}};
82 if ($menu ne 'undef') {
83 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
84 print "menu \"$menu\"\n";
85 }
86 foreach my $pkg (@pkgs) {
87 my $title = $pkg->{name};
88 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
89 if ($c > 0) {
90 $title .= ("." x $c). " ". $pkg->{title};
91 }
92 print "\t";
93 $pkg->{menu} and print "menu";
94 print "config PACKAGE_".$pkg->{name}."\n";
95 print "\t\ttristate \"$title\"\n";
96 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
97 print "\t\tdefault $default\n";
98 }
99 foreach my $depend (@{$pkg->{depends}}) {
100 my $m = "depends";
101 $depend =~ s/^([@\+]+)//;
102 my $flags = $1;
103 my $vdep;
104
105 if ($vdep = $package{$depend}->{vdepends}) {
106 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
107 } else {
108 $flags =~ /@/ or $depend = "PACKAGE_$depend";
109 $flags =~ /\+/ and $m = "select";
110 }
111 print "\t\t$m $depend\n";
112 }
113 print "\t\thelp\n";
114 print $pkg->{description};
115 print "\n";
116
117 $pkg->{config} and print $pkg->{config}."\n";
118 }
119 if ($menu ne 'undef') {
120 print "endmenu\n";
121 $menu_dep{$menu} and print "endif\n";
122 }
123 }
124 print "endmenu\n\n";
125
126 undef $category{$cat};
127 }
128
129 my $line;
130 while ($line = <>) {
131 chomp $line;
132 $line =~ /^Source-Makefile: \s*(.+\/([^\/]+)\/Makefile)\s*$/ and do {
133 $makefile = $1;
134 $src = $2;
135 undef $pkg;
136 };
137 $line =~ /^Package: \s*(.+)\s*$/ and do {
138 $pkg = {};
139 $pkg->{src} = $src;
140 $pkg->{makefile} = $makefile;
141 $pkg->{name} = $1;
142 $pkg->{default} = "m if ALL";
143 $package{$1} = $pkg;
144 };
145 $line =~ /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
146 $line =~ /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
147 $line =~ /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
148 $line =~ /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
149 $line =~ /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
150 $line =~ /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
151 $line =~ /^Provides: \s*(.+)\s*$/ and do {
152 my @vpkg = split /\s+/, $1;
153 foreach my $vpkg (@vpkg) {
154 $package{$vpkg} or $package{$vpkg} = { vdepends => [] };
155 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
156 }
157 };
158 $line =~ /^Depends: \s*(.+)\s*$/ and do {
159 my @dep = split /\s+/, $1;
160 $pkg->{depends} = \@dep;
161 };
162 $line =~ /^Category: \s*(.+)\s*$/ and do {
163 $pkg->{category} = $1;
164 defined $category{$1} or $category{$1} = {};
165 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
166 push @{$category{$1}->{$src}}, $pkg;
167 };
168 $line =~ /^Description: \s*(.*)\s*$/ and do {
169 my $desc = "\t\t$1\n\n";
170 my $line;
171 while ($line = <>) {
172 last if $line =~ /^@@/;
173 $desc .= "\t\t$line";
174 }
175 $pkg->{description} = $desc;
176 };
177 $line =~ /^Config: \s*(.*)\s*$/ and do {
178 my $conf = "$1\n";
179 my $line;
180 while ($line = <>) {
181 last if $line =~ /^@@/;
182 $conf .= "$line";
183 }
184 $pkg->{config} = $conf;
185 }
186 }
187
188 print_category 'Base system';
189 foreach my $cat (keys %category) {
190 print_category $cat;
191 }