kirkwood: set default EA4500 switch config
[openwrt.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-Name:\s*(.+)\s*$/ and $target->{name} = $1;
49                 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
50                 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
51                 /^Target-Arch-Packages:\s*(.+)\s*$/ and $target->{arch_packages} = $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                 /^Target-Optimization:\s*(.+)\s*$/ and $target->{cflags} = $1;
56                 /^CPU-Type:\s*(.+)\s*$/ and $target->{cputype} = $1;
57                 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
58                 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
59                 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
60                 /^Default-Subtarget:\s*(.+)\s*$/ and $target->{def_subtarget} = $1;
61                 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
62                 /^Target-Profile:\s*(.+)\s*$/ and do {
63                         $profile = {
64                                 id => $1,
65                                 name => $1,
66                                 packages => []
67                         };
68                         push @{$target->{profiles}}, $profile;
69                 };
70                 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
71                 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
72                 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
73                 /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
74                 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
75         }
76         close FILE;
77         foreach my $target (@target) {
78                 if (@{$target->{subtargets}} > 0) {
79                         $target->{profiles} = [];
80                         next;
81                 }
82                 @{$target->{profiles}} > 0 or $target->{profiles} = [
83                         {
84                                 id => 'Default',
85                                 name => 'Default',
86                                 packages => []
87                         }
88                 ];
89         }
90         return @target;
91 }
92
93 sub gen_kconfig_overrides() {
94         my %config;
95         my %kconfig;
96         my $package;
97         my $pkginfo = shift @ARGV;
98         my $cfgfile = shift @ARGV;
99
100         # parameter 2: build system config
101         open FILE, "<$cfgfile" or return;
102         while (<FILE>) {
103                 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
104         }
105         close FILE;
106
107         # parameter 1: package metadata
108         open FILE, "<$pkginfo" or return;
109         while (<FILE>) {
110                 /^Package:\s*(.+?)\s*$/ and $package = $1;
111                 /^Kernel-Config:\s*(.+?)\s*$/ and do {
112                         my @config = split /\s+/, $1;
113                         foreach my $config (@config) {
114                                 my $val = 'm';
115                                 my $override;
116                                 if ($config =~ /^(.+?)=(.+)$/) {
117                                         $config = $1;
118                                         $override = 1;
119                                         $val = $2;
120                                 }
121                                 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
122                                         next if $kconfig{$config} eq 'y';
123                                         $kconfig{$config} = $val;
124                                 } elsif (!$override) {
125                                         $kconfig{$config} or $kconfig{$config} = 'n';
126                                 }
127                         }
128                 };
129         };
130         close FILE;
131
132         foreach my $kconfig (sort keys %kconfig) {
133                 if ($kconfig{$kconfig} eq 'n') {
134                         print "# $kconfig is not set\n";
135                 } else {
136                         print "$kconfig=$kconfig{$kconfig}\n";
137                 }
138         }
139 }
140
141 sub merge_package_lists($$) {
142         my $list1 = shift;
143         my $list2 = shift;
144         my @l = ();
145         my %pkgs;
146
147         foreach my $pkg (@$list1, @$list2) {
148                 $pkgs{$pkg} = 1;
149         }
150         foreach my $pkg (keys %pkgs) {
151                 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
152         }
153         return sort(@l);
154 }
155
156 sub target_config_features(@) {
157         my $ret;
158
159         while ($_ = shift @_) {
160                 /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
161                 /broken/ and $ret .= "\tdepends on BROKEN\n";
162                 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
163                 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
164                 /dt/ and $ret .= "\tselect USES_DEVICETREE\n";
165                 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
166                 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
167                 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
168                 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
169                 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
170                 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
171                 /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
172                 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
173                 /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
174                 /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
175                 /ext4/ and $ret .= "\tselect USES_EXT4\n";
176                 /targz/ and $ret .= "\tselect USES_TARGZ\n";
177                 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
178                 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
179                 /fpu/ and $ret .= "\tselect HAS_FPU\n";
180                 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
181                 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
182                 /powerpc64/ and $ret .= "\tselect powerpc64\n";
183                 /nommu/ and $ret .= "\tselect NOMMU\n";
184                 /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
185                 /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
186                 /low_mem/ and $ret .= "\tselect LOW_MEMORY_FOOTPRINT\n";
187                 /nand/ and $ret .= "\tselect NAND_SUPPORT\n";
188         }
189         return $ret;
190 }
191
192 sub target_name($) {
193         my $target = shift;
194         my $parent = $target->{parent};
195         if ($parent) {
196                 return $target->{parent}->{name}." - ".$target->{name};
197         } else {
198                 return $target->{name};
199         }
200 }
201
202 sub kver($) {
203         my $v = shift;
204         $v =~ tr/\./_/;
205         if (substr($v,0,2) eq "2_") {
206                 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
207         } else {
208                 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
209         }
210         return $v;
211 }
212
213 sub print_target($) {
214         my $target = shift;
215         my $features = target_config_features(@{$target->{features}});
216         my $help = $target->{desc};
217         my $confstr;
218
219         chomp $features;
220         $features .= "\n";
221         if ($help =~ /\w+/) {
222                 $help =~ s/^\s*/\t  /mg;
223                 $help = "\thelp\n$help";
224         } else {
225                 undef $help;
226         }
227
228         my $v = kver($target->{version});
229         if (@{$target->{subtargets}} == 0) {
230         $confstr = <<EOF;
231 config TARGET_$target->{conf}
232         bool "$target->{name}"
233         select LINUX_$v
234 EOF
235         }
236         else {
237                 $confstr = <<EOF;
238 config TARGET_$target->{conf}
239         bool "$target->{name}"
240 EOF
241         }
242         if ($target->{subtarget}) {
243                 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
244         }
245         if (@{$target->{subtargets}} > 0) {
246                 $confstr .= "\tselect HAS_SUBTARGETS\n";
247                 grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
248         } else {
249                 $confstr .= $features;
250         }
251
252         if ($target->{arch} =~ /\w/) {
253                 $confstr .= "\tselect $target->{arch}\n";
254         }
255         foreach my $dep (@{$target->{depends}}) {
256                 my $mode = "depends on";
257                 my $flags;
258                 my $name;
259
260                 $dep =~ /^([@\+\-]+)(.+)$/;
261                 $flags = $1;
262                 $name = $2;
263
264                 next if $name =~ /:/;
265                 $flags =~ /-/ and $mode = "deselect";
266                 $flags =~ /\+/ and $mode = "select";
267                 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
268         }
269         $confstr .= "$help\n\n";
270         print $confstr;
271 }
272
273 sub gen_target_config() {
274         my @target = parse_target_metadata();
275         my %defaults;
276
277         my @target_sort = sort {
278                 target_name($a) cmp target_name($b);
279         } @target;
280
281
282         print <<EOF;
283 choice
284         prompt "Target System"
285         default TARGET_ar71xx
286         reset if !DEVEL
287         
288 EOF
289
290         foreach my $target (@target_sort) {
291                 next if $target->{subtarget};
292                 print_target($target);
293         }
294
295         print <<EOF;
296 endchoice
297
298 choice
299         prompt "Subtarget" if HAS_SUBTARGETS
300 EOF
301         foreach my $target (@target) {
302                 next unless $target->{def_subtarget};
303                 print <<EOF;
304         default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
305 EOF
306         }
307         print <<EOF;
308
309 EOF
310         foreach my $target (@target) {
311                 next unless $target->{subtarget};
312                 print_target($target);
313         }
314
315 print <<EOF;
316 endchoice
317
318 choice
319         prompt "Target Profile"
320
321 EOF
322
323         foreach my $target (@target) {
324                 my $profiles = $target->{profiles};
325
326                 foreach my $profile (@$profiles) {
327                         print <<EOF;
328 config TARGET_$target->{conf}_$profile->{id}
329         bool "$profile->{name}"
330         depends on TARGET_$target->{conf}
331 $profile->{config}
332 EOF
333                         $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
334                         my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
335                         foreach my $pkg (@pkglist) {
336                                 print "\tselect DEFAULT_$pkg\n";
337                                 $defaults{$pkg} = 1;
338                         }
339                         my $help = $profile->{desc};
340                         if ($help =~ /\w+/) {
341                                 $help =~ s/^\s*/\t  /mg;
342                                 $help = "\thelp\n$help";
343                         } else {
344                                 undef $help;
345                         }
346                         print "$help\n";
347                 }
348         }
349
350         print <<EOF;
351 endchoice
352
353 config HAS_SUBTARGETS
354         bool
355
356 config TARGET_BOARD
357         string
358
359 EOF
360         foreach my $target (@target) {
361                 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
362         }
363         print <<EOF;
364 config TARGET_ARCH_PACKAGES
365         string
366         
367 EOF
368         foreach my $target (@target) {
369                 next if @{$target->{subtargets}} > 0;
370                 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
371         }
372         print <<EOF;
373
374 config DEFAULT_TARGET_OPTIMIZATION
375         string
376 EOF
377         foreach my $target (@target) {
378                 next if @{$target->{subtargets}} > 0;
379                 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
380         }
381         print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
382         print <<EOF;
383
384 config CPU_TYPE
385         string
386 EOF
387         foreach my $target (@target) {
388                 next if @{$target->{subtargets}} > 0;
389                 print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
390         }
391         print "\tdefault \"\"\n";
392
393         my %kver;
394         foreach my $target (@target) {
395                 my $v = kver($target->{version});
396                 next if $kver{$v};
397                 $kver{$v} = 1;
398                 print <<EOF;
399
400 config LINUX_$v
401         bool
402
403 EOF
404         }
405         foreach my $def (sort keys %defaults) {
406                 print "\tconfig DEFAULT_".$def."\n";
407                 print "\t\tbool\n\n";
408         }
409 }
410
411 my %dep_check;
412 sub __find_package_dep($$) {
413         my $pkg = shift;
414         my $name = shift;
415         my $deps = ($pkg->{vdepends} or $pkg->{depends});
416
417         return 0 unless defined $deps;
418         foreach my $dep (@{$deps}) {
419                 next if $dep_check{$dep};
420                 $dep_check{$dep} = 1;
421                 return 1 if $dep eq $name;
422                 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
423         }
424         return 0;
425 }
426
427 # wrapper to avoid infinite recursion
428 sub find_package_dep($$) {
429         my $pkg = shift;
430         my $name = shift;
431
432         %dep_check = ();
433         return __find_package_dep($pkg, $name);
434 }
435
436 sub package_depends($$) {
437         my $a = shift;
438         my $b = shift;
439         my $ret;
440
441         return 0 if ($a->{submenu} ne $b->{submenu});
442         if (find_package_dep($a, $b->{name}) == 1) {
443                 $ret = 1;
444         } elsif (find_package_dep($b, $a->{name}) == 1) {
445                 $ret = -1;
446         } else {
447                 return 0;
448         }
449         return $ret;
450 }
451
452 sub mconf_depends {
453         my $pkgname = shift;
454         my $depends = shift;
455         my $only_dep = shift;
456         my $res;
457         my $dep = shift;
458         my $seen = shift;
459         my $parent_condition = shift;
460         $dep or $dep = {};
461         $seen or $seen = {};
462         my @t_depends;
463
464         $depends or return;
465         my @depends = @$depends;
466         foreach my $depend (@depends) {
467                 my $m = "depends on";
468                 my $flags = "";
469                 $depend =~ s/^([@\+]+)// and $flags = $1;
470                 my $vdep;
471                 my $condition = $parent_condition;
472
473                 next if $condition eq $depend;
474                 next if $seen->{"$parent_condition:$depend"};
475                 next if $seen->{":$depend"};
476                 $seen->{"$parent_condition:$depend"} = 1;
477                 if ($depend =~ /^(.+):(.+)$/) {
478                         if ($1 ne "PACKAGE_$pkgname") {
479                                 if ($condition) {
480                                         $condition = "$condition && $1";
481                                 } else {
482                                         $condition = $1;
483                                 }
484                         }
485                         $depend = $2;
486                 }
487                 next if $package{$depend} and $package{$depend}->{buildonly};
488                 if ($vdep = $package{$depend}->{vdepends}) {
489                         $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
490                 } else {
491                         $flags =~ /\+/ and do {
492                                 # Menuconfig will not treat 'select FOO' as a real dependency
493                                 # thus if FOO depends on other config options, these dependencies
494                                 # will not be checked. To fix this, we simply emit all of FOO's
495                                 # depends here as well.
496                                 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
497
498                                 $m = "select";
499                                 next if $only_dep;
500                         };
501                         $flags =~ /@/ or $depend = "PACKAGE_$depend";
502                         if ($condition) {
503                                 if ($m =~ /select/) {
504                                         next if $depend eq $condition;
505                                         $depend = "$depend if $condition";
506                                 } else {
507                                         $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
508                                 }
509                         }
510                 }
511                 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
512         }
513
514         foreach my $tdep (@t_depends) {
515                 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
516         }
517
518         foreach my $depend (keys %$dep) {
519                 my $m = $dep->{$depend};
520                 $res .= "\t\t$m $depend\n";
521         }
522         return $res;
523 }
524
525 sub mconf_conflicts {
526         my $pkgname = shift;
527         my $depends = shift;
528         my $res = "";
529
530         foreach my $depend (@$depends) {
531                 next unless $package{$depend};
532                 $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
533         }
534         return $res;
535 }
536
537 sub print_package_config_category($) {
538         my $cat = shift;
539         my %menus;
540         my %menu_dep;
541
542         return unless $category{$cat};
543
544         print "menu \"$cat\"\n\n";
545         my %spkg = %{$category{$cat}};
546
547         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
548                 foreach my $pkg (@{$spkg{$spkg}}) {
549                         next if $pkg->{buildonly};
550                         my $menu = $pkg->{submenu};
551                         if ($menu) {
552                                 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
553                         } else {
554                                 $menu = 'undef';
555                         }
556                         $menus{$menu} or $menus{$menu} = [];
557                         push @{$menus{$menu}}, $pkg;
558                 }
559         }
560         my @menus = sort {
561                 ($a eq 'undef' ?  1 : 0) or
562                 ($b eq 'undef' ? -1 : 0) or
563                 ($a cmp $b)
564         } keys %menus;
565
566         foreach my $menu (@menus) {
567                 my @pkgs = sort {
568                         package_depends($a, $b) or
569                         ($a->{name} cmp $b->{name})
570                 } @{$menus{$menu}};
571                 if ($menu ne 'undef') {
572                         $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
573                         print "menu \"$menu\"\n";
574                 }
575                 foreach my $pkg (@pkgs) {
576                         my $title = $pkg->{name};
577                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
578                         if ($c > 0) {
579                                 $title .= ("." x $c). " ". $pkg->{title};
580                         }
581                         $title = "\"$title\"";
582                         print "\t";
583                         $pkg->{menu} and print "menu";
584                         print "config PACKAGE_".$pkg->{name}."\n";
585                         $pkg->{hidden} and $title = "";
586                         print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
587                         print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
588                         unless ($pkg->{hidden}) {
589                                 $pkg->{default} ||= "m if ALL";
590                         }
591                         if ($pkg->{default}) {
592                                 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
593                                         print "\t\tdefault $default\n";
594                                 }
595                         }
596                         print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
597                         print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
598                         print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
599                         print "\t\thelp\n";
600                         print $pkg->{description};
601                         print "\n";
602
603                         $pkg->{config} and print $pkg->{config}."\n";
604                 }
605                 if ($menu ne 'undef') {
606                         print "endmenu\n";
607                         $menu_dep{$menu} and print "endif\n";
608                 }
609         }
610         print "endmenu\n\n";
611
612         undef $category{$cat};
613 }
614
615 sub print_package_features() {
616         keys %features > 0 or return;
617         print "menu \"Package features\"\n";
618         foreach my $n (keys %features) {
619                 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
620                 print <<EOF;
621 choice
622         prompt "$features[0]->{target_title}"
623         default FEATURE_$features[0]->{name}
624 EOF
625
626                 foreach my $feature (@features) {
627                         print <<EOF;
628         config FEATURE_$feature->{name}
629                 bool "$feature->{title}"
630 EOF
631                         $feature->{description} =~ /\w/ and do {
632                                 print "\t\thelp\n".$feature->{description}."\n";
633                         };
634                 }
635                 print "endchoice\n"
636         }
637         print "endmenu\n\n";
638 }
639
640 sub gen_package_config() {
641         parse_package_metadata($ARGV[0]) or exit 1;
642         print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
643         foreach my $preconfig (keys %preconfig) {
644                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
645                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
646                         $conf =~ tr/\.-/__/;
647                         print <<EOF
648         config UCI_PRECONFIG_$conf
649                 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
650                 depends on PACKAGE_$preconfig
651                 default "$preconfig{$preconfig}->{$cfg}->{default}"
652
653 EOF
654                 }
655         }
656         print "source \"package/*/image-config.in\"\n";
657         if (scalar glob "package/feeds/*/*/image-config.in") {
658             print "source \"package/feeds/*/*/image-config.in\"\n";
659         }
660         print_package_features();
661         print_package_config_category 'Base system';
662         foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
663                 print_package_config_category $cat;
664         }
665 }
666
667 sub get_conditional_dep($$) {
668         my $condition = shift;
669         my $depstr = shift;
670         if ($condition) {
671                 if ($condition =~ /^!(.+)/) {
672                         return "\$(if \$(CONFIG_$1),,$depstr)";
673                 } else {
674                         return "\$(if \$(CONFIG_$condition),$depstr)";
675                 }
676         } else {
677                 return $depstr;
678         }
679 }
680
681 sub gen_package_mk() {
682         my %conf;
683         my %dep;
684         my %done;
685         my $line;
686
687         parse_package_metadata($ARGV[0]) or exit 1;
688         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
689                 my $config;
690                 my $pkg = $package{$name};
691                 my @srcdeps;
692
693                 next if defined $pkg->{vdepends};
694
695                 $config = "\$(CONFIG_PACKAGE_$name)";
696                 if ($config) {
697                         $pkg->{buildonly} and $config = "";
698                         print "package-$config += $pkg->{subdir}$pkg->{src}\n";
699                         if ($pkg->{variant}) {
700                                 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
701                                         print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
702                                 }
703                                 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
704                         }
705                         $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
706                 }
707
708                 next if $done{$pkg->{src}};
709                 $done{$pkg->{src}} = 1;
710
711                 if (@{$pkg->{buildtypes}} > 0) {
712                         print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
713                 }
714
715                 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
716                         foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
717                                 $dep =~ /@/ or do {
718                                         $dep =~ s/\+//g;
719                                         push @srcdeps, $dep;
720                                 };
721                         }
722                 }
723                 foreach my $type (@{$pkg->{buildtypes}}) {
724                         my @extra_deps;
725                         my %deplines;
726
727                         next unless $pkg->{"builddepends/$type"};
728                         foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
729                                 my $suffix = "";
730                                 my $condition;
731
732                                 if ($dep =~ /^(.+):(.+)/) {
733                                         $condition = $1;
734                                         $dep = $2;
735                                 }
736                                 if ($dep =~ /^(.+)(\/.+)/) {
737                                         $dep = $1;
738                                         $suffix = $2;
739                                 }
740
741                                 my $idx = "";
742                                 my $pkg_dep = $package{$dep};
743                                 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
744                                         $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
745                                 } elsif (defined($srcpackage{$dep})) {
746                                         $idx = $subdir{$dep}.$dep;
747                                 } else {
748                                         next;
749                                 }
750                                 my $depstr = "\$(curdir)/$idx$suffix/compile";
751                                 my $depline = get_conditional_dep($condition, $depstr);
752                                 if ($depline) {
753                                         $deplines{$depline}++;
754                                 }
755                         }
756                         my $depline = join(" ", sort keys %deplines);
757                         if ($depline) {
758                                 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
759                         }
760                 }
761
762                 my $hasdeps = 0;
763                 my %deplines;
764                 foreach my $deps (@srcdeps) {
765                         my $idx;
766                         my $condition;
767                         my $prefix = "";
768                         my $suffix = "";
769
770                         if ($deps =~ /^(.+):(.+)/) {
771                                 $condition = $1;
772                                 $deps = $2;
773                         }
774                         if ($deps =~ /^(.+)(\/.+)/) {
775                                 $deps = $1;
776                                 $suffix = $2;
777                         }
778
779                         my $pkg_dep = $package{$deps};
780                         my @deps;
781
782                         if ($pkg_dep->{vdepends}) {
783                                 @deps = @{$pkg_dep->{vdepends}};
784                         } else {
785                                 @deps = ($deps);
786                         }
787
788                         foreach my $dep (@deps) {
789                                 $pkg_dep = $package{$deps};
790                                 if (defined $pkg_dep->{src}) {
791                                         ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
792                                 } elsif (defined($srcpackage{$dep})) {
793                                         $idx = $subdir{$dep}.$dep;
794                                 }
795                                 undef $idx if $idx eq 'base-files';
796                                 if ($idx) {
797                                         $idx .= $suffix;
798
799                                         my $depline;
800                                         next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
801                                         next if $dep{$condition.":".$pkg->{src}."->".$idx};
802                                         next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
803                                         my $depstr;
804
805                                         if ($pkg_dep->{vdepends}) {
806                                                 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
807                                                 $dep{$pkg->{src}."->($dep)".$idx} = 1;
808                                         } else {
809                                                 $depstr = "\$(curdir)/$idx/compile";
810                                                 $dep{$pkg->{src}."->".$idx} = 1;
811                                         }
812                                         $depline = get_conditional_dep($condition, $depstr);
813                                         if ($depline) {
814                                                 $deplines{$depline}++;
815                                         }
816                                 }
817                         }
818                 }
819                 my $depline = join(" ", sort keys %deplines);
820                 if ($depline) {
821                         $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
822                 }
823         }
824
825         if ($line ne "") {
826                 print "\n$line";
827         }
828         foreach my $preconfig (keys %preconfig) {
829                 my $cmds;
830                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
831                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
832                         $conf =~ tr/\.-/__/;
833                         $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
834                 }
835                 next unless $cmds;
836                 print <<EOF
837
838 ifndef DUMP_TARGET_DB
839 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
840         ( \\
841 $cmds \\
842         ) > \$@
843         
844 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
845   package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
846 endif
847 endif
848
849 EOF
850         }
851 }
852
853 sub gen_package_source() {
854         parse_package_metadata($ARGV[0]) or exit 1;
855         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
856                 my $pkg = $package{$name};
857                 if ($pkg->{name} && $pkg->{source}) {
858                         print "$pkg->{name}: ";
859                         print "$pkg->{source}\n";
860                 }
861         }
862 }
863
864 sub gen_package_feeds() {
865         parse_package_metadata($ARGV[0]) or exit 1;
866         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
867                 my $pkg = $package{$name};
868                 if ($pkg->{name} && $pkg->{feed}) {
869                         print "Package/$name/feed = $pkg->{feed}\n";
870                 }
871         }
872 }
873
874 sub gen_package_license($) {
875         my $level = shift;
876         parse_package_metadata($ARGV[0]) or exit 1;
877         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
878                 my $pkg = $package{$name};
879                 if ($pkg->{name}) {
880                         if ($pkg->{license}) {
881                                 print "$pkg->{name}: ";
882                                 print "$pkg->{license}\n";
883                                 if ($pkg->{licensefiles} && $level == 0) {
884                                         print "\tFiles: $pkg->{licensefiles}\n";
885                                 }
886                         } else {
887                                 if ($level == 1) {
888                                         print "$pkg->{name}: Missing license! ";
889                                         print "Please fix $pkg->{makefile}\n";
890                                 }
891                         }
892                 }
893         }
894 }
895
896 sub parse_command() {
897         my $cmd = shift @ARGV;
898         for ($cmd) {
899                 /^target_config$/ and return gen_target_config();
900                 /^package_mk$/ and return gen_package_mk();
901                 /^package_config$/ and return gen_package_config();
902                 /^kconfig/ and return gen_kconfig_overrides();
903                 /^package_source$/ and return gen_package_source();
904                 /^package_feeds$/ and return gen_package_feeds();
905                 /^package_license$/ and return gen_package_license(0);
906                 /^package_licensefull$/ and return gen_package_license(1);
907         }
908         print <<EOF
909 Available Commands:
910         $0 target_config [file]         Target metadata in Kconfig format
911         $0 package_mk [file]            Package metadata in makefile format
912         $0 package_config [file]        Package metadata in Kconfig format
913         $0 kconfig [file] [config]      Kernel config overrides
914         $0 package_source [file]        Package source file information
915         $0 package_feeds [file]         Package feed information in makefile format
916         $0 package_license [file]       Package license information
917         $0 package_licensefull [file]   Package license information (full list)
918
919 EOF
920 }
921
922 parse_command();