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