2527915bcaaf8f01a192cfd19e2c14e2ff62a965
[openwrt-10.03/.git] / scripts / metadata.pl
1 #!/usr/bin/perl
2 use strict;
3 my %preconfig;
4 my %package;
5 my %srcpackage;
6 my %category;
7
8 sub get_multiline {
9         my $prefix = shift;
10         my $str;
11         while (<>) {
12                 last if /^@@/;
13                 s/^\s*//g;
14                 $str .= (($_ and $prefix) ? $prefix . $_ : $_);
15         }
16
17         return $str;
18 }
19
20 sub parse_target_metadata() {
21         my ($target, @target, $profile);        
22         while (<>) {
23                 chomp;
24                 /^Target:\s*((.+)-(\d+\.\d+))\s*$/ and do {
25                         my $conf = uc $3.'_'.$2;
26                         $conf =~ tr/\.-/__/;
27                         $target = {
28                                 id => $1,
29                                 conf => $conf,
30                                 board => $2,
31                                 kernel => $3,
32                                 profiles => []
33                         };
34                         push @target, $target;
35                 };
36                 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
37                 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
38                 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
39                 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
40                 /^Target-Description:/ and $target->{desc} = get_multiline();
41                 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
42                 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
43                 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
44                 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
45                 /^Target-Profile:\s*(.+)\s*$/ and do {
46                         $profile = {
47                                 id => $1,
48                                 name => $1,
49                                 packages => []
50                         };
51                         push @{$target->{profiles}}, $profile;
52                 };
53                 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
54                 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
55                 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline();
56                 /^Target-Profile-Config:/ and $profile->{config} = get_multiline("\t");
57                 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
58         }
59         foreach my $target (@target) {
60                 @{$target->{profiles}} > 0 or $target->{profiles} = [
61                         {
62                                 id => 'Default',
63                                 name => 'Default',
64                                 packages => []
65                         }
66                 ];
67         }
68         return @target;
69 }
70
71 sub parse_package_metadata() {
72         my $pkg;
73         my $makefile;
74         my $preconfig;
75         my $src;
76         while (<>) {
77                 chomp;
78                 /^Source-Makefile: \s*(.+\/([^\/]+)\/Makefile)\s*$/ and do {
79                         $makefile = $1;
80                         $src = $2;
81                         $srcpackage{$src} = [];
82                         undef $pkg;
83                 };
84                 /^Package:\s*(.+?)\s*$/ and do {
85                         $pkg = {};
86                         $pkg->{src} = $src;
87                         $pkg->{makefile} = $makefile;
88                         $pkg->{name} = $1;
89                         $pkg->{default} = "m if ALL";
90                         $pkg->{depends} = [];
91                         $pkg->{builddepends} = [];
92                         $package{$1} = $pkg;
93                         push @{$srcpackage{$src}}, $pkg;
94                 };
95                 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
96                 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
97                 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
98                 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
99                 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
100                 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
101                 /^Provides: \s*(.+)\s*$/ and do {
102                         my @vpkg = split /\s+/, $1;
103                         foreach my $vpkg (@vpkg) {
104                                 $package{$vpkg} or $package{$vpkg} = { vdepends => [] };
105                                 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
106                         }
107                 };
108                 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
109                 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
110                 /^Category: \s*(.+)\s*$/ and do {
111                         $pkg->{category} = $1;
112                         defined $category{$1} or $category{$1} = {};
113                         defined $category{$1}->{$src} or $category{$1}->{$src} = [];
114                         push @{$category{$1}->{$src}}, $pkg;
115                 };
116                 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline("\t\t ");
117                 /^Config: \s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline();
118                 /^Prereq-Check:/ and $pkg->{prereq} = 1;
119                 /^Preconfig:\s*(.+)\s*$/ and do {
120                         my $pkgname = $pkg->{name};
121                         $preconfig{$pkgname} or $preconfig{$pkgname} = [];
122                         $preconfig = {
123                                 id => $1
124                         };
125                         push @{$preconfig{$pkgname}}, $preconfig;
126                 };
127                 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
128                 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
129                 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
130         }
131         return %category;
132 }
133
134
135 sub gen_target_mk() {
136         my @target = parse_target_metadata();
137         
138         @target = sort {
139                 $a->{id} cmp $b->{id}
140         } @target;
141         
142         foreach my $target (@target) {
143                 my ($profiles_def, $profiles_eval);
144                 my $conf = uc $target->{kernel}.'_'.$target->{board};
145                 $conf =~ tr/\.-/__/;
146                 
147                 foreach my $profile (@{$target->{profiles}}) {
148                         $profiles_def .= "
149   define Profile/$conf\_$profile->{id}
150     ID:=$profile->{id}
151     NAME:=$profile->{name}
152     PACKAGES:=".join(" ", @{$profile->{packages}})."\n";
153                         $profile->{kconfig} and $profiles_def .= "    KCONFIG:=1\n";
154                         $profiles_def .= "  endef";
155                         $profiles_eval .= "
156 \$(eval \$(call AddProfile,$conf\_$profile->{id}))"
157                 }
158                 print "
159 ifeq (\$(CONFIG_LINUX_$conf),y)
160   define Target
161     KERNEL:=$target->{kernel}
162     BOARD:=$target->{board}
163     BOARDNAME:=$target->{name}
164     LINUX_VERSION:=$target->{version}
165     LINUX_RELEASE:=$target->{release}
166     LINUX_KARCH:=$target->{karch}
167     DEFAULT_PACKAGES:=".join(" ", @{$target->{packages}})."
168   endef$profiles_def
169 endif$profiles_eval
170
171 "
172         }
173         print "\$(eval \$(call Target))\n";
174 }
175
176 sub target_config_features(@) {
177         my $ret;
178
179         while ($_ = shift @_) {
180                 /broken/ and $ret .= "\tdepends BROKEN\n";
181                 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
182                 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
183                 /atm/ and $ret .= "\tselect ATM_SUPPORT\n";
184                 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
185                 /video/ and $ret .= "\tselect VIDEO_SUPPORT\n";
186                 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
187                 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
188                 /ext2/ and $ret .= "\tselect USES_EXT2\n";
189         }
190         return $ret;
191 }
192
193
194 sub gen_target_config() {
195         my @target = parse_target_metadata();
196
197         @target = sort {
198                 $a->{name} cmp $b->{name}
199         } @target;
200         
201         
202         print <<EOF;
203 choice
204         prompt "Target System"
205         default LINUX_2_4_BRCM
206         reset if !DEVEL
207         
208 EOF
209
210         foreach my $target (@target) {
211                 my $features = target_config_features(@{$target->{features}});
212                 my $help = $target->{desc};
213                 my $kernel = $target->{kernel};
214                 $kernel =~ tr/./_/;
215
216                 chomp $features;
217                 $features .= "\n";
218                 if ($help =~ /\w+/) {
219                         $help =~ s/^\s*/\t  /mg;
220                         $help = "\thelp\n$help";
221                 } else {
222                         undef $help;
223                 }
224         
225                 print <<EOF
226 config LINUX_$target->{conf}
227         bool "$target->{name}"
228         select $target->{arch}
229         select LINUX_$kernel
230 $features$help
231
232 EOF
233         }
234
235         print <<EOF;
236 if DEVEL
237
238 config LINUX_2_6_ARM
239         bool "UNSUPPORTED little-endian arm platform"
240         depends BROKEN
241         select LINUX_2_6
242         select arm
243
244 config LINUX_2_6_CRIS
245         bool "UNSUPPORTED cris platform"
246         depends BROKEN
247         select LINUX_2_6
248         select cris
249
250 config LINUX_2_6_M68K
251         bool "UNSUPPORTED m68k platform"
252         depends BROKEN
253         select LINUX_2_6
254         select m68k
255
256 config LINUX_2_6_SH3
257         bool "UNSUPPORTED little-endian sh3 platform"
258         depends BROKEN
259         select LINUX_2_6
260         select sh3
261
262 config LINUX_2_6_SH3EB
263         bool "UNSUPPORTED big-endian sh3 platform"
264         depends BROKEN
265         select LINUX_2_6
266         select sh3eb
267
268 config LINUX_2_6_SH4
269         bool "UNSUPPORTED little-endian sh4 platform"
270         depends BROKEN
271         select LINUX_2_6
272         select sh4
273
274 config LINUX_2_6_SH4EB
275         bool "UNSUPPORTED big-endian sh4 platform"
276         depends BROKEN
277         select LINUX_2_6
278         select sh4eb
279
280 config LINUX_2_6_SPARC
281         bool "UNSUPPORTED sparc platform"
282         depends BROKEN
283         select LINUX_2_6
284         select sparc
285
286 endif
287
288 endchoice
289
290 choice
291         prompt "Target Profile"
292
293 EOF
294         
295         foreach my $target (@target) {
296                 my $profiles = $target->{profiles};
297                 
298                 foreach my $profile (@$profiles) {
299                         print <<EOF;
300 config LINUX_$target->{conf}_$profile->{id}
301         bool "$profile->{name}"
302         depends LINUX_$target->{conf}
303 $profile->{config}
304 EOF
305                         $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
306                         my %pkgs;
307                         foreach my $pkg (@{$target->{packages}}, @{$profile->{packages}}) {
308                                 $pkgs{$pkg} = 1;
309                         }
310                         foreach my $pkg (keys %pkgs) {
311                                 print "\tselect DEFAULT_$pkg\n" unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
312                         }
313                         print "\n";
314                 }
315         }
316
317         print "endchoice\n";
318 }
319
320 sub find_package_dep($$) {
321         my $pkg = shift;
322         my $name = shift;
323         my $deps = ($pkg->{vdepends} or $pkg->{depends});
324
325         return 0 unless defined $deps;
326         foreach my $dep (@{$deps}) {
327                 return 1 if $dep eq $name;
328                 return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
329         }
330         return 0;
331 }
332
333 sub package_depends($$) {
334         my $a = shift;
335         my $b = shift;
336         my $ret;
337
338         return 0 if ($a->{submenu} ne $b->{submenu});
339         if (find_package_dep($a, $b->{name}) == 1) {
340                 $ret = 1;
341         } elsif (find_package_dep($b, $a->{name}) == 1) {
342                 $ret = -1;
343         } else {
344                 return 0;
345         }
346         return $ret;
347 }
348
349 sub mconf_depends($$) {
350         my $depends = shift;
351         my $only_dep = shift;
352         my $res;
353
354         $depends or return;
355         my @depends = @$depends;
356         foreach my $depend (@depends) {
357                 my $m = "depends";
358                 $depend =~ s/^([@\+]+)//;
359                 my $flags = $1;
360                 my $vdep;
361         
362                 if ($vdep = $package{$depend}->{vdepends}) {
363                         $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
364                 } else {
365                         $flags =~ /\+/ and do {
366                                 next if $only_dep;
367                                 $m = "select";
368
369                                 # Menuconfig will not treat 'select FOO' as a real dependency
370                                 # thus if FOO depends on other config options, these dependencies
371                                 # will not be checked. To fix this, we simply emit all of FOO's
372                                 # depends here as well.
373                                 $package{$depend} and $res .= mconf_depends($package{$depend}->{depends}, 1);
374                         };
375                         $flags =~ /@/ or $depend = "PACKAGE_$depend";
376                 }
377                 $res .= "\t\t$m $depend\n";
378         }
379         return $res;
380 }
381
382 sub print_package_config_category($) {
383         my $cat = shift;
384         my %menus;
385         my %menu_dep;
386         
387         return unless $category{$cat};
388         
389         print "menu \"$cat\"\n\n";
390         my %spkg = %{$category{$cat}};
391         
392         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
393                 foreach my $pkg (@{$spkg{$spkg}}) {
394                         my $menu = $pkg->{submenu};
395                         if ($menu) {
396                                 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
397                         } else {
398                                 $menu = 'undef';
399                         }
400                         $menus{$menu} or $menus{$menu} = [];
401                         push @{$menus{$menu}}, $pkg;
402                         print "\tconfig DEFAULT_".$pkg->{name}."\n";
403                         print "\t\tbool\n\n";
404                 }
405         }
406         my @menus = sort {
407                 ($a eq 'undef' ?  1 : 0) or
408                 ($b eq 'undef' ? -1 : 0) or
409                 ($a cmp $b)
410         } keys %menus;
411
412         foreach my $menu (@menus) {
413                 my @pkgs = sort {
414                         package_depends($a, $b) or
415                         ($a->{name} cmp $b->{name})
416                 } @{$menus{$menu}};
417                 if ($menu ne 'undef') {
418                         $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
419                         print "menu \"$menu\"\n";
420                 }
421                 foreach my $pkg (@pkgs) {
422                         my $title = $pkg->{name};
423                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
424                         if ($c > 0) {
425                                 $title .= ("." x $c). " ". $pkg->{title};
426                         }
427                         print "\t";
428                         $pkg->{menu} and print "menu";
429                         print "config PACKAGE_".$pkg->{name}."\n";
430                         print "\t\ttristate \"$title\"\n";
431                         print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
432                         foreach my $default (split /\s*,\s*/, $pkg->{default}) {
433                                 print "\t\tdefault $default\n";
434                         }
435                         print mconf_depends($pkg->{depends}, 0);
436                         print "\t\thelp\n";
437                         print $pkg->{description};
438                         print "\n";
439
440                         $pkg->{config} and print $pkg->{config}."\n";
441                 }
442                 if ($menu ne 'undef') {
443                         print "endmenu\n";
444                         $menu_dep{$menu} and print "endif\n";
445                 }
446         }
447         print "endmenu\n\n";
448         
449         undef $category{$cat};
450 }
451
452 sub gen_package_config() {
453         parse_package_metadata();
454         print "menu \"Image configuration\"\n";
455         foreach my $preconfig (keys %preconfig) {
456                 print "\tcomment \"$preconfig\"\n";
457                 foreach my $cfg (@{$preconfig{$preconfig}}) {
458                         my $conf = $cfg->{id};
459                         $conf =~ tr/\.-/__/;
460                         print <<EOF
461         config UCI_PRECONFIG_$conf
462                 string "$cfg->{label}"
463                 depends PACKAGE_$preconfig
464                 default "$cfg->{default}"
465
466 EOF
467                 }
468         }
469         print "endmenu\n\n";
470         print_package_config_category 'Base system';
471         foreach my $cat (keys %category) {
472                 print_package_config_category $cat;
473         }
474 }
475
476 sub gen_package_mk() {
477         my %conf;
478         my %dep;
479         my $line;
480
481         parse_package_metadata();
482         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
483                 my $config;
484                 my $pkg = $package{$name};
485                 
486                 next if defined $pkg->{vdepends};
487                 if ($ENV{SDK}) {
488                         $conf{$pkg->{src}} or do {
489                                 $config = 'm';
490                                 $conf{$pkg->{src}} = 1;
491                         };
492                 } else {
493                         $config = "\$(CONFIG_PACKAGE_$name)"
494                 }
495                 if ($config) {
496                         print "package-$config += $pkg->{src}\n";
497                         $pkg->{prereq} and print "prereq-$config += $pkg->{src}\n";
498                 }
499         
500                 my $hasdeps = 0;
501                 my $depline = "";
502                 foreach my $dep (@{$pkg->{depends}}, @{$pkg->{builddepends}}) {
503                         next if $dep =~ /@/;
504                         $dep =~ s/\+//;
505                         my $idx;
506                         my $pkg_dep = $package{$dep};
507                         $pkg_dep or $pkg_dep = $srcpackage{$dep}->[0];
508                         next unless defined $pkg_dep;
509                         next if defined $pkg_dep->{vdepends};
510
511                         if (defined $pkg_dep->{src}) {
512                                 ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{src};
513                         } elsif (defined($pkg_dep) && !defined($ENV{SDK})) {
514                                 $idx = $dep;
515                         }
516                         undef $idx if $idx =~ /^(kernel)|(base-files)$/;
517                         if ($idx) {
518                                 next if $dep{$pkg->{src}."->".$idx};
519                                 $depline .= " $idx\-compile";
520                                 $dep{$pkg->{src}."->".$idx} = 1;
521                         }
522                 }
523                 if ($depline) {
524                         $line .= "$pkg->{src}-compile: $depline\n";
525                 }
526         }
527         
528         if ($line ne "") {
529                 print "\n$line";
530         }
531         foreach my $preconfig (keys %preconfig) {
532                 my $cmds;
533                 foreach my $cfg (@{$preconfig{$preconfig}}) {
534                         my $conf = $cfg->{id};
535                         $conf =~ tr/\.-/__/;
536                         $cmds .= "\techo \"uci set '$cfg->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
537                 }
538                 next unless $cmds;
539                 print <<EOF
540
541 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
542         ( \\
543 $cmds \\
544         ) > \$@
545
546 preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
547 EOF
548         }
549 }
550
551
552 sub parse_command() {
553         my $cmd = shift @ARGV;
554         for ($cmd) {
555                 /^target_mk$/ and return gen_target_mk();
556                 /^target_config$/ and return gen_target_config();
557                 /^package_mk$/ and return gen_package_mk();
558                 /^package_config$/ and return gen_package_config();
559         }
560         print <<EOF
561 Available Commands:
562         $0 target_mk [file]             Target metadata in makefile format
563         $0 target_config [file]         Target metadata in Kconfig format
564         $0 package_mk [file]            Package metadata in makefile format
565         $0 package_config [file]        Package metadata in Kconfig format
566 EOF
567 }
568
569 parse_command();