7496f33a7d6723e1ca3080a7ec7ec7c3fd426f1f
[openwrt-10.03/.git] / scripts / metadata.pl
1 #!/usr/bin/env perl
2 use FindBin;
3 use lib "$FindBin::Bin";
4 use strict;
5 use metadata;
6
7 my %board;
8
9 sub confstr($) {
10         my $conf = shift;
11         $conf =~ tr#/\.\-/#___#;
12         return $conf;
13 }
14
15 sub parse_target_metadata() {
16         my $file = shift @ARGV;
17         my ($target, @target, $profile);
18         my %target;
19
20         open FILE, "<$file" or do {
21                 warn "Can't open file '$file': $!\n";
22                 return;
23         };
24         while (<FILE>) {
25                 chomp;
26                 /^Target:\s*(.+)\s*$/ and do {
27                         my $name = $1;
28                         $target = {
29                                 id => $name,
30                                 board => $name,
31                                 boardconf => confstr($name),
32                                 conf => confstr($name),
33                                 profiles => [],
34                                 features => [],
35                                 depends => [],
36                                 subtargets => []
37                         };
38                         push @target, $target;
39                         $target{$name} = $target;
40                         if ($name =~ /([^\/]+)\/([^\/]+)/) {
41                                 push @{$target{$1}->{subtargets}}, $2;
42                                 $target->{board} = $1;
43                                 $target->{boardconf} = confstr($1);
44                                 $target->{subtarget} = 1;
45                                 $target->{parent} = $target{$1};
46                         }
47                 };
48                 /^Target-Kernel:\s*(\d+\.\d+)\s*$/ and $target->{kernel} = $1;
49                 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
50                 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
51                 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
52                 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
53                 /^Target-Depends:\s*(.+)\s*$/ and $target->{depends} = [ split(/\s+/, $1) ];
54                 /^Target-Description:/ and $target->{desc} = get_multiline(*FILE);
55                 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
56                 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
57                 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
58                 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
59                 /^Target-Profile:\s*(.+)\s*$/ and do {
60                         $profile = {
61                                 id => $1,
62                                 name => $1,
63                                 packages => []
64                         };
65                         push @{$target->{profiles}}, $profile;
66                 };
67                 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
68                 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
69                 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
70                 /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
71                 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
72         }
73         close FILE;
74         foreach my $target (@target) {
75                 next if @{$target->{subtargets}} > 0;
76                 @{$target->{profiles}} > 0 or $target->{profiles} = [
77                         {
78                                 id => 'Default',
79                                 name => 'Default',
80                                 packages => []
81                         }
82                 ];
83         }
84         return @target;
85 }
86
87 sub gen_kconfig_overrides() {
88         my %config;
89         my %kconfig;
90         my $package;
91         my $pkginfo = shift @ARGV;
92         my $cfgfile = shift @ARGV;
93
94         # parameter 2: build system config
95         open FILE, "<$cfgfile" or return;
96         while (<FILE>) {
97                 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
98         }
99         close FILE;
100
101         # parameter 1: package metadata
102         open FILE, "<$pkginfo" or return;
103         while (<FILE>) {
104                 /^Package:\s*(.+?)\s*$/ and $package = $1;
105                 /^Kernel-Config:\s*(.+?)\s*$/ and do {
106                         my @config = split /\s+/, $1;
107                         foreach my $config (@config) {
108                                 my $val = 'm';
109                                 my $override;
110                                 if ($config =~ /^(.+?)=(.+)$/) {
111                                         $config = $1;
112                                         $override = 1;
113                                         $val = $2;
114                                 }
115                                 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
116                                         $kconfig{$config} = $val;
117                                 } elsif (!$override) {
118                                         $kconfig{$config} or $kconfig{$config} = 'n';
119                                 }
120                         }
121                 };
122         };
123         close FILE;
124
125         foreach my $kconfig (sort keys %kconfig) {
126                 if ($kconfig{$kconfig} eq 'n') {
127                         print "# $kconfig is not set\n";
128                 } else {
129                         print "$kconfig=$kconfig{$kconfig}\n";
130                 }
131         }
132 }
133
134 sub merge_package_lists($$) {
135         my $list1 = shift;
136         my $list2 = shift;
137         my @l = ();
138         my %pkgs;
139
140         foreach my $pkg (@$list1, @$list2) {
141                 $pkgs{$pkg} = 1;
142         }
143         foreach my $pkg (keys %pkgs) {
144                 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
145         }
146         return sort(@l);
147 }
148
149 sub target_config_features(@) {
150         my $ret;
151
152         while ($_ = shift @_) {
153                 /broken/ and $ret .= "\tdepends BROKEN\n";
154                 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
155                 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
156                 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
157                 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
158                 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
159                 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
160                 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
161                 /ext2/ and $ret .= "\tselect USES_EXT2\n";
162                 /tgz/ and $ret .= "\tselect USES_TGZ\n";
163                 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
164                 /fpu/ and $ret .= "\tselect HAS_FPU\n";
165         }
166         return $ret;
167 }
168
169 sub target_name($) {
170         my $target = shift;
171         my $parent = $target->{parent};
172         if ($parent) {
173                 return $target->{parent}->{name}." - ".$target->{name};
174         } else {
175                 return $target->{name};
176         }
177 }
178
179 sub kver($) {
180         my $v = shift;
181         $v =~ tr/\./_/;
182         $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
183         return $v;
184 }
185
186 sub print_target($) {
187         my $target = shift;
188         my $features = target_config_features(@{$target->{features}});
189         my $help = $target->{desc};
190         my $kernel = $target->{kernel};
191         my $confstr;
192         $kernel =~ tr/./_/;
193
194         chomp $features;
195         $features .= "\n";
196         if ($help =~ /\w+/) {
197                 $help =~ s/^\s*/\t  /mg;
198                 $help = "\thelp\n$help";
199         } else {
200                 undef $help;
201         }
202
203         my $v = kver($target->{version});
204         if (@{$target->{subtargets}} == 0) {
205         $confstr = <<EOF;
206 config TARGET_$target->{conf}
207         bool "$target->{name}"
208         select LINUX_$kernel
209         select LINUX_$v
210 EOF
211         }
212         else {
213                 $confstr = <<EOF;
214 config TARGET_$target->{conf}
215         bool "$target->{name}"
216 EOF
217         }
218         if ($target->{subtarget}) {
219                 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
220         }
221         if (@{$target->{subtargets}} > 0) {
222                 $confstr .= "\tselect HAS_SUBTARGETS\n";
223         } else {
224                 $confstr .= "\tselect $target->{arch}\n";
225                 foreach my $dep (@{$target->{depends}}) {
226                         my $mode = "depends";
227                         my $flags;
228                         my $name;
229
230                         $dep =~ /^([@\+\-]+)(.+)$/;
231                         $flags = $1;
232                         $name = $2;
233
234                         next if $name =~ /:/;
235                         $flags =~ /-/ and $mode = "deselect";
236                         $flags =~ /\+/ and $mode = "select";
237                         $flags =~ /@/ and $confstr .= "\t$mode $name\n";
238                 }
239                 $confstr .= $features;
240         }
241
242         $confstr .= "$help\n\n";
243         print $confstr;
244 }
245
246 sub gen_target_config() {
247         my @target = parse_target_metadata();
248         my %defaults;
249
250         my @target_sort = sort {
251                 target_name($a) cmp target_name($b);
252         } @target;
253
254
255         print <<EOF;
256 choice
257         prompt "Target System"
258         default TARGET_brcm_2_4
259         reset if !DEVEL
260         
261 EOF
262
263         foreach my $target (@target_sort) {
264                 next if $target->{subtarget};
265                 print_target($target);
266         }
267
268         print <<EOF;
269 endchoice
270
271 choice
272         prompt "Subtarget" if HAS_SUBTARGETS
273
274 EOF
275         foreach my $target (@target) {
276                 next unless $target->{subtarget};
277                 print_target($target);
278         }
279
280 print <<EOF;
281 endchoice
282
283 choice
284         prompt "Target Profile"
285
286 EOF
287
288         foreach my $target (@target) {
289                 my $profiles = $target->{profiles};
290
291                 foreach my $profile (@$profiles) {
292                         print <<EOF;
293 config TARGET_$target->{conf}_$profile->{id}
294         bool "$profile->{name}"
295         depends TARGET_$target->{conf}
296 $profile->{config}
297 EOF
298                         $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
299                         my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
300                         foreach my $pkg (@pkglist) {
301                                 print "\tselect DEFAULT_$pkg\n";
302                                 $defaults{$pkg} = 1;
303                         }
304                         my $help = $profile->{desc};
305                         if ($help =~ /\w+/) {
306                                 $help =~ s/^\s*/\t  /mg;
307                                 $help = "\thelp\n$help";
308                         } else {
309                                 undef $help;
310                         }
311                         print "$help\n";
312                 }
313         }
314
315         print <<EOF;
316 endchoice
317
318 config HAS_SUBTARGETS
319         bool
320
321 config TARGET_BOARD
322         string
323
324 EOF
325         foreach my $target (@target) {
326                 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
327         }
328
329         my %kver;
330         foreach my $target (@target) {
331                 my $v = kver($target->{version});
332                 next if $kver{$v};
333                 $kver{$v} = 1;
334                 print <<EOF;
335 config LINUX_$v
336         bool
337 EOF
338         }
339         foreach my $def (sort keys %defaults) {
340                 print "\tconfig DEFAULT_".$def."\n";
341                 print "\t\tbool\n\n";
342         }
343 }
344
345 my %dep_check;
346 sub __find_package_dep($$) {
347         my $pkg = shift;
348         my $name = shift;
349         my $deps = ($pkg->{vdepends} or $pkg->{depends});
350
351         return 0 unless defined $deps;
352         foreach my $dep (@{$deps}) {
353                 next if $dep_check{$dep};
354                 $dep_check{$dep} = 1;
355                 return 1 if $dep eq $name;
356                 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
357         }
358         return 0;
359 }
360
361 # wrapper to avoid infinite recursion
362 sub find_package_dep($$) {
363         my $pkg = shift;
364         my $name = shift;
365
366         %dep_check = ();
367         return __find_package_dep($pkg, $name);
368 }
369
370 sub package_depends($$) {
371         my $a = shift;
372         my $b = shift;
373         my $ret;
374
375         return 0 if ($a->{submenu} ne $b->{submenu});
376         if (find_package_dep($a, $b->{name}) == 1) {
377                 $ret = 1;
378         } elsif (find_package_dep($b, $a->{name}) == 1) {
379                 $ret = -1;
380         } else {
381                 return 0;
382         }
383         return $ret;
384 }
385
386 sub mconf_depends {
387         my $pkgname = shift;
388         my $depends = shift;
389         my $only_dep = shift;
390         my $res;
391         my $dep = shift;
392         my $seen = shift;
393         my $parent_condition = shift;
394         $dep or $dep = {};
395         $seen or $seen = {};
396
397         $depends or return;
398         my @depends = @$depends;
399         foreach my $depend (@depends) {
400                 my $m = "depends";
401                 $depend =~ s/^([@\+]+)//;
402                 my $flags = $1;
403                 my $vdep;
404                 my $condition;
405
406                 if ($depend =~ /^(.+):(.+)$/) {
407                         if ($1 ne "PACKAGE_$pkgname") {
408                                 if ($parent_condition) {
409                                         $condition = "$parent_condition && $1";
410                                 } else {
411                                         $condition = $1;
412                                 }
413                         }
414                         $depend = $2;
415                 }
416                 next if $seen->{$depend};
417                 next if $package{$depend} and $package{$depend}->{buildonly};
418                 $seen->{$depend} = 1;
419                 if ($vdep = $package{$depend}->{vdepends}) {
420                         $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
421                 } else {
422                         $flags =~ /\+/ and do {
423                                 # Menuconfig will not treat 'select FOO' as a real dependency
424                                 # thus if FOO depends on other config options, these dependencies
425                                 # will not be checked. To fix this, we simply emit all of FOO's
426                                 # depends here as well.
427                                 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
428
429                                 $m = "select";
430                                 next if $only_dep;
431                         };
432                         $flags =~ /@/ or $depend = "PACKAGE_$depend";
433                         if ($condition) {
434                                 if ($m =~ /select/) {
435                                         $depend = "$depend if $condition";
436                                 } else {
437                                         $depend = "!($condition) || $depend";
438                                 }
439                         }
440                 }
441                 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
442         }
443         foreach my $depend (keys %$dep) {
444                 my $m = $dep->{$depend};
445                 $res .= "\t\t$m $depend\n";
446         }
447         return $res;
448 }
449
450 sub print_package_config_category($) {
451         my $cat = shift;
452         my %menus;
453         my %menu_dep;
454
455         return unless $category{$cat};
456
457         print "menu \"$cat\"\n\n";
458         my %spkg = %{$category{$cat}};
459
460         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
461                 foreach my $pkg (@{$spkg{$spkg}}) {
462                         next if $pkg->{buildonly};
463                         my $menu = $pkg->{submenu};
464                         if ($menu) {
465                                 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
466                         } else {
467                                 $menu = 'undef';
468                         }
469                         $menus{$menu} or $menus{$menu} = [];
470                         push @{$menus{$menu}}, $pkg;
471                 }
472         }
473         my @menus = sort {
474                 ($a eq 'undef' ?  1 : 0) or
475                 ($b eq 'undef' ? -1 : 0) or
476                 ($a cmp $b)
477         } keys %menus;
478
479         foreach my $menu (@menus) {
480                 my @pkgs = sort {
481                         package_depends($a, $b) or
482                         ($a->{name} cmp $b->{name})
483                 } @{$menus{$menu}};
484                 if ($menu ne 'undef') {
485                         $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
486                         print "menu \"$menu\"\n";
487                 }
488                 foreach my $pkg (@pkgs) {
489                         my $title = $pkg->{name};
490                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
491                         if ($c > 0) {
492                                 $title .= ("." x $c). " ". $pkg->{title};
493                         }
494                         print "\t";
495                         $pkg->{menu} and print "menu";
496                         print "config PACKAGE_".$pkg->{name}."\n";
497                         print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
498                         print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
499                         foreach my $default (split /\s*,\s*/, $pkg->{default}) {
500                                 print "\t\tdefault $default\n";
501                         }
502                         print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
503                         print "\t\thelp\n";
504                         print $pkg->{description};
505                         print "\n";
506
507                         $pkg->{config} and print $pkg->{config}."\n";
508                 }
509                 if ($menu ne 'undef') {
510                         print "endmenu\n";
511                         $menu_dep{$menu} and print "endif\n";
512                 }
513         }
514         print "endmenu\n\n";
515
516         undef $category{$cat};
517 }
518
519 sub gen_package_config() {
520         parse_package_metadata($ARGV[0]) or exit 1;
521         print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n" if %preconfig;
522         foreach my $preconfig (keys %preconfig) {
523                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
524                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
525                         $conf =~ tr/\.-/__/;
526                         print <<EOF
527         config UCI_PRECONFIG_$conf
528                 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
529                 depends PACKAGE_$preconfig
530                 default "$preconfig{$preconfig}->{$cfg}->{default}"
531
532 EOF
533                 }
534         }
535         print_package_config_category 'Base system';
536         foreach my $cat (keys %category) {
537                 print_package_config_category $cat;
538         }
539 }
540
541 sub gen_package_mk() {
542         my %conf;
543         my %dep;
544         my %done;
545         my $line;
546
547         parse_package_metadata($ARGV[0]) or exit 1;
548         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
549                 my $config;
550                 my $pkg = $package{$name};
551                 my @srcdeps;
552
553                 next if defined $pkg->{vdepends};
554
555                 if ($ENV{SDK}) {
556                         $conf{$pkg->{src}} or do {
557                                 $config = 'm';
558                                 $conf{$pkg->{src}} = 1;
559                         };
560                 } else {
561                         $config = "\$(CONFIG_PACKAGE_$name)"
562                 }
563                 if ($config) {
564                         $pkg->{buildonly} and $config = "";
565                         print "package-$config += $pkg->{subdir}$pkg->{src}\n";
566                         $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
567                 }
568
569                 next if $done{$pkg->{src}};
570                 $done{$pkg->{src}} = 1;
571
572                 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
573                         foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
574                                 $dep =~ /@/ or do {
575                                         $dep =~ s/\+//g;
576                                         push @srcdeps, $dep;
577                                 };
578                         }
579                 }
580
581                 my $hasdeps = 0;
582                 my %deplines;
583                 foreach my $deps (@srcdeps) {
584                         my $idx;
585                         my $condition;
586
587                         if ($deps =~ /^(.+):(.+)/) {
588                                 $condition = $1;
589                                 $deps = $2;
590                         }
591
592                         my $pkg_dep = $package{$deps};
593                         my @deps;
594
595                         if ($pkg_dep->{vdepends}) {
596                                 @deps = @{$pkg_dep->{vdepends}};
597                         } else {
598                                 @deps = ($deps);
599                         }
600
601                         foreach my $dep (@deps) {
602                                 $pkg_dep = $package{$deps};
603                                 if (defined $pkg_dep->{src}) {
604                                         ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
605                                 } elsif (defined($srcpackage{$dep})) {
606                                         $idx = $subdir{$dep}.$dep;
607                                 }
608                                 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
609                                 if ($idx) {
610                                         my $depline;
611                                         next if $pkg->{src} eq $pkg_dep->{src};
612                                         next if $dep{$pkg->{src}."->".$idx};
613                                         next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
614                                         my $depstr;
615
616                                         if ($pkg_dep->{vdepends}) {
617                                                 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
618                                                 $dep{$pkg->{src}."->($dep)".$idx} = 1;
619                                         } else {
620                                                 $depstr = "\$(curdir)/$idx/compile";
621                                                 $dep{$pkg->{src}."->".$idx} = 1;
622                                         }
623                                         if ($condition) {
624                                                 if ($condition =~ /^!(.+)/) {
625                                                         $depline = "\$(if \$(CONFIG_$1),,$depstr)";
626                                                 } else {
627                                                         $depline = "\$(if \$(CONFIG_$condition),$depstr)";
628                                                 }
629                                         } else {
630                                                 $depline = $depstr;
631                                         }
632                                         if ($depline) {
633                                                 $deplines{$idx.$dep} = $depline;
634                                         }
635                                 }
636                         }
637                 }
638                 my $depline = join(" ", values %deplines);
639                 if ($depline) {
640                         $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
641                 }
642         }
643
644         if ($line ne "") {
645                 print "\n$line";
646         }
647         foreach my $preconfig (keys %preconfig) {
648                 my $cmds;
649                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
650                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
651                         $conf =~ tr/\.-/__/;
652                         $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
653                 }
654                 next unless $cmds;
655                 print <<EOF
656
657 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
658         ( \\
659 $cmds \\
660         ) > \$@
661         
662 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
663   package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
664 endif
665 EOF
666         }
667 }
668
669 sub gen_package_source() {
670         parse_package_metadata($ARGV[0]) or exit 1;
671         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
672                 my $pkg = $package{$name};
673                 if ($pkg->{name} && $pkg->{source}) {
674                         print "$pkg->{name}: ";
675                         print "$pkg->{source}\n";
676                 }
677         }
678 }
679
680 sub parse_command() {
681         my $cmd = shift @ARGV;
682         for ($cmd) {
683                 /^target_config$/ and return gen_target_config();
684                 /^package_mk$/ and return gen_package_mk();
685                 /^package_config$/ and return gen_package_config();
686                 /^kconfig/ and return gen_kconfig_overrides();
687                 /^package_source$/ and return gen_package_source();
688         }
689         print <<EOF
690 Available Commands:
691         $0 target_config [file]         Target metadata in Kconfig format
692         $0 package_mk [file]            Package metadata in makefile format
693         $0 package_config [file]        Package metadata in Kconfig format
694         $0 kconfig [file] [config]      Kernel config overrides
695         $0 package_source [file]        Package source file information
696
697 EOF
698 }
699
700 parse_command();