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