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