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