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