implement sorting for menuconfig submenus
[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 %category;
15 my $cur_menu;
16 my $cur_menu_dep;
17
18 sub close_submenu {
19         if ($cur_menu) {
20                 print "endmenu\n";
21                 $cur_menu_dep and do {
22                         print "endif\n";
23                         $cur_menu_dep = undef;
24                 };
25                 undef $cur_menu;
26         } 
27 }
28
29 sub print_category($) {
30         my $cat = shift;
31         
32         return unless $category{$cat};
33         
34         print "menu \"$cat\"\n\n";
35         my %spkg = %{$category{$cat}};
36         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
37                 my @pkgs = sort {
38                         $a->{submenu}."->".$a->{name} cmp $b->{submenu}."->".$b->{name}
39                 } @{$spkg{$spkg}};
40                 foreach my $pkg (@pkgs) {
41                         if ($cur_menu ne $pkg->{submenu}) {
42                                 close_submenu();
43                                 if ($pkg->{submenu}) {
44                                         $cur_menu = $pkg->{submenu};
45                                         $cur_menu_dep = $pkg->{submenudep} and print "if $cur_menu_dep\n";
46                                         print "menu \"$cur_menu\"\n";
47                                 }
48                         }
49                         my $title = $pkg->{name};
50                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
51                         if ($c > 0) {
52                                 $title .= ("." x $c). " ". $pkg->{title};
53                         }
54                         print "\t";
55                         $pkg->{menu} and print "menu";
56                         print "config PACKAGE_".$pkg->{name}."\n";
57                         print "\t\ttristate \"$title\"\n";
58                         foreach my $default (split /\s*,\s*/, $pkg->{default}) {
59                                 print "\t\tdefault $default\n";
60                         }
61                         foreach my $depend (@{$pkg->{depends}}) {
62                                 my $m = "depends";
63                                 $depend =~ s/^([@\+]+)//;
64                                 my $flags = $1;
65                                 $flags =~ /@/ or $depend = "PACKAGE_$depend";
66                                 $flags =~ /\+/ and $m = "select";
67                                 print "\t\t$m $depend\n";
68                         }
69                         print "\t\thelp\n";
70                         print $pkg->{description};
71                         print "\n";
72
73                         $pkg->{config} and print $pkg->{config}."\n";
74                 }
75         }
76         close_submenu();
77         print "endmenu\n\n";
78         
79         undef $category{$cat};
80 }
81
82 my $line;
83 while ($line = <>) {
84         chomp $line;
85         $line =~ /^Source-Makefile: \s*(.+\/([^\/]+)\/Makefile)\s*$/ and do {
86                 $makefile = $1;
87                 $src = $2;
88                 undef $pkg;
89         };
90         $line =~ /^Package: \s*(.+)\s*$/ and do {
91                 $pkg = {};
92                 $pkg->{src} = $src;
93                 $pkg->{makefile} = $makefile;
94                 $pkg->{name} = $1;
95                 $pkg->{default} = "m if ALL";
96         };
97         $line =~ /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
98         $line =~ /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
99         $line =~ /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
100         $line =~ /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
101         $line =~ /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
102         $line =~ /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
103         $line =~ /^Depends: \s*(.+)\s*$/ and do {
104                 my @dep = split /\s+/, $1;
105                 $pkg->{depends} = \@dep;
106         };
107         $line =~ /^Category: \s*(.+)\s*$/ and do {
108                 $pkg->{category} = $1;
109                 defined $category{$1} or $category{$1} = {};
110                 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
111                 push @{$category{$1}->{$src}}, $pkg;
112         };
113         $line =~ /^Description: \s*(.*)\s*$/ and do {
114                 my $desc = "\t\t$1\n\n";
115                 my $line;
116                 while ($line = <>) {
117                         last if $line =~ /^@@/;
118                         $desc .= "\t\t$line";
119                 }
120                 $pkg->{description} = $desc;
121         };
122         $line =~ /^Config: \s*(.*)\s*$/ and do {
123                 my $conf = "$1\n";
124                 my $line;
125                 while ($line = <>) {
126                         last if $line =~ /^@@/;
127                         $conf .= "$line";
128                 }
129                 $pkg->{config} = $conf;
130         }
131 }
132
133 print_category 'Base system';
134 foreach my $cat (keys %category) {
135         print_category $cat;
136 }