include the default profile in target.mk as well - required for the image builder...
[openwrt.git] / scripts / metadata.pl
1 #!/usr/bin/perl
2 use strict;
3 my %package;
4 my %category;
5
6 sub parse_target_metadata() {
7         my ($target, @target, $profile);        
8         while (<>) {
9                 chomp;
10                 /^Target:\s*((.+)-(\d+\.\d+))\s*$/ and do {
11                         my $conf = uc $3.'_'.$2;
12                         $conf =~ tr/\.-/__/;
13                         $target = {
14                                 id => $1,
15                                 conf => $conf,
16                                 board => $2,
17                                 kernel => $3,
18                                 profiles => []
19                         };
20                         push @target, $target;
21                 };
22                 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
23                 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
24                 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
25                 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
26                 /^Target-Description:/ and do {
27                         my $desc;
28                         while (<>) {
29                                 last if /^@@/;
30                                 $desc .= $_;
31                         }
32                         $target->{desc} = $desc;
33                 };
34                 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
35                 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
36                 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
37                 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
38                 /^Target-Profile:\s*(.+)\s*$/ and do {
39                         $profile = {
40                                 id => $1,
41                                 name => $1,
42                                 packages => []
43                         };
44                         push @{$target->{profiles}}, $profile;
45                 };
46                 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
47                 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
48         }
49         foreach my $target (@target) {
50                 @{$target->{profiles}} > 0 or $target->{profiles} = [
51                         {
52                                 id => 'Default',
53                                 name => 'Default',
54                                 packages => []
55                         }
56                 ];
57         }
58         return @target;
59 }
60
61 sub parse_package_metadata() {
62         my $pkg;
63         my $makefile;
64         my $src;
65         while (<>) {
66                 chomp;
67                 /^Source-Makefile: \s*(.+\/([^\/]+)\/Makefile)\s*$/ and do {
68                         $makefile = $1;
69                         $src = $2;
70                         undef $pkg;
71                 };
72                 /^Package: \s*(.+)\s*$/ and do {
73                         $pkg = {};
74                         $pkg->{src} = $src;
75                         $pkg->{makefile} = $makefile;
76                         $pkg->{name} = $1;
77                         $pkg->{default} = "m if ALL";
78                         $package{$1} = $pkg;
79                 };
80                 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
81                 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
82                 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
83                 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
84                 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
85                 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
86                 /^Provides: \s*(.+)\s*$/ and do {
87                         my @vpkg = split /\s+/, $1;
88                         foreach my $vpkg (@vpkg) {
89                                 $package{$vpkg} or $package{$vpkg} = { vdepends => [] };
90                                 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
91                         }
92                 };
93                 /^Depends: \s*(.+)\s*$/ and do {
94                         my @dep = split /\s+/, $1;
95                         $pkg->{depends} = \@dep;
96                 };
97                 /^Category: \s*(.+)\s*$/ and do {
98                         $pkg->{category} = $1;
99                         defined $category{$1} or $category{$1} = {};
100                         defined $category{$1}->{$src} or $category{$1}->{$src} = [];
101                         push @{$category{$1}->{$src}}, $pkg;
102                 };
103                 /^Description: \s*(.*)\s*$/ and do {
104                         my $desc = "\t\t$1\n\n";
105                         my $line;
106                         while ($line = <>) {
107                                 last if $line =~ /^@@/;
108                                 $desc .= "\t\t$line";
109                         }
110                         $pkg->{description} = $desc;
111                 };
112                 /^Config: \s*(.*)\s*$/ and do {
113                         my $conf = "$1\n";
114                         my $line;
115                         while ($line = <>) {
116                                 last if $line =~ /^@@/;
117                                 $conf .= "$line";
118                         }
119                         $pkg->{config} = $conf;
120                 }
121         }
122         return %category;
123 }
124
125
126 sub gen_target_mk() {
127         my @target = parse_target_metadata();
128         
129         @target = sort {
130                 $a->{id} cmp $b->{id}
131         } @target;
132         
133         foreach my $target (@target) {
134                 my ($profiles_def, $profiles_eval);
135                 my $conf = uc $target->{kernel}.'_'.$target->{board};
136                 $conf =~ tr/\.-/__/;
137                 
138                 foreach my $profile (@{$target->{profiles}}) {
139                         $profiles_def .= "
140   define Profile/$conf\_$profile->{id}
141     ID:=$profile->{id}
142     NAME:=$profile->{name}
143     PACKAGES:=".join(" ", @{$profile->{packages}})."
144   endef";
145   $profiles_eval .= "
146 \$(eval \$(call Profile,$conf\_$profile->{id}))"
147                 }
148                 print "
149 ifeq (\$(CONFIG_LINUX_$conf),y)
150   define Target
151     KERNEL:=$target->{kernel}
152     BOARD:=$target->{board}
153     BOARDNAME:=$target->{name}
154     LINUX_VERSION:=$target->{version}
155     LINUX_RELEASE:=$target->{release}
156     LINUX_KARCH:=$target->{karch}
157     DEFAULT_PACKAGES:=".join(" ", @{$target->{packages}})."
158   endef$profiles_def
159 endif$profiles_eval
160
161 "
162         }
163         print "\$(eval \$(call Target))\n";
164 }
165
166 sub target_config_features(@) {
167         my $ret;
168
169         while ($_ = shift @_) {
170                 /broken/ and $ret .= "\tdepends BROKEN\n";
171                 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
172                 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
173                 /atm/ and $ret .= "\tselect ATM_SUPPORT\n";
174                 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
175                 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
176                 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
177                 /ext2/ and $ret .= "\tselect USES_EXT2\n";
178         }
179         return $ret;
180 }
181
182
183 sub gen_target_config() {
184         my @target = parse_target_metadata();
185
186         @target = sort {
187                 $a->{name} cmp $b->{name}
188         } @target;
189         
190         
191         print <<EOF;
192 choice
193         prompt "Target System"
194         default LINUX_2_4_BRCM
195         
196 EOF
197
198         foreach my $target (@target) {
199                 my $features = target_config_features(@{$target->{features}});
200                 my $help = $target->{desc};
201                 my $kernel = $target->{kernel};
202                 $kernel =~ tr/./_/;
203
204                 chomp $features;
205                 $features .= "\n";
206                 if ($help =~ /\w+/) {
207                         $help =~ s/^\s*/\t  /mg;
208                         $help = "\thelp\n$help";
209                 } else {
210                         undef $help;
211                 }
212         
213                 print <<EOF
214 config LINUX_$target->{conf}
215         bool "$target->{name}"
216         select $target->{arch}
217         select LINUX_$kernel
218 $features$help
219
220 EOF
221         }
222
223         print <<EOF;
224 if DEVEL
225
226 config LINUX_2_6_ARM
227         bool "UNSUPPORTED little-endian arm platform"
228         depends BROKEN
229         select LINUX_2_6
230         select arm
231
232 config LINUX_2_6_CRIS
233         bool "UNSUPPORTED cris platform"
234         depends BROKEN
235         select LINUX_2_6
236         select cris
237
238 config LINUX_2_6_M68K
239         bool "UNSUPPORTED m68k platform"
240         depends BROKEN
241         select LINUX_2_6
242         select m68k
243
244 config LINUX_2_6_SH3
245         bool "UNSUPPORTED little-endian sh3 platform"
246         depends BROKEN
247         select LINUX_2_6
248         select sh3
249
250 config LINUX_2_6_SH3EB
251         bool "UNSUPPORTED big-endian sh3 platform"
252         depends BROKEN
253         select LINUX_2_6
254         select sh3eb
255
256 config LINUX_2_6_SH4
257         bool "UNSUPPORTED little-endian sh4 platform"
258         depends BROKEN
259         select LINUX_2_6
260         select sh4
261
262 config LINUX_2_6_SH4EB
263         bool "UNSUPPORTED big-endian sh4 platform"
264         depends BROKEN
265         select LINUX_2_6
266         select sh4eb
267
268 config LINUX_2_6_SPARC
269         bool "UNSUPPORTED sparc platform"
270         depends BROKEN
271         select LINUX_2_6
272         select sparc
273
274 endif
275
276 endchoice
277
278 choice
279         prompt "Target Profile"
280
281 EOF
282         
283         foreach my $target (@target) {
284                 my $profiles = $target->{profiles};
285                 
286                 foreach my $profile (@$profiles) {
287                         print <<EOF;
288 config LINUX_$target->{conf}_$profile->{id}
289         bool "$profile->{name}"
290         depends LINUX_$target->{conf}
291 EOF
292                         foreach my $pkg (@{$target->{packages}}, @{$profile->{packages}}) {
293                                 print "\tselect DEFAULT_$pkg\n";
294                         }
295                         print "\n";
296                 }
297         }
298
299         print "endchoice\n";
300 }
301
302 sub find_package_dep($$) {
303         my $pkg = shift;
304         my $name = shift;
305         my $deps = ($pkg->{vdepends} or $pkg->{depends});
306
307         return 0 unless defined $deps;
308         foreach my $dep (@{$deps}) {
309                 return 1 if $dep eq $name;
310                 return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
311         }
312         return 0;
313 }
314
315 sub package_depends($$) {
316         my $a = shift;
317         my $b = shift;
318         my $ret;
319
320         return 0 if ($a->{submenu} ne $b->{submenu});
321         if (find_package_dep($a, $b->{name}) == 1) {
322                 $ret = 1;
323         } elsif (find_package_dep($b, $a->{name}) == 1) {
324                 $ret = -1;
325         } else {
326                 return 0;
327         }
328         return $ret;
329 }
330
331 sub print_package_config_category($) {
332         my $cat = shift;
333         my %menus;
334         my %menu_dep;
335         
336         return unless $category{$cat};
337         
338         print "menu \"$cat\"\n\n";
339         my %spkg = %{$category{$cat}};
340         
341         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
342                 foreach my $pkg (@{$spkg{$spkg}}) {
343                         my $menu = $pkg->{submenu};
344                         if ($menu) {
345                                 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
346                         } else {
347                                 $menu = 'undef';
348                         }
349                         $menus{$menu} or $menus{$menu} = [];
350                         push @{$menus{$menu}}, $pkg;
351                         print "\tconfig DEFAULT_".$pkg->{name}."\n";
352                         print "\t\tbool\n\n";
353                 }
354         }
355         my @menus = sort {
356                 ($a eq 'undef' ?  1 : 0) or
357                 ($b eq 'undef' ? -1 : 0) or
358                 ($a cmp $b)
359         } keys %menus;
360
361         foreach my $menu (@menus) {
362                 my @pkgs = sort {
363                         package_depends($a, $b) or
364                         ($a->{name} cmp $b->{name})
365                 } @{$menus{$menu}};
366                 if ($menu ne 'undef') {
367                         $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
368                         print "menu \"$menu\"\n";
369                 }
370                 foreach my $pkg (@pkgs) {
371                         my $title = $pkg->{name};
372                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
373                         if ($c > 0) {
374                                 $title .= ("." x $c). " ". $pkg->{title};
375                         }
376                         print "\t";
377                         $pkg->{menu} and print "menu";
378                         print "config PACKAGE_".$pkg->{name}."\n";
379                         print "\t\ttristate \"$title\"\n";
380                         print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
381                         foreach my $default (split /\s*,\s*/, $pkg->{default}) {
382                                 print "\t\tdefault $default\n";
383                         }
384                         foreach my $depend (@{$pkg->{depends}}) {
385                                 my $m = "depends";
386                                 $depend =~ s/^([@\+]+)//;
387                                 my $flags = $1;
388                                 my $vdep;
389                                 
390                                 if ($vdep = $package{$depend}->{vdepends}) {
391                                         $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
392                                 } else {
393                                         $flags =~ /@/ or $depend = "PACKAGE_$depend";
394                                         $flags =~ /\+/ and $m = "select";
395                                 }
396                                 print "\t\t$m $depend\n";
397                         }
398                         print "\t\thelp\n";
399                         print $pkg->{description};
400                         print "\n";
401
402                         $pkg->{config} and print $pkg->{config}."\n";
403                 }
404                 if ($menu ne 'undef') {
405                         print "endmenu\n";
406                         $menu_dep{$menu} and print "endif\n";
407                 }
408         }
409         print "endmenu\n\n";
410         
411         undef $category{$cat};
412 }
413
414 sub gen_package_config() {
415         parse_package_metadata();
416         print_package_config_category 'Base system';
417         foreach my $cat (keys %category) {
418                 print_package_config_category $cat;
419         }
420 }
421
422 sub parse_command() {
423         my $cmd = shift @ARGV;
424         for ($cmd) {
425                 /^target_mk$/ and return gen_target_mk();
426                 /^target_config$/ and return gen_target_config();
427                 /^package_config$/ and return gen_package_config();
428         }
429         print <<EOF
430 Available Commands:
431         $0 target_mk [file]             Target metadata in makefile format
432         $0 target_config [file]         Target metadata in Kconfig format
433         $0 package_config [file]        Package metadata in Kconfig format
434 EOF
435 }
436
437 parse_command();