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