move <packages/phone> into own feed <feeds/phone> and add entry to <feeds.conf.default>
[openwrt/svn-archive/archive.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 $confstr = <<EOF;
205 config TARGET_$target->{conf}
206 bool "$target->{name}"
207 select LINUX_$kernel
208 select LINUX_$v
209 EOF
210 if ($target->{subtarget}) {
211 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
212 }
213 if (@{$target->{subtargets}} > 0) {
214 $confstr .= "\tselect HAS_SUBTARGETS\n";
215 } else {
216 $confstr .= "\tselect $target->{arch}\n";
217 foreach my $dep (@{$target->{depends}}) {
218 my $mode = "depends";
219 my $flags;
220 my $name;
221
222 $dep =~ /^([@\+\-]+)(.+)$/;
223 $flags = $1;
224 $name = $2;
225
226 next if $name =~ /:/;
227 $flags =~ /-/ and $mode = "deselect";
228 $flags =~ /\+/ and $mode = "select";
229 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
230 }
231 $confstr .= $features;
232 }
233
234 $confstr .= "$help\n\n";
235 print $confstr;
236 }
237
238 sub gen_target_config() {
239 my @target = parse_target_metadata();
240 my %defaults;
241
242 my @target_sort = sort {
243 target_name($a) cmp target_name($b);
244 } @target;
245
246
247 print <<EOF;
248 choice
249 prompt "Target System"
250 default TARGET_brcm_2_4
251 reset if !DEVEL
252
253 EOF
254
255 foreach my $target (@target_sort) {
256 next if $target->{subtarget};
257 print_target($target);
258 }
259
260 print <<EOF;
261 endchoice
262
263 choice
264 prompt "Subtarget" if HAS_SUBTARGETS
265
266 EOF
267 foreach my $target (@target) {
268 next unless $target->{subtarget};
269 print_target($target);
270 }
271
272 print <<EOF;
273 endchoice
274
275 choice
276 prompt "Target Profile"
277
278 EOF
279
280 foreach my $target (@target) {
281 my $profiles = $target->{profiles};
282
283 foreach my $profile (@$profiles) {
284 print <<EOF;
285 config TARGET_$target->{conf}_$profile->{id}
286 bool "$profile->{name}"
287 depends TARGET_$target->{conf}
288 $profile->{config}
289 EOF
290 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
291 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
292 foreach my $pkg (@pkglist) {
293 print "\tselect DEFAULT_$pkg\n";
294 $defaults{$pkg} = 1;
295 }
296 print "\n";
297 }
298 }
299
300 print <<EOF;
301 endchoice
302
303 config HAS_SUBTARGETS
304 bool
305
306 config TARGET_BOARD
307 string
308
309 EOF
310 foreach my $target (@target) {
311 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
312 }
313
314 my %kver;
315 foreach my $target (@target) {
316 my $v = kver($target->{version});
317 next if $kver{$v};
318 $kver{$v} = 1;
319 print <<EOF;
320 config LINUX_$v
321 bool
322 EOF
323 }
324 foreach my $def (sort keys %defaults) {
325 print "\tconfig DEFAULT_".$def."\n";
326 print "\t\tbool\n\n";
327 }
328 }
329
330 my %dep_check;
331 sub __find_package_dep($$) {
332 my $pkg = shift;
333 my $name = shift;
334 my $deps = ($pkg->{vdepends} or $pkg->{depends});
335
336 return 0 unless defined $deps;
337 foreach my $dep (@{$deps}) {
338 next if $dep_check{$dep};
339 $dep_check{$dep} = 1;
340 return 1 if $dep eq $name;
341 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
342 }
343 return 0;
344 }
345
346 # wrapper to avoid infinite recursion
347 sub find_package_dep($$) {
348 my $pkg = shift;
349 my $name = shift;
350
351 %dep_check = ();
352 return __find_package_dep($pkg, $name);
353 }
354
355 sub package_depends($$) {
356 my $a = shift;
357 my $b = shift;
358 my $ret;
359
360 return 0 if ($a->{submenu} ne $b->{submenu});
361 if (find_package_dep($a, $b->{name}) == 1) {
362 $ret = 1;
363 } elsif (find_package_dep($b, $a->{name}) == 1) {
364 $ret = -1;
365 } else {
366 return 0;
367 }
368 return $ret;
369 }
370
371 sub mconf_depends {
372 my $pkgname = shift;
373 my $depends = shift;
374 my $only_dep = shift;
375 my $res;
376 my $dep = shift;
377 my $seen = shift;
378 my $condition = shift;
379 $dep or $dep = {};
380 $seen or $seen = {};
381
382 $depends or return;
383 my @depends = @$depends;
384 foreach my $depend (@depends) {
385 my $m = "depends";
386 $depend =~ s/^([@\+]+)//;
387 my $flags = $1;
388 my $vdep;
389
390 if ($depend =~ /^(.+):(.+)$/) {
391 if ($1 ne "PACKAGE_$pkgname") {
392 if ($condition) {
393 $condition = "$condition && $1";
394 } else {
395 $condition = $1;
396 }
397 }
398 $depend = $2;
399 }
400 next if $seen->{$depend};
401 next if $package{$depend} and $package{$depend}->{buildonly};
402 $seen->{$depend} = 1;
403 if ($vdep = $package{$depend}->{vdepends}) {
404 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
405 } else {
406 $flags =~ /\+/ and do {
407 # Menuconfig will not treat 'select FOO' as a real dependency
408 # thus if FOO depends on other config options, these dependencies
409 # will not be checked. To fix this, we simply emit all of FOO's
410 # depends here as well.
411 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
412
413 $m = "select";
414 next if $only_dep;
415 };
416 $flags =~ /@/ or $depend = "PACKAGE_$depend";
417 if ($condition) {
418 if ($m =~ /select/) {
419 $depend = "$depend if $condition";
420 } else {
421 $depend = "!($condition) || $depend";
422 }
423 }
424 }
425 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
426 }
427 foreach my $depend (keys %$dep) {
428 my $m = $dep->{$depend};
429 $res .= "\t\t$m $depend\n";
430 }
431 return $res;
432 }
433
434 sub print_package_config_category($) {
435 my $cat = shift;
436 my %menus;
437 my %menu_dep;
438
439 return unless $category{$cat};
440
441 print "menu \"$cat\"\n\n";
442 my %spkg = %{$category{$cat}};
443
444 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
445 foreach my $pkg (@{$spkg{$spkg}}) {
446 next if $pkg->{buildonly};
447 my $menu = $pkg->{submenu};
448 if ($menu) {
449 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
450 } else {
451 $menu = 'undef';
452 }
453 $menus{$menu} or $menus{$menu} = [];
454 push @{$menus{$menu}}, $pkg;
455 }
456 }
457 my @menus = sort {
458 ($a eq 'undef' ? 1 : 0) or
459 ($b eq 'undef' ? -1 : 0) or
460 ($a cmp $b)
461 } keys %menus;
462
463 foreach my $menu (@menus) {
464 my @pkgs = sort {
465 package_depends($a, $b) or
466 ($a->{name} cmp $b->{name})
467 } @{$menus{$menu}};
468 if ($menu ne 'undef') {
469 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
470 print "menu \"$menu\"\n";
471 }
472 foreach my $pkg (@pkgs) {
473 my $title = $pkg->{name};
474 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
475 if ($c > 0) {
476 $title .= ("." x $c). " ". $pkg->{title};
477 }
478 print "\t";
479 $pkg->{menu} and print "menu";
480 print "config PACKAGE_".$pkg->{name}."\n";
481 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
482 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
483 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
484 print "\t\tdefault $default\n";
485 }
486 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
487 print "\t\thelp\n";
488 print $pkg->{description};
489 print "\n";
490
491 $pkg->{config} and print $pkg->{config}."\n";
492 }
493 if ($menu ne 'undef') {
494 print "endmenu\n";
495 $menu_dep{$menu} and print "endif\n";
496 }
497 }
498 print "endmenu\n\n";
499
500 undef $category{$cat};
501 }
502
503 sub gen_package_config() {
504 parse_package_metadata($ARGV[0]) or exit 1;
505 print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n" if %preconfig;
506 foreach my $preconfig (keys %preconfig) {
507 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
508 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
509 $conf =~ tr/\.-/__/;
510 print <<EOF
511 config UCI_PRECONFIG_$conf
512 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
513 depends PACKAGE_$preconfig
514 default "$preconfig{$preconfig}->{$cfg}->{default}"
515
516 EOF
517 }
518 }
519 print_package_config_category 'Base system';
520 foreach my $cat (keys %category) {
521 print_package_config_category $cat;
522 }
523 }
524
525 sub gen_package_mk() {
526 my %conf;
527 my %dep;
528 my %done;
529 my $line;
530
531 parse_package_metadata($ARGV[0]) or exit 1;
532 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
533 my $config;
534 my $pkg = $package{$name};
535 my @srcdeps;
536
537 next if defined $pkg->{vdepends};
538
539 if ($ENV{SDK}) {
540 $conf{$pkg->{src}} or do {
541 $config = 'm';
542 $conf{$pkg->{src}} = 1;
543 };
544 } else {
545 $config = "\$(CONFIG_PACKAGE_$name)"
546 }
547 if ($config) {
548 $pkg->{buildonly} and $config = "";
549 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
550 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
551 }
552
553 next if $done{$pkg->{src}};
554 $done{$pkg->{src}} = 1;
555
556 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
557 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
558 $dep =~ /@/ or do {
559 $dep =~ s/\+//g;
560 push @srcdeps, $dep;
561 };
562 }
563 }
564
565 my $hasdeps = 0;
566 my %deplines;
567 foreach my $deps (@srcdeps) {
568 my $idx;
569 my $condition;
570
571 if ($deps =~ /^(.+):(.+)/) {
572 $condition = $1;
573 $deps = $2;
574 }
575
576 my $pkg_dep = $package{$deps};
577 my @deps;
578
579 if ($pkg_dep->{vdepends}) {
580 @deps = @{$pkg_dep->{vdepends}};
581 } else {
582 @deps = ($deps);
583 }
584
585 foreach my $dep (@deps) {
586 $pkg_dep = $package{$deps};
587 if (defined $pkg_dep->{src}) {
588 ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
589 } elsif (defined($srcpackage{$dep})) {
590 $idx = $subdir{$dep}.$dep;
591 }
592 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
593 if ($idx) {
594 my $depline;
595 next if $pkg->{src} eq $pkg_dep->{src};
596 next if $dep{$pkg->{src}."->".$idx};
597 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
598 my $depstr;
599
600 if ($pkg_dep->{vdepends}) {
601 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
602 $dep{$pkg->{src}."->($dep)".$idx} = 1;
603 } else {
604 $depstr = "\$(curdir)/$idx/compile";
605 $dep{$pkg->{src}."->".$idx} = 1;
606 }
607 if ($condition) {
608 if ($condition =~ /^!(.+)/) {
609 $depline = "\$(if \$(CONFIG_$1),,$depstr)";
610 } else {
611 $depline = "\$(if \$(CONFIG_$condition),$depstr)";
612 }
613 } else {
614 $depline = $depstr;
615 }
616 if ($depline) {
617 $deplines{$idx.$dep} = $depline;
618 }
619 }
620 }
621 }
622 my $depline = join(" ", values %deplines);
623 if ($depline) {
624 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
625 }
626 }
627
628 if ($line ne "") {
629 print "\n$line";
630 }
631 foreach my $preconfig (keys %preconfig) {
632 my $cmds;
633 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
634 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
635 $conf =~ tr/\.-/__/;
636 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
637 }
638 next unless $cmds;
639 print <<EOF
640
641 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
642 ( \\
643 $cmds \\
644 ) > \$@
645
646 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
647 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
648 endif
649 EOF
650 }
651 }
652
653
654 sub parse_command() {
655 my $cmd = shift @ARGV;
656 for ($cmd) {
657 /^target_config$/ and return gen_target_config();
658 /^package_mk$/ and return gen_package_mk();
659 /^package_config$/ and return gen_package_config();
660 /^kconfig/ and return gen_kconfig_overrides();
661 }
662 print <<EOF
663 Available Commands:
664 $0 target_config [file] Target metadata in Kconfig format
665 $0 package_mk [file] Package metadata in makefile format
666 $0 package_config [file] Package metadata in Kconfig format
667 $0 kconfig [file] [config] Kernel config overrides
668
669 EOF
670 }
671
672 parse_command();