ar71xx: add support for compex wpj344
[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                 if ($target->{arch} =~ /\w/) {
209                         $confstr .= "\tselect $target->{arch}\n";
210                 }
211         }
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                                 if ($pkg->{name} =~ /^kmod-/) {
549                                         $pkg->{default} ||= "m if ALL_KMODS";
550                                 } else {
551                                         $pkg->{default} ||= "m if ALL";
552                                 }
553                         }
554                         if ($pkg->{default}) {
555                                 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
556                                         print "\t\tdefault $default\n";
557                                 }
558                         }
559                         print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
560                         print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
561                         print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
562                         print "\t\thelp\n";
563                         print $pkg->{description};
564                         print "\n";
565
566                         $pkg->{config} and print $pkg->{config}."\n";
567                 }
568                 if ($menu ne 'undef') {
569                         print "endmenu\n";
570                         $menu_dep{$menu} and print "endif\n";
571                 }
572         }
573         print "endmenu\n\n";
574
575         undef $category{$cat};
576 }
577
578 sub print_package_features() {
579         keys %features > 0 or return;
580         print "menu \"Package features\"\n";
581         foreach my $n (keys %features) {
582                 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
583                 print <<EOF;
584 choice
585         prompt "$features[0]->{target_title}"
586         default FEATURE_$features[0]->{name}
587 EOF
588
589                 foreach my $feature (@features) {
590                         print <<EOF;
591         config FEATURE_$feature->{name}
592                 bool "$feature->{title}"
593 EOF
594                         $feature->{description} =~ /\w/ and do {
595                                 print "\t\thelp\n".$feature->{description}."\n";
596                         };
597                 }
598                 print "endchoice\n"
599         }
600         print "endmenu\n\n";
601 }
602
603 sub print_package_overrides() {
604         keys %overrides > 0 or return;
605         print "\tconfig OVERRIDE_PKGS\n";
606         print "\t\tstring\n";
607         print "\t\tdefault \"".join(" ", keys %overrides)."\"\n\n";
608 }
609
610 sub gen_package_config() {
611         parse_package_metadata($ARGV[0]) or exit 1;
612         print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
613         foreach my $preconfig (keys %preconfig) {
614                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
615                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
616                         $conf =~ tr/\.-/__/;
617                         print <<EOF
618         config UCI_PRECONFIG_$conf
619                 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
620                 depends on PACKAGE_$preconfig
621                 default "$preconfig{$preconfig}->{$cfg}->{default}"
622
623 EOF
624                 }
625         }
626         print "source \"package/*/image-config.in\"\n";
627         if (scalar glob "package/feeds/*/*/image-config.in") {
628             print "source \"package/feeds/*/*/image-config.in\"\n";
629         }
630         print_package_features();
631         print_package_config_category 'Base system';
632         foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
633                 print_package_config_category $cat;
634         }
635         print_package_overrides();
636 }
637
638 sub get_conditional_dep($$) {
639         my $condition = shift;
640         my $depstr = shift;
641         if ($condition) {
642                 if ($condition =~ /^!(.+)/) {
643                         return "\$(if \$(CONFIG_$1),,$depstr)";
644                 } else {
645                         return "\$(if \$(CONFIG_$condition),$depstr)";
646                 }
647         } else {
648                 return $depstr;
649         }
650 }
651
652 sub gen_package_mk() {
653         my %conf;
654         my %dep;
655         my %done;
656         my $line;
657
658         parse_package_metadata($ARGV[0]) or exit 1;
659         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
660                 my $config;
661                 my $pkg = $package{$name};
662                 my @srcdeps;
663
664                 next if defined $pkg->{vdepends};
665
666                 $config = "\$(CONFIG_PACKAGE_$name)";
667                 if ($config) {
668                         $pkg->{buildonly} and $config = "";
669                         print "package-$config += $pkg->{subdir}$pkg->{src}\n";
670                         if ($pkg->{variant}) {
671                                 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
672                                         print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
673                                 }
674                                 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
675                         }
676                         $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
677                 }
678
679                 next if $done{$pkg->{src}};
680                 $done{$pkg->{src}} = 1;
681
682                 if (@{$pkg->{buildtypes}} > 0) {
683                         print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
684                 }
685
686                 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
687                         foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
688                                 $dep =~ /@/ or do {
689                                         $dep =~ s/\+//g;
690                                         push @srcdeps, $dep;
691                                 };
692                         }
693                 }
694                 foreach my $type (@{$pkg->{buildtypes}}) {
695                         my @extra_deps;
696                         my %deplines;
697
698                         next unless $pkg->{"builddepends/$type"};
699                         foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
700                                 my $suffix = "";
701                                 my $condition;
702
703                                 if ($dep =~ /^(.+):(.+)/) {
704                                         $condition = $1;
705                                         $dep = $2;
706                                 }
707                                 if ($dep =~ /^(.+)(\/.+)/) {
708                                         $dep = $1;
709                                         $suffix = $2;
710                                 }
711
712                                 my $idx = "";
713                                 my $pkg_dep = $package{$dep};
714                                 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
715                                         $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
716                                 } elsif (defined($srcpackage{$dep})) {
717                                         $idx = $subdir{$dep}.$dep;
718                                 } else {
719                                         next;
720                                 }
721                                 my $depstr = "\$(curdir)/$idx$suffix/compile";
722                                 my $depline = get_conditional_dep($condition, $depstr);
723                                 if ($depline) {
724                                         $deplines{$depline}++;
725                                 }
726                         }
727                         my $depline = join(" ", sort keys %deplines);
728                         if ($depline) {
729                                 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
730                         }
731                 }
732
733                 my $hasdeps = 0;
734                 my %deplines;
735                 foreach my $deps (@srcdeps) {
736                         my $idx;
737                         my $condition;
738                         my $prefix = "";
739                         my $suffix = "";
740
741                         if ($deps =~ /^(.+):(.+)/) {
742                                 $condition = $1;
743                                 $deps = $2;
744                         }
745                         if ($deps =~ /^(.+)(\/.+)/) {
746                                 $deps = $1;
747                                 $suffix = $2;
748                         }
749
750                         my $pkg_dep = $package{$deps};
751                         my @deps;
752
753                         if ($pkg_dep->{vdepends}) {
754                                 @deps = @{$pkg_dep->{vdepends}};
755                         } else {
756                                 @deps = ($deps);
757                         }
758
759                         foreach my $dep (@deps) {
760                                 $pkg_dep = $package{$deps};
761                                 if (defined $pkg_dep->{src}) {
762                                         ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
763                                 } elsif (defined($srcpackage{$dep})) {
764                                         $idx = $subdir{$dep}.$dep;
765                                 }
766                                 undef $idx if $idx eq 'base-files';
767                                 if ($idx) {
768                                         $idx .= $suffix;
769
770                                         my $depline;
771                                         next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
772                                         next if $dep{$condition.":".$pkg->{src}."->".$idx};
773                                         next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
774                                         my $depstr;
775
776                                         if ($pkg_dep->{vdepends}) {
777                                                 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
778                                                 $dep{$pkg->{src}."->($dep)".$idx} = 1;
779                                         } else {
780                                                 $depstr = "\$(curdir)/$idx/compile";
781                                                 $dep{$pkg->{src}."->".$idx} = 1;
782                                         }
783                                         $depline = get_conditional_dep($condition, $depstr);
784                                         if ($depline) {
785                                                 $deplines{$depline}++;
786                                         }
787                                 }
788                         }
789                 }
790                 my $depline = join(" ", sort keys %deplines);
791                 if ($depline) {
792                         $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
793                 }
794         }
795
796         if ($line ne "") {
797                 print "\n$line";
798         }
799         foreach my $preconfig (keys %preconfig) {
800                 my $cmds;
801                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
802                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
803                         $conf =~ tr/\.-/__/;
804                         $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
805                 }
806                 next unless $cmds;
807                 print <<EOF
808
809 ifndef DUMP_TARGET_DB
810 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
811         ( \\
812 $cmds \\
813         ) > \$@
814         
815 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
816   package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
817 endif
818 endif
819
820 EOF
821         }
822 }
823
824 sub gen_package_source() {
825         parse_package_metadata($ARGV[0]) or exit 1;
826         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
827                 my $pkg = $package{$name};
828                 if ($pkg->{name} && $pkg->{source}) {
829                         print "$pkg->{name}: ";
830                         print "$pkg->{source}\n";
831                 }
832         }
833 }
834
835 sub gen_package_feeds() {
836         parse_package_metadata($ARGV[0]) or exit 1;
837         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
838                 my $pkg = $package{$name};
839                 if ($pkg->{name} && $pkg->{feed}) {
840                         print "Package/$name/feed = $pkg->{feed}\n";
841                 }
842         }
843 }
844
845 sub gen_package_license($) {
846         my $level = shift;
847         parse_package_metadata($ARGV[0]) or exit 1;
848         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
849                 my $pkg = $package{$name};
850                 if ($pkg->{name}) {
851                         if ($pkg->{license}) {
852                                 print "$pkg->{name}: ";
853                                 print "$pkg->{license}\n";
854                                 if ($pkg->{licensefiles} && $level == 0) {
855                                         print "\tFiles: $pkg->{licensefiles}\n";
856                                 }
857                         } else {
858                                 if ($level == 1) {
859                                         print "$pkg->{name}: Missing license! ";
860                                         print "Please fix $pkg->{makefile}\n";
861                                 }
862                         }
863                 }
864         }
865 }
866
867 sub gen_version_filtered_list() {
868         foreach my $item (version_filter_list(@ARGV)) {
869                 print "$item\n";
870         }
871 }
872
873 sub parse_command() {
874         my $cmd = shift @ARGV;
875         for ($cmd) {
876                 /^target_config$/ and return gen_target_config();
877                 /^package_mk$/ and return gen_package_mk();
878                 /^package_config$/ and return gen_package_config();
879                 /^kconfig/ and return gen_kconfig_overrides();
880                 /^package_source$/ and return gen_package_source();
881                 /^package_feeds$/ and return gen_package_feeds();
882                 /^package_license$/ and return gen_package_license(0);
883                 /^package_licensefull$/ and return gen_package_license(1);
884                 /^version_filter$/ and return gen_version_filtered_list();
885         }
886         print <<EOF
887 Available Commands:
888         $0 target_config [file]                 Target metadata in Kconfig format
889         $0 package_mk [file]                    Package metadata in makefile format
890         $0 package_config [file]                Package metadata in Kconfig format
891         $0 kconfig [file] [config] [patchver]   Kernel config overrides
892         $0 package_source [file]                Package source file information
893         $0 package_feeds [file]                 Package feed information in makefile format
894         $0 package_license [file]               Package license information
895         $0 package_licensefull [file]           Package license information (full list)
896         $0 version_filter [patchver] [list...]  Filter list of version tagged strings
897
898 EOF
899 }
900
901 parse_command();